Skip to content

Commit 974ecd3

Browse files
grdownsbobbrow
authored andcommitted
Grdowns/detect compile commands (#2137)
1 parent 7be69a0 commit 974ecd3

3 files changed

Lines changed: 74 additions & 0 deletions

File tree

Extension/src/LanguageServer/client.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ interface CustomConfigurationParams {
9292
configurationItems: SourceFileConfigurationItem[];
9393
}
9494

95+
interface CompileCommandsPaths {
96+
paths: string[];
97+
}
98+
9599
// Requests
96100
const NavigationListRequest: RequestType<TextDocumentIdentifier, string, void, void> = new RequestType<TextDocumentIdentifier, string, void, void>('cpptools/requestNavigationList');
97101
const GoToDeclarationRequest: RequestType<void, void, void, void> = new RequestType<void, void, void, void>('cpptools/goToDeclaration');
@@ -122,6 +126,7 @@ const ReportStatusNotification: NotificationType<ReportStatusNotificationBody, v
122126
const DebugProtocolNotification: NotificationType<OutputNotificationBody, void> = new NotificationType<OutputNotificationBody, void>('cpptools/debugProtocol');
123127
const DebugLogNotification: NotificationType<OutputNotificationBody, void> = new NotificationType<OutputNotificationBody, void>('cpptools/debugLog');
124128
const InactiveRegionNotification: NotificationType<InactiveRegionParams, void> = new NotificationType<InactiveRegionParams, void>('cpptools/inactiveRegions');
129+
const CompileCommandsPathsNotification: NotificationType<CompileCommandsPaths, void> = new NotificationType<CompileCommandsPaths, void>('cpptools/compileCommandsPaths');
125130

126131
let failureMessageShown: boolean = false;
127132

@@ -521,6 +526,7 @@ class DefaultClient implements Client {
521526
this.languageClient.onNotification(ReportStatusNotification, (e) => this.updateStatus(e));
522527
this.languageClient.onNotification(ReportTagParseStatusNotification, (e) => this.updateTagParseStatus(e));
523528
this.languageClient.onNotification(InactiveRegionNotification, (e) => this.updateInactiveRegions(e));
529+
this.languageClient.onNotification(CompileCommandsPathsNotification, (e) => this.promptCompileCommands(e));
524530
this.setupOutputHandlers();
525531
}
526532

@@ -745,6 +751,46 @@ class DefaultClient implements Client {
745751
}
746752
}
747753

754+
private promptCompileCommands(params: CompileCommandsPaths) : void {
755+
if (this.configuration.Configurations[this.configuration.CurrentConfiguration].compileCommands !== undefined) {
756+
return;
757+
}
758+
759+
let showCompileCommandsSelection: PersistentState<boolean> = new PersistentState<boolean>("CPP.showCompileCommandsSelection", true);
760+
if (!showCompileCommandsSelection.Value) {
761+
return;
762+
}
763+
764+
let compileCommandStr: string = params.paths.length > 1 ? "a compile_commands.json file" : params.paths[0];
765+
let folderStr: string = (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 1) ? "the " + this.Name : "this";
766+
const message: string = `Would you like to use ${compileCommandStr} to auto-configure IntelliSense for ${folderStr} folder?`;
767+
768+
const yes: string = "Yes";
769+
const notNow: string = "Not Now";
770+
const dontAskAgain: string = "Don't Ask Again";
771+
vscode.window.showInformationMessage(message, yes, notNow, dontAskAgain).then((value) => {
772+
switch (value) {
773+
case yes:
774+
if (params.paths.length > 1) {
775+
ui.showCompileCommands(params.paths).then((index) => {
776+
if (index < 0) {
777+
return;
778+
}
779+
this.configuration.setCompileCommands(params.paths[index]);
780+
});
781+
} else {
782+
this.configuration.setCompileCommands(params.paths[0]);
783+
}
784+
break;
785+
case notNow:
786+
break;
787+
case dontAskAgain:
788+
showCompileCommandsSelection.Value = false;
789+
break;
790+
}
791+
});
792+
}
793+
748794
/*********************************************
749795
* requests to the language server
750796
*********************************************/

Extension/src/LanguageServer/configurations.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,16 @@ export class CppProperties {
311311
});
312312
}
313313

314+
public setCompileCommands(path: string): void {
315+
this.handleConfigurationEditCommand((document: vscode.TextDocument) => {
316+
this.parsePropertiesFile(); // Clear out any modifications we may have made internally.
317+
let config: Configuration = this.configurationJson.configurations[this.CurrentConfiguration];
318+
config.compileCommands = path;
319+
fs.writeFileSync(this.propertiesFile.fsPath, JSON.stringify(this.configurationJson, null, 4));
320+
this.updateServerOnFolderSettingsChange();
321+
});
322+
}
323+
314324
public select(index: number): Configuration {
315325
if (index === this.configurationJson.configurations.length) {
316326
this.handleConfigurationEditCommand(vscode.window.showTextDocument);

Extension/src/LanguageServer/ui.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,24 @@ export class UI {
168168
});
169169
}
170170

171+
public showCompileCommands(paths: string[]): Thenable<number> {
172+
let options: vscode.QuickPickOptions = {};
173+
options.placeHolder = "Select a compile_commands.json...";
174+
175+
let items: IndexableQuickPickItem[] = [];
176+
for (let i: number = 0; i < paths.length; i++) {
177+
items.push({label: paths[i], description: "", index: i});
178+
}
179+
180+
return vscode.window.showQuickPick(items, options)
181+
.then(selection => {
182+
if (!selection) {
183+
return -1;
184+
}
185+
return selection.index;
186+
});
187+
}
188+
171189
public showWorkspaces(workspaceNames: { name: string; key: string }[]): Thenable<string> {
172190
let options: vscode.QuickPickOptions = {};
173191
options.placeHolder = "Select a Workspace...";

0 commit comments

Comments
 (0)