-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathextension.ts
314 lines (299 loc) · 11.5 KB
/
extension.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
import * as path from 'path';
import { promises as fs } from 'fs';
import { homedir } from 'os';
import { spawn } from 'child_process';
import deepEqual from 'deep-equal';
import WebRequest from 'web-request';
import deepmerge from 'deepmerge';
import { Mutex } from 'async-mutex';
import vscode, { ExtensionContext, OutputChannel } from 'vscode';
import { LanguageClient, CloseAction, ErrorAction, InitializeError, Message, RevealOutputChannelOn, LanguageClientOptions } from 'vscode-languageclient';
import { DidCompleteBuildNotification, DidCompleteBuildParams } from './protocol';
interface LanguageServerConfig {
readonly lsPath: string;
readonly cliDaemonAddr: string;
readonly cliDaemonInstance: string;
readonly clangdPath: string;
readonly board: {
readonly fqbn: string;
readonly name?: string;
}
/**
* `true` if the LS should generate log files into the default location. The default location is `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 folder where the log files should be generated. If the path is invalid (does not exist, not a folder),
* the log files will be generated into the default location.
*/
readonly log?: boolean | string;
readonly env?: any;
readonly flags?: string[];
readonly realTimeDiagnostics?: boolean;
readonly silentOutput?: boolean;
}
interface DebugConfig {
readonly cliPath?: string;
readonly cliDaemonAddr?: string;
readonly board: {
readonly fqbn: string;
readonly name?: string;
}
readonly sketchPath: string;
/**
* Location where the `launch.config` will be created on the fly before starting every debug session.
* If not defined, it falls back to `sketchPath/.vscode/launch.json`.
*/
readonly configPath?: string;
/**
* Absolute path to the `arduino-cli.yaml` file. If not specified, it falls back to `~/.arduinoIDE/arduino-cli.yaml`.
*/
readonly cliConfigPath?: string;
}
interface DebugInfo {
readonly executable: string;
readonly toolchain: string;
readonly toolchain_path: string;
readonly toolchain_prefix: string;
readonly server: string;
readonly server_path: string;
readonly server_configuration: {
readonly path: string;
readonly script: string;
readonly scripts_dir: string;
}
}
let languageClient: LanguageClient | undefined;
let languageServerDisposable: vscode.Disposable | undefined;
let latestConfig: LanguageServerConfig | undefined;
let crashCount = 0;
const languageServerStartMutex = new Mutex();
export let languageServerIsRunning = false; // TODO: use later for `start`, `stop`, and `restart` language server.
export function activate(context: ExtensionContext) {
context.subscriptions.push(
vscode.commands.registerCommand('arduino.languageserver.start', async (config: LanguageServerConfig) => {
const unlock = await languageServerStartMutex.acquire();
try {
const started = await startLanguageServer(context, 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(context);
languageServerIsRunning = false;
} finally {
unlock();
}
}),
vscode.commands.registerCommand('arduino.languageserver.restart', async () => {
if (latestConfig) {
return vscode.commands.executeCommand('arduino.languageserver.start', latestConfig);
}
}),
vscode.commands.registerCommand('arduino.debug.start', (config: DebugConfig) => startDebug(context, config)),
vscode.commands.registerCommand('arduino.languageserver.notifyBuildDidComplete', (params: DidCompleteBuildParams) => {
if (languageClient) {
languageClient.sendNotification(DidCompleteBuildNotification.TYPE, params);
} else {
vscode.window.showWarningMessage('Language server is not running.');
}
})
);
}
async function exec(command: string, args: string[]): Promise<{ stdout: string, stderr: string }> {
return new Promise<{ stdout: string, stderr: string }>((resolve, reject) => {
let out = '';
let err = '';
const cp = spawn(command, args);
cp.stdout.on('data', data => out += data.toString());
cp.stderr.on('data', data => err += data.toString());
cp.on('error', reject);
cp.on('close', (code, signal) => {
const stdout = out.trim();
const stderr = err.trim();
if (code) {
reject(new Error(stderr ?? `Exit code: ${code}`));
}
if (signal) {
reject(new Error(stderr ?? `Exit signal: ${signal}`));
}
if (err.trim()) {
reject(new Error(stderr));
}
resolve({ stdout, stderr });
});
});
}
function resolveCliConfigPath(config: DebugConfig): string {
return config.cliConfigPath ?? path.join(homedir(), '.arduinoIDE', 'arduino-cli.yaml');
}
async function startDebug(_: ExtensionContext, config: DebugConfig): Promise<boolean> {
const cliConfigPath = resolveCliConfigPath(config);
let info: DebugInfo | undefined = undefined;
try {
const args = ['debug', '-I', '-b', config.board.fqbn, config.sketchPath, '--format', 'json', '--config-file', cliConfigPath];
const { stdout, stderr } = await exec(config?.cliPath || '.', args);
if (!stdout && stderr) {
throw new Error(stderr);
}
info = JSON.parse(stdout);
if (!info) {
return false;
}
} catch (err) {
throw err;
}
const defaultDebugConfig = {
cwd: '${workspaceRoot}',
name: 'Arduino',
request: 'launch',
type: 'cortex-debug',
executable: info.executable,
servertype: info.server,
serverpath: info.server_path,
armToolchainPath: info.toolchain_path,
configFiles: [
info.server_configuration.script
]
};
let customDebugConfig = {};
try {
const raw = await fs.readFile(path.join(config.sketchPath, 'debug_custom.json'), { encoding: 'utf8' });
customDebugConfig = JSON.parse(raw);
} catch { }
const mergedDebugConfig = deepmerge(defaultDebugConfig, customDebugConfig);
const launchConfig = {
version: '0.2.0',
'configurations': [
{
...mergedDebugConfig
}
]
};
await updateLaunchConfig(config, launchConfig);
return vscode.debug.startDebugging(undefined, mergedDebugConfig);
}
async function stopLanguageServer(context: ExtensionContext): Promise<void> {
if (languageClient) {
if (languageClient.diagnostics) {
languageClient.diagnostics.clear();
}
await languageClient.stop();
if (languageServerDisposable) {
languageServerDisposable.dispose();
}
}
}
async function startLanguageServer(context: ExtensionContext, config: LanguageServerConfig): Promise<boolean> {
await stopLanguageServer(context);
if (!languageClient || !deepEqual(latestConfig, config)) {
latestConfig = config;
languageClient = await buildLanguageClient(config);
crashCount = 0;
}
languageServerDisposable = languageClient.start();
context.subscriptions.push(languageServerDisposable);
await languageClient.onReady();
return true;
}
async function buildLanguageClient(config: LanguageServerConfig): Promise<LanguageClient> {
const { lsPath: command, clangdPath, cliDaemonAddr, cliDaemonInstance, board, flags, env, log } = config;
const args = ['-clangd', clangdPath, '-cli-daemon-addr', cliDaemonAddr, '-cli-daemon-instance', cliDaemonInstance, '-fqbn', board.fqbn, '-skip-libraries-discovery-on-rebuild'];
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);
}
}
const clientOptions = {
initializationOptions: {},
documentSelector: ['ino', 'c', 'cpp', '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: WebRequest.ResponseError<InitializeError>): 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;
}
}
} as LanguageClientOptions;
if (!!config.silentOutput) {
clientOptions.outputChannel = noopOutputChannel('Arduino Language Server');
}
const serverOptions = {
command,
args,
options: { env },
};
return new LanguageClient(
'ino',
'Arduino Language Server',
serverOptions,
clientOptions
);
}
/**
* Instead of writing the `launch.json` to the workspace, the file is written to the temporary binary output location.
*/
async function updateLaunchConfig(debugConfig: DebugConfig, launchConfig: object): Promise<void> {
if (debugConfig.configPath) {
await fs.mkdir(debugConfig.configPath, { recursive: true });
await fs.writeFile(path.join(debugConfig.configPath, 'launch.json'), JSON.stringify(launchConfig, null, 2));
} else {
const configuration = vscode.workspace.getConfiguration();
await configuration.update('launch', launchConfig, false);
}
}
function noopOutputChannel(name: string): OutputChannel {
return {
append: () => {},
appendLine: () => {},
clear: () => {},
dispose: () => {},
hide: () => {},
show: () => {},
name
};
}