forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaseLinter.ts
More file actions
166 lines (149 loc) · 6.98 KB
/
baseLinter.ts
File metadata and controls
166 lines (149 loc) · 6.98 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
'use strict';
import { execPythonFile } from './../common/utils';
import * as settings from './../common/configSettings';
import { OutputChannel } from 'vscode';
import { isNotInstalledError } from '../common/helpers';
import { Installer, Product, disableLinter } from '../common/installer';
import * as vscode from 'vscode';
let NamedRegexp = null;
const REGEX = '(?<line>\\d+),(?<column>\\d+),(?<type>\\w+),(?<code>\\w\\d+):(?<message>.*)\\r?(\\n|$)';
export interface IRegexGroup {
line: number;
column: number;
code: string;
message: string;
type: string;
}
export interface ILintMessage {
line: number;
column: number;
code: string;
message: string;
type: string;
possibleWord?: string;
severity?: LintMessageSeverity;
provider: string;
}
export enum LintMessageSeverity {
Hint,
Error,
Warning,
Information
}
export function matchNamedRegEx(data, regex): IRegexGroup {
if (NamedRegexp === null) {
NamedRegexp = require('named-js-regexp');
}
let compiledRegexp = NamedRegexp(regex, 'g');
let rawMatch = compiledRegexp.exec(data);
if (rawMatch !== null) {
return <IRegexGroup>rawMatch.groups();
}
return null;
}
export abstract class BaseLinter {
public Id: string;
private installer: Installer;
protected pythonSettings: settings.IPythonSettings;
private _workspaceRootPath: string;
protected get workspaceRootPath(): string {
return typeof this._workspaceRootPath === 'string' ? this._workspaceRootPath : vscode.workspace.rootPath;
}
constructor(id: string, private product: Product, protected outputChannel: OutputChannel, workspaceRootPath: string) {
this.Id = id;
this.installer = new Installer();
this._workspaceRootPath = workspaceRootPath;
this.pythonSettings = settings.PythonSettings.getInstance();
}
public abstract isEnabled(): Boolean;
public abstract runLinter(document: vscode.TextDocument, cancellation: vscode.CancellationToken): Promise<ILintMessage[]>;
protected run(command: string, args: string[], document: vscode.TextDocument, cwd: string, cancellation: vscode.CancellationToken, regEx: string = REGEX): Promise<ILintMessage[]> {
let outputChannel = this.outputChannel;
return new Promise<ILintMessage[]>((resolve, reject) => {
execPythonFile(command, args, cwd, true, null, cancellation).then(data => {
outputChannel.append('#'.repeat(10) + 'Linting Output - ' + this.Id + '#'.repeat(10) + '\n');
outputChannel.append(data);
let outputLines = data.split(/\r?\n/g);
let diagnostics: ILintMessage[] = [];
outputLines.filter((value, index) => index <= this.pythonSettings.linting.maxNumberOfProblems).forEach(line => {
let match = matchNamedRegEx(line, regEx);
if (!match) {
return;
}
try {
match.line = Number(<any>match.line);
match.column = Number(<any>match.column);
let possibleWord: string;
if (!isNaN(match.column)) {
let sourceLine = document.lineAt(match.line - 1).text;
let sourceStart = sourceLine.substring(match.column - 1);
// try to get the first word from the startig position
let possibleProblemWords = sourceStart.match(/\w+/g);
if (possibleProblemWords != null && possibleProblemWords.length > 0 && sourceStart.startsWith(possibleProblemWords[0])) {
possibleWord = possibleProblemWords[0];
}
}
diagnostics.push({
code: match.code,
message: match.message,
column: isNaN(match.column) || match.column === 0 ? 0 : match.column - 1,
line: match.line,
possibleWord: possibleWord,
type: match.type,
provider: this.Id
});
}
catch (ex) {
// Hmm, need to handle this later
// TODO:
}
});
resolve(diagnostics);
}).catch(error => {
this.handleError(this.Id, command, error);
resolve([]);
});
});
}
protected handleError(expectedFileName: string, fileName: string, error: Error) {
let customError = `Linting with ${this.Id} failed.`;
if (isNotInstalledError(error)) {
// Check if we have some custom arguments such as "pylint --load-plugins pylint_django"
// Such settings are no longer supported
let stuffAfterFileName = fileName.substring(fileName.toUpperCase().lastIndexOf(expectedFileName) + expectedFileName.length);
// Ok if we have a space after the file name, this means we have some arguments defined and this isn't supported
if (stuffAfterFileName.trim().indexOf(' ') > 0) {
customError = `Linting failed, custom arguments in the 'python.linting.${this.Id}Path' is not supported.\n` +
`Custom arguments to the linters can be defined in 'python.linting.${this.Id}Args' setting of settings.json.\n` +
'For further details, please see https://github.com/DonJayamanne/pythonVSCode/wiki/Troubleshooting-Linting#2-linting-with-xxx-failed-';
vscode.window.showErrorMessage(`Unsupported configuration for '${this.Id}'`, 'View Errors').then(item => {
if (item === 'View Errors') {
this.outputChannel.show();
}
});
}
else {
customError += `\nYou could either install the '${this.Id}' linter or turn it off in setings.json via "python.linting.${this.Id}Enabled = false".`;
this.installer.promptToInstall(this.product);
}
}
else {
if (typeof error === 'string' && (error as string).indexOf("OSError: [Errno 2] No such file or directory: '/") > 0) {
return;
}
vscode.window.showErrorMessage(`There was an error in running the linter '${this.Id}'`, 'Disable linter', 'View Errors').then(item => {
switch (item) {
case 'Disable linter': {
disableLinter(this.product);
break;
}
case 'View Errors': {
this.outputChannel.show();
break;
}
}
});
}
this.outputChannel.appendLine(`\n${customError}\n${error + ''}`);
}
}