forked from arduino/arduino-ide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathino-language.ts
359 lines (347 loc) · 11.8 KB
/
ino-language.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import {
Disposable,
DisposableCollection,
} from '@theia/core/lib/common/disposable';
import { inject, injectable } from '@theia/core/shared/inversify';
import { Mutex } from 'async-mutex';
import {
ArduinoDaemon,
BoardIdentifier,
BoardsService,
CompileSummary,
ExecutableService,
isBoardIdentifierChangeEvent,
sanitizeFqbn,
} from '../../common/protocol';
import {
defaultAsyncWorkers,
maxAsyncWorkers,
minAsyncWorkers,
} from '../arduino-preferences';
import { BoardsDataStore } from '../boards/boards-data-store';
import { BoardsServiceProvider } from '../boards/boards-service-provider';
import { HostedPluginEvents } from '../hosted/hosted-plugin-events';
import { NotificationCenter } from '../notification-center';
import { CurrentSketch } from '../sketches-service-client-impl';
import { SketchContribution, URI } from './contribution';
import { CompileSummaryProvider } from './verify-sketch';
interface DaemonAddress {
/**
* The host where the Arduino CLI daemon is available.
*/
readonly hostname: string;
/**
* The port where the Arduino CLI daemon is listening.
*/
readonly port: number;
/**
* The [id](https://arduino.github.io/arduino-cli/latest/rpc/commands/#instance) of the initialized core Arduino client instance.
*/
readonly instance: number;
}
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: { fqbn: string; name?: string };
/**
* `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.
*/
type StartLanguageServerResult = string | undefined;
@injectable()
export class InoLanguage extends SketchContribution {
@inject(HostedPluginEvents)
private readonly hostedPluginEvents: HostedPluginEvents;
@inject(ExecutableService)
private readonly executableService: ExecutableService;
@inject(ArduinoDaemon)
private readonly daemon: ArduinoDaemon;
@inject(BoardsService)
private readonly boardsService: BoardsService;
@inject(BoardsServiceProvider)
private readonly boardsServiceProvider: BoardsServiceProvider;
@inject(NotificationCenter)
private readonly notificationCenter: NotificationCenter;
@inject(BoardsDataStore)
private readonly boardDataStore: BoardsDataStore;
@inject(CompileSummaryProvider)
private readonly compileSummaryProvider: CompileSummaryProvider;
private readonly toDispose = new DisposableCollection();
private readonly languageServerStartMutex = new Mutex();
private languageServerFqbn?: string;
override onReady(): void {
const start = (
selectedBoard: BoardIdentifier | undefined,
forceStart = false
) => {
if (selectedBoard) {
const { name, fqbn } = selectedBoard;
if (fqbn) {
this.startLanguageServer(fqbn, name, forceStart);
}
}
};
const forceRestart = () => {
start(this.boardsServiceProvider.boardsConfig.selectedBoard, true);
};
this.toDispose.pushAll([
this.boardsServiceProvider.onBoardsConfigDidChange((event) => {
if (isBoardIdentifierChangeEvent(event)) {
start(event.selectedBoard);
}
}),
this.hostedPluginEvents.onPluginsDidStart(() =>
start(this.boardsServiceProvider.boardsConfig.selectedBoard)
),
this.hostedPluginEvents.onPluginsWillUnload(
() => (this.languageServerFqbn = undefined)
),
this.preferences.onPreferenceChanged(
({ preferenceName, oldValue, newValue }) => {
if (oldValue !== newValue) {
switch (preferenceName) {
case 'arduino.language.log':
case 'arduino.language.realTimeDiagnostics':
case 'arduino.language.asyncWorkers':
forceRestart();
}
}
}
),
this.notificationCenter.onLibraryDidInstall(() => forceRestart()),
this.notificationCenter.onLibraryDidUninstall(() => forceRestart()),
this.notificationCenter.onPlatformDidInstall(() => forceRestart()),
this.notificationCenter.onPlatformDidUninstall(() => forceRestart()),
this.notificationCenter.onDidReinitialize(() => forceRestart()),
this.boardDataStore.onDidChange((event) => {
if (this.languageServerFqbn) {
const sanitizedFQBN = sanitizeFqbn(this.languageServerFqbn);
// The incoming FQBNs might contain custom boards configs, sanitize them before the comparison.
// https://github.com/arduino/arduino-ide/pull/2113#pullrequestreview-1499998328
const matchingChange = event.changes.find(
(change) => sanitizedFQBN === sanitizeFqbn(change.fqbn)
);
const { boardsConfig } = this.boardsServiceProvider;
if (
matchingChange &&
boardsConfig.selectedBoard?.fqbn === matchingChange.fqbn
) {
start(boardsConfig.selectedBoard);
}
}
}),
this.compileSummaryProvider.onDidChangeCompileSummary(
(compileSummary) => {
if (compileSummary) {
this.fireBuildDidComplete(compileSummary);
}
}
),
]);
Promise.all([
this.boardsServiceProvider.ready,
this.preferences.ready,
]).then(() => {
start(this.boardsServiceProvider.boardsConfig.selectedBoard);
});
}
onStop(): void {
this.toDispose.dispose();
}
private async startLanguageServer(
fqbn: string,
name: string | undefined,
forceStart = false
): Promise<void> {
const port = await this.daemon.tryGetPort();
if (typeof port !== 'number') {
return;
}
const release = await this.languageServerStartMutex.acquire();
const toDisposeOnRelease = new DisposableCollection();
try {
await this.hostedPluginEvents.didStart;
const details = await this.boardsService.getBoardDetails({ fqbn });
if (!details) {
// Core is not installed for the selected board.
console.info(
`Could not start language server for ${fqbn}. The core is not installed for the board.`
);
if (this.languageServerFqbn) {
try {
await this.commandService.executeCommand(
'arduino.languageserver.stop'
);
console.info(
`Stopped language server process for ${this.languageServerFqbn}.`
);
this.languageServerFqbn = undefined;
} catch (e) {
console.error(
`Failed to start language server process for ${this.languageServerFqbn}`,
e
);
throw e;
}
}
return;
}
const fqbnWithConfig = await this.boardDataStore.appendConfigToFqbn(fqbn);
if (!fqbnWithConfig) {
throw new Error(
`Failed to append boards config to the FQBN. Original FQBN was: ${fqbn}`
);
}
if (!forceStart && fqbnWithConfig === this.languageServerFqbn) {
// NOOP
return;
}
const log = this.preferences.get('arduino.language.log');
const realTimeDiagnostics = this.preferences.get(
'arduino.language.realTimeDiagnostics'
);
const jobs = this.getAsyncWorkersPreferenceSafe();
this.logger.info(
`Starting language server: ${fqbnWithConfig}${
jobs ? ` (async worker count: ${jobs})` : ''
}`
);
let currentSketchPath: string | undefined = undefined;
if (log) {
const currentSketch = await this.sketchServiceClient.currentSketch();
if (CurrentSketch.isValid(currentSketch)) {
currentSketchPath = await this.fileService.fsPath(
new URI(currentSketch.uri)
);
}
}
const { clangdUri, lsUri } = await this.executableService.list();
const [clangdPath, lsPath] = await Promise.all([
this.fileService.fsPath(new URI(clangdUri)),
this.fileService.fsPath(new URI(lsUri)),
]);
this.languageServerFqbn = await Promise.race([
new Promise<undefined>((_, reject) => {
const timer = setTimeout(
() => reject(new Error(`Timeout after ${20_000} ms.`)),
20_000
);
toDisposeOnRelease.push(Disposable.create(() => clearTimeout(timer)));
}),
this.start({
lsPath,
daemonAddress: {
hostname: 'localhost',
port,
instance: 1, // TODO: get it from the backend
},
clangdPath,
log: currentSketchPath ? currentSketchPath : log,
board: {
fqbn: fqbnWithConfig,
name,
},
realTimeDiagnostics,
silentOutput: true,
jobs,
}),
]);
} catch (e) {
console.log(`Failed to start language server. Original FQBN: ${fqbn}`, e);
this.languageServerFqbn = undefined;
} finally {
toDisposeOnRelease.dispose();
release();
}
}
// The Theia preference UI validation is bogus.
// To restrict the number of jobs to a valid value.
private getAsyncWorkersPreferenceSafe(): number {
const jobs = this.preferences.get(
'arduino.language.asyncWorkers',
defaultAsyncWorkers
);
if (jobs < minAsyncWorkers) {
return minAsyncWorkers;
}
if (jobs > maxAsyncWorkers) {
return maxAsyncWorkers;
}
return jobs;
}
private async start(
params: StartLanguageServerParams
): Promise<StartLanguageServerResult | undefined> {
return this.commandService.executeCommand<StartLanguageServerResult>(
'arduino.languageserver.start',
params
);
}
// Execute the a command contributed by the Arduino Tools VSIX to send the `ino/buildDidComplete` notification to the language server
private async fireBuildDidComplete(
compileSummary: CompileSummary
): Promise<void> {
const params = {
...compileSummary,
};
console.info(
`Executing 'arduino.languageserver.notifyBuildDidComplete' with ${JSON.stringify(
params.buildOutputUri
)}`
);
try {
await this.commandService.executeCommand(
'arduino.languageserver.notifyBuildDidComplete',
params
);
} catch (err) {
console.error(
`Unexpected error when firing event on build did complete. ${JSON.stringify(
params.buildOutputUri
)}`,
err
);
}
}
}