Skip to content

Commit

Permalink
Merge pull request #436 from KxSystems/ee-fixes
Browse files Browse the repository at this point in the history
[LS] Fix multiple files windows bug
  • Loading branch information
ecmel authored Oct 7, 2024
2 parents 767e5ba + b9c970e commit 0c222ac
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
80 changes: 44 additions & 36 deletions server/src/qLangServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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[] {
Expand All @@ -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],
Expand Down
8 changes: 6 additions & 2 deletions test/suite/qLangServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand All @@ -31,9 +32,10 @@ describe("qLangServer", () => {

function createDocument(content: string, offset?: number) {
content = content.trim();
const document = TextDocument.create("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("test.q");
const textDocument = TextDocumentIdentifier.create(uri);
sinon.stub(server.documents, "get").value(() => document);
sinon.stub(server.documents, "all").value(() => [document]);
return {
Expand Down Expand Up @@ -404,6 +406,7 @@ describe("qLangServer", () => {
.stub(connection.workspace, "getWorkspaceFolders")
.value(async () => []);
server.scan();
assert.strictEqual(server["cached"].size, 0);
});
});

Expand All @@ -413,6 +416,7 @@ describe("qLangServer", () => {
.stub(connection.workspace, "getWorkspaceFolders")
.value(async () => []);
server.onDidChangeWatchedFiles({ changes: [] });
assert.strictEqual(server["cached"].size, 0);
});
});
});

0 comments on commit 0c222ac

Please sign in to comment.