forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinterProvider.ts
More file actions
113 lines (98 loc) · 5.02 KB
/
linterProvider.ts
File metadata and controls
113 lines (98 loc) · 5.02 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { inject, injectable } from 'inversify';
import * as path from 'path';
import { ConfigurationChangeEvent, Disposable, TextDocument, Uri, workspace } from 'vscode';
import { IExtensionActivationService } from '../activation/types';
import { IDocumentManager, IWorkspaceService } from '../common/application/types';
import { isTestExecution } from '../common/constants';
import '../common/extensions';
import { IFileSystem } from '../common/platform/types';
import { IConfigurationService, IDisposable } from '../common/types';
import { IInterpreterService } from '../interpreter/contracts';
import { IServiceContainer } from '../ioc/types';
import { ILinterManager, ILintingEngine } from '../linters/types';
@injectable()
export class LinterProvider implements IExtensionActivationService, Disposable {
private interpreterService: IInterpreterService;
private documents: IDocumentManager;
private configuration: IConfigurationService;
private linterManager: ILinterManager;
private engine: ILintingEngine;
private fs: IFileSystem;
private readonly disposables: IDisposable[] = [];
private workspaceService: IWorkspaceService;
private activatedOnce: boolean = false;
constructor(@inject(IServiceContainer) private serviceContainer: IServiceContainer) {
this.serviceContainer = serviceContainer;
this.fs = this.serviceContainer.get<IFileSystem>(IFileSystem);
this.engine = this.serviceContainer.get<ILintingEngine>(ILintingEngine);
this.linterManager = this.serviceContainer.get<ILinterManager>(ILinterManager);
this.interpreterService = this.serviceContainer.get<IInterpreterService>(IInterpreterService);
this.documents = this.serviceContainer.get<IDocumentManager>(IDocumentManager);
this.configuration = this.serviceContainer.get<IConfigurationService>(IConfigurationService);
this.workspaceService = this.serviceContainer.get<IWorkspaceService>(IWorkspaceService);
}
public async activate(): Promise<void> {
if (this.activatedOnce) {
return;
}
this.activatedOnce = true;
this.disposables.push(this.interpreterService.onDidChangeInterpreter(() => this.engine.lintOpenPythonFiles()));
this.documents.onDidOpenTextDocument(e => this.onDocumentOpened(e), this.disposables);
this.documents.onDidCloseTextDocument(e => this.onDocumentClosed(e), this.disposables);
this.documents.onDidSaveTextDocument(e => this.onDocumentSaved(e), this.disposables);
const disposable = this.workspaceService.onDidChangeConfiguration(this.lintSettingsChangedHandler.bind(this));
this.disposables.push(disposable);
// 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()) {
const timer = setTimeout(() => this.engine.lintOpenPythonFiles().ignoreErrors(), 1200);
this.disposables.push({ dispose: () => clearTimeout(timer) });
}
}
public dispose() {
this.disposables.forEach(d => d.dispose());
}
private isDocumentOpen(uri: Uri): boolean {
return this.documents.textDocuments.some(document => this.fs.arePathsSame(document.uri.fsPath, uri.fsPath));
}
private lintSettingsChangedHandler(e: ConfigurationChangeEvent) {
// Look for python files that belong to the specified workspace folder.
workspace.textDocuments.forEach(document => {
if (e.affectsConfiguration('python.linting', document.uri)) {
this.engine.lintDocument(document, 'auto').ignoreErrors();
}
});
}
private onDocumentOpened(document: TextDocument): void {
this.engine.lintDocument(document, 'auto').ignoreErrors();
}
private onDocumentSaved(document: 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;
}
this.linterManager
.getActiveLinters(false, document.uri)
.then(linters => {
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);
}
})
.ignoreErrors();
}
private onDocumentClosed(document: 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);
}
}
}