forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
152 lines (143 loc) · 6.58 KB
/
main.ts
File metadata and controls
152 lines (143 loc) · 6.58 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
import { CancellationToken, Disposable, languages, OutputChannel, TextDocument } from 'vscode';
import { IApplicationShell, ICommandManager, IDocumentManager, IWorkspaceService } from '../common/application/types';
import { Commands, STANDARD_OUTPUT_CHANNEL } from '../common/constants';
import { isNotInstalledError } from '../common/helpers';
import { IFileSystem } from '../common/platform/types';
import { IProcessServiceFactory } from '../common/process/types';
import { IConfigurationService, IInstaller, InstallerResponse, IOutputChannel, Product } from '../common/types';
import { IServiceContainer } from '../ioc/types';
import { Generator } from './generator';
import { WorkspaceSymbolProvider } from './provider';
const MAX_NUMBER_OF_ATTEMPTS_TO_INSTALL_AND_BUILD = 2;
export class WorkspaceSymbols implements Disposable {
private disposables: Disposable[];
private generators: Generator[] = [];
private readonly outputChannel: OutputChannel;
private commandMgr: ICommandManager;
private fs: IFileSystem;
private workspace: IWorkspaceService;
private processFactory: IProcessServiceFactory;
private appShell: IApplicationShell;
private configurationService: IConfigurationService;
private documents: IDocumentManager;
constructor(private serviceContainer: IServiceContainer) {
this.outputChannel = this.serviceContainer.get<OutputChannel>(IOutputChannel, STANDARD_OUTPUT_CHANNEL);
this.commandMgr = this.serviceContainer.get<ICommandManager>(ICommandManager);
this.fs = this.serviceContainer.get<IFileSystem>(IFileSystem);
this.workspace = this.serviceContainer.get<IWorkspaceService>(IWorkspaceService);
this.processFactory = this.serviceContainer.get<IProcessServiceFactory>(IProcessServiceFactory);
this.appShell = this.serviceContainer.get<IApplicationShell>(IApplicationShell);
this.configurationService = this.serviceContainer.get<IConfigurationService>(IConfigurationService);
this.documents = this.serviceContainer.get<IDocumentManager>(IDocumentManager);
this.disposables = [];
this.disposables.push(this.outputChannel);
this.registerCommands();
this.initializeGenerators();
languages.registerWorkspaceSymbolProvider(
new WorkspaceSymbolProvider(this.fs, this.commandMgr, this.generators)
);
this.disposables.push(this.workspace.onDidChangeWorkspaceFolders(() => this.initializeGenerators()));
this.disposables.push(this.documents.onDidSaveTextDocument(e => this.onDocumentSaved(e)));
this.buildSymbolsOnStart();
}
public dispose() {
this.disposables.forEach(d => d.dispose());
}
private initializeGenerators() {
while (this.generators.length > 0) {
const generator = this.generators.shift()!;
generator.dispose();
}
if (Array.isArray(this.workspace.workspaceFolders)) {
this.workspace.workspaceFolders.forEach(wkSpc => {
this.generators.push(
new Generator(
wkSpc.uri,
this.outputChannel,
this.appShell,
this.fs,
this.processFactory,
this.configurationService
)
);
});
}
}
private buildSymbolsOnStart() {
if (Array.isArray(this.workspace.workspaceFolders)) {
this.workspace.workspaceFolders.forEach(workspaceFolder => {
const pythonSettings = this.configurationService.getSettings(workspaceFolder.uri);
if (pythonSettings.workspaceSymbols.rebuildOnStart) {
const promises = this.buildWorkspaceSymbols(true);
return Promise.all(promises);
}
});
}
}
private registerCommands() {
this.disposables.push(
this.commandMgr.registerCommand(
Commands.Build_Workspace_Symbols,
async (rebuild: boolean = true, token?: CancellationToken) => {
const promises = this.buildWorkspaceSymbols(rebuild, token);
return Promise.all(promises);
}
)
);
}
private onDocumentSaved(document: TextDocument) {
const workspaceFolder = this.workspace.getWorkspaceFolder(document.uri);
const pythonSettings = this.configurationService.getSettings(workspaceFolder?.uri);
if (pythonSettings.workspaceSymbols.rebuildOnFileSave) {
const promises = this.buildWorkspaceSymbols(true);
return Promise.all(promises);
}
}
// tslint:disable-next-line:no-any
private buildWorkspaceSymbols(rebuild: boolean = true, token?: CancellationToken): Promise<any>[] {
if (token && token.isCancellationRequested) {
return [];
}
if (this.generators.length === 0) {
return [];
}
let promptPromise: Promise<InstallerResponse>;
let promptResponse: InstallerResponse;
return this.generators.map(async generator => {
if (!generator.enabled) {
return;
}
const exists = await this.fs.fileExists(generator.tagFilePath);
// If file doesn't exist, then run the ctag generator,
// or check if required to rebuild.
if (!rebuild && exists) {
return;
}
for (let counter = 0; counter < MAX_NUMBER_OF_ATTEMPTS_TO_INSTALL_AND_BUILD; counter += 1) {
try {
await generator.generateWorkspaceTags();
return;
} catch (error) {
if (!isNotInstalledError(error)) {
return;
}
}
if (!token || token.isCancellationRequested) {
return;
}
// Display prompt once for all workspaces.
if (promptPromise) {
promptResponse = await promptPromise;
continue;
} else {
const installer = this.serviceContainer.get<IInstaller>(IInstaller);
promptPromise = installer.promptToInstall(Product.ctags, this.workspace.workspaceFolders![0]!.uri);
promptResponse = await promptPromise;
}
if (promptResponse !== InstallerResponse.Installed || !token || token.isCancellationRequested) {
return;
}
}
});
}
}