forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinterManager.ts
More file actions
163 lines (148 loc) · 6.88 KB
/
linterManager.ts
File metadata and controls
163 lines (148 loc) · 6.88 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { inject, injectable } from 'inversify';
import {
CancellationToken, OutputChannel, TextDocument, Uri
} from 'vscode';
import { IWorkspaceService } from '../common/application/types';
import {
IConfigurationService, ILogger, Product
} from '../common/types';
import { IServiceContainer } from '../ioc/types';
import { Bandit } from './bandit';
import { Flake8 } from './flake8';
import { LinterInfo, PylintLinterInfo } from './linterInfo';
import { MyPy } from './mypy';
import { Pep8 } from './pep8';
import { Prospector } from './prospector';
import { PyDocStyle } from './pydocstyle';
import { PyLama } from './pylama';
import { Pylint } from './pylint';
import {
IAvailableLinterActivator,
ILinter,
ILinterInfo,
ILinterManager,
ILintMessage
} from './types';
class DisabledLinter implements ILinter {
constructor(private configService: IConfigurationService) { }
public get info() {
return new LinterInfo(Product.pylint, 'pylint', this.configService);
}
public async lint(_document: TextDocument, _cancellation: CancellationToken): Promise<ILintMessage[]> {
return [];
}
}
@injectable()
export class LinterManager implements ILinterManager {
protected linters: ILinterInfo[];
private configService: IConfigurationService;
private checkedForInstalledLinters = new Set<string>();
constructor(@inject(IServiceContainer) private serviceContainer: IServiceContainer,
@inject(IWorkspaceService) private readonly workspaceService: IWorkspaceService) {
this.configService = serviceContainer.get<IConfigurationService>(IConfigurationService);
// Note that we use unit tests to ensure all the linters are here.
this.linters = [
new LinterInfo(Product.bandit, 'bandit', this.configService),
new LinterInfo(Product.flake8, 'flake8', this.configService),
new PylintLinterInfo(this.configService, this.workspaceService, ['.pylintrc', 'pylintrc']),
new LinterInfo(Product.mypy, 'mypy', this.configService),
new LinterInfo(Product.pep8, 'pep8', this.configService),
new LinterInfo(Product.prospector, 'prospector', this.configService),
new LinterInfo(Product.pydocstyle, 'pydocstyle', this.configService),
new LinterInfo(Product.pylama, 'pylama', this.configService)
];
}
public getAllLinterInfos(): ILinterInfo[] {
return this.linters;
}
public getLinterInfo(product: Product): ILinterInfo {
const x = this.linters.findIndex((value, _index, _obj) => value.product === product);
if (x >= 0) {
return this.linters[x];
}
throw new Error(`Invalid linter '${Product[product]}'`);
}
public async isLintingEnabled(silent: boolean, resource?: Uri): Promise<boolean> {
const settings = this.configService.getSettings(resource);
const activeLintersPresent = await this.getActiveLinters(silent, resource);
return settings.linting.enabled && activeLintersPresent.length > 0;
}
public async enableLintingAsync(enable: boolean, resource?: Uri): Promise<void> {
await this.configService.updateSetting('linting.enabled', enable, resource);
}
public async getActiveLinters(silent: boolean, resource?: Uri): Promise<ILinterInfo[]> {
if (!silent) {
await this.enableUnconfiguredLinters(resource);
}
return this.linters.filter(x => x.isEnabled(resource));
}
public async setActiveLintersAsync(products: Product[], resource?: Uri): Promise<void> {
// ensure we only allow valid linters to be set, otherwise leave things alone.
// filter out any invalid products:
const validProducts = products.filter(product => {
const foundIndex = this.linters.findIndex(validLinter => validLinter.product === product);
return foundIndex !== -1;
});
// if we have valid linter product(s), enable only those
if (validProducts.length > 0) {
const active = await this.getActiveLinters(true, resource);
for (const x of active) {
await x.enableAsync(false, resource);
}
if (products.length > 0) {
const toActivate = this.linters.filter(x => products.findIndex(p => x.product === p) >= 0);
for (const x of toActivate) {
await x.enableAsync(true, resource);
}
await this.enableLintingAsync(true, resource);
}
}
}
public async createLinter(product: Product, outputChannel: OutputChannel, serviceContainer: IServiceContainer, resource?: Uri): Promise<ILinter> {
if (!await this.isLintingEnabled(true, resource)) {
return new DisabledLinter(this.configService);
}
const error = 'Linter manager: Unknown linter';
switch (product) {
case Product.bandit:
return new Bandit(outputChannel, serviceContainer);
case Product.flake8:
return new Flake8(outputChannel, serviceContainer);
case Product.pylint:
return new Pylint(outputChannel, serviceContainer);
case Product.mypy:
return new MyPy(outputChannel, serviceContainer);
case Product.prospector:
return new Prospector(outputChannel, serviceContainer);
case Product.pylama:
return new PyLama(outputChannel, serviceContainer);
case Product.pydocstyle:
return new PyDocStyle(outputChannel, serviceContainer);
case Product.pep8:
return new Pep8(outputChannel, serviceContainer);
default:
serviceContainer.get<ILogger>(ILogger).logError(error);
break;
}
throw new Error(error);
}
protected async enableUnconfiguredLinters(resource?: Uri): Promise<void> {
const settings = this.configService.getSettings(resource);
if (!settings.linting.pylintEnabled || !settings.linting.enabled) {
return;
}
// If we've already checked during this session for the same workspace and Python path, then don't bother again.
const workspaceKey = `${this.workspaceService.getWorkspaceFolderIdentifier(resource)}${settings.pythonPath}`;
if (this.checkedForInstalledLinters.has(workspaceKey)) {
return;
}
this.checkedForInstalledLinters.add(workspaceKey);
// only check & ask the user if they'd like to enable pylint
const pylintInfo = this.linters.find(linter => linter.id === 'pylint');
const activator = this.serviceContainer.get<IAvailableLinterActivator>(IAvailableLinterActivator);
await activator.promptIfLinterAvailable(pylintInfo!, resource);
}
}