-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathlanguageServer.ts
More file actions
98 lines (82 loc) · 3.43 KB
/
languageServer.ts
File metadata and controls
98 lines (82 loc) · 3.43 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
import * as path from "path";
import * as vscode from "vscode";
import {Commands} from "@actions/languageserver/commands";
import {InitializationOptions, LogLevel} from "@actions/languageserver/initializationOptions";
import {ReadFileRequest, Requests} from "@actions/languageserver/request";
import {BaseLanguageClient, LanguageClientOptions} from "vscode-languageclient";
import {LanguageClient as BrowserLanguageClient} from "vscode-languageclient/browser";
import {LanguageClient as NodeLanguageClient, ServerOptions, TransportKind} from "vscode-languageclient/node";
import {userAgent} from "../api/api";
import {getSession} from "../auth/auth";
import {getGitHubContext} from "../git/repository";
import {WorkflowSelector, ActionSelector} from "./documentSelector";
import {getGitHubApiUri, useEnterprise} from "../configuration/configuration";
let client: BaseLanguageClient;
/** Helper function determining whether we are executing with node runtime */
function isNode(): boolean {
return typeof process !== "undefined" && process.versions?.node != null;
}
export async function initLanguageServer(context: vscode.ExtensionContext) {
const session = await getSession();
const ghContext = await getGitHubContext();
const initializationOptions: InitializationOptions = {
sessionToken: session?.accessToken,
userAgent: userAgent,
gitHubApiUrl: useEnterprise() ? getGitHubApiUri() : undefined,
repos: ghContext?.repos.map(repo => ({
id: repo.id,
owner: repo.owner,
name: repo.name,
workspaceUri: repo.workspaceUri.toString(),
organizationOwned: repo.organizationOwned
})),
logLevel: PRODUCTION ? LogLevel.Warn : LogLevel.Debug,
experimentalFeatures: {
allowCaseFunction: true
}
};
const clientOptions: LanguageClientOptions = {
documentSelector: [WorkflowSelector, ActionSelector],
initializationOptions: initializationOptions,
progressOnInitialization: true
};
// Create the language client and start the client.
if (isNode()) {
const debugOptions = {execArgv: ["--nolazy", "--inspect=6010"]};
const serverModule = context.asAbsolutePath(path.join("dist", "server-node.js"));
const serverOptions: ServerOptions = {
run: {module: serverModule, transport: TransportKind.ipc},
debug: {
module: serverModule,
transport: TransportKind.ipc,
options: debugOptions
}
};
client = new NodeLanguageClient("actions-language", "GitHub Actions Language Server", serverOptions, clientOptions);
} else {
const serverModule = vscode.Uri.joinPath(context.extensionUri, "dist", "server-web.js");
const worker = new Worker(serverModule.toString());
client = new BrowserLanguageClient("actions-language", "GitHub Actions Language Server", clientOptions, worker);
}
client.onRequest(Requests.ReadFile, async (event: ReadFileRequest) => {
if (typeof event?.path !== "string") {
return null;
}
const uri = vscode.Uri.parse(event?.path);
const content = await vscode.workspace.fs.readFile(uri);
return new TextDecoder().decode(content);
});
return client.start();
}
export function deactivateLanguageServer(): Promise<void> {
if (!client) {
return Promise.resolve();
}
return client.stop();
}
export function executeCacheClearCommand(): Promise<void> {
if (!client) {
return Promise.resolve();
}
return client.sendRequest("workspace/executeCommand", {command: Commands.ClearCache});
}