-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathlanguageServerApiManager.ts
More file actions
152 lines (133 loc) · 6.98 KB
/
languageServerApiManager.ts
File metadata and controls
152 lines (133 loc) · 6.98 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import { commands, Event, Extension, extensions, Uri, window } from "vscode";
import { Commands } from "../commands";
import { Context, ExtensionName } from "../constants";
import { contextManager } from "../contextManager";
import { Settings } from "../settings";
import { syncHandler } from "../syncHandler";
import { LanguageServerMode } from "./LanguageServerMode";
class LanguageServerApiManager {
private extensionApi: any;
private isServerReady: boolean = false;
private isServerRunning: boolean = false;
public async ready(): Promise<boolean> {
if (this.isServerReady) {
return true;
}
if (!this.isApiInitialized()) {
await this.initializeJavaLanguageServerApis();
}
const serverMode: LanguageServerMode | undefined = this.extensionApi?.serverMode;
if (!serverMode || serverMode === LanguageServerMode.LightWeight) {
return false;
}
// Use serverRunning() if available (API >= 0.14) for progressive loading.
// This resolves when the server process is alive and can handle requests,
// even if project imports haven't completed yet. This enables the tree view
// to show projects incrementally as they are imported.
if (!this.isServerRunning && this.extensionApi.serverRunning) {
await this.extensionApi.serverRunning();
this.isServerRunning = true;
// Start background wait for full server readiness (import complete).
// When the server finishes importing, trigger a full refresh to replace
// progressive placeholder items with proper data from the server.
if (this.extensionApi.serverReady) {
this.extensionApi.serverReady().then(() => {
this.isServerReady = true;
commands.executeCommand(Commands.VIEW_PACKAGE_INTERNAL_REFRESH, /* debounce = */false);
});
}
return true;
}
if (this.isServerRunning) {
return true;
}
// Fallback for older API versions: wait for full server readiness
await this.extensionApi.serverReady();
this.isServerReady = true;
return true;
}
public async initializeJavaLanguageServerApis(): Promise<void> {
if (this.isApiInitialized()) {
return;
}
const extension: Extension<any> | undefined = extensions.getExtension(ExtensionName.JAVA_LANGUAGE_SUPPORT);
if (extension) {
contextManager.setContextValue(Context.LANGUAGE_SUPPORT_INSTALLED, true);
await extension.activate();
const extensionApi: any = extension.exports;
if (!extensionApi) {
window.showErrorMessage("Please update 'redhat.java' to the latest version.");
return;
}
this.extensionApi = extensionApi;
if (extensionApi.onDidClasspathUpdate) {
const onDidClasspathUpdate: Event<Uri> = extensionApi.onDidClasspathUpdate;
contextManager.context.subscriptions.push(onDidClasspathUpdate((uri: Uri) => {
if (this.isServerReady) {
// Server is fully ready — do a normal refresh to get full project data.
commands.executeCommand(Commands.VIEW_PACKAGE_INTERNAL_REFRESH, /* debounce = */false);
} else {
// During import, the server is blocked and can't respond to queries.
// Don't clear progressive items. Try to add the project if not
// already present (typically a no-op since ProjectsImported fires first).
commands.executeCommand(Commands.VIEW_PACKAGE_INTERNAL_ADD_PROJECTS, [uri.toString()]);
}
syncHandler.updateFileWatcher(Settings.autoRefresh());
}));
}
if (extensionApi.onDidProjectsImport) {
const onDidProjectsImport: Event<Uri[]> = extensionApi.onDidProjectsImport;
contextManager.context.subscriptions.push(onDidProjectsImport((uris: Uri[]) => {
// Server is sending project data, so it's definitely running.
// Mark as running so ready() returns immediately on subsequent calls.
this.isServerRunning = true;
// During import, the JDTLS server is blocked by Eclipse workspace
// operations and cannot respond to queries. Instead of triggering
// a refresh (which queries the server), directly add projects to
// the tree view from the notification data.
const projectUris = uris.map(u => u.toString());
commands.executeCommand(Commands.VIEW_PACKAGE_INTERNAL_ADD_PROJECTS, projectUris);
syncHandler.updateFileWatcher(Settings.autoRefresh());
}));
}
if (extensionApi.onDidProjectsDelete) {
const onDidProjectsDelete: Event<Uri[]> = extensionApi.onDidProjectsDelete;
contextManager.context.subscriptions.push(onDidProjectsDelete(() => {
commands.executeCommand(Commands.VIEW_PACKAGE_INTERNAL_REFRESH, /* debounce = */true);
syncHandler.updateFileWatcher(Settings.autoRefresh());
}));
}
if (this.extensionApi?.serverMode === LanguageServerMode.LightWeight) {
if (extensionApi.onDidServerModeChange) {
const onDidServerModeChange: Event<string> = extensionApi.onDidServerModeChange;
contextManager.context.subscriptions.push(onDidServerModeChange((mode: LanguageServerMode) => {
if (mode === LanguageServerMode.Hybrid) {
commands.executeCommand(Commands.VIEW_PACKAGE_INTERNAL_REFRESH, /* debounce = */false);
}
}));
}
}
}
}
private isApiInitialized(): boolean {
return this.extensionApi !== undefined;
}
/**
* Returns true if the server has fully completed initialization (import finished).
* During progressive loading, this returns false even though ready() has resolved.
*/
public isFullyReady(): boolean {
return this.isServerReady;
}
/**
* Check if the language server is ready in the given timeout.
* @param timeout the timeout in milliseconds to wait
* @returns false if the language server is not ready in the given timeout, otherwise true
*/
public isReady(timeout: number): Promise<boolean> {
return Promise.race([this.ready(), new Promise<boolean>((resolve) => setTimeout(() => resolve(false), timeout))]);
}
}
export const languageServerApiManager: LanguageServerApiManager = new LanguageServerApiManager();