-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy pathlazyClientHost.ts
102 lines (89 loc) · 3.67 KB
/
lazyClientHost.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
/*---------------------------------------------------------------------------------------------
* 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 { CommandManager } from './commands/commandManager';
import { IExperimentationTelemetryReporter } from './experimentTelemetryReporter';
import { OngoingRequestCancellerFactory } from './tsServer/cancellation';
import { ILogDirectoryProvider } from './tsServer/logDirectoryProvider';
import { TsServerProcessFactory } from './tsServer/server';
import { ITypeScriptVersionProvider } from './tsServer/versionProvider';
import TypeScriptServiceClientHost from './typeScriptServiceClientHost';
import { ActiveJsTsEditorTracker } from './ui/activeJsTsEditorTracker';
import ManagedFileContextManager from './ui/managedFileContext';
import { ServiceConfigurationProvider } from './configuration/configuration';
import * as fileSchemes from './configuration/fileSchemes';
import { standardLanguageDescriptions } from './configuration/languageDescription';
import { Lazy, lazy } from './utils/lazy';
import { Logger } from './logging/logger';
import { PluginManager } from './tsServer/plugins';
export function createLazyClientHost(
context: vscode.ExtensionContext,
onCaseInsensitiveFileSystem: boolean,
services: {
pluginManager: PluginManager;
commandManager: CommandManager;
logDirectoryProvider: ILogDirectoryProvider;
cancellerFactory: OngoingRequestCancellerFactory;
versionProvider: ITypeScriptVersionProvider;
processFactory: TsServerProcessFactory;
activeJsTsEditorTracker: ActiveJsTsEditorTracker;
serviceConfigurationProvider: ServiceConfigurationProvider;
experimentTelemetryReporter: IExperimentationTelemetryReporter | undefined;
logger: Logger;
},
onCompletionAccepted: (item: vscode.CompletionItem) => void,
): Lazy<TypeScriptServiceClientHost> {
return lazy(() => {
const clientHost = new TypeScriptServiceClientHost(
standardLanguageDescriptions,
context,
onCaseInsensitiveFileSystem,
services,
onCompletionAccepted);
context.subscriptions.push(clientHost);
return clientHost;
});
}
export function lazilyActivateClient(
lazyClientHost: Lazy<TypeScriptServiceClientHost>,
pluginManager: PluginManager,
activeJsTsEditorTracker: ActiveJsTsEditorTracker,
onActivate: () => Promise<void> = () => Promise.resolve(),
): vscode.Disposable {
const disposables: vscode.Disposable[] = [];
const supportedLanguage = [
...standardLanguageDescriptions.map(x => x.languageIds),
...pluginManager.plugins.map(x => x.languages)
].flat();
let hasActivated = false;
const maybeActivate = (textDocument: vscode.TextDocument): boolean => {
if (!hasActivated && isSupportedDocument(supportedLanguage, textDocument)) {
hasActivated = true;
onActivate().then(() => {
// Force activation
void lazyClientHost.value;
disposables.push(new ManagedFileContextManager(activeJsTsEditorTracker));
});
return true;
}
return false;
};
const didActivate = vscode.workspace.textDocuments.some(maybeActivate);
if (!didActivate) {
const openListener = vscode.workspace.onDidOpenTextDocument(doc => {
if (maybeActivate(doc)) {
openListener.dispose();
}
}, undefined, disposables);
}
return vscode.Disposable.from(...disposables);
}
function isSupportedDocument(
supportedLanguage: readonly string[],
document: vscode.TextDocument
): boolean {
return supportedLanguage.indexOf(document.languageId) >= 0
&& !fileSchemes.disabledSchemes.has(document.uri.scheme);
}