Skip to content

Commit 5bb03ac

Browse files
committed
More misc fixes
1 parent c706cb1 commit 5bb03ac

File tree

11 files changed

+89
-53
lines changed

11 files changed

+89
-53
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ All notable changes to the "nwscript-ee-language-server" extension will be docum
5151

5252
## [1.5.3]
5353

54-
- Goto will now work for functions from `nwscript.nss` if the file is in your project.
54+
- Goto will now work for functions and constants from `nwscript.nss` if the file is in your project.
55+
- Fixed a few issues with the tokenizer.
56+
- Fixed a small issue with `const` expressions resolution.
5557
- Fixed the compilation provider not reporting warnings.
5658
- New compiler setting `reportWarnings`. True by default.
5759
- New formatter setting `verbose`. False by default.

server/src/Documents/Document.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ export default class Document {
3939
}
4040

4141
public getGlobalComplexTokensWithRef(computedChildren: string[] = []): OwnedComplexTokens[] {
42-
return [{ owner: this.uri, tokens: this.complexTokens }].concat(
42+
const localStandardLibDefinitions = this.collection.get("nwscript");
43+
return [
44+
{ owner: this.uri, tokens: this.complexTokens },
45+
...(localStandardLibDefinitions
46+
? [{ owner: localStandardLibDefinitions.uri, tokens: localStandardLibDefinitions.complexTokens }]
47+
: []),
48+
].concat(
4349
this.children.flatMap((child) => {
4450
// Cycling children or/and duplicates
4551
if (computedChildren.includes(child)) {
@@ -59,25 +65,27 @@ export default class Document {
5965
);
6066
}
6167

62-
public getGlobalComplexTokens(computedChildren: string[] = []): ComplexToken[] {
63-
return this.complexTokens.concat(
64-
this.children.flatMap((child) => {
65-
// Cycling children or/and duplicates
66-
if (computedChildren.includes(child)) {
67-
return [];
68-
} else {
69-
computedChildren.push(child);
70-
}
71-
72-
const childDocument = this.collection.get(child);
73-
74-
if (!childDocument) {
75-
return [];
76-
}
77-
78-
return childDocument.getGlobalComplexTokens(computedChildren);
79-
}),
80-
);
68+
public getGlobalComplexTokens(computedChildren: string[] = [], localFunctionIdentifiers: string[] = []): ComplexToken[] {
69+
return this.complexTokens
70+
.filter((token) => !localFunctionIdentifiers.includes(token.identifier))
71+
.concat(
72+
this.children.flatMap((child) => {
73+
// Cycling children or/and duplicates
74+
if (computedChildren.includes(child)) {
75+
return [];
76+
} else {
77+
computedChildren.push(child);
78+
}
79+
80+
const childDocument = this.collection.get(child);
81+
82+
if (!childDocument) {
83+
return [];
84+
}
85+
86+
return childDocument.getGlobalComplexTokens(computedChildren);
87+
}),
88+
);
8189
}
8290

8391
public getGlobalStructComplexTokensWithRef(computedChildren: string[] = []): OwnedStructComplexTokens[] {

server/src/Providers/CompletionItemsProvider.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export default class CompletionItemsProvider extends Provider {
6161
}
6262

6363
return this.getLocalScopeCompletionItems(localScope)
64-
.concat(this.getGlobalScopeCompletionItems(document))
64+
.concat(this.getGlobalScopeCompletionItems(document, localScope))
6565
.concat(this.getStandardLibCompletionItems());
6666
}
6767
}
@@ -77,8 +77,15 @@ export default class CompletionItemsProvider extends Provider {
7777
return functionVariablesCompletionItems.concat(functionsCompletionItems);
7878
}
7979

80-
private getGlobalScopeCompletionItems(document: Document | undefined) {
81-
return document?.getGlobalComplexTokens().map((token) => CompletionItemBuilder.buildItem(token)) || [];
80+
private getGlobalScopeCompletionItems(document: Document | undefined, localScope: LocalScopeTokenizationResult) {
81+
return (
82+
document
83+
?.getGlobalComplexTokens(
84+
[],
85+
localScope.functionsComplexTokens.map((token) => token.identifier),
86+
)
87+
.map((token) => CompletionItemBuilder.buildItem(token)) || []
88+
);
8289
}
8390

8491
private getStandardLibCompletionItems() {

server/src/Providers/DiagnosticsProvider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default class DiagnoticsProvider extends Provider {
4242
start: { line: linePosition, character: 0 },
4343
end: { line: linePosition, character: Number.MAX_VALUE },
4444
},
45-
message: lineMessage.exec(line)![1].trim(),
45+
message: lineMessage.exec(line)![2].trim(),
4646
};
4747

4848
files[uri].push(diagnostic);
@@ -71,7 +71,7 @@ export default class DiagnoticsProvider extends Provider {
7171
return async () => {
7272
return await new Promise<boolean>((resolve, reject) => {
7373
const { enabled, nwnHome, reportWarnings, nwnInstallation, verbose } = this.server.config.compiler;
74-
if (!enabled) {
74+
if (!enabled || uri.includes("nwscript.nss")) {
7575
return resolve(true);
7676
}
7777

server/src/Providers/Formatters/ClangFormatter.ts

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
import { spawn } from "child_process";
22
import { parser, Tag } from "sax";
3-
import { Range } from "vscode-languageserver";
4-
import { TextDocument, TextEdit } from "vscode-languageserver-textdocument";
3+
import { TextDocument, Range, TextEdit } from "vscode-languageserver-textdocument";
54

65
import Formatter from "./Formatter";
76

8-
type CurrentEdit = { length: number; offset: number; text: string } | null;
7+
type CurrentEdit = { length: number; offset: number; text: string };
8+
99
export default class ClangFormatter extends Formatter {
10-
private xmlParseOnText(currentEdit: CurrentEdit) {
10+
edits: TextEdit[] = [];
11+
currentEdit: CurrentEdit | null = null;
12+
13+
private xmlParseOnText() {
1114
return (text: string) => {
12-
if (!currentEdit) {
15+
if (!this.currentEdit) {
1316
return;
1417
}
1518

16-
currentEdit.text = text;
19+
this.currentEdit.text = text;
1720
};
1821
}
1922

20-
private xmlParserOnOpenTag(currentEdit: CurrentEdit, reject: (reason: any) => void) {
23+
private xmlParserOnOpenTag(reject: (reason: any) => void) {
2124
return (tag: Tag) => {
22-
if (currentEdit) {
25+
if (this.currentEdit) {
2326
reject(new Error("Malformed output."));
2427
}
2528

@@ -28,7 +31,7 @@ export default class ClangFormatter extends Formatter {
2831
return;
2932

3033
case "replacement":
31-
currentEdit = {
34+
this.currentEdit = {
3235
length: parseInt(tag.attributes.length.toString()),
3336
offset: parseInt(tag.attributes.offset.toString()),
3437
text: "",
@@ -41,17 +44,17 @@ export default class ClangFormatter extends Formatter {
4144
};
4245
}
4346

44-
private xmlParserOnCloseTag(document: TextDocument, edits: TextEdit[], currentEdit: CurrentEdit) {
47+
private xmlParserOnCloseTag(document: TextDocument) {
4548
return () => {
46-
if (!currentEdit) {
49+
if (!this.currentEdit) {
4750
return;
4851
}
4952

50-
const start = document.positionAt(currentEdit.offset);
51-
const end = document.positionAt(currentEdit.offset + currentEdit.length);
53+
const start = document.positionAt(this.currentEdit.offset);
54+
const end = document.positionAt(this.currentEdit.offset + this.currentEdit.length);
5255

53-
edits.push({ range: { start, end }, newText: currentEdit.text });
54-
currentEdit = null;
56+
this.edits.push({ range: { start, end }, newText: this.currentEdit.text });
57+
this.currentEdit = null;
5558
};
5659
}
5760

@@ -81,7 +84,9 @@ export default class ClangFormatter extends Formatter {
8184
this.logger.info(`Resolving clang-format's executable with: ${this.executable}.`);
8285
}
8386

84-
const child = spawn(this.executable, args, { shell: true });
87+
const child = spawn(this.executable, args, {
88+
cwd: this.workspaceFilesSystem.getWorkspaceRootPath(),
89+
});
8590

8691
child.stdin.end(document.getText());
8792
child.stdout.on("data", (chunk: string) => (stdout += chunk));
@@ -98,25 +103,23 @@ export default class ClangFormatter extends Formatter {
98103
reject(new Error(stderr));
99104
}
100105

101-
let currentEdit: CurrentEdit = null;
102-
const edits: TextEdit[] = [];
103106
const xmlParser = parser(true, {
104107
trim: false,
105108
normalize: false,
106109
});
107110

108111
xmlParser.onerror = (err) => reject(err);
109-
xmlParser.ontext = this.xmlParseOnText(currentEdit);
110-
xmlParser.onopentag = this.xmlParserOnOpenTag(currentEdit, reject);
111-
xmlParser.onclosetag = this.xmlParserOnCloseTag(document, edits, currentEdit);
112+
xmlParser.ontext = this.xmlParseOnText();
113+
xmlParser.onopentag = this.xmlParserOnOpenTag(reject);
114+
xmlParser.onclosetag = this.xmlParserOnCloseTag(document);
112115
xmlParser.write(stdout);
113116
xmlParser.end();
114117

115118
if (this.verbose) {
116119
this.logger.info("Done.\n");
117120
}
118121

119-
resolve(edits);
122+
resolve(this.edits);
120123
});
121124
});
122125
}

server/src/Providers/Formatters/Formatter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { fileURLToPath } from "url";
2+
import { Range, TextDocument, TextEdit } from "vscode-languageserver-textdocument";
23

34
import { WorkspaceFilesSystem } from "../../WorkspaceFilesSystem";
45
import { Logger } from "../../Logger";
56

6-
export default class Formatter {
7+
export default abstract class Formatter {
78
constructor(
89
protected readonly workspaceFilesSystem: WorkspaceFilesSystem,
910
protected readonly enabled: boolean,
@@ -21,4 +22,6 @@ export default class Formatter {
2122
return this.workspaceFilesSystem.getGlobPaths(glob).some((path) => path === documentPath);
2223
});
2324
}
25+
26+
protected abstract formatDocument(document: TextDocument, range: Range | null): Promise<TextEdit[] | null>;
2427
}

server/src/Tokenizer/Tokenizer.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ export default class Tokenizer {
110110
private getInlineFunctionParams(line: string, lineIndex: number, tokensArray: IToken[]) {
111111
const functionParamTokens = tokensArray.filter(
112112
(token) =>
113-
token.scopes.includes(LanguageScopes.functionParameter) || token.scopes.includes(LanguageScopes.variableIdentifer),
113+
token.scopes.includes(LanguageScopes.functionParameters) &&
114+
(token.scopes.includes(LanguageScopes.functionParameter) || token.scopes.includes(LanguageScopes.variableIdentifer)),
114115
);
115116

116117
return functionParamTokens.map((token) => {
@@ -167,7 +168,14 @@ export default class Tokenizer {
167168
while (!isLastParamsLine) {
168169
isLastParamsLine = Boolean(tokensArray.find((token) => token.scopes.includes(LanguageScopes.rightParametersRoundBracket)));
169170

170-
if (isLastParamsLine && Boolean(tokensArray.find((token) => token.scopes.includes(LanguageScopes.terminatorStatement)))) {
171+
if (
172+
isLastParamsLine &&
173+
Boolean(
174+
tokensArray.find(
175+
(token) => token.scopes.includes(LanguageScopes.terminatorStatement) && !token.scopes.includes(LanguageScopes.block),
176+
),
177+
)
178+
) {
171179
isFunctionDeclaration = true;
172180
}
173181

server/src/Tokenizer/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export enum LanguageScopes {
3535
functionCall = "meta.function-call.nss",
3636
functionDeclaration = "meta.function.nss",
3737
functionParameter = "variable.parameter.nss",
38+
functionParameters = "meta.function.definition.parameters.nss",
3839
block = "meta.block.nss",
3940
blockDeclaraction = "punctuation.section.block.begin.bracket.curly.nss",
4041
blockTermination = "punctuation.section.block.end.bracket.curly.nss",

server/src/WorkspaceFilesSystem/WorkspaceFilesSystem.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ export default class WorkspaceFilesSystem {
1818
public getGlobPaths(glob: string) {
1919
return new GlobSync(glob).found.map((filename) => this.normalizedAbsolutePath(filename));
2020
}
21+
22+
public getWorkspaceRootPath() {
23+
return this.rootPath;
24+
}
2125
}

syntaxes/nwscript-ee.tmLanguage

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3730,7 +3730,7 @@ U[a-fA-F0-9]{,8} )</string>
37303730
<array>
37313731
<dict>
37323732
<key>match</key>
3733-
<string>(const){0,1} *\b(action|effect|event|float|int|itemproperty|location|object|string|talent|vector|void|json|sqlquery|cassowary)\b (?(1)(\S*)|)</string>
3733+
<string>(const){0,1} *\b(action|effect|event|float|int|itemproperty|location|object|string|talent|vector|void|json|sqlquery|cassowary)\b (?(1)([a-zA-Z0-9_]+)|)</string>
37343734
<key>captures</key>
37353735
<dict>
37363736
<key>1</key>

0 commit comments

Comments
 (0)