forked from microsoft/vscode-cpptools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclientCollection.ts
More file actions
219 lines (190 loc) · 9.57 KB
/
Copy pathclientCollection.ts
File metadata and controls
219 lines (190 loc) · 9.57 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* See 'LICENSE' in the project root for license information.
* ------------------------------------------------------------------------------------------ */
'use strict';
import * as vscode from 'vscode';
import * as util from '../common';
import * as telemetry from '../telemetry';
import * as cpptools from './client';
import { getCustomConfigProviders } from './customProviders';
const defaultClientKey: string = "@@default@@";
export interface ClientKey {
name: string;
key: string;
}
export class ClientCollection {
private disposables: vscode.Disposable[] = [];
private languageClients = new Map<string, cpptools.Client>();
private defaultClient: cpptools.Client;
private activeClient: cpptools.Client;
private activeDocument?: vscode.TextDocument;
public get ActiveClient(): cpptools.Client { return this.activeClient; }
public get Names(): ClientKey[] {
const result: ClientKey[] = [];
this.languageClients.forEach((client, key) => {
result.push({ name: client.Name, key: key });
});
return result;
}
public get Count(): number { return this.languageClients.size; }
constructor() {
let key: string = defaultClientKey;
if (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0) {
let isFirstWorkspaceFolder: boolean = true;
vscode.workspace.workspaceFolders.forEach(folder => {
const newClient: cpptools.Client = cpptools.createClient(this, folder);
this.languageClients.set(util.asFolder(folder.uri), newClient);
if (isFirstWorkspaceFolder) {
isFirstWorkspaceFolder = false;
} else {
newClient.deactivate();
}
});
key = util.asFolder(vscode.workspace.workspaceFolders[0].uri);
const client: cpptools.Client | undefined = this.languageClients.get(key);
if (!client) {
throw new Error("Failed to construct default client");
}
this.activeClient = client;
} else {
this.activeClient = cpptools.createClient(this);
}
this.defaultClient = this.activeClient;
this.languageClients.set(key, this.activeClient);
this.disposables.push(vscode.workspace.onDidChangeWorkspaceFolders(e => this.onDidChangeWorkspaceFolders(e)));
this.disposables.push(vscode.workspace.onDidCloseTextDocument(d => this.onDidCloseTextDocument(d)));
}
public activeDocumentChanged(document: vscode.TextDocument): void {
this.activeDocument = document;
const activeClient: cpptools.Client = this.getClientFor(document.uri);
// Notify the active client that the document has changed.
activeClient.activeDocumentChanged(document);
// If the active client changed, resume the new client and tell the currently active client to deactivate.
if (activeClient !== this.activeClient) {
activeClient.activate();
this.activeClient.deactivate();
this.activeClient = activeClient;
}
}
/**
* get a handle to a language client. returns undefined if the client was not found.
*/
public get(key: string): cpptools.Client | undefined {
const client: cpptools.Client | undefined = this.languageClients.get(key);
console.assert(client, "key not found");
return client;
}
public forEach(callback: (client: cpptools.Client) => void): void {
this.languageClients.forEach(callback);
}
public checkOwnership(client: cpptools.Client, document: vscode.TextDocument): boolean {
return (this.getClientFor(document.uri) === client);
}
/**
* creates a new client to replace one that crashed.
*/
public replace(client: cpptools.Client, transferFileOwnership: boolean): cpptools.Client | undefined {
let key: string | undefined;
for (const pair of this.languageClients) {
if (pair[1] === client) {
key = pair[0];
break;
}
}
if (key) {
this.languageClients.delete(key);
if (transferFileOwnership) {
// This will create a new Client for the workspace since we removed the old one from this.languageClients.
client.TrackedDocuments.forEach(document => this.transferOwnership(document, client));
client.TrackedDocuments.clear();
} else {
// Create an empty client that will continue to "own" files matching this workspace, but ignore all messages from VS Code.
this.languageClients.set(key, cpptools.createNullClient());
}
if (this.activeClient === client && this.activeDocument) {
this.activeClient = this.getClientFor(this.activeDocument.uri);
this.activeClient.activeDocumentChanged(this.activeDocument);
}
client.dispose();
return this.languageClients.get(key);
} else {
console.assert(key, "unable to locate language client");
return undefined;
}
}
private onDidChangeWorkspaceFolders(e?: vscode.WorkspaceFoldersChangeEvent): void {
const folderCount: number = vscode.workspace.workspaceFolders ? vscode.workspace.workspaceFolders.length : 0;
if (folderCount > 1) {
telemetry.logLanguageServerEvent("workspaceFoldersChange", { "count": folderCount.toString() });
}
if (e !== undefined) {
e.removed.forEach(folder => {
const path: string = util.asFolder(folder.uri);
const client: cpptools.Client | undefined = this.languageClients.get(path);
if (client) {
this.languageClients.delete(path); // Do this first so that we don't iterate on it during the ownership transfer process.
// Transfer ownership of the client's documents to another client.
// (this includes calling textDocument/didOpen on the new client so that the server knows it's open too)
client.TrackedDocuments.forEach(document => this.transferOwnership(document, client));
client.TrackedDocuments.clear();
if (this.activeClient === client && this.activeDocument) {
// Need to make a different client the active client.
this.activeClient = this.getClientFor(this.activeDocument.uri);
this.activeClient.activeDocumentChanged(this.activeDocument);
// may not need this, the navigation UI should not have changed.
// this.activeClient.selectionChanged(Range.create(vscode.window.activeTextEditor.selection.start, vscode.window.activeTextEditor.selection.end);
}
client.dispose();
}
});
e.added.forEach(folder => {
const path: string = util.asFolder(folder.uri);
const client: cpptools.Client | undefined = this.languageClients.get(path);
if (!client) {
const newClient: cpptools.Client = cpptools.createClient(this, folder);
this.languageClients.set(path, newClient);
newClient.deactivate(); // e.g. prevent the current config from switching.
const defaultClient: cpptools.DefaultClient = <cpptools.DefaultClient>newClient;
defaultClient.sendAllSettings();
}
});
}
}
private transferOwnership(document: vscode.TextDocument, oldOwner: cpptools.Client): void {
const newOwner: cpptools.Client = this.getClientFor(document.uri);
if (newOwner !== oldOwner) {
newOwner.takeOwnership(document);
}
}
public getClientFor(uri: vscode.Uri): cpptools.Client {
const folder: vscode.WorkspaceFolder | undefined = uri ? vscode.workspace.getWorkspaceFolder(uri) : undefined;
if (!folder) {
return this.defaultClient;
} else {
const key: string = util.asFolder(folder.uri);
const client: cpptools.Client | undefined = this.languageClients.get(key);
if (client) {
return client;
}
const newClient: cpptools.Client = cpptools.createClient(this, folder);
this.languageClients.set(key, newClient);
getCustomConfigProviders().forEach(provider => newClient.onRegisterCustomConfigurationProvider(provider));
const defaultClient: cpptools.DefaultClient = <cpptools.DefaultClient>newClient;
defaultClient.sendAllSettings();
return newClient;
}
}
private onDidCloseTextDocument(document: vscode.TextDocument): void {
// Don't seem to need to do anything here since we clean up when the workspace is closed instead.
}
public dispose(): Thenable<void> {
const promises: Thenable<void>[] = [];
this.disposables.forEach((d: vscode.Disposable) => d.dispose());
// this.defaultClient is already in this.languageClients, so do not call dispose() on it.
this.languageClients.forEach(client => promises.push(client.dispose()));
this.languageClients.clear();
cpptools.disposeWorkspaceData();
return Promise.all(promises).then(() => undefined);
}
}