Skip to content
This repository has been archived by the owner on Feb 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #140 from zzzz465/feature/support-1.4
Browse files Browse the repository at this point in the history
feature: support 1.4
  • Loading branch information
zzzz465 authored Oct 10, 2022
2 parents f6b5c8f + 62873f2 commit 30c24ff
Show file tree
Hide file tree
Showing 77 changed files with 657,290 additions and 530 deletions.
23 changes: 9 additions & 14 deletions analyzer/src/parser/domhandler/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,27 +503,22 @@ function cloneChildren(childs: Node[]): Node[] {

type RangedNode = Element | Comment | Text | DataNode

// TODO: return null instead of undefined
function findNodeAt(node: RangedNode, offset: number): RangedNode | undefined {
if (node instanceof Document) {
const index = sortedFindFirst(node.childNodes, (node: any) => node.nodeRange && offset <= node.nodeRange.start) - 1
if (index >= 0) {
const child = node.childNodes[index] as unknown
if ((child instanceof Element || child instanceof DataNode) && child.nodeRange.include(offset)) {
return findNodeAt(child, offset) ?? child
}
}
} else if (node instanceof Element) {
const index = sortedFindFirst(node.childNodes, (node: any) => node.nodeRange && offset <= node.nodeRange.start) - 1
if (node instanceof Element || node instanceof Document) {
const index = sortedFindFirst(node.childNodes, (child: any) => child.nodeRange && child.nodeRange.start <= offset)
if (index >= 0) {
const child = node.childNodes[index]
if ((child instanceof Element || child instanceof DataNode) && child.nodeRange.include(offset)) {
if (child instanceof Element) {
return findNodeAt(child, offset) ?? child
} else if (child instanceof Text || child instanceof DataNode) {
return child
}
}
} else if (node.nodeRange.include(offset)) {
return node
} else {
if (node.dataRange.include(offset)) {
return node
}
return undefined
}
}

Expand Down
22 changes: 22 additions & 0 deletions analyzer/src/test/parser/offset.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { parse, Text } from '../../parser'

const xml = `
<?xml version="1.0" encoding="utf-8" ?>
<Defs>
<CultureDef>
<defName>test_def_1</defName>
<ideoNameMaker MayRequire="Ludeon.RimWorld.Ideology">NamerIdeoAstropolitan</ideoNameMaker>
</CultureDef>
</Defs>
`

// jest 가 <, > 를 인식 못함..
describe('findNodeAt offset should find Text Node', () => {
test('offset 154 should find Text Node inside ideoNameMaker tag', () => {
const root = parse(xml)
const node = root.findNodeAt(154)!
expect(node).toBeDefined()
expect(node).toBeInstanceOf(Text)
})
})
18 changes: 10 additions & 8 deletions analyzer/src/utils/arrays.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
/**
* Takes a sorted array and a function p. The array is sorted in such a way that all elements where p(x) is false
* are located before all elements where p(x) is true.
* @param p predicate function that returns true if the element's start Index is greater than the given index
* @returns the least x for which p(x) is true or array.length if no element fullfills the given function.
*/
export function sortedFindFirst<T>(array: T[], p: (x: T) => boolean): number {
let low = 0,
high = array.length
if (high === 0) {
return 0 // no children
}
let low = 0
let high = array.length
while (low < high) {
const mid = Math.floor((low + high) / 2)
if (p(array[mid])) {
high = mid
} else {
low = mid + 1
} else {
high = mid
}
}
return low
if (low < array.length && p(array[low])) {
return low
} else {
return low - 1
}
}

export function binarySearch<T>(array: T[], key: T, comparator: (op1: T, op2: T) => number): number {
Expand Down
41 changes: 41 additions & 0 deletions extractor/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/extractor/extractor.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/extractor/extractor.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/extractor/extractor.csproj"
],
"problemMatcher": "$msCompile"
}
]
}
6 changes: 1 addition & 5 deletions extractor/extractor/AssemblyLoader.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Runtime.Remoting.Lifetime;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace extractor
Expand Down
5 changes: 1 addition & 4 deletions extractor/extractor/CompletionItem.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using Newtonsoft.Json;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace extractor
{
Expand Down
69 changes: 57 additions & 12 deletions extractor/extractor/Extractor.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace extractor
{
Expand Down Expand Up @@ -81,8 +78,20 @@ static void CollectData_BFS(IEnumerable<Type> _types)
RawTypeInfo typeInfo;
if (!typeDict.TryGetValue(type, out typeInfo))
{
typeInfo = new RawTypeInfo(type);
typeDict.Add(type, typeInfo);
Exception ex = null;
if (TryNewRawTypeInfo(type, out typeInfo, out ex))
{
typeDict.Add(type, typeInfo);
}
else
{
Log.Warn($"failed to add type: {ex}");
}
}

if (typeInfo == null)
{
continue;
}

if (typeInfo.childCollected)
Expand All @@ -106,8 +115,16 @@ static void CollectData_BFS(IEnumerable<Type> _types)
continue;
}

typeDict.Add(fieldType, new RawTypeInfo(fieldType));
types.Enqueue(fieldType);
Exception ex = null;
if (TryNewRawTypeInfo(fieldType, out RawTypeInfo value, out ex))
{
typeDict.Add(fieldType, value);
types.Enqueue(fieldType);
}
else
{
Log.Warn(ex.ToString());
}
}

