Skip to content

Commit 2b6c455

Browse files
patrysDonJayamanne
authored andcommitted
Allow linters to specify custom offsets (#970)
* Allow linters to specify custom offsets Also stop trying to find a word at error position, vscode does that automatically when given a zero-length range. * Remove possibleWord from linter tests
1 parent ebf75e7 commit 2b6c455

7 files changed

Lines changed: 55 additions & 78 deletions

File tree

src/client/linters/baseLinter.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export interface ILintMessage {
2323
code: string;
2424
message: string;
2525
type: string;
26-
possibleWord?: string;
2726
severity?: LintMessageSeverity;
2827
provider: string;
2928
}
@@ -53,6 +52,7 @@ export abstract class BaseLinter {
5352
private installer: Installer;
5453
protected pythonSettings: settings.IPythonSettings;
5554
private _workspaceRootPath: string;
55+
protected _columnOffset = 0;
5656
protected get workspaceRootPath(): string {
5757
return typeof this._workspaceRootPath === 'string' ? this._workspaceRootPath : vscode.workspace.rootPath;
5858
}
@@ -84,24 +84,11 @@ export abstract class BaseLinter {
8484
match.line = Number(<any>match.line);
8585
match.column = Number(<any>match.column);
8686

87-
let possibleWord: string;
88-
if (!isNaN(match.column)) {
89-
let sourceLine = document.lineAt(match.line - 1).text;
90-
let sourceStart = sourceLine.substring(match.column - 1);
91-
92-
// try to get the first word from the startig position
93-
let possibleProblemWords = sourceStart.match(/\w+/g);
94-
if (possibleProblemWords != null && possibleProblemWords.length > 0 && sourceStart.startsWith(possibleProblemWords[0])) {
95-
possibleWord = possibleProblemWords[0];
96-
}
97-
}
98-
9987
diagnostics.push({
10088
code: match.code,
10189
message: match.message,
102-
column: isNaN(match.column) || match.column === 0 ? 0 : match.column - 1,
90+
column: isNaN(match.column) || match.column === 0 ? 0 : match.column - this._columnOffset,
10391
line: match.line,
104-
possibleWord: possibleWord,
10592
type: match.type,
10693
provider: this.Id
10794
});

src/client/linters/flake8.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { Product } from '../common/installer';
66
import { TextDocument, CancellationToken } from 'vscode';
77

88
export class Linter extends baseLinter.BaseLinter {
9+
_columnOffset = 1;
10+
911
constructor(outputChannel: OutputChannel, workspaceRootPath?: string) {
1012
super('flake8', Product.flake8, outputChannel, workspaceRootPath);
1113
}

src/client/linters/pep8Linter.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { Product } from '../common/installer';
66
import { TextDocument, CancellationToken } from 'vscode';
77

88
export class Linter extends baseLinter.BaseLinter {
9+
_columnOffset = 1;
10+
911
constructor(outputChannel: OutputChannel, workspaceRootPath?: string) {
1012
super('pep8', Product.pep8, outputChannel, workspaceRootPath);
1113
}

src/client/linters/prospector.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,12 @@ export class Linter extends baseLinter.BaseLinter {
5454
parsedData.messages.filter((value, index) => index <= this.pythonSettings.linting.maxNumberOfProblems).forEach(msg => {
5555

5656
let lineNumber = msg.location.line === null || isNaN(msg.location.line) ? 1 : msg.location.line;
57-
let sourceLine = document.lineAt(lineNumber - 1).text;
58-
let sourceStart = sourceLine.substring(msg.location.character);
59-
60-
// try to get the first word from the starting position
61-
let possibleProblemWords = sourceStart.match(/\w+/g);
62-
let possibleWord: string;
63-
if (possibleProblemWords != null && possibleProblemWords.length > 0 && sourceStart.startsWith(possibleProblemWords[0])) {
64-
possibleWord = possibleProblemWords[0];
65-
}
6657

6758
diagnostics.push({
6859
code: msg.code,
6960
message: msg.message,
7061
column: msg.location.character,
7162
line: lineNumber,
72-
possibleWord: possibleWord,
7363
type: msg.code,
7464
provider: `${this.Id} - ${msg.source}`
7565
});

src/client/linters/pylama.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { TextDocument, CancellationToken } from 'vscode';
88
const REGEX = '(?<file>.py):(?<line>\\d+):(?<column>\\d+): \\[(?<type>\\w+)\\] (?<code>\\w\\d+):? (?<message>.*)\\r?(\\n|$)';
99

1010
export class Linter extends baseLinter.BaseLinter {
11+
_columnOffset = 1;
12+
1113
constructor(outputChannel: OutputChannel, workspaceRootPath?: string) {
1214
super('pylama', Product.pylama, outputChannel, workspaceRootPath);
1315
}

src/client/providers/lintProvider.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,8 @@ lintSeverityToVSSeverity.set(linter.LintMessageSeverity.Information, vscode.Diag
2222
lintSeverityToVSSeverity.set(linter.LintMessageSeverity.Warning, vscode.DiagnosticSeverity.Warning);
2323

2424
function createDiagnostics(message: linter.ILintMessage, document: vscode.TextDocument): vscode.Diagnostic {
25-
let endCol = document.lineAt(message.line - 1).text.length;
26-
27-
// try to get the first word from the startig position
28-
if (message.possibleWord === 'string' && message.possibleWord.length > 0) {
29-
endCol = message.column + message.possibleWord.length;
30-
}
31-
32-
let range = new vscode.Range(new vscode.Position(message.line - 1, message.column), new vscode.Position(message.line - 1, endCol));
25+
let position = new vscode.Position(message.line - 1, message.column);
26+
let range = new vscode.Range(position, position);
3327

3428
let severity = lintSeverityToVSSeverity.get(message.severity);
3529
let diagnostic = new vscode.Diagnostic(range, message.code + ':' + message.message, severity);

0 commit comments

Comments
 (0)