-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathlanguage-client.ts
More file actions
99 lines (92 loc) · 3.17 KB
/
language-client.ts
File metadata and controls
99 lines (92 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import type { TextEditor } from "vscode";
import { ProgressLocation, window } from "vscode";
import type { StreamInfo } from "vscode-languageclient/node";
import { LanguageClient, NotificationType } from "vscode-languageclient/node";
import { shouldDebugLanguageServer, spawnServer } from "../codeql-cli/cli";
import type { QueryServerConfig } from "../config";
import { languageServerLogger } from "../common/logging/vscode";
/**
* Managing the language client and corresponding server process for CodeQL.
*/
/**
* Create a new CodeQL language client connected to a language server.
*/
export function createLanguageClient(
config: QueryServerConfig,
): CodeQLLanguageClient {
return new CodeQLLanguageClient(config);
}
/**
* CodeQL language client.
*/
export class CodeQLLanguageClient extends LanguageClient {
constructor(config: QueryServerConfig) {
super(
"codeQL.lsp",
"CodeQL Language Server",
() => spawnLanguageServer(config),
{
documentSelector: [
{ language: "ql", scheme: "file" },
{ language: "yaml", scheme: "file", pattern: "**/qlpack.yml" },
{ language: "yaml", scheme: "file", pattern: "**/codeql-pack.yml" },
],
synchronize: {
configurationSection: "codeQL",
},
// Ensure that language server exceptions are logged to the same channel as its output.
outputChannel: languageServerLogger.outputChannel,
},
true,
);
}
notifyVisibilityChange(editors: readonly TextEditor[]) {
// Only send notification if the language client is running to avoid race conditions
if (!this.isRunning()) {
return;
}
const files = editors
.filter((e) => e.document.uri.scheme === "file")
.map((e) => e.document.uri.toString());
void this.sendNotification(didChangeVisibileFiles, {
visibleFiles: files,
});
}
}
/** Starts a new CodeQL language server process, sending progress messages to the status bar. */
async function spawnLanguageServer(
config: QueryServerConfig,
): Promise<StreamInfo> {
return window.withProgress(
{ title: "CodeQL language server", location: ProgressLocation.Window },
async (progressReporter, _) => {
const args = ["--check-errors", "ON_CHANGE"];
if (shouldDebugLanguageServer()) {
args.push(
"-J=-agentlib:jdwp=transport=dt_socket,address=localhost:9009,server=y,suspend=n,quiet=y",
);
}
const child = spawnServer(
config.codeQlPath,
"CodeQL language server",
["execute", "language-server"],
args,
languageServerLogger,
(data) =>
languageServerLogger.log(data.toString(), { trailingNewline: false }),
(data) =>
languageServerLogger.log(data.toString(), { trailingNewline: false }),
progressReporter,
);
return { writer: child.stdin, reader: child.stdout };
},
);
}
/**
* Custom notification type for when the set of visible files changes.
*/
interface DidChangeVisibileFilesParams {
visibleFiles: string[];
}
const didChangeVisibileFiles: NotificationType<DidChangeVisibileFilesParams> =
new NotificationType("textDocument/codeQLDidChangeVisibleFiles");