Skip to content

Commit d125910

Browse files
validate advanced settings input (#3838)
1 parent 3926110 commit d125910

File tree

3 files changed

+82
-35
lines changed

3 files changed

+82
-35
lines changed

Extension/src/LanguageServer/configurations.ts

Lines changed: 67 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ export interface ConfigurationErrors {
6868
compilerPath?: string;
6969
includePath?: string;
7070
intelliSenseMode?: string;
71+
macFrameworkPath?: string;
72+
forcedInclude?: string;
73+
compileCommands?: string;
74+
browsePath?: string;
75+
databaseFilename?: string;
7176
}
7277

7378
export interface Browse {
@@ -907,7 +912,6 @@ export class CppProperties {
907912
private getErrorsForConfigUI(configIndex: number): ConfigurationErrors {
908913
let errors: ConfigurationErrors = {};
909914
const isWindows: boolean = os.platform() === 'win32';
910-
911915
let config: Configuration = this.configurationJson.configurations[configIndex];
912916

913917
// Validate compilerPath
@@ -971,51 +975,79 @@ export class CppProperties {
971975
}
972976
}
973977

974-
// Validate includePath
975-
let includePathErrors: string[] = [];
976-
if (config.includePath) {
977-
for (let includePath of config.includePath) {
978-
let pathExists: boolean = true;
979-
let resolvedIncludePath: string = this.resolvePath(includePath, isWindows);
980-
if (!resolvedIncludePath) {
981-
continue;
982-
}
978+
// Validate paths (directories)
979+
errors.includePath = this.validatePath(config.includePath);
980+
errors.macFrameworkPath = this.validatePath(config.macFrameworkPath);
981+
errors.browsePath = this.validatePath(config.browse ? config.browse.path : undefined);
983982

984-
// Check if resolved path exists
985-
if (!fs.existsSync(resolvedIncludePath)) {
986-
// Check for relative path if resolved path does not exists
987-
const relativePath: string = this.rootUri.fsPath + path.sep + resolvedIncludePath;
988-
if (!fs.existsSync(relativePath)) {
989-
pathExists = false;
990-
} else {
991-
resolvedIncludePath = relativePath;
992-
}
993-
}
983+
// Validate files
984+
errors.forcedInclude = this.validatePath(config.forcedInclude, false);
985+
errors.compileCommands = this.validatePath(config.compileCommands, false);
986+
errors.databaseFilename = this.validatePath((config.browse ? config.browse.databaseFilename : undefined), false);
994987

995-
if (!pathExists) {
996-
let message: string = `Cannot find: ${resolvedIncludePath}`;
997-
includePathErrors.push(message);
998-
continue;
999-
}
988+
// Validate intelliSenseMode
989+
if (isWindows && !this.isCompilerIntelliSenseModeCompatible(config)) {
990+
errors.intelliSenseMode = `IntelliSense mode ${config.intelliSenseMode} is incompatible with compiler path.`;
991+
}
992+
993+
return errors;
994+
}
1000995

1001-
// Check if path is a directory
1002-
if (!util.checkDirectoryExistsSync(resolvedIncludePath)) {
1003-
let message: string = `Path is not a directory: ${resolvedIncludePath}`;
1004-
includePathErrors.push(message);
996+
private validatePath(input: string|string[], isDirectory: boolean = true): string {
997+
if (!input) {
998+
return undefined;
999+
}
1000+
1001+
const isWindows: boolean = os.platform() === 'win32';
1002+
let errorMsg: string = undefined;
1003+
let errors: string[] = [];
1004+
let paths: string[] = [];
1005+
1006+
if (util.isString(input)) {
1007+
paths.push(input);
1008+
} else {
1009+
paths = input;
1010+
}
1011+
1012+
for (let p of paths) {
1013+
let pathExists: boolean = true;
1014+
let resolvedPath: string = this.resolvePath(p, isWindows);
1015+
if (!resolvedPath) {
1016+
continue;
1017+
}
1018+
1019+
// Check if resolved path exists
1020+
if (!fs.existsSync(resolvedPath)) {
1021+
// Check for relative path if resolved path does not exists
1022+
const relativePath: string = this.rootUri.fsPath + path.sep + resolvedPath;
1023+
if (!fs.existsSync(relativePath)) {
1024+
pathExists = false;
1025+
} else {
1026+
resolvedPath = relativePath;
10051027
}
10061028
}
10071029

1008-
if (includePathErrors.length > 0) {
1009-
errors.includePath = includePathErrors.join('\n');
1030+
if (!pathExists) {
1031+
let message: string = `Cannot find: ${resolvedPath}`;
1032+
errors.push(message);
1033+
continue;
1034+
}
1035+
1036+
// Check if path is a directory or file
1037+
if (isDirectory && !util.checkDirectoryExistsSync(resolvedPath)) {
1038+
let message: string = `Path is not a directory: ${resolvedPath}`;
1039+
errors.push(message);
1040+
} else if (!isDirectory && !util.checkFileExistsSync(resolvedPath)) {
1041+
let message: string = `Path is not a file: ${resolvedPath}`;
1042+
errors.push(message);
10101043
}
10111044
}
10121045

1013-
// Validate intelliSenseMode
1014-
if (isWindows && !this.isCompilerIntelliSenseModeCompatible(config)) {
1015-
errors.intelliSenseMode = `IntelliSense mode ${config.intelliSenseMode} is incompatible with compiler path.`;
1046+
if (errors.length > 0) {
1047+
errorMsg = errors.join('\n');
10161048
}
10171049

1018-
return errors;
1050+
return errorMsg;
10191051
}
10201052

10211053
private handleSquiggles(): void {

Extension/ui/settings.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,7 @@
597597
<div>
598598
<div class="section-note">One path per line.</div>
599599
<textarea name="inputValue" id="macFrameworkPath" rows="4" cols="93" style="width: 800px"></textarea>
600+
<div id="macFrameworkPathInvalid" class="error" style="margin-top: -4px; width: 794px"></div>
600601
</div>
601602
</div>
602603

@@ -608,6 +609,7 @@
608609
<div>
609610
<div class="section-note">One file per line.</div>
610611
<textarea name="inputValue" id="forcedInclude" rows="4" cols="93" style="width: 800px"></textarea>
612+
<div id="forcedIncludeInvalid" class="error" style="margin-top: -4px; width: 794px"></div>
611613
</div>
612614
</div>
613615

@@ -618,6 +620,7 @@
618620
</div>
619621
<div>
620622
<input name="inputValue" id="compileCommands" style="width: 798px"></input>
623+
<div id="compileCommandsInvalid" class="error" style="width: 800px"></div>
621624
</div>
622625
</div>
623626

@@ -631,6 +634,7 @@
631634
<div>
632635
<div class="section-note">One browse path per line.</div>
633636
<textarea name="inputValue" id="browsePath" rows="4" cols="93" style="width: 800px"></textarea>
637+
<div id="browsePathInvalid" class="error" style="margin-top: -4px; width: 794px"></div>
634638
</div>
635639
</div>
636640

@@ -651,6 +655,7 @@
651655
</div>
652656
<div>
653657
<input name="inputValue" id="databaseFilename" style="width: 798px"></input>
658+
<div id="databaseFilenameInvalid" class="error" style="width: 800px"></div>
654659
</div>
655660
</div>
656661

Extension/ui/settings.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,19 @@ const elementId: { [key: string]: string } = {
3030
// Advanced settings
3131
windowsSdkVersion: "windowsSdkVersion",
3232
macFrameworkPath: "macFrameworkPath",
33+
macFrameworkPathInvalid: "macFrameworkPathInvalid",
3334
compileCommands: "compileCommands",
35+
compileCommandsInvalid: "compileCommandsInvalid",
3436
configurationProvider: "configurationProvider",
3537
forcedInclude: "forcedInclude",
38+
forcedIncludeInvalid: "forcedIncludeInvalid",
3639

3740
// Browse properties
3841
browsePath: "browsePath",
42+
browsePathInvalid: "browsePathInvalid",
3943
limitSymbolsToIncludedHeaders: "limitSymbolsToIncludedHeaders",
4044
databaseFilename: "databaseFilename",
45+
databaseFilenameInvalid: "databaseFilenameInvalid",
4146

4247
// Other
4348
showAdvanced: "showAdvanced",
@@ -288,6 +293,11 @@ class SettingsApp {
288293
this.showErrorWithInfo(elementId.intelliSenseModeInvalid, errors.intelliSenseMode);
289294
this.showErrorWithInfo(elementId.compilerPathInvalid, errors.compilerPath);
290295
this.showErrorWithInfo(elementId.includePathInvalid, errors.includePath);
296+
this.showErrorWithInfo(elementId.macFrameworkPathInvalid, errors.macFrameworkPath);
297+
this.showErrorWithInfo(elementId.forcedIncludeInvalid, errors.forcedInclude);
298+
this.showErrorWithInfo(elementId.compileCommandsInvalid, errors.compileCommands);
299+
this.showErrorWithInfo(elementId.browsePathInvalid, errors.browsePath);
300+
this.showErrorWithInfo(elementId.databaseFilenameInvalid, errors.databaseFilename);
291301
} finally {
292302
this.updating = false;
293303
}

0 commit comments

Comments
 (0)