-
-
Notifications
You must be signed in to change notification settings - Fork 170
/
ClientConnectionManager.ts
331 lines (290 loc) · 10.3 KB
/
ClientConnectionManager.ts
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import * as vscode from "vscode";
import * as fs from "fs";
import GDScriptLanguageClient, { ClientStatus } from "./GDScriptLanguageClient";
import {
get_configuration,
get_free_port,
get_project_version,
get_project_dir,
set_context,
register_command,
set_configuration,
} from "../utils";
import { createLogger } from "../logger";
import { execSync } from "child_process";
import { subProcess, killSubProcesses } from '../utils/subspawn';
const log = createLogger("lsp.manager");
enum ManagerStatus {
INITIALIZING,
INITIALIZING_LSP,
PENDING,
PENDING_LSP,
DISCONNECTED,
CONNECTED,
RETRYING,
}
export class ClientConnectionManager {
private context: vscode.ExtensionContext;
public client: GDScriptLanguageClient = null;
private reconnection_attempts = 0;
private status: ManagerStatus = ManagerStatus.INITIALIZING;
private statusWidget: vscode.StatusBarItem = null;
constructor(p_context: vscode.ExtensionContext) {
this.context = p_context;
this.client = new GDScriptLanguageClient(p_context);
this.client.watch_status(this.on_client_status_changed.bind(this));
setInterval(() => {
this.retry_callback();
}, get_configuration("lsp.autoReconnect.cooldown"));
register_command("startLanguageServer", () => {
this.start_language_server();
this.reconnection_attempts = 0;
this.client.connect_to_server();
});
register_command("stopLanguageServer", this.stop_language_server.bind(this));
register_command("checkStatus", this.on_status_item_click.bind(this));
set_context("connectedToLSP", false);
this.statusWidget = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right);
this.statusWidget.command = "godotTools.checkStatus";
this.statusWidget.show();
this.update_status_widget();
this.connect_to_language_server();
}
private async connect_to_language_server() {
this.client.port = -1;
if (get_configuration("lsp.headless")) {
await this.start_language_server();
}
this.reconnection_attempts = 0;
this.client.connect_to_server();
}
private stop_language_server() {
killSubProcesses('LSP');
}
private async start_language_server() {
this.stop_language_server();
const projectDir = await get_project_dir();
if (!projectDir) {
vscode.window.showErrorMessage("Current workspace is not a Godot project");
return;
}
const projectVersion = await get_project_version();
let minimumVersion = '6';
let targetVersion = '3.6';
if (projectVersion.startsWith('4')) {
minimumVersion = '2';
targetVersion = '4.2';
}
const settingName = `editorPath.godot${projectVersion[0]}`;
const godotPath = get_configuration(settingName);
try {
const output = execSync(`${godotPath} --version`).toString().trim();
const pattern = /([34])\.([0-9]+)\.(?:[0-9]+\.)?\w+.\w+.[0-9a-f]{9}/;
const match = output.match(pattern);
if (!match) {
const message = `Cannot launch headless LSP: '${settingName}' of '${godotPath}' is not a valid Godot executable`;
vscode.window.showErrorMessage(message, "Select Godot executable", "Ignore").then(item => {
if (item == "Select Godot executable") {
this.select_godot_executable(settingName);
}
});
return;
}
if (match[1] !== projectVersion[0]) {
const message = `Cannot launch headless LSP: The current project uses Godot v${projectVersion}, but the specified Godot executable is version ${match[0]}`;
vscode.window.showErrorMessage(message, "Select Godot executable", "Ignore").then(item => {
if (item == "Select Godot executable") {
this.select_godot_executable(settingName);
}
});
return;
}
if (match[2] < minimumVersion) {
const message = `Cannot launch headless LSP: Headless LSP mode is only available on version ${targetVersion} or newer, but the specified Godot executable is version ${match[0]}.`;
vscode.window.showErrorMessage(message, "Select Godot executable", "Disable Headless LSP", "Ignore").then(item => {
if (item == "Select Godot executable") {
this.select_godot_executable(settingName);
} else if (item == "Disable Headless LSP") {
set_configuration("lsp.headless", false);
this.prompt_for_reload();
}
});
return;
}
} catch (e) {
const message = `Cannot launch headless LSP: ${settingName} of ${godotPath} is not a valid Godot executable`;
vscode.window.showErrorMessage(message, "Select Godot executable", "Ignore").then(item => {
if (item == "Select Godot executable") {
this.select_godot_executable(settingName);
}
});
return;
}
this.client.port = await get_free_port();
log.info(`starting headless LSP on port ${this.client.port}`);
const headlessFlags = "--headless --no-window";
const command = `${godotPath} --path "${projectDir}" --editor ${headlessFlags} --lsp-port ${this.client.port}`;
const lspProcess = subProcess("LSP", command, { shell: true });
const lspStdout = createLogger("lsp.stdout");
lspProcess.stdout.on('data', (data) => {
const out = data.toString().trim();
if (out) {
lspStdout.debug(out);
}
});
// const lspStderr = createLogger("lsp.stderr");
// lspProcess.stderr.on('data', (data) => {
// const out = data.toString().trim();
// if (out) {
// lspStderr.debug(out);
// }
// });
lspProcess.on('close', (code) => {
log.info(`LSP process exited with code ${code}`);
});
}
private async select_godot_executable(settingName: string) {
vscode.window.showOpenDialog({
openLabel: "Select Godot executable",
filters: process.platform === "win32" ? { "Godot Editor Binary": ["exe", "EXE"] } : undefined
}).then(async (uris: vscode.Uri[]) => {
if (!uris) {
return;
}
const path = uris[0].fsPath;
set_configuration(settingName, path);
this.prompt_for_reload();
});
}
private async prompt_for_reload() {
const message = `Reload VSCode to apply settings`;
vscode.window.showErrorMessage(message, "Reload").then(item => {
if (item == "Reload") {
vscode.commands.executeCommand("workbench.action.reloadWindow");
}
});
}
private get_lsp_connection_string() {
let host = get_configuration("lsp.serverHost");
let port = get_configuration("lsp.serverPort");
if (this.client.port !== -1) {
port = this.client.port;
}
return `${host}:${port}`;
}
private on_status_item_click() {
const lsp_target = this.get_lsp_connection_string();
// TODO: fill these out with the ACTIONS a user could perform in each state
switch (this.status) {
case ManagerStatus.INITIALIZING:
// vscode.window.showInformationMessage("Initializing extension");
break;
case ManagerStatus.INITIALIZING_LSP:
// vscode.window.showInformationMessage("Initializing LSP");
break;
case ManagerStatus.PENDING:
// vscode.window.showInformationMessage(`Connecting to the GDScript language server at ${lsp_target}`);
break;
case ManagerStatus.CONNECTED:
// vscode.window.showInformationMessage("Connected to the GDScript language server.");
break;
case ManagerStatus.DISCONNECTED:
this.retry_connect_client();
break;
case ManagerStatus.RETRYING:
break;
}
}
private update_status_widget() {
const lsp_target = this.get_lsp_connection_string();
switch (this.status) {
case ManagerStatus.INITIALIZING:
// this.statusWidget.text = `INITIALIZING`;
this.statusWidget.text = `$(sync~spin) Initializing`;
this.statusWidget.tooltip = `Initializing extension...`;
break;
case ManagerStatus.INITIALIZING_LSP:
// this.statusWidget.text = `INITIALIZING_LSP ` + this.reconnection_attempts;
this.statusWidget.text = `$(sync~spin) Initializing LSP`;
this.statusWidget.tooltip = `Connecting to headless GDScript language server at ${lsp_target}`;
break;
case ManagerStatus.PENDING:
// this.statusWidget.text = `PENDING`;
this.statusWidget.text = `$(sync~spin) Connecting`;
this.statusWidget.tooltip = `Connecting to the GDScript language server at ${lsp_target}`;
break;
case ManagerStatus.CONNECTED:
// this.statusWidget.text = `CONNECTED`;
this.statusWidget.text = `$(check) Connected`;
this.statusWidget.tooltip = `Connected to the GDScript language server.`;
break;
case ManagerStatus.DISCONNECTED:
// this.statusWidget.text = `DISCONNECTED`;
this.statusWidget.text = `$(x) Disconnected`;
this.statusWidget.tooltip = `Disconnected from the GDScript language server.`;
break;
case ManagerStatus.RETRYING:
// this.statusWidget.text = `RETRYING ` + this.reconnection_attempts;
this.statusWidget.text = `$(sync~spin) Connecting ` + this.reconnection_attempts;
this.statusWidget.tooltip = `Connecting to the GDScript language server at ${lsp_target}`;
break;
}
}
private on_client_status_changed(status: ClientStatus) {
switch (status) {
case ClientStatus.PENDING:
this.status = ManagerStatus.PENDING;
break;
case ClientStatus.CONNECTED:
this.retry = false;
set_context("connectedToLSP", true);
this.status = ManagerStatus.CONNECTED;
if (!this.client.started) {
this.context.subscriptions.push(this.client.start());
}
break;
case ClientStatus.DISCONNECTED:
set_context("connectedToLSP", false);
if (this.retry) {
if (this.client.port != -1) {
this.status = ManagerStatus.INITIALIZING_LSP;
} else {
this.status = ManagerStatus.RETRYING;
}
} else {
this.status = ManagerStatus.DISCONNECTED;
}
this.retry = true;
break;
default:
break;
}
this.update_status_widget();
}
private retry = false;
private retry_callback() {
if (this.retry) {
this.retry_connect_client();
}
}
private retry_connect_client() {
const auto_retry = get_configuration("lsp.autoReconnect.enabled");
const max_attempts = get_configuration("lsp.autoReconnect.attempts");
if (auto_retry && this.reconnection_attempts <= max_attempts - 1) {
this.reconnection_attempts++;
this.client.connect_to_server();
this.retry = true;
return;
}
this.retry = false;
this.status = ManagerStatus.DISCONNECTED;
this.update_status_widget();
const lsp_target = this.get_lsp_connection_string();
let message = `Couldn't connect to the GDScript language server at ${lsp_target}. Is the Godot editor or language server running?`;
vscode.window.showErrorMessage(message, "Retry", "Ignore").then(item => {
if (item == "Retry") {
this.connect_to_language_server();
}
});
}
}