forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
103 lines (96 loc) · 4.39 KB
/
main.ts
File metadata and controls
103 lines (96 loc) · 4.39 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
import * as vscode from 'vscode';
import { Generator } from './generator';
import { Product, Installer } from '../common/installer';
import { PythonSettings } from '../common/configSettings';
import { fsExistsAsync } from '../common/utils';
import { isNotInstalledError } from '../common/helpers';
import { PythonLanguage, Commands } from '../common/constants';
import { WorkspaceSymbolProvider } from './provider';
const pythonSettings = PythonSettings.getInstance();
export class WorkspaceSymbols implements vscode.Disposable {
private disposables: vscode.Disposable[];
private generator: Generator;
private installer: Installer;
constructor(private outputChannel: vscode.OutputChannel) {
this.disposables = [];
this.disposables.push(this.outputChannel);
this.generator = new Generator(this.outputChannel);
this.disposables.push(this.generator);
this.installer = new Installer();
this.disposables.push(this.installer);
this.registerCommands();
// The extension has just loaded, so lets rebuild the tags
vscode.languages.registerWorkspaceSymbolProvider(new WorkspaceSymbolProvider(this.generator, this.outputChannel));
this.buildWorkspaceSymbols(true);
}
registerCommands() {
this.disposables.push(vscode.commands.registerCommand(Commands.Build_Workspace_Symbols, this.buildWorkspaceSymbols.bind(this, true)));
}
registerOnSaveHandlers() {
this.disposables.push(vscode.workspace.onDidSaveTextDocument(this.onDidSaveTextDocument.bind(this)));
}
onDidSaveTextDocument(textDocument: vscode.TextDocument) {
if (textDocument.languageId === PythonLanguage.language) {
this.rebuildTags();
}
}
private timeout: number;
rebuildTags() {
if (this.timeout) {
clearTimeout(this.timeout);
this.timeout = null;
}
this.timeout = setTimeout(() => {
this.buildWorkspaceSymbols(true);
}, 5000);
}
dispose() {
this.disposables.forEach(d => d.dispose());
}
disableDocumentLanguageProvider(): Thenable<any> {
const pythonConfig = vscode.workspace.getConfiguration('python');
return pythonConfig.update('python.workspaceSymbols.enabled', false);
}
buildWorkspaceSymbols(rebuild: boolean = true, token?: vscode.CancellationToken): Promise<any> {
if (!pythonSettings.workspaceSymbols.enabled || (token && token.isCancellationRequested)) {
return Promise.resolve([]);
}
if (!vscode.workspace || typeof vscode.workspace.rootPath !== 'string' || vscode.workspace.rootPath.length === 0) {
return Promise.resolve([]);
}
return fsExistsAsync(pythonSettings.workspaceSymbols.tagFilePath).then(exits => {
let promise = Promise.resolve();
// if file doesn't exist, then run the ctag generator
// Or check if required to rebuild
if (rebuild || !exits) {
promise = this.generator.generateWorkspaceTags();
}
return promise.catch(reason => {
if (!isNotInstalledError(reason)) {
this.outputChannel.show();
return Promise.reject(reason);
}
if (!token || token.isCancellationRequested) {
return;
}
return new Promise<any>((resolve, reject) => {
vscode.window.showErrorMessage('CTags needs to be installed to get support for Python workspace symbols',
'Install', `Don't ask again`).then(item => {
switch (item) {
case 'Install': {
this.installer.installProduct(Product.ctags).then(() => {
return this.buildWorkspaceSymbols(rebuild, token);
}).catch(reason => reject(reason));
break;
}
case `Don't ask again`: {
this.disableDocumentLanguageProvider().then(() => resolve(), reason => reject(reason));
break;
}
}
});
});
});
});
}
}