-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathextension.ts
More file actions
465 lines (410 loc) · 15.5 KB
/
extension.ts
File metadata and controls
465 lines (410 loc) · 15.5 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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
'use strict';
import * as vscode from 'vscode';
import { LanguageClient, LanguageClientOptions, ServerOptions, TextDocumentFilter } from 'vscode-languageclient/node';
import * as net from 'net';
import * as child_process from 'child_process';
import { existsSync } from 'fs';
interface Invoking {
kind: 'invoking';
workspaceFolder: vscode.WorkspaceFolder;
process: child_process.ChildProcessWithoutNullStreams;
}
interface Running {
kind: 'running';
workspaceFolder: vscode.WorkspaceFolder;
client: LanguageClient;
}
type State = Invoking | Running;
const CONFIGURATION_ROOT_SECTION = 'typeprof';
let statusBarItem: vscode.StatusBarItem;
function addToggleButton(context: vscode.ExtensionContext) {
statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100);
const disposable = vscode.commands.registerCommand(
'typeprof.toggle',
(arg0: any, arg1: any, arg2: any, arg3: any) => {
if (statusBarItem.text === 'TypeProf $(eye)') {
statusBarItem.text = 'TypeProf $(eye-closed)';
vscode.commands.executeCommand('typeprof.disableSignature');
} else {
statusBarItem.text = 'TypeProf $(eye)';
vscode.commands.executeCommand('typeprof.enableSignature');
}
},
);
context.subscriptions.push(disposable);
}
function showToggleBar() {
statusBarItem.text = 'TypeProf $(eye)';
statusBarItem.command = 'typeprof.toggle';
statusBarItem.show();
}
function addJumpToRBS(context: vscode.ExtensionContext) {
const disposable = vscode.commands.registerCommand(
'typeprof.jumpToRBS',
(arg0: any, arg1: any, arg2: any, arg3: any) => {
const uri0 = vscode.Uri.parse(arg0);
const pos0 = new vscode.Position(arg1.line, arg1.character);
const uri1 = vscode.Uri.parse(arg2);
const pos1 = new vscode.Position(arg3.start.line, arg3.start.character);
const pos2 = new vscode.Position(arg3.end.line, arg3.end.character);
const range = new vscode.Range(pos1, pos2);
const loc = new vscode.Location(uri1, range);
vscode.commands.executeCommand('editor.action.peekLocations', uri0, pos0, [loc], 'peek');
},
);
context.subscriptions.push(disposable);
}
let progressBarItem: vscode.StatusBarItem;
function addJumpToOutputChannel(context: vscode.ExtensionContext) {
progressBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 0);
progressBarItem.command = 'typeprof.jumpToOutputChannel';
const disposable = vscode.commands.registerCommand('typeprof.jumpToOutputChannel', () => {
outputChannel.show();
progressBarItem.hide();
});
context.subscriptions.push(disposable);
}
function showErrorStatusBar() {
statusBarItem.text = '$(error) TypeProf';
statusBarItem.command = 'typeprof.jumpToOutputChannel';
statusBarItem.show();
}
function executeTypeProf(folder: vscode.WorkspaceFolder, arg: String): child_process.ChildProcessWithoutNullStreams {
const configuration = vscode.workspace.getConfiguration(CONFIGURATION_ROOT_SECTION);
const customServerPath = configuration.get<string | null>('server.path');
const cwd = folder.uri.fsPath;
let cmd: string;
if (existsSync(`${cwd}/bin/typeprof`)) {
cmd = './bin/typeprof';
} else if (customServerPath) {
cmd = customServerPath;
} else if (existsSync(`${cwd}/Gemfile`)) {
cmd = 'bundle exec typeprof';
} else {
cmd = 'typeprof';
}
cmd = cmd + ' ' + arg;
const shell = process.env.SHELL;
let typeprof: child_process.ChildProcessWithoutNullStreams;
if (shell && (shell.endsWith('bash') || shell.endsWith('zsh') || shell.endsWith('fish'))) {
const args: string[] = [];
if (shell.endsWith('zsh')) {
// As the recommended way, initialization commands for rbenv are written in ".zshrc".
// However, it's not loaded on the non-interactive shell.
// Thus, we need to run this command as the interactive shell.
// FYI: https://zsh.sourceforge.io/Guide/zshguide02.html
args.push('-i');
}
args.push('-l', '-c', cmd);
typeprof = child_process.spawn(shell, args, { cwd });
} else if (process.platform === 'win32') {
typeprof = child_process.spawn(process.env.SYSTEMROOT + '\\System32\\cmd.exe', ['/c', cmd], { cwd });
} else {
const cmds = cmd.split(' ');
typeprof = child_process.spawn(cmds[0], cmds.slice(1), { cwd });
}
return typeprof;
}
function getTypeProfVersion(
folder: vscode.WorkspaceFolder,
callback: (err: Error | null, version: string) => void,
): child_process.ChildProcessWithoutNullStreams {
const typeprof = executeTypeProf(folder, '--version');
let output = '';
const log = (msg: string) => {
outputChannel.appendLine('[vscode] ' + msg);
console.info(msg);
};
typeprof.stdout?.on('data', (out) => {
output += out;
});
typeprof.stderr?.on('data', (out: Buffer) => {
const str = ('' + out).trim();
for (const line of str.split('\n')) {
log('stderr: ' + line);
}
});
typeprof.on('error', (e) => {
log(`typeprof is not supported for this folder: ${folder.name}`);
log(`because: ${e}`);
});
typeprof.on('exit', (code) => {
if (code === 0) {
const str = output.trim();
log(`typeprof version: ${str}`);
const version = /typeprof (\d+.\d+.\d+)$/.exec(str);
if (version && version.length === 2) {
if (compareVersions(version[1], '0.20.0') >= 0) {
callback(null, version[1]);
} else {
const err = new Error(
`typeprof version ${str} is too old; please use 0.20.0 or later for IDE feature`,
);
log(err.message);
callback(err, '');
}
} else {
const err = new Error(`typeprof --version showed unknown message`);
log(err.message);
callback(err, '');
}
} else {
const err = new Error(`failed to invoke typeprof: error code ${code}`);
log(err.message);
callback(err, '');
}
typeprof.kill();
});
return typeprof;
}
function getTypeProfStream(
folder: vscode.WorkspaceFolder,
error: (msg: string) => void,
): Promise<{ host: string; port: number; pid: number; stop: () => void }> {
return new Promise((resolve, reject) => {
const typeprof = executeTypeProf(folder, '--lsp');
let buffer = '';
typeprof.stdout.on('data', (data) => {
buffer += data;
try {
const json = JSON.parse(data);
json['stop'] = () => typeprof.kill('SIGINT');
resolve(json);
} catch (err) {}
});
let err = '';
typeprof.stderr.on('data', (data) => {
err += data;
while (true) {
const i = err.indexOf('\n');
if (i < 0) {
break;
}
error(err.slice(0, i));
err = err.slice(i + 1);
}
});
typeprof.on('exit', (code) => reject(`error code ${code}`));
});
}
function invokeTypeProf(version: string, folder: vscode.WorkspaceFolder): LanguageClient {
const reportError = (msg: string) => client?.info(msg);
const serverOptions: ServerOptions = async () => {
const { host, port, stop } = await getTypeProfStream(folder, reportError);
const socket: net.Socket = net.createConnection(port, host);
socket.on('close', (_hadError) => stop());
return {
reader: socket,
writer: socket,
};
};
const documentSelector: TextDocumentFilter[] = [
{ scheme: 'file', language: 'ruby' },
{ scheme: 'file', language: 'rbs' },
];
if (compareVersions(version, '0.30.1') < 0) {
// I don't know why, but this prevents the notification of changes of RBS files.
// This is needed because the old version of TypeProf does not support RBS changes.
documentSelector[0].pattern = '**/*.rb';
}
const clientOptions: LanguageClientOptions = {
documentSelector,
outputChannel,
synchronize: {
fileEvents: vscode.workspace.createFileSystemWatcher('{**/*.rb,**/*.rbs}'),
},
};
const configuration = vscode.workspace.getConfiguration(CONFIGURATION_ROOT_SECTION);
const trace = configuration.get<string>('trace.server');
if (trace !== 'off') {
traceOutputChannel = vscode.window.createOutputChannel('Ruby TypeProf(server)', 'typeprof');
clientOptions.traceOutputChannel = traceOutputChannel;
}
return new LanguageClient('typeprof', 'Ruby TypeProf', serverOptions, clientOptions);
}
const clientSessions: Map<vscode.WorkspaceFolder, State> = new Map();
const failedTimeoutSec = 10000;
let client: LanguageClient | undefined;
function startTypeProf(context: vscode.ExtensionContext, folder: vscode.WorkspaceFolder) {
const showStatus = (msg: string) => {
outputChannel.appendLine('[vscode] ' + msg);
progressBarItem.text = `$(sync~spin) ${msg}`;
};
showStatus('Try to start TypeProf for IDE');
progressBarItem.show();
const typeprof = getTypeProfVersion(folder, async (err, version) => {
if (err !== null) {
showStatus(`Ruby TypeProf is not configured`);
setTimeout(() => {
showFailedStatus();
}, failedTimeoutSec);
clientSessions.delete(folder);
return;
}
showStatus(`Starting Ruby TypeProf (${version})...`);
client = invokeTypeProf(version, folder);
await client.start();
showStatus('Ruby TypeProf is running');
if (compareVersions(version, '0.21.8') >= 0) {
// When `typeprof.restart` is executed without opening Ruby program, the message in the progress bar is not hidden.
// Thus, we need to set timeout here.
setTimeout(() => progressBarItem.hide(), 3000);
context.subscriptions.push(
client.onNotification('typeprof.enableToggleButton', () => {
enableToggleButton();
}),
client.onNotification('typeprof.showErrorStatus', () => {
showFailedStatus();
client?.stop();
}),
);
} else {
// The old version does not support `typeprof.enableToggleButton`.
// The toggle button is displayed after a few seconds then.
setTimeout(() => enableToggleButton(), 3000);
}
clientSessions.set(folder, { kind: 'running', workspaceFolder: folder, client });
});
clientSessions.set(folder, { kind: 'invoking', workspaceFolder: folder, process: typeprof });
}
function showFailedStatus() {
progressBarItem.hide();
showErrorStatusBar();
}
function enableToggleButton() {
progressBarItem.hide();
showToggleBar();
}
function stopTypeProf(state: State) {
switch (state.kind) {
case 'invoking':
state.process.kill();
break;
case 'running':
state.client.stop();
break;
}
clientSessions.delete(state.workspaceFolder);
}
function restartTypeProf(context: vscode.ExtensionContext) {
if (!vscode.workspace.workspaceFolders) {
return;
}
stopAllSessions();
for (const folder of vscode.workspace.workspaceFolders) {
if (folder.uri.scheme === 'file') {
let state = clientSessions.get(folder);
if (state) {
stopTypeProf(state);
}
startTypeProf(context, folder);
break;
}
}
}
function ensureTypeProf(context: vscode.ExtensionContext) {
if (!vscode.workspace.workspaceFolders) {
return;
}
const activeFolders = new Set(vscode.workspace.workspaceFolders);
clientSessions.forEach((state) => {
if (!activeFolders.has(state.workspaceFolder)) {
stopTypeProf(state);
}
});
for (const folder of activeFolders) {
if (folder.uri.scheme === 'file' && !clientSessions.has(folder)) {
startTypeProf(context, folder);
break;
}
}
}
function addRestartCommand(context: vscode.ExtensionContext) {
const disposable = vscode.commands.registerCommand('typeprof.restart', () => {
progressBarItem.hide();
statusBarItem.hide();
outputChannel.clear();
if (traceOutputChannel) {
traceOutputChannel.dispose();
}
restartTypeProf(context);
});
context.subscriptions.push(disposable);
}
function addInitCommand(context: vscode.ExtensionContext) {
const disposable = vscode.commands.registerCommand('typeprof.init', () => {
if (!vscode.workspace.workspaceFolders) {
return;
}
for (const folder of vscode.workspace.workspaceFolders) {
if (folder.uri.scheme === 'file') {
let typeprof = executeTypeProf(folder, '--init');
typeprof.stdout.on('data', (data) => {
let mes = ('' + data).trim();
if (mes === 'invalid option: --init') {
vscode.window.showErrorMessage(
'The version of TypeProf does not support the `--init` option. Please check the version of TypeProf and update it.',
);
}
});
typeprof.stderr.on('data', (data) => {
vscode.window.showErrorMessage(`Failed to generate TypeProf config file: ${data}`);
});
break;
}
}
});
context.subscriptions.push(disposable);
}
let outputChannel: vscode.OutputChannel;
let traceOutputChannel: vscode.OutputChannel | undefined;
export function activate(context: vscode.ExtensionContext) {
outputChannel = vscode.window.createOutputChannel('Ruby TypeProf');
addInitCommand(context);
addToggleButton(context);
addJumpToOutputChannel(context);
addJumpToRBS(context);
addRestartCommand(context);
ensureTypeProf(context);
}
function stopAllSessions() {
clientSessions.forEach((state) => {
stopTypeProf(state);
});
}
export function deactivate() {
progressBarItem.dispose();
statusBarItem.dispose();
stopAllSessions();
if (client !== undefined) {
return client.stop();
}
}
const versionRegexp = /^(\d+).(\d+).(\d+)$/;
// compareVersions returns the following values:
// v1 === v2 => return 0
// v1 > v2 => return 1
// v1 < v2 => return -1
function compareVersions(v1: string, v2: string) {
const v1Versions = versionRegexp.exec(v1);
const v2Versions = versionRegexp.exec(v2);
if (v1Versions && v1Versions.length === 4 && v2Versions && v2Versions.length === 4) {
return (
compareNumbers(v1Versions[1], v2Versions[1]) ||
compareNumbers(v1Versions[2], v2Versions[2]) ||
compareNumbers(v1Versions[3], v2Versions[3])
);
}
throw new Error('The format of version is invalid.');
}
function compareNumbers(v1: string, v2: string) {
if (v1 === v2) {
return 0;
}
const v1Num = Number(v1);
const v2Num = Number(v2);
if (v1Num > v2Num) {
return 1;
}
return -1;
}