-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathino.ts
307 lines (296 loc) · 8.54 KB
/
ino.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
import { Mutex } from 'async-mutex';
import deepEqual from 'deep-equal';
import { promises as fs } from 'node:fs';
import vscode from 'vscode';
import {
CloseAction,
DocumentUri,
ErrorAction,
LanguageClient,
LanguageClientOptions,
Message,
NotificationType,
RevealOutputChannelOn,
} from 'vscode-languageclient';
import type { BoardIdentifier, DaemonAddress } from './typings';
export interface StartLanguageServerParams {
/**
* Absolute filesystem path to the Arduino Language Server executable.
*/
readonly lsPath: string;
/**
* The hostname and the port for the gRPC channel connecting to the Arduino CLI daemon.
* The `instance` number is for the initialized core Arduino client.
*/
readonly daemonAddress: DaemonAddress;
/**
* Absolute filesystem path to [`clangd`](https://clangd.llvm.org/).
*/
readonly clangdPath: string;
/**
* The board is relevant to start a specific "flavor" of the language.
*/
readonly board: BoardIdentifier;
/**
* `true` if the LS should generate the log files into the default location. The default location is the `cwd` of the process.
* It's very often the same as the workspace root of the IDE, aka the sketch folder.
* When it is a string, it is the absolute filesystem path to the folder to generate the log files.
* If `string`, but the path is inaccessible, the log files will be generated into the default location.
*/
readonly log?: boolean | string;
/**
* Optional `env` for the language server process.
*/
readonly env?: NodeJS.ProcessEnv;
/**
* Additional flags for the Arduino Language server process.
*/
readonly flags?: readonly string[];
/**
* Set to `true`, to enable `Diagnostics`.
*/
readonly realTimeDiagnostics?: boolean;
/**
* If `true`, the logging is not forwarded to the _Output_ view via the language client.
*/
readonly silentOutput?: boolean;
/**
* Number of async workers used by `clangd`. Background index also uses this many workers. If `0`, `clangd` uses all available cores. It's `0` by default.
*/
readonly jobs?: number;
}
/**
* The FQBN the language server runs with or `undefined` if it could not start.
*/
export type StartLanguageServerResult = string | undefined;
export interface DidCompleteBuildParams {
readonly buildOutputUri: DocumentUri;
}
export namespace DidCompleteBuildNotification {
export const TYPE = new NotificationType<DidCompleteBuildParams, void>(
'ino/didCompleteBuild'
);
}
let languageClient: LanguageClient | undefined;
let languageServerDisposable: vscode.Disposable | undefined;
let latestParams: StartLanguageServerParams | undefined;
let crashCount = 0;
const languageServerStartMutex = new Mutex();
// TODO: use later for `start`, `stop`, and `restart` language server. (https://code.visualstudio.com/api/references/when-clause-contexts)
let languageServerIsRunning = false;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function activateIno(_: vscode.ExtensionContext): vscode.Disposable {
const toDispose = [
vscode.commands.registerCommand(
'arduino.languageserver.start',
async (config: StartLanguageServerParams) => {
const unlock = await languageServerStartMutex.acquire();
try {
const started = await startLanguageServer(config);
languageServerIsRunning = started;
return languageServerIsRunning ? config.board.fqbn : undefined;
} catch (err) {
console.error('Failed to start the language server.', err);
languageServerIsRunning = false;
throw err;
} finally {
unlock();
}
}
),
vscode.commands.registerCommand('arduino.languageserver.stop', async () => {
const unlock = await languageServerStartMutex.acquire();
try {
await stopLanguageServer();
languageServerIsRunning = false;
} finally {
unlock();
}
}),
vscode.commands.registerCommand(
'arduino.languageserver.restart',
async () => {
if (!latestParams) {
return undefined;
}
return vscode.commands.executeCommand(
'arduino.languageserver.start',
latestParams
);
}
),
vscode.commands.registerCommand(
'arduino.languageserver.notifyBuildDidComplete',
(params: DidCompleteBuildParams) => {
if (languageClient) {
languageClient.sendNotification(
DidCompleteBuildNotification.TYPE,
params
);
} else {
vscode.window.showWarningMessage('Language server is not running.');
}
}
),
new vscode.Disposable(() => languageServerDisposable?.dispose()),
];
return vscode.Disposable.from(...toDispose);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async function stopLanguageServer(): Promise<void> {
if (languageClient) {
if (languageClient.diagnostics) {
languageClient.diagnostics.clear();
}
await languageClient.stop();
if (languageServerDisposable) {
languageServerDisposable.dispose();
}
}
}
async function startLanguageServer(
config: StartLanguageServerParams
): Promise<boolean> {
await stopLanguageServer();
if (!languageClient || !deepEqual(latestParams, config)) {
latestParams = config;
languageClient = await buildLanguageClient(config);
crashCount = 0;
}
languageServerDisposable = languageClient.start();
await languageClient.onReady();
return true;
}
async function buildLanguageClient(
config: StartLanguageServerParams
): Promise<LanguageClient> {
const {
lsPath: command,
clangdPath,
daemonAddress,
board,
flags,
env,
log,
jobs,
} = config;
const args = [
'-clangd',
clangdPath,
'-cli-daemon-addr',
`${daemonAddress.hostname}:${daemonAddress.port}`,
'-cli-daemon-instance',
String(daemonAddress.instance),
'-fqbn',
board.fqbn,
'-skip-libraries-discovery-on-rebuild', // The default Arduino IDE behavior
];
if (board.name) {
args.push('-board-name', board.name);
}
if (
typeof config.realTimeDiagnostics === 'boolean' &&
!config.realTimeDiagnostics
) {
args.push('-no-real-time-diagnostics');
}
if (flags && flags.length) {
args.push(...flags);
}
if (log) {
args.push('-log');
let logPath: string | undefined = undefined;
if (typeof log === 'string') {
try {
const stats = await fs.stat(log);
if (stats.isDirectory()) {
logPath = log;
}
} catch {}
}
if (logPath) {
args.push('-logpath', logPath);
}
}
// https://github.com/arduino/arduino-language-server/pull/177
if (jobs) {
args.push('-jobs', String(jobs));
}
const clientOptions: LanguageClientOptions = {
initializationOptions: {},
documentSelector: ['ino', 'c', 'cpp', 'cc', 'cxx', 'h', 'hpp', 'pde'],
uriConverters: {
code2Protocol: (uri: vscode.Uri): string =>
(uri.scheme ? uri : uri.with({ scheme: 'file' })).toString(),
protocol2Code: (uri: string) => vscode.Uri.parse(uri),
},
revealOutputChannelOn: RevealOutputChannelOn.Never,
initializationFailedHandler: (error): boolean => {
vscode.window.showErrorMessage(
`The language server is not able to serve any features. Initialization failed: ${error}.`
);
return false;
},
errorHandler: {
error: (error: Error, message: Message, count: number): ErrorAction => {
vscode.window.showErrorMessage(
`Error communicating with the language server: ${error}: ${message}.`
);
if (count < 5) {
return ErrorAction.Continue;
}
return ErrorAction.Shutdown;
},
closed: (): CloseAction => {
crashCount++;
if (crashCount < 5) {
return CloseAction.Restart;
}
return CloseAction.DoNotRestart;
},
},
};
if (!!config.silentOutput) {
clientOptions.outputChannel = noopOutputChannel('Arduino Language Server');
}
const serverOptions = {
command,
args,
options: { env },
};
return new LanguageClient(
'ino',
'Arduino Language Server',
serverOptions,
clientOptions
);
}
const noopChannel: Omit<vscode.OutputChannel, 'name'> = {
append: () => {
// NOOP
},
appendLine: () => {
// NOOP
},
clear: () => {
// NOOP
},
dispose: () => {
// NOOP
},
hide: () => {
// NOOP
},
show: () => {
// NOOP
},
replace: () => {
// NOOP
},
};
function noopOutputChannel(name: string): vscode.OutputChannel {
return {
...noopChannel,
name,
};
}