forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinterProvider.ts
More file actions
105 lines (90 loc) · 4.77 KB
/
linterProvider.ts
File metadata and controls
105 lines (90 loc) · 4.77 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as path from 'path';
import * as vscode from 'vscode';
import { ConfigurationTarget, Uri, workspace } from 'vscode';
import { IDocumentManager } from '../common/application/types';
import { ConfigSettingMonitor } from '../common/configSettingMonitor';
import { isTestExecution } from '../common/constants';
import { IFileSystem } from '../common/platform/types';
import { IConfigurationService } from '../common/types';
import { IInterpreterService } from '../interpreter/contracts';
import { IServiceContainer } from '../ioc/types';
import { ILinterManager, ILintingEngine } from '../linters/types';
export class LinterProvider implements vscode.Disposable {
private context: vscode.ExtensionContext;
private disposables: vscode.Disposable[];
private configMonitor: ConfigSettingMonitor;
private interpreterService: IInterpreterService;
private documents: IDocumentManager;
private configuration: IConfigurationService;
private linterManager: ILinterManager;
private engine: ILintingEngine;
private fs: IFileSystem;
public constructor(context: vscode.ExtensionContext, serviceContainer: IServiceContainer) {
this.context = context;
this.disposables = [];
this.fs = serviceContainer.get<IFileSystem>(IFileSystem);
this.engine = serviceContainer.get<ILintingEngine>(ILintingEngine);
this.linterManager = serviceContainer.get<ILinterManager>(ILinterManager);
this.interpreterService = serviceContainer.get<IInterpreterService>(IInterpreterService);
this.documents = serviceContainer.get<IDocumentManager>(IDocumentManager);
this.configuration = serviceContainer.get<IConfigurationService>(IConfigurationService);
this.disposables.push(this.interpreterService.onDidChangeInterpreter(() => this.engine.lintOpenPythonFiles()));
this.documents.onDidOpenTextDocument(e => this.onDocumentOpened(e), this.context.subscriptions);
this.documents.onDidCloseTextDocument(e => this.onDocumentClosed(e), this.context.subscriptions);
this.documents.onDidSaveTextDocument((e) => this.onDocumentSaved(e), this.context.subscriptions);
this.configMonitor = new ConfigSettingMonitor('linting');
this.configMonitor.on('change', this.lintSettingsChangedHandler.bind(this));
// On workspace reopen we don't get `onDocumentOpened` since it is first opened
// and then the extension is activated. So schedule linting pass now.
if (!isTestExecution()) {
setTimeout(() => this.engine.lintOpenPythonFiles().ignoreErrors(), 1200);
}
}
public dispose() {
this.disposables.forEach(d => d.dispose());
this.configMonitor.dispose();
}
private isDocumentOpen(uri: vscode.Uri): boolean {
return this.documents.textDocuments.some(document => this.fs.arePathsSame(document.uri.fsPath, uri.fsPath));
}
private lintSettingsChangedHandler(configTarget: ConfigurationTarget, wkspaceOrFolder: Uri) {
if (configTarget === ConfigurationTarget.Workspace) {
this.engine.lintOpenPythonFiles().ignoreErrors();
return;
}
// Look for python files that belong to the specified workspace folder.
workspace.textDocuments.forEach(async document => {
const wkspaceFolder = workspace.getWorkspaceFolder(document.uri);
if (wkspaceFolder && wkspaceFolder.uri.fsPath === wkspaceOrFolder.fsPath) {
this.engine.lintDocument(document, 'auto').ignoreErrors();
}
});
}
private onDocumentOpened(document: vscode.TextDocument): void {
this.engine.lintDocument(document, 'auto').ignoreErrors();
}
private onDocumentSaved(document: vscode.TextDocument): void {
const settings = this.configuration.getSettings(document.uri);
if (document.languageId === 'python' && settings.linting.enabled && settings.linting.lintOnSave) {
this.engine.lintDocument(document, 'save').ignoreErrors();
return;
}
const linters = this.linterManager.getActiveLinters(document.uri);
const fileName = path.basename(document.uri.fsPath).toLowerCase();
const watchers = linters.filter((info) => info.configFileNames.indexOf(fileName) >= 0);
if (watchers.length > 0) {
setTimeout(() => this.engine.lintOpenPythonFiles(), 1000);
}
}
private onDocumentClosed(document: vscode.TextDocument) {
if (!document || !document.fileName || !document.uri) {
return;
}
// Check if this document is still open as a duplicate editor.
if (!this.isDocumentOpen(document.uri)) {
this.engine.clearDiagnostics(document);
}
}
}