Skip to content

Commit 8f55a2e

Browse files
add second prompt for when a compiler needs config (#10419)
* add second prompt for when a compiler needs config * fix other bugs Co-authored-by: Sean McManus <[email protected]>
1 parent 5f68e6a commit 8f55a2e

File tree

2 files changed

+72
-51
lines changed

2 files changed

+72
-51
lines changed

Extension/src/LanguageServer/client.ts

Lines changed: 68 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ let compilerDefaults: configs.CompilerDefaults;
7070
let diagnosticsCollectionIntelliSense: vscode.DiagnosticCollection;
7171
let diagnosticsCollectionRefactor: vscode.DiagnosticCollection;
7272
let displayedSelectCompiler: boolean = false;
73+
let secondPromptCounter: number = 0;
7374

7475
let workspaceDisposables: vscode.Disposable[] = [];
7576
export let workspaceReferences: refs.ReferencesManager;
@@ -889,7 +890,7 @@ export class DefaultClient implements Client {
889890
return workspaceFolder ? workspaceFolder.name : "untitled";
890891
}
891892

892-
public updateClientConfigurations(): void {
893+
public static updateClientConfigurations(): void {
893894
clients.forEach(client => {
894895
if (client instanceof DefaultClient) {
895896
const defaultClient: DefaultClient = <DefaultClient>client;
@@ -899,17 +900,19 @@ export class DefaultClient implements Client {
899900
});
900901
}
901902

902-
public async showSelectCompiler(paths: string[]): Promise<number> {
903+
public async showSelectDefaultCompiler(paths: string[]): Promise<number> {
903904
const options: vscode.QuickPickOptions = {};
904905
options.placeHolder = localize("select.compile.commands", "Select a compiler to configure for IntelliSense");
905906

906907
const items: IndexableQuickPickItem[] = [];
907908
for (let i: number = 0; i < paths.length; i++) {
908909
let option: string | undefined;
909910
let isCompiler: boolean = false;
911+
let isCl: boolean = false;
910912
const slash: string = (os.platform() === 'win32') ? "\\" : "/";
911913

912914
if (paths[i].includes(slash)) {
915+
isCl = util.isCl(paths[i]);
913916
if (paths[i].split(slash).pop() !== undefined) {
914917
option = paths[i].split(slash).pop();
915918
isCompiler = true;
@@ -918,7 +921,8 @@ export class DefaultClient implements Client {
918921

919922
if (option !== undefined && isCompiler) {
920923
const path: string | undefined = paths[i].replace(option, "");
921-
items.push({ label: option, description: localize("found.string", "Found at {0}", path), index: i });
924+
const description: string = isCl ? "" : localize("found.string", "Found at {0}", path);
925+
items.push({ label: option, description: description, index: i });
922926
} else {
923927
items.push({ label: paths[i], index: i });
924928
}
@@ -928,22 +932,55 @@ export class DefaultClient implements Client {
928932
return (selection) ? selection.index : -1;
929933
}
930934

931-
public async handleCompilerQuickPick(): Promise<void> {
935+
public async showPrompt(buttonMessage: string, showSecondPrompt: boolean): Promise<void> {
936+
if (secondPromptCounter < 1) {
937+
const value: string | undefined = await vscode.window.showInformationMessage(localize("setCompiler.message", "You do not have IntelliSense configured. Unless you set your own configurations, IntelliSense may not be functional."), buttonMessage);
938+
secondPromptCounter++;
939+
if (value === buttonMessage) {
940+
this.handleCompilerQuickPick(showSecondPrompt);
941+
}
942+
}
943+
}
944+
945+
public async handleCompilerQuickPick(showSecondPrompt: boolean): Promise<void> {
932946
const settings: OtherSettings = new OtherSettings();
933-
let paths: string[] = [];
947+
const selectCompiler: string = localize("selectCompiler.string", "Select Compiler");
948+
const paths: string[] = [];
934949
if (compilerDefaults.knownCompilers !== undefined) {
935-
paths = compilerDefaults.knownCompilers.map(function (a: configs.KnownCompiler): string { return a.path; });
950+
const tempPaths: string[] = compilerDefaults.knownCompilers.map(function (a: configs.KnownCompiler): string { return a.path; });
951+
let clFound: boolean = false;
952+
// Remove all but the first cl path.
953+
for (const path of tempPaths) {
954+
if (clFound) {
955+
if (!util.isCl(path)) {
956+
paths.push(path);
957+
}
958+
} else {
959+
if (util.isCl(path)) {
960+
clFound = true;
961+
}
962+
paths.push(path);
963+
}
964+
}
936965
}
937966
paths.push(localize("selectAnotherCompiler.string", "Select another compiler on my machine"));
938967
paths.push(localize("installCompiler.string", "Help me install a compiler"));
939968
paths.push(localize("noConfig.string", "Do not configure a compiler (not recommended)"));
940-
const index: number = await this.showSelectCompiler(paths);
969+
const index: number = await this.showSelectDefaultCompiler(paths);
941970
if (index === -1) {
971+
if (showSecondPrompt) {
972+
this.showPrompt(selectCompiler, true);
973+
}
942974
return;
943975
}
944976
if (index === paths.length - 1) {
945977
settings.defaultCompiler = "";
946-
} else if (index === paths.length - 2) {
978+
if (showSecondPrompt) {
979+
this.showPrompt(selectCompiler, true);
980+
}
981+
return;
982+
}
983+
if (index === paths.length - 2) {
947984
switch (os.platform()) {
948985
case 'win32':
949986
vscode.commands.executeCommand('vscode.open', "https://go.microsoft.com/fwlink/?linkid=2217614");
@@ -955,55 +992,45 @@ export class DefaultClient implements Client {
955992
vscode.commands.executeCommand('vscode.open', "https://go.microsoft.com/fwlink/?linkid=2217615");
956993
return;
957994
}
958-
} else if (index === paths.length - 3) {
995+
}
996+
if (index === paths.length - 3) {
959997
const result: vscode.Uri[] | undefined = await vscode.window.showOpenDialog();
960-
if (result !== undefined && result.length > 0) {
961-
util.addTrustedCompiler(compilerPaths, result[0].fsPath);
962-
settings.defaultCompiler = result[0].fsPath;
963-
compilerDefaults = await this.requestCompiler(compilerPaths);
964-
this.updateClientConfigurations();
998+
if (result === undefined || result.length === 0) {
965999
return;
9661000
}
1001+
settings.defaultCompiler = result[0].fsPath;
9671002
} else {
968-
util.addTrustedCompiler(compilerPaths, paths[index]);
969-
}
970-
// If a compiler is selected, update the default.compilerPath user setting.
971-
if (index < paths.length - 3) {
972-
settings.defaultCompiler = paths[index];
1003+
settings.defaultCompiler = util.isCl(paths[index]) ? "cl.exe" : paths[index];
9731004
}
1005+
util.addTrustedCompiler(compilerPaths, settings.defaultCompiler);
9741006
compilerDefaults = await this.requestCompiler(compilerPaths);
975-
this.updateClientConfigurations();
1007+
DefaultClient.updateClientConfigurations();
9761008
}
9771009

978-
async promptSelectCompiler(command: boolean): Promise<void> {
1010+
async promptSelectCompiler(isCommand: boolean): Promise<void> {
1011+
if (compilerDefaults === undefined) {
1012+
return;
1013+
}
9791014
const selectCompiler: string = localize("selectCompiler.string", "Select Compiler");
9801015
const confirmCompiler: string = localize("confirmCompiler.string", "Yes");
9811016
const settings: OtherSettings = new OtherSettings();
982-
if (compilerDefaults.compilerPath !== "") {
983-
if (!command && (compilerDefaults.compilerPath !== undefined)) {
984-
const value: string | undefined = await vscode.window.showInformationMessage(localize("selectCompiler.message", "The compiler {0} was found on this computer. Do you want to configure it for IntelliSense?", compilerDefaults.compilerPath), confirmCompiler, selectCompiler);
1017+
if (isCommand || compilerDefaults.compilerPath !== "") {
1018+
if (!isCommand && (compilerDefaults.compilerPath !== undefined)) {
1019+
const value: string | undefined = await vscode.window.showInformationMessage(localize("selectCompiler.message", "The compiler {0} was found. Do you want to configure IntelliSense with this compiler?", compilerDefaults.compilerPath), confirmCompiler, selectCompiler);
9851020
if (value === confirmCompiler) {
9861021
compilerPaths = await util.addTrustedCompiler(compilerPaths, compilerDefaults.compilerPath);
9871022
settings.defaultCompiler = compilerDefaults.compilerPath;
9881023
compilerDefaults = await this.requestCompiler(compilerPaths);
989-
this.updateClientConfigurations();
1024+
DefaultClient.updateClientConfigurations();
9901025
} else if (value === selectCompiler) {
991-
this.handleCompilerQuickPick();
1026+
this.handleCompilerQuickPick(true);
9921027
} else {
993-
const setCompiler: string = localize("setCompiler.string", "Set Compiler");
994-
const value: string | undefined = await vscode.window.showInformationMessage(localize("setCompiler.message", "You do not have a compiler configured. Unless you set your own configurations, IntelliSense may not be functional."), selectCompiler);
995-
if (value === setCompiler) {
996-
this.handleCompilerQuickPick();
997-
}
998-
}
999-
} else if (!command && (compilerDefaults.compilerPath === undefined)) {
1000-
const setCompiler: string = localize("setCompiler.string", "Set Compiler");
1001-
const value: string | undefined = await vscode.window.showInformationMessage(localize("setCompiler.message", "You do not have a compiler configured. Unless you set your own configurations, IntelliSense may not be functional."), selectCompiler);
1002-
if (value === setCompiler) {
1003-
this.handleCompilerQuickPick();
1028+
this.showPrompt(selectCompiler, true);
10041029
}
1030+
} else if (!isCommand && (compilerDefaults.compilerPath === undefined)) {
1031+
this.showPrompt(selectCompiler, false);
10051032
} else {
1006-
this.handleCompilerQuickPick();
1033+
this.handleCompilerQuickPick(false);
10071034
}
10081035
}
10091036
}
@@ -1125,16 +1152,9 @@ export class DefaultClient implements Client {
11251152
if ((vscode.workspace.workspaceFolders === undefined) || (initializedClientCount >= vscode.workspace.workspaceFolders.length)) {
11261153
// The configurations will not be sent to the language server until the default include paths and frameworks have been set.
11271154
// The event handlers must be set before this happens.
1128-
const inputCompilerDefaults: configs.CompilerDefaults = await this.requestCompiler(compilerPaths);
1129-
compilerDefaults = inputCompilerDefaults;
1130-
clients.forEach(client => {
1131-
if (client instanceof DefaultClient) {
1132-
const defaultClient: DefaultClient = <DefaultClient>client;
1133-
defaultClient.configuration.CompilerDefaults = compilerDefaults;
1134-
defaultClient.configuration.handleConfigurationChange();
1135-
}
1136-
});
1137-
if (!compilerDefaults.trustedCompilerFound && !displayedSelectCompiler) {
1155+
compilerDefaults = await this.requestCompiler(compilerPaths);
1156+
DefaultClient.updateClientConfigurations();
1157+
if (!compilerDefaults.trustedCompilerFound && !displayedSelectCompiler && (compilerPaths.length !== 1 || compilerPaths[0] !== "")) {
11381158
// if there is no compilerPath in c_cpp_properties.json, prompt user to configure a compiler
11391159
this.promptSelectCompiler(false);
11401160
displayedSelectCompiler = true;

Extension/src/common.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,10 +1046,11 @@ function extractArgs(argsString: string): string[] {
10461046
}
10471047
}
10481048

1049-
function isCl(compilerPath: string): boolean {
1049+
export function isCl(compilerPath: string): boolean {
10501050
const compilerPathLowercase: string = compilerPath.toLowerCase();
1051-
return (compilerPathLowercase.endsWith("\\cl.exe") || compilerPathLowercase.endsWith("/cl.exe") || (compilerPathLowercase === "cl.exe")
1052-
|| compilerPathLowercase.endsWith("\\cl") || compilerPathLowercase.endsWith("/cl") || (compilerPathLowercase === "cl"));
1051+
return compilerPathLowercase === "cl" || compilerPathLowercase === "cl.exe"
1052+
|| compilerPathLowercase.endsWith("\\cl.exe") || compilerPathLowercase.endsWith("/cl.exe")
1053+
|| compilerPathLowercase.endsWith("\\cl") || compilerPathLowercase.endsWith("/cl");
10531054
}
10541055

10551056
/** CompilerPathAndArgs retains original casing of text input for compiler path and args */

0 commit comments

Comments
 (0)