Skip to content

Commit

Permalink
Handle files with their uri instead of their path (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeChab authored Aug 14, 2022
1 parent df3b6f9 commit a650fdc
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 50 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ All notable changes to the "nwscript-ee-language-server" extension will be docum
## [1.5.1]

- Eslint has been configured along with prettier and the project will be linted from now on.
- File handling is now done with their uri instead of their path.
8 changes: 4 additions & 4 deletions server/src/Documents/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ export type OwnedStructComplexTokens = { owner: string; tokens: StructComplexTok

export default class Document {
constructor(
readonly path: string,
readonly uri: string,
readonly children: string[],
readonly complexTokens: ComplexToken[],
readonly structComplexTokens: StructComplexToken[],
private readonly collection: DocumentsCollection,
) {}

public getKey() {
return this.collection.getKey(this.path);
return this.collection.getKey(this.uri);
}

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

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

public getGlobalStructComplexTokensWithRef(computedChildren: string[] = []): OwnedStructComplexTokens[] {
return [{ owner: this.path, tokens: this.structComplexTokens }].concat(
return [{ owner: this.uri, tokens: this.structComplexTokens }].concat(
this.children.flatMap((child) => {
// Cycling children or/and duplicates
if (computedChildren.includes(child)) {
Expand Down
20 changes: 9 additions & 11 deletions server/src/Documents/DocumentsCollection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { basename, join } from "path";
import { fileURLToPath } from "url";
import { readFileSync } from "fs";
import { TextDocument } from "vscode-languageserver-textdocument";

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

private initializeDocument(filePath: string, globalScope: GlobalScopeTokenizationResult) {
return new Document(filePath, globalScope.children, globalScope.complexTokens, globalScope.structComplexTokens, this);
private initializeDocument(uri: string, globalScope: GlobalScopeTokenizationResult) {
return new Document(uri, globalScope.children, globalScope.complexTokens, globalScope.structComplexTokens, this);
}

public getKey(path: string) {
return basename(path, FILES_EXTENSION).slice(0, -1);
public getKey(uri: string) {
return basename(uri, FILES_EXTENSION).slice(0, -1);
}

public getFromPath(path: string) {
return this.get(this.getKey(path));
public getFromUri(uri: string) {
return this.get(this.getKey(uri));
}

public createDocument(filePath: string, globalScope: GlobalScopeTokenizationResult) {
this.addDocument(this.initializeDocument(filePath, globalScope));
public createDocument(uri: string, globalScope: GlobalScopeTokenizationResult) {
this.addDocument(this.initializeDocument(uri, globalScope));
}

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

this.overwriteDocument(this.initializeDocument(filePath, globalScope));
this.overwriteDocument(this.initializeDocument(document.uri, globalScope));
}
}
4 changes: 1 addition & 3 deletions server/src/Providers/CompletionItemsProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { fileURLToPath } from "url";
import { CompletionParams } from "vscode-languageserver";

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

const liveDocument = this.server.liveDocumentsManager.get(uri);
const path = fileURLToPath(uri);
const document = this.server.documentsCollection.getFromPath(path);
const document = this.server.documentsCollection.getFromUri(uri);

if (liveDocument) {
const localScope = this.server.tokenizer?.tokenizeContent(liveDocument.getText(), TokenizedScope.local, 0, position.line);
Expand Down
38 changes: 18 additions & 20 deletions server/src/Providers/DiagnosticsProvider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { spawn } from "child_process";
import { type } from "os";
import { join, dirname, basename } from "path";
import { fileURLToPath, pathToFileURL } from "url";
import { fileURLToPath } from "url";
import { Diagnostic, DiagnosticSeverity } from "vscode-languageserver";

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

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

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

files[fileUri].push(diagnostic);
files[uri].push(diagnostic);
}
};
}
Expand Down Expand Up @@ -86,8 +85,7 @@ export default class DiagnoticsProvider extends Provider {
return reject(new Error(errorMessage));
}

const path = fileURLToPath(uri);
const document = this.server.documentsCollection.getFromPath(path);
const document = this.server.documentsCollection.getFromUri(uri);

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

const children = document.getChildren();
const files: FilesDiagnostics = { [pathToFileURL(document.path).toString()]: [] };
const paths: string[] = [];
const files: FilesDiagnostics = { [document.uri]: [] };
const uris: string[] = [];
children.forEach((child) => {
const filePath = this.server.documentsCollection?.get(child)?.path;
if (filePath) {
files[pathToFileURL(filePath).toString()] = [];
paths.push(filePath);
const fileUri = this.server.documentsCollection?.get(child)?.uri;
if (fileUri) {
files[fileUri] = [];
uris.push(fileUri);
}
});

if (verbose) {
this.server.logger.info(`Compiling ${basename(document.path)}:`);
this.server.logger.info(`Compiling ${document.uri}:`);
}
// The compiler command:
// - y; continue on error
Expand All @@ -133,9 +131,9 @@ export default class DiagnoticsProvider extends Provider {
}
if (children.length > 0) {
args.push("-i");
args.push(`"${[...new Set(paths.map((path) => dirname(path)))].join(";")}"`);
args.push(`"${[...new Set(uris.map((uri) => dirname(fileURLToPath(uri))))].join(";")}"`);
}
args.push(`"${path}"`);
args.push(`"${fileURLToPath(uri)}"`);

let stdout = "";
let stderr = "";
Expand Down Expand Up @@ -200,9 +198,9 @@ export default class DiagnoticsProvider extends Provider {
this.server.logger.info("Done.\n");
}

paths.push(document.path);
errors.forEach(this.generateDiagnostic(paths, files, DiagnosticSeverity.Error));
warnings.forEach(this.generateDiagnostic(paths, files, DiagnosticSeverity.Warning));
uris.push(document.uri);
errors.forEach(this.generateDiagnostic(uris, files, DiagnosticSeverity.Error));
warnings.forEach(this.generateDiagnostic(uris, files, DiagnosticSeverity.Warning));

for (const [uri, diagnostics] of Object.entries(files)) {
this.sendDiagnostics(uri, diagnostics);
Expand Down
6 changes: 2 additions & 4 deletions server/src/Providers/GotoDefinitionProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { fileURLToPath, pathToFileURL } from "url";
import { CompletionItemKind, DefinitionParams } from "vscode-languageserver";

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

const liveDocument = this.server.liveDocumentsManager.get(uri);
const path = fileURLToPath(uri);
const document = this.server.documentsCollection.getFromPath(path);
const document = this.server.documentsCollection.getFromUri(uri);

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

if (token) {
return {
uri: ref ? pathToFileURL(ref.owner).toString() : uri,
uri: ref ? ref.owner : uri,
range: {
start: { line: token.position.line, character: token.position.character },
end: { line: token.position.line, character: token.position.character },
Expand Down
4 changes: 1 addition & 3 deletions server/src/Providers/HoverContentProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { fileURLToPath } from "url";
import { CompletionItemKind, HoverParams } from "vscode-languageserver";

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

const liveDocument = this.server.liveDocumentsManager.get(uri);
const path = fileURLToPath(uri);
const document = this.server.documentsCollection.getFromPath(path);
const document = this.server.documentsCollection.getFromUri(uri);

if (liveDocument && this.server.tokenizer) {
let token: ComplexToken | undefined;
Expand Down
4 changes: 1 addition & 3 deletions server/src/Providers/SignatureHelpProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { fileURLToPath } from "url";
import { SignatureHelpParams } from "vscode-languageserver/node";

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

const liveDocument = this.server.liveDocumentsManager.get(uri);
const path = fileURLToPath(uri);
const document = this.server.documentsCollection.getFromPath(path);
const document = this.server.documentsCollection.getFromUri(uri);

let functionComplexToken: ComplexToken | undefined;
if (liveDocument) {
Expand Down
5 changes: 3 additions & 2 deletions server/src/ServerManager/ServerManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { Connection, InitializeParams } from "vscode-languageserver";
import { cpus } from "os";
import { join } from "path";
import { pathToFileURL } from "url";
import * as clustering from "cluster";
import type { Connection, InitializeParams } from "vscode-languageserver";

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

0 comments on commit a650fdc

Please sign in to comment.