/
extension.ts
368 lines (327 loc) · 12.1 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
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
import * as path from 'path';
import * as fs from 'fs';
import { workspace, window, commands, QuickPickItem, ExtensionContext, StatusBarAlignment, TextEditor,ThemeColor, Disposable, TextDocumentSaveReason, Uri } from 'vscode';
import {
LanguageClient, LanguageClientOptions, SettingMonitor, ServerOptions, TextEdit,
RequestType, TextDocumentIdentifier, ResponseError, InitializeError, State as ClientState, NotificationType, TransportKind,
ProposedProtocol
} from 'vscode-languageclient';
import { exec } from 'child_process';
import * as open from 'open';
interface AllFixesParams {
readonly textDocument: TextDocumentIdentifier;
readonly isOnSave: boolean;
}
interface AllFixesResult {
readonly documentVersion: number;
readonly edits: TextEdit[];
readonly ruleId?: string;
}
namespace AllFixesRequest {
export const type = new RequestType<AllFixesParams, AllFixesResult, void, void>('textDocument/tslint/allFixes');
}
interface NoTSLintLibraryParams {
readonly source: TextDocumentIdentifier;
}
interface NoTSLintLibraryResult {
}
namespace NoTSLintLibraryRequest {
export const type = new RequestType<NoTSLintLibraryParams, NoTSLintLibraryResult, void, void>('tslint/noLibrary');
}
enum Status {
ok = 1,
warn = 2,
error = 3
}
interface StatusParams {
state: Status;
}
namespace StatusNotification {
export const type = new NotificationType<StatusParams, void>('tslint/status');
}
let willSaveTextDocument: Disposable | undefined;
let configurationChangedListener: Disposable;
export function activate(context: ExtensionContext) {
let statusBarItem = window.createStatusBarItem(StatusBarAlignment.Right, 0);
let tslintStatus: Status = Status.ok;
let serverRunning: boolean = false;
statusBarItem.text = 'TSLint';
statusBarItem.command = 'tslint.showOutputChannel';
let errorColor = new ThemeColor('tslint.error');
let warningColor = new ThemeColor('tslint.warning');
console.log('Congratulations, the extension is working!');
function showStatusBarItem(show: boolean): void {
if (show) {
statusBarItem.show();
} else {
statusBarItem.hide();
}
}
function updateStatus(status: Status) {
switch (status) {
case Status.ok:
statusBarItem.color = undefined;
break;
case Status.warn:
statusBarItem.color = warningColor;
break;
case Status.error:
statusBarItem.color = errorColor; // darkred doesn't work
break;
}
if (tslintStatus !== Status.ok && status === Status.ok) { // an error got addressed fix, write to the output that the status is OK
client.info('vscode-tslint: Status is OK');
}
tslintStatus = status;
udpateStatusBarVisibility(window.activeTextEditor);
}
function isTypeScriptDocument(languageId) {
return languageId === 'typescript' || languageId === 'typescriptreact' || languageId === 'vue';
}
function isJavaScriptDocument(languageId) {
return languageId === 'javascript' || languageId === 'javascriptreact';
}
function isEnableForJavaScriptDocument(languageId) {
let isJsEnable = workspace.getConfiguration('tslint').get('jsEnable', true);
if (isJsEnable && isJavaScriptDocument(languageId)) {
return true;
}
return false;
}
function udpateStatusBarVisibility(editor: TextEditor | undefined): void {
switch (tslintStatus) {
case Status.ok:
statusBarItem.text = 'TSLint';
break;
case Status.warn:
statusBarItem.text = 'TSLint: Warning';
break;
case Status.error:
statusBarItem.text = 'TSLint: Error';
break;
}
let enabled = workspace.getConfiguration('tslint')['enable'];
if (!editor || !enabled) {
showStatusBarItem(false);
return;
}
showStatusBarItem(
serverRunning &&
(
tslintStatus !== Status.ok ||
((isTypeScriptDocument(editor.document.languageId) || isEnableForJavaScriptDocument(editor.document.languageId)))
)
);
}
window.onDidChangeActiveTextEditor(udpateStatusBarVisibility);
udpateStatusBarVisibility(window.activeTextEditor);
// We need to go one level up since an extension compile the js code into
// the output folder.
let serverModulePath = path.join(__dirname, '..', 'server', 'server.js');
// break on start options
//let debugOptions = { execArgv: ["--nolazy", "--debug=6010", "--debug-brk"] };
let debugOptions = { execArgv: ["--nolazy", "--inspect=6010"], cwd: process.cwd() };
let runOptions = {cwd: process.cwd()};
let serverOptions: ServerOptions = {
run: { module: serverModulePath, transport: TransportKind.ipc, options: runOptions },
debug: { module: serverModulePath, transport: TransportKind.ipc, options: debugOptions }
};
let clientOptions: LanguageClientOptions = {
documentSelector: ['typescript', 'typescriptreact', 'vue', 'javascript', 'javascriptreact'],
synchronize: {
configurationSection: 'tslint',
fileEvents: workspace.createFileSystemWatcher('**/tslint.json')
},
diagnosticCollectionName: 'tslint',
initializationFailedHandler: (error) => {
client.error('Server initialization failed.', error);
client.outputChannel.show(true);
return false;
},
};
let client = new LanguageClient('tslint', serverOptions, clientOptions);
client.registerFeatures(ProposedProtocol(client));
const running = 'Linter is running.';
const stopped = 'Linter has stopped.';
client.onDidChangeState((event) => {
if (event.newState === ClientState.Running) {
client.info(running);
statusBarItem.tooltip = running;
serverRunning = true;
} else {
client.info(stopped);
statusBarItem.tooltip = stopped;
serverRunning = false;
}
udpateStatusBarVisibility(window.activeTextEditor);
});
client.onReady().then(() => {
client.onNotification(StatusNotification.type, (params) => {
updateStatus(params.state);
});
client.onRequest(NoTSLintLibraryRequest.type, (params) => {
let uri: Uri = Uri.parse(params.source.uri);
if (workspace.rootPath) {
client.info([
'',
`Failed to load the TSLint library for the document ${uri.fsPath}`,
'',
'To use TSLint in this workspace please install tslint using \'npm install tslint\' or globally using \'npm install -g tslint\'.',
'TSLint has a peer dependency on `typescript`, make sure that `typescript` is installed as well.',
'You need to reopen the workspace after installing tslint.',
].join('\n'));
} else {
client.info([
`Failed to load the TSLint library for the document ${uri.fsPath}`,
'To use TSLint for single file install tslint globally using \'npm install -g tslint\'.',
'TSLint has a peer dependency on `typescript`, make sure that `typescript` is installed as well.',
'You need to reopen VS Code after installing tslint.',
].join('\n'));
}
updateStatus(Status.warn);
return {};
});
});
function applyTextEdits(uri: string, documentVersion: number, edits: TextEdit[]) {
let textEditor = window.activeTextEditor;
if (textEditor && textEditor.document.uri.toString() === uri) {
if (textEditor.document.version !== documentVersion) {
window.showInformationMessage(`TSLint fixes are outdated and can't be applied to the document.`);
}
textEditor.edit(mutator => {
for (let edit of edits) {
mutator.replace(client.protocol2CodeConverter.asRange(edit.range), edit.newText);
}
}).then((success) => {
if (!success) {
window.showErrorMessage('Failed to apply TSLint fixes to the document. Please consider opening an issue with steps to reproduce.');
}
});
}
}
function applyDisableRuleEdit(uri: string, documentVersion: number, edits: TextEdit[]) {
let textEditor = window.activeTextEditor;
if (textEditor && textEditor.document.uri.toString() === uri) {
if (textEditor.document.version !== documentVersion) {
window.showInformationMessage(`TSLint fixes are outdated and can't be applied to the document.`);
}
// prefix disable comment with same indent as line with the diagnostic
let edit = edits[0];
let ruleLine = textEditor.document.lineAt(edit.range.start.line);
let prefixIndex = ruleLine.firstNonWhitespaceCharacterIndex;
let prefix = ruleLine.text.substr(0, prefixIndex);
edit.newText = prefix + edit.newText;
applyTextEdits(uri, documentVersion, edits);
}
}
function showRuleDocumentation(uri: string, documentVersion: number, edits: TextEdit[], ruleId: string) {
const tslintDocBaseURL = "https://palantir.github.io/tslint/rules";
if (!ruleId) {
return;
}
open(tslintDocBaseURL+'/'+ruleId);
}
function fixAllProblems() {
// server is not running so there can be no problems to fix
if (!serverRunning) {
return;
}
let textEditor = window.activeTextEditor;
if (!textEditor) {
return;
}
let uri: string = textEditor.document.uri.toString();
client.sendRequest(AllFixesRequest.type, { textDocument: { uri } }).then((result) => {
if (result) {
applyTextEdits(uri, result.documentVersion, result.edits);
}
}, (error) => {
window.showErrorMessage('Failed to apply TSLint fixes to the document. Please consider opening an issue with steps to reproduce.');
});
}
async function createDefaultConfiguration() {
let folders = workspace.workspaceFolders;
if (!folders) {
window.showErrorMessage('A TSLint configuration file can only be generated if VS Code is opened on a folder.');
return;
}
let folderPicks = folders.map(each => {
return {label: each.name, description: each.uri.fsPath};
});
let selection;
if (folderPicks.length === 1) {
selection = folderPicks[0];
} else {
selection = await window.showQuickPick(folderPicks, {placeHolder: 'Select the target folder for the tslint.json'});
}
if(!selection) {
return;
}
let tslintConfigFile = path.join(selection.description, 'tslint.json');
if (fs.existsSync(tslintConfigFile)) {
window.showInformationMessage('A TSLint configuration file already exists.');
let document = await workspace.openTextDocument(tslintConfigFile);
window.showTextDocument(document);
} else {
const cmd = 'tslint --init';
const p = exec(cmd, { cwd: selection.description, env: process.env });
p.on('exit', async (code: number, signal: string) => {
if (code === 0) {
let document = await workspace.openTextDocument(tslintConfigFile);
window.showTextDocument(document);
}
});
}
}
function configurationChanged() {
let config = workspace.getConfiguration('tslint');
let autoFix = config.get('autoFixOnSave', false);
if (autoFix && !willSaveTextDocument) {
willSaveTextDocument = workspace.onWillSaveTextDocument((event) => {
let document = event.document;
// only auto fix when the document was manually saved by the user
if (!(isTypeScriptDocument(document.languageId) || isEnableForJavaScriptDocument(document.languageId))
|| event.reason !== TextDocumentSaveReason.Manual) {
return;
}
const version = document.version;
event.waitUntil(
client.sendRequest(AllFixesRequest.type, { textDocument: { uri: document.uri.toString() }, isOnSave: true }).then((result) => {
if (result && version === result.documentVersion) {
return client.protocol2CodeConverter.asTextEdits(result.edits);
} else {
return [];
}
})
);
});
} else if (!autoFix && willSaveTextDocument) {
willSaveTextDocument.dispose();
willSaveTextDocument = undefined;
}
udpateStatusBarVisibility(window.activeTextEditor);
}
configurationChangedListener = workspace.onDidChangeConfiguration(configurationChanged);
configurationChanged();
context.subscriptions.push(
client.start(),
configurationChangedListener,
// internal commands
commands.registerCommand('_tslint.applySingleFix', applyTextEdits),
commands.registerCommand('_tslint.applySameFixes', applyTextEdits),
commands.registerCommand('_tslint.applyAllFixes', applyTextEdits),
commands.registerCommand('_tslint.applyDisableRule', applyDisableRuleEdit),
commands.registerCommand('_tslint.showRuleDocumentation', showRuleDocumentation),
// user commands
commands.registerCommand('tslint.fixAllProblems', fixAllProblems),
commands.registerCommand('tslint.createConfig', createDefaultConfiguration),
commands.registerCommand('tslint.showOutputChannel', () => { client.outputChannel.show(); }),
statusBarItem
);
}
export function deactivate() {
if (willSaveTextDocument) {
willSaveTextDocument.dispose();
willSaveTextDocument = undefined;
}
}