From 8e45d0bed12dede0e9b77e7ebddadfe939cb6307 Mon Sep 17 00:00:00 2001 From: ecmel Date: Mon, 7 Oct 2024 13:42:08 +0300 Subject: [PATCH 1/3] fixed windows bug --- CHANGELOG.md | 1 + server/src/qLangServer.ts | 80 +++++++++++++++++++--------------- test/suite/qLangServer.test.ts | 2 + 3 files changed, 47 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eba4acd7..6c4cc33d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to the **kdb VS Code extension** are documented in this file ### Enhancements +- Add ability to add multiple labels to a single connection - Show KDB+ process explorer item content when clicked - Add ability to export and import connections - All files in the workspace are considered when using language server features diff --git a/server/src/qLangServer.ts b/server/src/qLangServer.ts index a45e17b1..ea13469f 100644 --- a/server/src/qLangServer.ts +++ b/server/src/qLangServer.ts @@ -49,7 +49,7 @@ import { TextEdit, WorkspaceEdit, } from "vscode-languageserver/node"; -import { glob } from "glob"; +import { sync as glob } from "glob"; import { fileURLToPath, pathToFileURL } from "node:url"; import { FindKind, @@ -73,7 +73,7 @@ import { RCurly, } from "./parser"; import { lint } from "./linter"; -import { readFile } from "node:fs"; +import { readFileSync } from "node:fs"; interface Settings { debug: boolean; @@ -168,16 +168,20 @@ export default class QLangServer { /* istanbul ignore next */ public onDidChangeWatchedFiles({ changes }: DidChangeWatchedFilesParams) { - this.parseFiles( - changes.reduce((matches, change) => { - if (change.type === FileChangeType.Deleted) { - this.cached.delete(change.uri); - } else { - matches.push(fileURLToPath(change.uri)); - } - return matches; - }, [] as string[]), - ); + try { + this.parseFiles( + changes.reduce((matches, change) => { + if (change.type === FileChangeType.Deleted) { + this.cached.delete(change.uri); + } else { + matches.push(fileURLToPath(change.uri)); + } + return matches; + }, [] as string[]), + ); + } catch (error) { + this.connection.window.showErrorMessage(`${error}`); + } } public onDidChangeContent({ @@ -443,32 +447,33 @@ export default class QLangServer { /* istanbul ignore next */ public scan() { - this.connection.workspace.getWorkspaceFolders().then((folders) => { - if (folders) { - folders.forEach((folder) => { - glob( - "**/*.{q,quke}", - { cwd: fileURLToPath(folder.uri), ignore: "node_modules/**" }, - (err, matches) => { - if (!err) { - this.parseFiles(matches); - } - }, + const folders = this.params.workspaceFolders; + if (folders) { + try { + for (const folder of folders) { + this.parseFiles( + glob("**/*.{q,quke}", { + dot: true, + absolute: true, + nodir: true, + follow: false, + ignore: "node_modules/**/*.*", + cwd: fileURLToPath(folder.uri), + }), ); - }); + } + } catch (error) { + this.connection.window.showErrorMessage(`${error}`); } - }); + } } /* istanbul ignore next */ private parseFiles(matches: string[]) { - matches.forEach((match) => - readFile(match, "utf-8", (err, file) => { - if (!err) { - this.cached.set(pathToFileURL(match).toString(), parse(file)); - } - }), - ); + for (const match of matches) { + const file = readFileSync(match, "utf-8"); + this.cached.set(pathToFileURL(match).toString(), parse(file)); + } } private parse(textDocument: TextDocumentIdentifier): Token[] { @@ -482,10 +487,13 @@ export default class QLangServer { private context({ uri, tokens }: Tokenized, all = true): Tokenized[] { if (all) { this.documents.all().forEach((document) => { - this.cached.set( - document.uri, - document.uri === uri ? tokens : parse(document.getText()), - ); + const path = fileURLToPath(document.uri); + if (path) { + this.cached.set( + pathToFileURL(path).toString(), + document.uri === uri ? tokens : parse(document.getText()), + ); + } }); return Array.from(this.cached.entries(), (entry) => ({ uri: entry[0], diff --git a/test/suite/qLangServer.test.ts b/test/suite/qLangServer.test.ts index a5568c8d..9c2828ae 100644 --- a/test/suite/qLangServer.test.ts +++ b/test/suite/qLangServer.test.ts @@ -404,6 +404,7 @@ describe("qLangServer", () => { .stub(connection.workspace, "getWorkspaceFolders") .value(async () => []); server.scan(); + assert.strictEqual(server["cached"].size, 0); }); }); @@ -413,6 +414,7 @@ describe("qLangServer", () => { .stub(connection.workspace, "getWorkspaceFolders") .value(async () => []); server.onDidChangeWatchedFiles({ changes: [] }); + assert.strictEqual(server["cached"].size, 0); }); }); }); From 7f24feb0a0c85d347bef823f23d9d226e34ea02c Mon Sep 17 00:00:00 2001 From: ecmel Date: Mon, 7 Oct 2024 13:47:27 +0300 Subject: [PATCH 2/3] fixed test setup --- test/suite/qLangServer.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/suite/qLangServer.test.ts b/test/suite/qLangServer.test.ts index 9c2828ae..22622c4f 100644 --- a/test/suite/qLangServer.test.ts +++ b/test/suite/qLangServer.test.ts @@ -31,9 +31,9 @@ describe("qLangServer", () => { function createDocument(content: string, offset?: number) { content = content.trim(); - const document = TextDocument.create("test.q", "q", 1, content); + const document = TextDocument.create("file:///test.q", "q", 1, content); const position = document.positionAt(offset || content.length); - const textDocument = TextDocumentIdentifier.create("test.q"); + const textDocument = TextDocumentIdentifier.create("file:///test.q"); sinon.stub(server.documents, "get").value(() => document); sinon.stub(server.documents, "all").value(() => [document]); return { From b9c970e3709bb1654f6a30397ead2a638cdbe10b Mon Sep 17 00:00:00 2001 From: ecmel Date: Mon, 7 Oct 2024 13:55:04 +0300 Subject: [PATCH 3/3] fix windows test setup --- test/suite/qLangServer.test.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/suite/qLangServer.test.ts b/test/suite/qLangServer.test.ts index 22622c4f..5d4e14c9 100644 --- a/test/suite/qLangServer.test.ts +++ b/test/suite/qLangServer.test.ts @@ -22,6 +22,7 @@ import { } from "vscode-languageserver"; import { TextDocument } from "vscode-languageserver-textdocument"; import QLangServer from "../../server/src/qLangServer"; +import { pathToFileURL } from "url"; const context = { includeDeclaration: true }; @@ -31,9 +32,10 @@ describe("qLangServer", () => { function createDocument(content: string, offset?: number) { content = content.trim(); - const document = TextDocument.create("file:///test.q", "q", 1, content); + const uri = pathToFileURL("test.q").toString(); + const document = TextDocument.create(uri, "q", 1, content); const position = document.positionAt(offset || content.length); - const textDocument = TextDocumentIdentifier.create("file:///test.q"); + const textDocument = TextDocumentIdentifier.create(uri); sinon.stub(server.documents, "get").value(() => document); sinon.stub(server.documents, "all").value(() => [document]); return {