Skip to content

Commit a650fdc

Browse files
authored
Handle files with their uri instead of their path (#42)
1 parent df3b6f9 commit a650fdc

File tree

9 files changed

+40
-50
lines changed

9 files changed

+40
-50
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ All notable changes to the "nwscript-ee-language-server" extension will be docum
4343
## [1.5.1]
4444

4545
- Eslint has been configured along with prettier and the project will be linted from now on.
46+
- File handling is now done with their uri instead of their path.

server/src/Documents/Document.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ export type OwnedStructComplexTokens = { owner: string; tokens: StructComplexTok
66

77
export default class Document {
88
constructor(
9-
readonly path: string,
9+
readonly uri: string,
1010
readonly children: string[],
1111
readonly complexTokens: ComplexToken[],
1212
readonly structComplexTokens: StructComplexToken[],
1313
private readonly collection: DocumentsCollection,
1414
) {}
1515

1616
public getKey() {
17-
return this.collection.getKey(this.path);
17+
return this.collection.getKey(this.uri);
1818
}
1919

2020
public getChildren(computedChildren: string[] = []): string[] {
@@ -39,7 +39,7 @@ export default class Document {
3939
}
4040

4141
public getGlobalComplexTokensWithRef(computedChildren: string[] = []): OwnedComplexTokens[] {
42-
return [{ owner: this.path, tokens: this.complexTokens }].concat(
42+
return [{ owner: this.uri, tokens: this.complexTokens }].concat(
4343
this.children.flatMap((child) => {
4444
// Cycling children or/and duplicates
4545
if (computedChildren.includes(child)) {
@@ -81,7 +81,7 @@ export default class Document {
8181
}
8282

8383
public getGlobalStructComplexTokensWithRef(computedChildren: string[] = []): OwnedStructComplexTokens[] {
84-
return [{ owner: this.path, tokens: this.structComplexTokens }].concat(
84+
return [{ owner: this.uri, tokens: this.structComplexTokens }].concat(
8585
this.children.flatMap((child) => {
8686
// Cycling children or/and duplicates
8787
if (computedChildren.includes(child)) {

server/src/Documents/DocumentsCollection.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { basename, join } from "path";
2-
import { fileURLToPath } from "url";
32
import { readFileSync } from "fs";
43
import { TextDocument } from "vscode-languageserver-textdocument";
54

@@ -29,26 +28,25 @@ export default class DocumentsCollection extends Dictionnary<string, Document> {
2928
this.overwrite(document.getKey(), document);
3029
}
3130

32-
private initializeDocument(filePath: string, globalScope: GlobalScopeTokenizationResult) {
33-
return new Document(filePath, globalScope.children, globalScope.complexTokens, globalScope.structComplexTokens, this);
31+
private initializeDocument(uri: string, globalScope: GlobalScopeTokenizationResult) {
32+
return new Document(uri, globalScope.children, globalScope.complexTokens, globalScope.structComplexTokens, this);
3433
}
3534

36-
public getKey(path: string) {
37-
return basename(path, FILES_EXTENSION).slice(0, -1);
35+
public getKey(uri: string) {
36+
return basename(uri, FILES_EXTENSION).slice(0, -1);
3837
}
3938

40-
public getFromPath(path: string) {
41-
return this.get(this.getKey(path));
39+
public getFromUri(uri: string) {
40+
return this.get(this.getKey(uri));
4241
}
4342

44-
public createDocument(filePath: string, globalScope: GlobalScopeTokenizationResult) {
45-
this.addDocument(this.initializeDocument(filePath, globalScope));
43+
public createDocument(uri: string, globalScope: GlobalScopeTokenizationResult) {
44+
this.addDocument(this.initializeDocument(uri, globalScope));
4645
}
4746

4847
public updateDocument(document: TextDocument, tokenizer: Tokenizer) {
49-
const filePath = fileURLToPath(document.uri);
5048
const globalScope = tokenizer.tokenizeContent(document.getText(), TokenizedScope.global);
5149

52-
this.overwriteDocument(this.initializeDocument(filePath, globalScope));
50+
this.overwriteDocument(this.initializeDocument(document.uri, globalScope));
5351
}
5452
}

server/src/Providers/CompletionItemsProvider.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { fileURLToPath } from "url";
21
import { CompletionParams } from "vscode-languageserver";
32

43
import type { ServerManager } from "../ServerManager";
@@ -27,8 +26,7 @@ export default class CompletionItemsProvider extends Provider {
2726
} = params;
2827

2928
const liveDocument = this.server.liveDocumentsManager.get(uri);
30-
const path = fileURLToPath(uri);
31-
const document = this.server.documentsCollection.getFromPath(path);
29+
const document = this.server.documentsCollection.getFromUri(uri);
3230

3331
if (liveDocument) {
3432
const localScope = this.server.tokenizer?.tokenizeContent(liveDocument.getText(), TokenizedScope.local, 0, position.line);

server/src/Providers/DiagnosticsProvider.ts

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { spawn } from "child_process";
22
import { type } from "os";
33
import { join, dirname, basename } from "path";
4-
import { fileURLToPath, pathToFileURL } from "url";
4+
import { fileURLToPath } from "url";
55
import { Diagnostic, DiagnosticSeverity } from "vscode-languageserver";
66

77
import { ServerManager } from "../ServerManager";
@@ -34,12 +34,11 @@ export default class DiagnoticsProvider extends Provider {
3434
this.server.connection.sendDiagnostics({ uri, diagnostics });
3535
}
3636

37-
private generateDiagnostic(paths: string[], files: FilesDiagnostics, severity: DiagnosticSeverity) {
37+
private generateDiagnostic(uris: string[], files: FilesDiagnostics, severity: DiagnosticSeverity) {
3838
return (line: string) => {
39-
const path = paths.find((path) => basename(path) === lineFilename.exec(line)![0]);
39+
const uri = uris.find((uri) => basename(fileURLToPath(uri)) === lineFilename.exec(line)![0]);
4040

41-
if (path) {
42-
const fileUri = pathToFileURL(path).toString();
41+
if (uri) {
4342
const linePosition = Number(lineNumber.exec(line)![1]) - 1;
4443
const diagnostic = {
4544
severity,
@@ -50,7 +49,7 @@ export default class DiagnoticsProvider extends Provider {
5049
message: lineMessage.exec(line)![1].trim(),
5150
};
5251

53-
files[fileUri].push(diagnostic);
52+
files[uri].push(diagnostic);
5453
}
5554
};
5655
}
@@ -86,8 +85,7 @@ export default class DiagnoticsProvider extends Provider {
8685
return reject(new Error(errorMessage));
8786
}
8887

89-
const path = fileURLToPath(uri);
90-
const document = this.server.documentsCollection.getFromPath(path);
88+
const document = this.server.documentsCollection.getFromUri(uri);
9189

9290
if (!this.server.hasIndexedDocuments || !document) {
9391
if (!this.server.documentsWaitingForPublish.includes(uri)) {
@@ -97,18 +95,18 @@ export default class DiagnoticsProvider extends Provider {
9795
}
9896

9997
const children = document.getChildren();
100-
const files: FilesDiagnostics = { [pathToFileURL(document.path).toString()]: [] };
101-
const paths: string[] = [];
98+
const files: FilesDiagnostics = { [document.uri]: [] };
99+
const uris: string[] = [];
102100
children.forEach((child) => {
103-
const filePath = this.server.documentsCollection?.get(child)?.path;
104-
if (filePath) {
105-
files[pathToFileURL(filePath).toString()] = [];
106-
paths.push(filePath);
101+
const fileUri = this.server.documentsCollection?.get(child)?.uri;
102+
if (fileUri) {
103+
files[fileUri] = [];
104+
uris.push(fileUri);
107105
}
108106
});
109107

110108
if (verbose) {
111-
this.server.logger.info(`Compiling ${basename(document.path)}:`);
109+
this.server.logger.info(`Compiling ${document.uri}:`);
112110
}
113111
// The compiler command:
114112
// - y; continue on error
@@ -133,9 +131,9 @@ export default class DiagnoticsProvider extends Provider {
133131
}
134132
if (children.length > 0) {
135133
args.push("-i");
136-
args.push(`"${[...new Set(paths.map((path) => dirname(path)))].join(";")}"`);
134+
args.push(`"${[...new Set(uris.map((uri) => dirname(fileURLToPath(uri))))].join(";")}"`);
137135
}
138-
args.push(`"${path}"`);
136+
args.push(`"${fileURLToPath(uri)}"`);
139137

140138
let stdout = "";
141139
let stderr = "";
@@ -200,9 +198,9 @@ export default class DiagnoticsProvider extends Provider {
200198
this.server.logger.info("Done.\n");
201199
}
202200

203-
paths.push(document.path);
204-
errors.forEach(this.generateDiagnostic(paths, files, DiagnosticSeverity.Error));
205-
warnings.forEach(this.generateDiagnostic(paths, files, DiagnosticSeverity.Warning));
201+
uris.push(document.uri);
202+
errors.forEach(this.generateDiagnostic(uris, files, DiagnosticSeverity.Error));
203+
warnings.forEach(this.generateDiagnostic(uris, files, DiagnosticSeverity.Warning));
206204

207205
for (const [uri, diagnostics] of Object.entries(files)) {
208206
this.sendDiagnostics(uri, diagnostics);

server/src/Providers/GotoDefinitionProvider.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { fileURLToPath, pathToFileURL } from "url";
21
import { CompletionItemKind, DefinitionParams } from "vscode-languageserver";
32

43
import type { OwnedComplexTokens, OwnedStructComplexTokens } from "../Documents/Document";
@@ -22,8 +21,7 @@ export default class GotoDefinitionProvider extends Provider {
2221
} = params;
2322

2423
const liveDocument = this.server.liveDocumentsManager.get(uri);
25-
const path = fileURLToPath(uri);
26-
const document = this.server.documentsCollection.getFromPath(path);
24+
const document = this.server.documentsCollection.getFromUri(uri);
2725

2826
if (liveDocument && this.server.tokenizer) {
2927
let token: ComplexToken | undefined;
@@ -90,7 +88,7 @@ export default class GotoDefinitionProvider extends Provider {
9088

9189
if (token) {
9290
return {
93-
uri: ref ? pathToFileURL(ref.owner).toString() : uri,
91+
uri: ref ? ref.owner : uri,
9492
range: {
9593
start: { line: token.position.line, character: token.position.character },
9694
end: { line: token.position.line, character: token.position.character },

server/src/Providers/HoverContentProvider.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { fileURLToPath } from "url";
21
import { CompletionItemKind, HoverParams } from "vscode-languageserver";
32

43
import type { ServerManager } from "../ServerManager";
@@ -22,8 +21,7 @@ export default class HoverContentProvider extends Provider {
2221
} = params;
2322

2423
const liveDocument = this.server.liveDocumentsManager.get(uri);
25-
const path = fileURLToPath(uri);
26-
const document = this.server.documentsCollection.getFromPath(path);
24+
const document = this.server.documentsCollection.getFromUri(uri);
2725

2826
if (liveDocument && this.server.tokenizer) {
2927
let token: ComplexToken | undefined;

server/src/Providers/SignatureHelpProvider.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { fileURLToPath } from "url";
21
import { SignatureHelpParams } from "vscode-languageserver/node";
32

43
import type { ServerManager } from "../ServerManager";
@@ -24,8 +23,7 @@ export default class SignatureHelpProvider extends Provider {
2423
} = params;
2524

2625
const liveDocument = this.server.liveDocumentsManager.get(uri);
27-
const path = fileURLToPath(uri);
28-
const document = this.server.documentsCollection.getFromPath(path);
26+
const document = this.server.documentsCollection.getFromUri(uri);
2927

3028
let functionComplexToken: ComplexToken | undefined;
3129
if (liveDocument) {

server/src/ServerManager/ServerManager.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import type { Connection, InitializeParams } from "vscode-languageserver";
21
import { cpus } from "os";
32
import { join } from "path";
3+
import { pathToFileURL } from "url";
44
import * as clustering from "cluster";
5+
import type { Connection, InitializeParams } from "vscode-languageserver";
56

67
import {
78
CompletionItemsProvider,
@@ -90,7 +91,7 @@ export default class ServerManger {
9091
worker.send(filesPath.slice(i * partCount, Math.min((i + 1) * partCount, filesCount - 1)).join(","));
9192
worker.on("message", (message: string) => {
9293
const { filePath, globalScope } = JSON.parse(message);
93-
this.documentsCollection?.createDocument(filePath, globalScope);
94+
this.documentsCollection?.createDocument(pathToFileURL(filePath).href, globalScope);
9495
filesIndexedCount++;
9596
progressReporter?.report(filesIndexedCount / filesCount);
9697
});

0 commit comments

Comments
 (0)