Skip to content

Commit 16c4def

Browse files
authored
Add a command to change the active custom config provider (#2247)
1 parent 725828c commit 16c4def

File tree

7 files changed

+78
-33
lines changed

7 files changed

+78
-33
lines changed

Extension/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,11 @@
415415
"title": "Select a Configuration...",
416416
"category": "C/Cpp"
417417
},
418+
{
419+
"command": "C_Cpp.ConfigurationProviderSelect",
420+
"title": "Change Configuration Provider...",
421+
"category": "C/Cpp"
422+
},
418423
{
419424
"command": "C_Cpp.ConfigurationEdit",
420425
"title": "Edit Configurations...",

Extension/src/LanguageServer/client.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ const ChangeCompileCommandsNotification: NotificationType<FileChangedParams, voi
119119
const ChangeSelectedSettingNotification: NotificationType<FolderSelectedSettingParams, void> = new NotificationType<FolderSelectedSettingParams, void>('cpptools/didChangeSelectedSetting');
120120
const IntervalTimerNotification: NotificationType<void, void> = new NotificationType<void, void>('cpptools/onIntervalTimer');
121121
const CustomConfigurationNotification: NotificationType<CustomConfigurationParams, void> = new NotificationType<CustomConfigurationParams, void>('cpptools/didChangeCustomConfiguration');
122+
const ClearCustomConfigurationsNotification: NotificationType<void, void> = new NotificationType<void, void>('cpptools/clearCustomConfigurations');
122123

123124
// Notifications from the server
124125
const ReloadWindowNotification: NotificationType<void, void> = new NotificationType<void, void>('cpptools/reloadWindow');
@@ -175,6 +176,7 @@ export interface Client {
175176
pauseParsing(): void;
176177
resumeParsing(): void;
177178
handleConfigurationSelectCommand(): void;
179+
handleConfigurationProviderSelectCommand(): void;
178180
handleShowParsingCommands(): void;
179181
handleConfigurationEditCommand(): void;
180182
handleAddToIncludePathCommand(path: string): void;
@@ -433,7 +435,7 @@ class DefaultClient implements Client {
433435
vscode.window.showInformationMessage(message, allow, notNow, dontAskAgain).then(result => {
434436
switch (result) {
435437
case allow: {
436-
this.configuration.addCustomConfigurationProvider(provider.extensionId).then(() => {
438+
this.configuration.updateCustomConfigurationProvider(provider.extensionId).then(() => {
437439
telemetry.logLanguageServerEvent("customConfigurationProvider", { "providerId": provider.extensionId });
438440
});
439441
break;
@@ -451,7 +453,7 @@ class DefaultClient implements Client {
451453
} else if (selectedProvider === provider.extensionId) {
452454
telemetry.logLanguageServerEvent("customConfigurationProvider", { "providerId": provider.extensionId });
453455
} else if (selectedProvider === provider.name) {
454-
this.configuration.addCustomConfigurationProvider(provider.extensionId); // update the configurationProvider in c_cpp_properties.json
456+
this.configuration.updateCustomConfigurationProvider(provider.extensionId); // v0 -> v1 upgrade. Update the configurationProvider in c_cpp_properties.json
455457
}
456458
});
457459
}
@@ -1031,6 +1033,10 @@ class DefaultClient implements Client {
10311033
this.notifyWhenReady(() => this.languageClient.sendNotification(CustomConfigurationNotification, params));
10321034
}
10331035

1036+
private clearCustomConfigurations(): void {
1037+
this.notifyWhenReady(() => this.languageClient.sendNotification(ClearCustomConfigurationsNotification));
1038+
}
1039+
10341040
/*********************************************
10351041
* command handlers
10361042
*********************************************/
@@ -1046,6 +1052,27 @@ class DefaultClient implements Client {
10461052
});
10471053
}
10481054

1055+
public handleConfigurationProviderSelectCommand(): void {
1056+
this.notifyWhenReady(() => {
1057+
ui.showConfigurationProviders()
1058+
.then(extensionId => {
1059+
if (extensionId === undefined) {
1060+
// operation was cancelled.
1061+
return;
1062+
}
1063+
this.configuration.updateCustomConfigurationProvider(extensionId)
1064+
.then(() => {
1065+
if (extensionId) {
1066+
this.updateCustomConfigurations(getCustomConfigProviders().get(extensionId));
1067+
telemetry.logLanguageServerEvent("customConfigurationProvider", { "providerId": extensionId });
1068+
} else {
1069+
this.clearCustomConfigurations();
1070+
}
1071+
});
1072+
});
1073+
});
1074+
}
1075+
10491076
public handleShowParsingCommands(): void {
10501077
this.notifyWhenReady(() => {
10511078
ui.showParsingCommands()
@@ -1143,6 +1170,7 @@ class NullClient implements Client {
11431170
pauseParsing(): void {}
11441171
resumeParsing(): void {}
11451172
handleConfigurationSelectCommand(): void {}
1173+
handleConfigurationProviderSelectCommand(): void {}
11461174
handleShowParsingCommands(): void {}
11471175
handleConfigurationEditCommand(): void {}
11481176
handleAddToIncludePathCommand(path: string): void {}

Extension/src/LanguageServer/configurations.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,17 @@ export class CppProperties {
319319
});
320320
}
321321

322-
public addCustomConfigurationProvider(providerId: string): Thenable<void> {
322+
public updateCustomConfigurationProvider(providerId: string): Thenable<void> {
323323
return new Promise<void>((resolve) => {
324324
if (this.propertiesFile) {
325325
this.handleConfigurationEditCommand((document: vscode.TextDocument) => {
326326
this.parsePropertiesFile(); // Clear out any modifications we may have made internally.
327327
let config: Configuration = this.CurrentConfiguration;
328-
config.configurationProvider = providerId;
328+
if (providerId) {
329+
config.configurationProvider = providerId;
330+
} else {
331+
delete config.configurationProvider;
332+
}
329333
fs.writeFileSync(this.propertiesFile.fsPath, JSON.stringify(this.configurationJson, null, 4));
330334
this.handleConfigurationChange();
331335
resolve();

Extension/src/LanguageServer/extension.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ function registerCommands(): void {
210210
disposables.push(vscode.commands.registerCommand('C_Cpp.SwitchHeaderSource', onSwitchHeaderSource));
211211
disposables.push(vscode.commands.registerCommand('C_Cpp.ResetDatabase', onResetDatabase));
212212
disposables.push(vscode.commands.registerCommand('C_Cpp.ConfigurationSelect', onSelectConfiguration));
213+
disposables.push(vscode.commands.registerCommand('C_Cpp.ConfigurationProviderSelect', onSelectConfigurationProvider));
213214
disposables.push(vscode.commands.registerCommand('C_Cpp.ConfigurationEdit', onEditConfiguration));
214215
disposables.push(vscode.commands.registerCommand('C_Cpp.AddToIncludePath', onAddToIncludePath));
215216
disposables.push(vscode.commands.registerCommand('C_Cpp.ToggleErrorSquiggles', onToggleSquiggles));
@@ -311,7 +312,7 @@ function selectClient(): Thenable<Client> {
311312
function onResetDatabase(): void {
312313
onActivationEvent();
313314
/* need to notify the affected client(s) */
314-
selectClient().then(client => client.resetDatabase(), rejected => { });
315+
selectClient().then(client => client.resetDatabase(), rejected => {});
315316
}
316317

317318
function onSelectConfiguration(): void {
@@ -325,12 +326,21 @@ function onSelectConfiguration(): void {
325326
}
326327
}
327328

329+
function onSelectConfigurationProvider(): void {
330+
onActivationEvent();
331+
if (!isFolderOpen()) {
332+
vscode.window.showInformationMessage('Open a folder first to select a configuration provider');
333+
} else {
334+
selectClient().then(client => client.handleConfigurationProviderSelectCommand(), rejected => {});
335+
}
336+
}
337+
328338
function onEditConfiguration(): void {
329339
onActivationEvent();
330340
if (!isFolderOpen()) {
331341
vscode.window.showInformationMessage('Open a folder first to edit configurations');
332342
} else {
333-
selectClient().then(client => client.handleConfigurationEditCommand(), rejected => { });
343+
selectClient().then(client => client.handleConfigurationEditCommand(), rejected => {});
334344
}
335345
}
336346

@@ -413,17 +423,17 @@ function onShowReleaseNotes(): void {
413423

414424
function onPauseParsing(): void {
415425
onActivationEvent();
416-
selectClient().then(client => client.pauseParsing(), rejected => { });
426+
selectClient().then(client => client.pauseParsing(), rejected => {});
417427
}
418428

419429
function onResumeParsing(): void {
420430
onActivationEvent();
421-
selectClient().then(client => client.resumeParsing(), rejected => { });
431+
selectClient().then(client => client.resumeParsing(), rejected => {});
422432
}
423433

424434
function onShowParsingCommands(): void {
425435
onActivationEvent();
426-
selectClient().then(client => client.handleShowParsingCommands(), rejected => { });
436+
selectClient().then(client => client.handleShowParsingCommands(), rejected => {});
427437
}
428438

429439
function onTakeSurvey(): void {

Extension/src/LanguageServer/ui.ts

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import * as vscode from 'vscode';
88
import { Client } from './client';
9+
import { getCustomConfigProviders, CustomConfigurationProviderCollection } from './customProviders';
910

1011
let ui: UI;
1112

@@ -160,12 +161,22 @@ export class UI {
160161
items.push({ label: "Edit Configurations...", description: "", index: configurationNames.length });
161162

162163
return vscode.window.showQuickPick(items, options)
163-
.then(selection => {
164-
if (!selection) {
165-
return -1;
166-
}
167-
return selection.index;
168-
});
164+
.then(selection => (selection) ? selection.index : -1);
165+
}
166+
167+
public showConfigurationProviders(): Thenable<string|undefined> {
168+
let options: vscode.QuickPickOptions = {};
169+
options.placeHolder = "Select a Configuration Provider...";
170+
let providers: CustomConfigurationProviderCollection = getCustomConfigProviders();
171+
172+
let items: KeyedQuickPickItem[] = [];
173+
providers.forEach(provider => {
174+
items.push({ label: provider.name, description: "", key: provider.extensionId });
175+
});
176+
items.push({ label: "(none)", description: "Disable the active configuration provider, if applicable.", key: "" });
177+
178+
return vscode.window.showQuickPick(items, options)
179+
.then(selection => (selection) ? selection.key : undefined);
169180
}
170181

171182
public showCompileCommands(paths: string[]): Thenable<number> {
@@ -178,12 +189,7 @@ export class UI {
178189
}
179190

180191
return vscode.window.showQuickPick(items, options)
181-
.then(selection => {
182-
if (!selection) {
183-
return -1;
184-
}
185-
return selection.index;
186-
});
192+
.then(selection => (selection) ? selection.index : -1);
187193
}
188194

189195
public showWorkspaces(workspaceNames: { name: string; key: string }[]): Thenable<string> {
@@ -194,12 +200,7 @@ export class UI {
194200
workspaceNames.forEach(name => items.push({ label: name.name, description: "", key: name.key }));
195201

196202
return vscode.window.showQuickPick(items, options)
197-
.then(selection => {
198-
if (!selection) {
199-
return "";
200-
}
201-
return selection.key;
202-
});
203+
.then(selection => (selection) ? selection.key : "");
203204
}
204205

205206
public showParsingCommands(): Thenable<number> {
@@ -215,12 +216,7 @@ export class UI {
215216
}
216217

217218
return vscode.window.showQuickPick(items, options)
218-
.then(selection => {
219-
if (!selection) {
220-
return -1;
221-
}
222-
return selection.index;
223-
});
219+
.then(selection => (selection) ? selection.index : -1);
224220
}
225221

226222
public dispose(): void {

Extension/src/commands.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class TemporaryCommandRegistrar {
1515
private commandsToRegister: string[] = [
1616
"C_Cpp.ConfigurationEdit",
1717
"C_Cpp.ConfigurationSelect",
18+
"C_Cpp.ConfigurationProviderSelect",
1819
"C_Cpp.SwitchHeaderSource",
1920
"C_Cpp.Navigate",
2021
"C_Cpp.GoToDeclaration",

Extension/src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ function rewriteManifest(): Promise<void> {
311311
"onCommand:extension.pickRemoteNativeProcess",
312312
"onCommand:C_Cpp.ConfigurationEdit",
313313
"onCommand:C_Cpp.ConfigurationSelect",
314+
"onCommand:C_Cpp.ConfigurationProviderSelect",
314315
"onCommand:C_Cpp.SwitchHeaderSource",
315316
"onCommand:C_Cpp.Navigate",
316317
"onCommand:C_Cpp.GoToDeclaration",

0 commit comments

Comments
 (0)