-
Notifications
You must be signed in to change notification settings - Fork 28.4k
/
extension.ts
149 lines (126 loc) · 5.06 KB
/
extension.ts
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import * as commands from './commands';
import { LanguageConfigurationManager } from './features/languageConfiguration';
import TypeScriptTaskProviderManager from './features/task';
import TypeScriptServiceClientHost from './typeScriptServiceClientHost';
import { flatten } from './utils/arrays';
import { CommandManager } from './utils/commandManager';
import * as fileSchemes from './utils/fileSchemes';
import { standardLanguageDescriptions } from './utils/languageDescription';
import { lazy, Lazy } from './utils/lazy';
import LogDirectoryProvider from './utils/logDirectoryProvider';
import ManagedFileContextManager from './utils/managedFileContext';
import { PluginManager } from './utils/plugins';
import * as ProjectStatus from './utils/projectStatus';
import { Surveyor } from './utils/surveyor';
interface ApiV0 {
readonly onCompletionAccepted: vscode.Event<vscode.CompletionItem>;
}
interface Api {
getAPI(version: 0): ApiV0 | undefined;
}
export function activate(
context: vscode.ExtensionContext
): Api {
const pluginManager = new PluginManager();
const commandManager = new CommandManager();
context.subscriptions.push(commandManager);
const onCompletionAccepted = new vscode.EventEmitter();
context.subscriptions.push(onCompletionAccepted);
const lazyClientHost = createLazyClientHost(context, pluginManager, commandManager, item => {
onCompletionAccepted.fire(item);
});
registerCommands(commandManager, lazyClientHost, pluginManager);
context.subscriptions.push(new TypeScriptTaskProviderManager(lazyClientHost.map(x => x.serviceClient)));
context.subscriptions.push(new LanguageConfigurationManager());
import('./features/tsconfig').then(module => {
context.subscriptions.push(module.register());
});
const supportedLanguage = flatten([
...standardLanguageDescriptions.map(x => x.modeIds),
...pluginManager.plugins.map(x => x.languages)
]);
function didOpenTextDocument(textDocument: vscode.TextDocument): boolean {
if (isSupportedDocument(supportedLanguage, textDocument)) {
openListener.dispose();
// Force activation
// tslint:disable-next-line:no-unused-expression
void lazyClientHost.value;
context.subscriptions.push(new ManagedFileContextManager(resource => {
return lazyClientHost.value.serviceClient.toPath(resource);
}));
return true;
}
return false;
}
const openListener = vscode.workspace.onDidOpenTextDocument(didOpenTextDocument, undefined, context.subscriptions);
for (const textDocument of vscode.workspace.textDocuments) {
if (didOpenTextDocument(textDocument)) {
break;
}
}
return {
getAPI(version) {
if (version === 0) {
return {
onCompletionAccepted: onCompletionAccepted.event
} as ApiV0;
}
return undefined;
}
} as Api;
}
function createLazyClientHost(
context: vscode.ExtensionContext,
pluginManager: PluginManager,
commandManager: CommandManager,
onCompletionAccepted: (item: vscode.CompletionItem) => void,
): Lazy<TypeScriptServiceClientHost> {
return lazy(() => {
const logDirectoryProvider = new LogDirectoryProvider(context);
const clientHost = new TypeScriptServiceClientHost(
standardLanguageDescriptions,
context.workspaceState,
pluginManager,
commandManager,
logDirectoryProvider,
onCompletionAccepted);
context.subscriptions.push(clientHost);
const surveyor = new Surveyor(context.globalState);
context.subscriptions.push(clientHost.serviceClient.onSurveyReady(e => surveyor.surveyReady(e.surveyId)));
clientHost.serviceClient.onReady(() => {
context.subscriptions.push(
ProjectStatus.create(
clientHost.serviceClient,
clientHost.serviceClient.telemetryReporter));
});
return clientHost;
});
}
function registerCommands(
commandManager: CommandManager,
lazyClientHost: Lazy<TypeScriptServiceClientHost>,
pluginManager: PluginManager,
) {
commandManager.register(new commands.ReloadTypeScriptProjectsCommand(lazyClientHost));
commandManager.register(new commands.ReloadJavaScriptProjectsCommand(lazyClientHost));
commandManager.register(new commands.SelectTypeScriptVersionCommand(lazyClientHost));
commandManager.register(new commands.OpenTsServerLogCommand(lazyClientHost));
commandManager.register(new commands.RestartTsServerCommand(lazyClientHost));
commandManager.register(new commands.TypeScriptGoToProjectConfigCommand(lazyClientHost));
commandManager.register(new commands.JavaScriptGoToProjectConfigCommand(lazyClientHost));
commandManager.register(new commands.ConfigurePluginCommand(pluginManager));
}
function isSupportedDocument(
supportedLanguage: string[],
document: vscode.TextDocument
): boolean {
if (supportedLanguage.indexOf(document.languageId) < 0) {
return false;
}
return fileSchemes.isSupportedScheme(document.uri.scheme);
}