Skip to content

Commit 1d7381a

Browse files
committed
performance improvements (fix #378)
1 parent 8802b7e commit 1d7381a

10 files changed

Lines changed: 88 additions & 72 deletions

File tree

src/client/linters/baseLinter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ export abstract class BaseLinter {
6363
this.pythonSettings = settings.PythonSettings.getInstance();
6464
}
6565
public abstract isEnabled(): Boolean;
66-
public abstract runLinter(filePath: string, txtDocumentLines: string[]): Promise<ILintMessage[]>;
66+
public abstract runLinter(document: vscode.TextDocument): Promise<ILintMessage[]>;
6767

68-
protected run(command: string, args: string[], filePath: string, txtDocumentLines: string[], cwd: string, regEx: string = REGEX): Promise<ILintMessage[]> {
68+
protected run(command: string, args: string[], document: vscode.TextDocument, cwd: string, regEx: string = REGEX): Promise<ILintMessage[]> {
6969
let outputChannel = this.outputChannel;
7070

7171
return new Promise<ILintMessage[]>((resolve, reject) => {
@@ -86,7 +86,7 @@ export abstract class BaseLinter {
8686

8787
let possibleWord: string;
8888
if (!isNaN(match.column)) {
89-
let sourceLine = txtDocumentLines[match.line - 1];
89+
let sourceLine = document.lineAt(match.line - 1).text;
9090
let sourceStart = sourceLine.substring(match.column - 1);
9191

9292
// try to get the first word from the startig position

src/client/linters/flake8.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict';
22

33
import * as baseLinter from './baseLinter';
4-
import {OutputChannel} from 'vscode';
4+
import { OutputChannel } from 'vscode';
55
import { Product } from '../common/installer';
6+
import { TextDocument } from 'vscode';
67

78
export class Linter extends baseLinter.BaseLinter {
89
constructor(outputChannel: OutputChannel, workspaceRootPath?: string) {
@@ -12,15 +13,15 @@ export class Linter extends baseLinter.BaseLinter {
1213
public isEnabled(): Boolean {
1314
return this.pythonSettings.linting.flake8Enabled;
1415
}
15-
public runLinter(filePath: string, txtDocumentLines: string[]): Promise<baseLinter.ILintMessage[]> {
16+
public runLinter(document: TextDocument): Promise<baseLinter.ILintMessage[]> {
1617
if (!this.pythonSettings.linting.flake8Enabled) {
1718
return Promise.resolve([]);
1819
}
1920

2021
let flake8Path = this.pythonSettings.linting.flake8Path;
2122
let flake8Args = Array.isArray(this.pythonSettings.linting.flake8Args) ? this.pythonSettings.linting.flake8Args : [];
2223
return new Promise<baseLinter.ILintMessage[]>((resolve, reject) => {
23-
this.run(flake8Path, flake8Args.concat(['--format=%(row)d,%(col)d,%(code)s,%(code)s:%(text)s', filePath]), filePath, txtDocumentLines, this.workspaceRootPath).then(messages => {
24+
this.run(flake8Path, flake8Args.concat(['--format=%(row)d,%(col)d,%(code)s,%(code)s:%(text)s', document.uri.fsPath]), document, this.workspaceRootPath).then(messages => {
2425
// All messages in pep8 are treated as warnings for now
2526
messages.forEach(msg => {
2627
msg.severity = baseLinter.LintMessageSeverity.Information;

src/client/linters/mypy.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict';
22

33
import * as baseLinter from './baseLinter';
4-
import {OutputChannel} from 'vscode';
4+
import { OutputChannel } from 'vscode';
55
import { Product } from '../common/installer';
6+
import { TextDocument } from 'vscode';
67

78
const REGEX = '(?<file>.py):(?<line>\\d+): (?<type>\\w+): (?<message>.*)\\r?(\\n|$)';
89

@@ -27,15 +28,15 @@ export class Linter extends baseLinter.BaseLinter {
2728
public isEnabled(): Boolean {
2829
return this.pythonSettings.linting.mypyEnabled;
2930
}
30-
public runLinter(filePath: string, txtDocumentLines: string[]): Promise<baseLinter.ILintMessage[]> {
31+
public runLinter(document: TextDocument): Promise<baseLinter.ILintMessage[]> {
3132
if (!this.pythonSettings.linting.mypyEnabled) {
3233
return Promise.resolve([]);
3334
}
3435

3536
let mypyPath = this.pythonSettings.linting.mypyPath;
3637
let mypyArgs = Array.isArray(this.pythonSettings.linting.mypyArgs) ? this.pythonSettings.linting.mypyArgs : [];
3738
return new Promise<baseLinter.ILintMessage[]>((resolve, reject) => {
38-
this.run(mypyPath, mypyArgs.concat([filePath]), filePath, txtDocumentLines, this.workspaceRootPath, REGEX).then(messages => {
39+
this.run(mypyPath, mypyArgs.concat([document.uri.fsPath]), document, this.workspaceRootPath, REGEX).then(messages => {
3940
messages.forEach(msg => {
4041
msg.severity = this.parseMessagesSeverity(msg.type);
4142
msg.code = msg.type;

src/client/linters/pep8Linter.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import * as baseLinter from './baseLinter';
44
import {OutputChannel} from 'vscode';
55
import { Product } from '../common/installer';
6+
import { TextDocument } from 'vscode';
67

78
export class Linter extends baseLinter.BaseLinter {
89
constructor(outputChannel: OutputChannel, workspaceRootPath?: string) {
@@ -12,15 +13,15 @@ export class Linter extends baseLinter.BaseLinter {
1213
public isEnabled(): Boolean {
1314
return this.pythonSettings.linting.pep8Enabled;
1415
}
15-
public runLinter(filePath: string, txtDocumentLines: string[]): Promise<baseLinter.ILintMessage[]> {
16+
public runLinter(document:TextDocument): Promise<baseLinter.ILintMessage[]> {
1617
if (!this.pythonSettings.linting.pep8Enabled) {
1718
return Promise.resolve([]);
1819
}
1920

2021
let pep8Path = this.pythonSettings.linting.pep8Path;
2122
let pep8Args = Array.isArray(this.pythonSettings.linting.pep8Args) ? this.pythonSettings.linting.pep8Args : [];
2223
return new Promise<baseLinter.ILintMessage[]>(resolve => {
23-
this.run(pep8Path, pep8Args.concat(['--format=%(row)d,%(col)d,%(code)s,%(code)s:%(text)s', filePath]), filePath, txtDocumentLines, this.workspaceRootPath).then(messages => {
24+
this.run(pep8Path, pep8Args.concat(['--format=%(row)d,%(col)d,%(code)s,%(code)s:%(text)s', document.uri.fsPath]), document, this.workspaceRootPath).then(messages => {
2425
// All messages in pep8 are treated as warnings for now
2526
messages.forEach(msg => {
2627
msg.severity = baseLinter.LintMessageSeverity.Information;

src/client/linters/prospector.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as baseLinter from './baseLinter';
44
import {OutputChannel} from 'vscode';
55
import {execPythonFile} from './../common/utils';
66
import { Product } from '../common/installer';
7+
import { TextDocument } from 'vscode';
78

89
interface IProspectorResponse {
910
messages: IProspectorMessage[];
@@ -30,7 +31,7 @@ export class Linter extends baseLinter.BaseLinter {
3031
public isEnabled(): Boolean {
3132
return this.pythonSettings.linting.prospectorEnabled;
3233
}
33-
public runLinter(filePath: string, txtDocumentLines: string[]): Promise<baseLinter.ILintMessage[]> {
34+
public runLinter(document:TextDocument): Promise<baseLinter.ILintMessage[]> {
3435
if (!this.pythonSettings.linting.prospectorEnabled) {
3536
return Promise.resolve([]);
3637
}
@@ -39,7 +40,7 @@ export class Linter extends baseLinter.BaseLinter {
3940
let outputChannel = this.outputChannel;
4041
let prospectorArgs = Array.isArray(this.pythonSettings.linting.prospectorArgs) ? this.pythonSettings.linting.prospectorArgs : [];
4142
return new Promise<baseLinter.ILintMessage[]>((resolve, reject) => {
42-
execPythonFile(prospectorPath, prospectorArgs.concat(['--absolute-paths', '--output-format=json', filePath]), this.workspaceRootPath, false).then(data => {
43+
execPythonFile(prospectorPath, prospectorArgs.concat(['--absolute-paths', '--output-format=json', document.uri.fsPath]), this.workspaceRootPath, false).then(data => {
4344
let parsedData: IProspectorResponse;
4445
try {
4546
parsedData = JSON.parse(data);
@@ -53,7 +54,7 @@ export class Linter extends baseLinter.BaseLinter {
5354
parsedData.messages.filter((value, index) => index <= this.pythonSettings.linting.maxNumberOfProblems).forEach(msg => {
5455

5556
let lineNumber = msg.location.line === null || isNaN(msg.location.line) ? 1 : msg.location.line;
56-
let sourceLine = txtDocumentLines[lineNumber - 1];
57+
let sourceLine = document.lineAt(lineNumber - 1).text;
5758
let sourceStart = sourceLine.substring(msg.location.character);
5859

5960
// try to get the first word from the starting position

src/client/linters/pydocstyle.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ILintMessage } from './baseLinter';
66
import { OutputChannel } from 'vscode';
77
import { execPythonFile, IS_WINDOWS } from './../common/utils';
88
import { Product } from '../common/installer';
9+
import { TextDocument } from 'vscode';
910

1011
export class Linter extends baseLinter.BaseLinter {
1112
constructor(outputChannel: OutputChannel, workspaceRootPath?: string) {
@@ -15,15 +16,15 @@ export class Linter extends baseLinter.BaseLinter {
1516
public isEnabled(): Boolean {
1617
return this.pythonSettings.linting.pydocstyleEnabled;
1718
}
18-
public runLinter(filePath: string, txtDocumentLines: string[]): Promise<baseLinter.ILintMessage[]> {
19+
public runLinter(document:TextDocument): Promise<baseLinter.ILintMessage[]> {
1920
if (!this.pythonSettings.linting.pydocstyleEnabled) {
2021
return Promise.resolve([]);
2122
}
2223

2324
let pydocstylePath = this.pythonSettings.linting.pydocstylePath;
2425
let pydocstyleArgs = Array.isArray(this.pythonSettings.linting.pydocstyleArgs) ? this.pythonSettings.linting.pydocstyleArgs : [];
2526
return new Promise<baseLinter.ILintMessage[]>(resolve => {
26-
this.run(pydocstylePath, pydocstyleArgs.concat([filePath]), filePath, txtDocumentLines).then(messages => {
27+
this.run(pydocstylePath, pydocstyleArgs.concat([document.uri.fsPath]), document).then(messages => {
2728
// All messages in pep8 are treated as warnings for now
2829
messages.forEach(msg => {
2930
msg.severity = baseLinter.LintMessageSeverity.Information;
@@ -34,7 +35,7 @@ export class Linter extends baseLinter.BaseLinter {
3435
});
3536
}
3637

37-
protected run(commandLine: string, args: string[], filePath: string, txtDocumentLines: string[]): Promise<ILintMessage[]> {
38+
protected run(commandLine: string, args: string[], document:TextDocument): Promise<ILintMessage[]> {
3839
let outputChannel = this.outputChannel;
3940

4041
return new Promise<ILintMessage[]>((resolve, reject) => {
@@ -43,7 +44,7 @@ export class Linter extends baseLinter.BaseLinter {
4344
outputChannel.append(data);
4445
let outputLines = data.split(/\r?\n/g);
4546
let diagnostics: ILintMessage[] = [];
46-
let baseFileName = path.basename(filePath);
47+
let baseFileName = path.basename(document.uri.fsPath);
4748

4849
// Remember, the first line of the response contains the file name and line number, the next line contains the error message
4950
// So we have two lines per message, hence we need to take lines in pairs
@@ -76,7 +77,7 @@ export class Linter extends baseLinter.BaseLinter {
7677
let code = part.substring(0, part.indexOf(':')).trim();
7778
let message = part.substring(part.indexOf(':') + 1).trim();
7879

79-
let sourceLine = txtDocumentLines[lineNumber - 1];
80+
let sourceLine = document.lineAt(lineNumber - 1).text;
8081
let trmmedSourceLine = sourceLine.trim();
8182
let sourceStart = sourceLine.indexOf(trmmedSourceLine);
8283

src/client/linters/pylama.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import * as baseLinter from './baseLinter';
44
import {OutputChannel} from 'vscode';
55
import { Product } from '../common/installer';
6+
import { TextDocument } from 'vscode';
67

78
const REGEX = '(?<file>.py):(?<line>\\d+):(?<column>\\d+): \\[(?<type>\\w+)\\] (?<code>\\w\\d+):? (?<message>.*)\\r?(\\n|$)';
89

@@ -14,15 +15,15 @@ export class Linter extends baseLinter.BaseLinter {
1415
public isEnabled(): Boolean {
1516
return this.pythonSettings.linting.pylamaEnabled;
1617
}
17-
public runLinter(filePath: string, txtDocumentLines: string[]): Promise<baseLinter.ILintMessage[]> {
18+
public runLinter(document:TextDocument): Promise<baseLinter.ILintMessage[]> {
1819
if (!this.pythonSettings.linting.pylamaEnabled) {
1920
return Promise.resolve([]);
2021
}
2122

2223
let pylamaPath = this.pythonSettings.linting.pylamaPath;
2324
let pylamaArgs = Array.isArray(this.pythonSettings.linting.pylamaArgs) ? this.pythonSettings.linting.pylamaArgs : [];
2425
return new Promise<baseLinter.ILintMessage[]>(resolve => {
25-
this.run(pylamaPath, pylamaArgs.concat(['--format=parsable', filePath]), filePath, txtDocumentLines, this.workspaceRootPath, REGEX).then(messages => {
26+
this.run(pylamaPath, pylamaArgs.concat(['--format=parsable', document.uri.fsPath]), document, this.workspaceRootPath, REGEX).then(messages => {
2627
// All messages in pylama are treated as warnings for now
2728
messages.forEach(msg => {
2829
msg.severity = baseLinter.LintMessageSeverity.Information;

src/client/linters/pylint.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict';
22

33
import * as baseLinter from './baseLinter';
4-
import {OutputChannel} from 'vscode';
4+
import { OutputChannel } from 'vscode';
55
import { Product } from '../common/installer';
6+
import { TextDocument } from 'vscode';
67

78
export class Linter extends baseLinter.BaseLinter {
89
constructor(outputChannel: OutputChannel, workspaceRootPath?: string) {
@@ -35,15 +36,15 @@ export class Linter extends baseLinter.BaseLinter {
3536
public isEnabled(): Boolean {
3637
return this.pythonSettings.linting.pylintEnabled;
3738
}
38-
public runLinter(filePath: string, txtDocumentLines: string[]): Promise<baseLinter.ILintMessage[]> {
39+
public runLinter(document: TextDocument): Promise<baseLinter.ILintMessage[]> {
3940
if (!this.pythonSettings.linting.pylintEnabled) {
4041
return Promise.resolve([]);
4142
}
4243

4344
let pylintPath = this.pythonSettings.linting.pylintPath;
4445
let pylintArgs = Array.isArray(this.pythonSettings.linting.pylintArgs) ? this.pythonSettings.linting.pylintArgs : [];
4546
return new Promise<baseLinter.ILintMessage[]>((resolve, reject) => {
46-
this.run(pylintPath, pylintArgs.concat(['--msg-template=\'{line},{column},{category},{msg_id}:{msg}\'', '--reports=n', '--output-format=text', filePath]), filePath, txtDocumentLines, this.workspaceRootPath).then(messages => {
47+
this.run(pylintPath, pylintArgs.concat(['--msg-template=\'{line},{column},{category},{msg_id}:{msg}\'', '--reports=n', '--output-format=text', document.uri.fsPath]), document, this.workspaceRootPath).then(messages => {
4748
messages.forEach(msg => {
4849
msg.severity = this.parseMessagesSeverity(msg.type);
4950
});

0 commit comments

Comments
 (0)