Repository navigation
Expand file tree
/
Copy pathlintProvider.ts
More file actions
141 lines (118 loc) · 6.23 KB
/
Copy pathlintProvider.ts
File metadata and controls
141 lines (118 loc) · 6.23 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
'use strict';
import * as vscode from 'vscode';
import * as path from 'path';
import * as linter from '../linters/baseLinter';
import * as prospector from './../linters/prospector';
import * as pylint from './../linters/pylint';
import * as pep8 from './../linters/pep8Linter';
import * as flake8 from './../linters/flake8';
import * as pydocstyle from './../linters/pydocstyle';
import * as settings from '../common/configSettings';
import * as telemetryHelper from '../common/telemetry';
import * as telemetryContracts from '../common/telemetryContracts';
const lintSeverityToVSSeverity = new Map<linter.LintMessageSeverity, vscode.DiagnosticSeverity>();
lintSeverityToVSSeverity.set(linter.LintMessageSeverity.Error, vscode.DiagnosticSeverity.Error)
lintSeverityToVSSeverity.set(linter.LintMessageSeverity.Hint, vscode.DiagnosticSeverity.Hint)
lintSeverityToVSSeverity.set(linter.LintMessageSeverity.Information, vscode.DiagnosticSeverity.Information)
lintSeverityToVSSeverity.set(linter.LintMessageSeverity.Warning, vscode.DiagnosticSeverity.Warning)
function createDiagnostics(message: linter.ILintMessage, txtDocumentLines: string[]): vscode.Diagnostic {
let sourceLine = txtDocumentLines[message.line - 1];
let sourceStart = sourceLine.substring(message.column - 1);
let endCol = txtDocumentLines[message.line - 1].length;
// try to get the first word from the startig position
if (message.possibleWord === 'string' && message.possibleWord.length > 0) {
endCol = message.column + message.possibleWord.length;
}
let range = new vscode.Range(new vscode.Position(message.line - 1, message.column), new vscode.Position(message.line - 1, endCol));
let severity = lintSeverityToVSSeverity.get(message.severity);
let diagnostic = new vscode.Diagnostic(range, message.code + ':' + message.message, severity);
diagnostic.code = message.code;
diagnostic.source = message.provider;
return diagnostic;
}
export class LintProvider extends vscode.Disposable {
private settings: settings.IPythonSettings;
private diagnosticCollection: vscode.DiagnosticCollection;
private linters: linter.BaseLinter[] = [];
private pendingLintings = new Map<string, vscode.CancellationTokenSource>();
private outputChannel: vscode.OutputChannel;
private context: vscode.ExtensionContext;
public constructor(context: vscode.ExtensionContext, outputChannel: vscode.OutputChannel, private workspaceRootPath: string) {
super(() => { });
this.outputChannel = outputChannel;
this.context = context;
this.settings = settings.PythonSettings.getInstance();
this.initialize();
}
private initialize() {
this.diagnosticCollection = vscode.languages.createDiagnosticCollection('python');
let disposables = [];
this.linters.push(new prospector.Linter(this.outputChannel, this.workspaceRootPath));
this.linters.push(new pylint.Linter(this.outputChannel, this.workspaceRootPath));
this.linters.push(new pep8.Linter(this.outputChannel, this.workspaceRootPath));
this.linters.push(new flake8.Linter(this.outputChannel, this.workspaceRootPath));
this.linters.push(new pydocstyle.Linter(this.outputChannel, this.workspaceRootPath));
let disposable = vscode.workspace.onDidSaveTextDocument((e) => {
if (e.languageId !== 'python' || !this.settings.linting.enabled || !this.settings.linting.lintOnSave) {
return;
}
this.lintDocument(e.uri, e.getText().split(/\r?\n/g), 100);
});
this.context.subscriptions.push(disposable);
}
private lastTimeout: number;
private lintDocument(documentUri: vscode.Uri, documentLines: string[], delay: number): void {
// Since this is a hack, lets wait for 2 seconds before linting
// Give user to continue typing before we waste CPU time
if (this.lastTimeout) {
clearTimeout(this.lastTimeout);
this.lastTimeout = 0;
}
this.lastTimeout = setTimeout(() => {
this.onLintDocument(documentUri, documentLines);
}, delay);
}
private onLintDocument(documentUri: vscode.Uri, documentLines: string[]): void {
if (this.pendingLintings.has(documentUri.fsPath)) {
this.pendingLintings.get(documentUri.fsPath).cancel();
this.pendingLintings.delete(documentUri.fsPath);
}
let cancelToken = new vscode.CancellationTokenSource();
cancelToken.token.onCancellationRequested(() => {
if (this.pendingLintings.has(documentUri.fsPath)) {
this.pendingLintings.delete(documentUri.fsPath);
}
});
this.pendingLintings.set(documentUri.fsPath, cancelToken);
this.outputChannel.clear();
let promises: Promise<linter.ILintMessage[]>[] = this.linters.map(linter => {
if (!linter.isEnabled()) {
return Promise.resolve([]);
}
let delays = new telemetryHelper.Delays();
return linter.runLinter(documentUri.fsPath, documentLines).then(results => {
delays.stop();
telemetryHelper.sendTelemetryEvent(telemetryContracts.IDE.Lint, { Lint_Provider: linter.Id }, delays.toMeasures());
return results;
});
});
// linters will resolve asynchronously - keep a track of all
// diagnostics reported as them come in
let diagnostics: vscode.Diagnostic[] = [];
promises.forEach(p => {
p.then(msgs => {
if (cancelToken.token.isCancellationRequested) {
return;
}
// Build the message and suffix the message with the name of the linter used
msgs.forEach(d => {
diagnostics.push(createDiagnostics(d, documentLines));
});
// Limit the number of messages to the max value
diagnostics = diagnostics.filter((value, index) => index <= this.settings.linting.maxNumberOfProblems);
// set all diagnostics found in this pass, as this method always clears existing diagnostics.
this.diagnosticCollection.set(documentUri, diagnostics)
})
})
}
}