forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinterCommands.ts
More file actions
90 lines (77 loc) · 3.65 KB
/
Copy pathlinterCommands.ts
File metadata and controls
90 lines (77 loc) · 3.65 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as vscode from 'vscode';
import { IApplicationShell, ICommandManager } from '../common/application/types';
import { Commands } from '../common/constants';
import { IServiceContainer } from '../ioc/types';
import { ILinterManager, ILintingEngine } from './types';
export class LinterCommands implements vscode.Disposable {
private disposables: vscode.Disposable[] = [];
private linterManager: ILinterManager;
private appShell: IApplicationShell;
constructor(private serviceContainer: IServiceContainer) {
this.linterManager = this.serviceContainer.get<ILinterManager>(ILinterManager);
this.appShell = this.serviceContainer.get<IApplicationShell>(IApplicationShell);
const commandManager = this.serviceContainer.get<ICommandManager>(ICommandManager);
commandManager.registerCommand(Commands.Set_Linter, this.setLinterAsync.bind(this));
commandManager.registerCommand(Commands.Enable_Linter, this.enableLintingAsync.bind(this));
commandManager.registerCommand(Commands.Run_Linter, this.runLinting.bind(this));
}
public dispose() {
this.disposables.forEach(disposable => disposable.dispose());
}
public async setLinterAsync(): Promise<void> {
const linters = this.linterManager.getAllLinterInfos();
const suggestions = linters.map(x => x.id).sort();
const activeLinters = this.linterManager.getActiveLinters(this.settingsUri);
let current: string;
switch (activeLinters.length) {
case 0:
current = 'none';
break;
case 1:
current = activeLinters[0].id;
break;
default:
current = 'multiple selected';
break;
}
const quickPickOptions: vscode.QuickPickOptions = {
matchOnDetail: true,
matchOnDescription: true,
placeHolder: `current: ${current}`
};
const selection = await this.appShell.showQuickPick(suggestions, quickPickOptions);
if (selection !== undefined) {
const index = linters.findIndex(x => x.id === selection);
if (activeLinters.length > 1) {
const response = await this.appShell.showWarningMessage(`Multiple linters are enabled in settings. Replace with '${selection}'?`, 'Yes', 'No');
if (response !== 'Yes') {
return;
}
}
await this.linterManager.setActiveLintersAsync([linters[index].product], this.settingsUri);
}
}
public async enableLintingAsync(): Promise<void> {
const options = ['on', 'off'];
const current = this.linterManager.isLintingEnabled(this.settingsUri) ? options[0] : options[1];
const quickPickOptions: vscode.QuickPickOptions = {
matchOnDetail: true,
matchOnDescription: true,
placeHolder: `current: ${current}`
};
const selection = await this.appShell.showQuickPick(options, quickPickOptions);
if (selection !== undefined) {
const enable = selection === options[0];
await this.linterManager.enableLintingAsync(enable, this.settingsUri);
}
}
public runLinting(): Promise<vscode.DiagnosticCollection> {
const engine = this.serviceContainer.get<ILintingEngine>(ILintingEngine);
return engine.lintOpenPythonFiles();
}
private get settingsUri(): vscode.Uri | undefined {
return vscode.window.activeTextEditor ? vscode.window.activeTextEditor.document.uri : undefined;
}
}