Repository navigation
Expand file tree
/
Copy pathbaseLinter.ts
More file actions
146 lines (133 loc) · 5 KB
/
Copy pathbaseLinter.ts
File metadata and controls
146 lines (133 loc) · 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
'use strict';
import { execPythonFile } from './../common/utils';
import * as settings from './../common/configSettings';
import { OutputChannel } from 'vscode';
import { Installer, Product } from '../common/installer';
import * as vscode from 'vscode';
import { ErrorHandler } from './errorHandlers/main';
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;
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;
protected pythonSettings: settings.IPythonSettings;
private _workspaceRootPath: string;
protected _columnOffset = 0;
private _errorHandler: ErrorHandler;
protected get workspaceRootPath(): string {
return typeof this._workspaceRootPath === 'string' ? this._workspaceRootPath : vscode.workspace.rootPath;
}
constructor(id: string, public product: Product, protected outputChannel: OutputChannel, workspaceRootPath: string) {
this.Id = id;
this._workspaceRootPath = workspaceRootPath;
this.pythonSettings = settings.PythonSettings.getInstance();
this._errorHandler = new ErrorHandler(this.Id, product, new Installer(), this.outputChannel);
}
public abstract isEnabled(): Boolean;
public abstract runLinter(document: vscode.TextDocument, cancellation: vscode.CancellationToken): Promise<ILintMessage[]>;
protected parseMessagesSeverity(error: string, categorySeverity: any): LintMessageSeverity {
if (categorySeverity[error]) {
let severityName = categorySeverity[error];
switch (severityName) {
case 'Error':
return LintMessageSeverity.Error;
case 'Hint':
return LintMessageSeverity.Hint;
case 'Information':
return LintMessageSeverity.Information;
case 'Warning':
return LintMessageSeverity.Warning;
default: {
if (LintMessageSeverity[severityName]) {
return <LintMessageSeverity><any>LintMessageSeverity[severityName];
}
}
}
}
return LintMessageSeverity.Information;
}
private parseLine(line: string, regEx: string) {
let match = matchNamedRegEx(line, regEx);
if (!match) {
return;
}
match.line = Number(<any>match.line);
match.column = Number(<any>match.column);
return {
code: match.code,
message: match.message,
column: isNaN(match.column) || match.column === 0 ? 0 : match.column - this._columnOffset,
line: match.line,
type: match.type,
provider: this.Id
};
}
private parseLines(outputLines: string[], regEx: string) {
let diagnostics: ILintMessage[] = [];
outputLines.filter((value, index) => index <= this.pythonSettings.linting.maxNumberOfProblems).forEach(line => {
try {
let msg = this.parseLine(line, regEx);
if (msg) {
diagnostics.push(msg);
}
}
catch (ex) {
// Hmm, need to handle this later
// TODO:
}
});
return diagnostics;
}
private displayLinterResultHeader(data: string) {
this.outputChannel.append('#'.repeat(10) + 'Linting Output - ' + this.Id + '#'.repeat(10) + '\n');
this.outputChannel.append(data);
}
protected run(command: string, args: string[], document: vscode.TextDocument, cwd: string, cancellation: vscode.CancellationToken, regEx: string = REGEX): Promise<ILintMessage[]> {
return execPythonFile(command, args, cwd, true, null, cancellation).then(data => {
if (!data) {
data = '';
}
this.displayLinterResultHeader(data);
let outputLines = data.split(/\r?\n/g);
return this.parseLines(outputLines, regEx);
}).catch(error => {
this.handleError(this.Id, command, error);
return [];
});
}
protected handleError(expectedFileName: string, fileName: string, error: Error) {
this._errorHandler.handleError(expectedFileName, fileName, error);
}
}