// generic type
Expand All @@ -117,8 +134,16 @@ static void CollectData_BFS(IEnumerable<Type> _types)
{
if (!typeDict.ContainsKey(T) && !T.IsGenericParameter)
{
typeDict.Add(T, new RawTypeInfo(T));
types.Enqueue(T);
Exception ex = null;
if (TryNewRawTypeInfo(T, out RawTypeInfo value, out ex))
{
typeDict.Add(T, new RawTypeInfo(T));
types.Enqueue(T);
}
else
{
Log.Warn(ex.ToString());
}
}
}
}
Expand All @@ -135,8 +160,12 @@ static void CollectData_BFS(IEnumerable<Type> _types)
continue;
}

typeDict.Add(iface, new RawTypeInfo(iface));
types.Enqueue(iface);
Exception ex = null;
if (TryNewRawTypeInfo(iface, out RawTypeInfo value, out ex))
{
typeDict.Add(iface, value);
types.Enqueue(iface);
}
}

typeInfo.childCollected = true;
Expand Down Expand Up @@ -193,5 +222,21 @@ static void PopulateData()
rawTypeInfo.populated = true;
}
}

static bool TryNewRawTypeInfo(Type T, out RawTypeInfo value, out Exception ex)
{
try
{
value = new RawTypeInfo(T);
ex = null;
return true;
}
catch (Exception e)
{
value = null;
ex = e;
return false;
}
}
}
}
34 changes: 23 additions & 11 deletions extractor/extractor/Log.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
using log4net;
using log4net;
using log4net.Appender;
using log4net.Core;
using log4net.Filter;
using log4net.Layout;
using log4net.Repository.Hierarchy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace extractor
{
Expand All @@ -19,19 +15,35 @@ public static class Log
static Log()
{
layout = new PatternLayout();
layout.ConversionPattern = "%d %c [%p]: %m%n";
layout.ConversionPattern = "%type %method %line - %c [%p]: %m%n";
layout.ActivateOptions();
}

public static void SetStdOutput()
{
var hierarchy = (Hierarchy)LogManager.GetRepository();

var appender = new ConsoleAppender();
appender.Layout = layout;
appender.ActivateOptions();
var appenderStdout = new ConsoleAppender();
appenderStdout.Layout = layout;
appenderStdout.Target = ConsoleAppender.ConsoleOut;
var stdOutFilter = new LevelRangeFilter();
stdOutFilter.LevelMax = Level.Warn;
stdOutFilter.LevelMin = Level.All;
appenderStdout.AddFilter(stdOutFilter);
appenderStdout.ActivateOptions();

hierarchy.Root.AddAppender(appender);
var appenderStderr = new ConsoleAppender();
appenderStderr.Layout = layout;
appenderStderr.Target = ConsoleAppender.ConsoleError;
var stdErrFilter = new LevelRangeFilter();
stdErrFilter.LevelMax = Level.Fatal;
stdErrFilter.LevelMin = Level.Error;
appenderStderr.Layout = layout;
appenderStderr.AddFilter(stdErrFilter);
appenderStderr.ActivateOptions();

hierarchy.Root.AddAppender(appenderStdout);
hierarchy.Root.AddAppender(appenderStderr);

hierarchy.Root.Level = Level.Info;
hierarchy.Configured = true;
Expand Down
4 changes: 1 addition & 3 deletions extractor/extractor/NameUtility.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace extractor
{
Expand Down
Loading

0 comments on commit 30c24ff

Please sign in to comment.