From aff4038dcdc859b223d886eb6829077f3fef9d8e Mon Sep 17 00:00:00 2001 From: Colen Garoutte-Carson Date: Thu, 20 May 2021 15:20:08 -0700 Subject: [PATCH 1/2] Use provider ID associated with cached browse config to auto-detect provider ID --- Extension/src/LanguageServer/client.ts | 29 ++++---- .../src/LanguageServer/configurations.ts | 72 +++++++++++++------ 2 files changed, 62 insertions(+), 39 deletions(-) diff --git a/Extension/src/LanguageServer/client.ts b/Extension/src/LanguageServer/client.ts index d054c4c18..4db164177 100644 --- a/Extension/src/LanguageServer/client.ts +++ b/Extension/src/LanguageServer/client.ts @@ -2332,7 +2332,11 @@ export class DefaultClient implements Client { private doneInitialCustomBrowseConfigurationCheck: Boolean = false; - private onConfigurationsChanged(configurations: configs.Configuration[]): void { + private onConfigurationsChanged(cppProperties: configs.CppProperties): void { + if (!cppProperties.Configurations) { + return; + } + const configurations: configs.Configuration[] = cppProperties.Configurations; const params: CppPropertiesParams = { configurations: configurations, currentConfiguration: this.configuration.CurrentConfigurationIndex, @@ -2346,29 +2350,20 @@ export class DefaultClient implements Client { c.compilerPath = compilerPathAndArgs.compilerPath; c.compilerArgs = compilerPathAndArgs.additionalArgs; }); - let sendLastCustomBrowseConfiguration: boolean = false; - const rootFolder: vscode.WorkspaceFolder | undefined = this.RootFolder; - if (!rootFolder) { - this.languageClient.sendNotification(ChangeCppPropertiesNotification, params); - } else { - const lastCustomBrowseConfigurationProviderId: PersistentFolderState = new PersistentFolderState("CPP.lastCustomBrowseConfigurationProviderId", undefined, rootFolder); - const lastCustomBrowseConfiguration: PersistentFolderState = new PersistentFolderState("CPP.lastCustomBrowseConfiguration", undefined, rootFolder); + this.languageClient.sendNotification(ChangeCppPropertiesNotification, params); + const lastCustomBrowseConfigurationProviderId: PersistentFolderState | undefined = cppProperties.LastCustomBrowseConfigurationProviderId; + const lastCustomBrowseConfiguration: PersistentFolderState | undefined = cppProperties.LastCustomBrowseConfiguration; + if (!!lastCustomBrowseConfigurationProviderId && !!lastCustomBrowseConfiguration) { if (!this.doneInitialCustomBrowseConfigurationCheck) { // Send the last custom browse configuration we received from this provider. // This ensures we don't start tag parsing without it, and undo'ing work we have to re-do when the (likely same) browse config arrives // Should only execute on launch, for the initial delivery of configurations - if (isSameProviderExtensionId(lastCustomBrowseConfigurationProviderId.Value, configurations[params.currentConfiguration].configurationProvider)) { - if (lastCustomBrowseConfiguration.Value) { - sendLastCustomBrowseConfiguration = true; - params.isReady = false; - } + if (lastCustomBrowseConfiguration.Value) { + this.sendCustomBrowseConfiguration(lastCustomBrowseConfiguration.Value, lastCustomBrowseConfigurationProviderId.Value); + params.isReady = false; } this.doneInitialCustomBrowseConfigurationCheck = true; } - this.languageClient.sendNotification(ChangeCppPropertiesNotification, params); - if (sendLastCustomBrowseConfiguration) { - this.sendCustomBrowseConfiguration(lastCustomBrowseConfiguration.Value, lastCustomBrowseConfigurationProviderId.Value); - } } const configName: string | undefined = configurations[params.currentConfiguration].name ?? ""; this.model.activeConfigName.setValueIfActive(configName); diff --git a/Extension/src/LanguageServer/configurations.ts b/Extension/src/LanguageServer/configurations.ts index 23cc35789..9a1fa578a 100644 --- a/Extension/src/LanguageServer/configurations.ts +++ b/Extension/src/LanguageServer/configurations.ts @@ -20,6 +20,7 @@ import * as jsonc from 'comment-json'; import * as nls from 'vscode-nls'; import { setTimeout } from 'timers'; import * as which from 'which'; +import { WorkspaceBrowseConfiguration } from 'vscode-cpptools'; nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })(); const localize: nls.LocalizeFunc = nls.loadMessageBundle(); @@ -138,13 +139,15 @@ export class CppProperties { private defaultCustomConfigurationVariables?: { [key: string]: string }; private readonly configurationGlobPattern: string = "c_cpp_properties.json"; private disposables: vscode.Disposable[] = []; - private configurationsChanged = new vscode.EventEmitter(); + private configurationsChanged = new vscode.EventEmitter(); private selectionChanged = new vscode.EventEmitter(); private compileCommandsChanged = new vscode.EventEmitter(); private diagnosticCollection: vscode.DiagnosticCollection; private prevSquiggleMetrics: Map = new Map(); private rootfs: string | null = null; private settingsPanel?: SettingsPanel; + private lastCustomBrowseConfiguration: PersistentFolderState | undefined; + private lastCustomBrowseConfigurationProviderId: PersistentFolderState | undefined; // Any time the default settings are parsed and assigned to `this.configurationJson`, // we want to track when the default includes have been added to it. @@ -155,6 +158,8 @@ export class CppProperties { const rootPath: string = rootUri ? rootUri.fsPath : ""; if (workspaceFolder) { this.currentConfigurationIndex = new PersistentFolderState("CppProperties.currentConfigurationIndex", -1, workspaceFolder); + this.lastCustomBrowseConfiguration = new PersistentFolderState("CPP.lastCustomBrowseConfiguration", undefined, workspaceFolder); + this.lastCustomBrowseConfigurationProviderId = new PersistentFolderState("CPP.lastCustomBrowseConfigurationProviderId", undefined, workspaceFolder); } this.configFolder = path.join(rootPath, ".vscode"); this.diagnosticCollection = vscode.languages.createDiagnosticCollection(rootPath); @@ -166,7 +171,7 @@ export class CppProperties { this.disposables.push(vscode.Disposable.from(this.configurationsChanged, this.selectionChanged, this.compileCommandsChanged)); } - public get ConfigurationsChanged(): vscode.Event { return this.configurationsChanged.event; } + public get ConfigurationsChanged(): vscode.Event { return this.configurationsChanged.event; } public get SelectionChanged(): vscode.Event { return this.selectionChanged.event; } public get CompileCommandsChanged(): vscode.Event { return this.compileCommandsChanged.event; } public get Configurations(): Configuration[] | undefined { return this.configurationJson ? this.configurationJson.configurations : undefined; } @@ -174,6 +179,9 @@ export class CppProperties { public get CurrentConfiguration(): Configuration | undefined { return this.Configurations ? this.Configurations[this.CurrentConfigurationIndex] : undefined; } public get KnownCompiler(): KnownCompiler[] | undefined { return this.knownCompilers; } + public get LastCustomBrowseConfiguration(): PersistentFolderState | undefined { return this.lastCustomBrowseConfiguration; } + public get LastCustomBrowseConfigurationProviderId(): PersistentFolderState | undefined { return this.lastCustomBrowseConfigurationProviderId; } + public get CurrentConfigurationProvider(): string | undefined { if (this.CurrentConfiguration?.configurationProvider) { return this.CurrentConfiguration.configurationProvider; @@ -289,7 +297,7 @@ export class CppProperties { private onConfigurationsChanged(): void { if (this.Configurations) { - this.configurationsChanged.fire(this.Configurations); + this.configurationsChanged.fire(this); } } @@ -843,25 +851,45 @@ export class CppProperties { configuration.browse.limitSymbolsToIncludedHeaders = this.updateConfigurationStringOrBoolean(configuration.browse.limitSymbolsToIncludedHeaders, settings.defaultLimitSymbolsToIncludedHeaders, env); configuration.browse.databaseFilename = this.updateConfigurationString(configuration.browse.databaseFilename, settings.defaultDatabaseFilename, env); - // If there is no c_cpp_properties.json, there are no relevant C_Cpp.default.* settings set, - // and there is only 1 registered custom config provider, default to using that provider. - const providers: CustomConfigurationProviderCollection = getCustomConfigProviders(); - if (providers.size === 1 - && !this.propertiesFile - && !settings.defaultCompilerPath - && settings.defaultCompilerPath !== "" - && !settings.defaultIncludePath - && !settings.defaultDefines - && !settings.defaultMacFrameworkPath - && settings.defaultWindowsSdkVersion === "" - && !settings.defaultForcedInclude - && settings.defaultCompileCommands === "" - && !settings.defaultCompilerArgs - && settings.defaultCStandard === "" - && settings.defaultCppStandard === "" - && settings.defaultIntelliSenseMode === "" - && settings.defaultConfigurationProvider === "") { - providers.forEach(provider => { configuration.configurationProvider = provider.extensionId; }); + if (i === this.CurrentConfigurationIndex) { + // If there is no c_cpp_properties.json, there are no relevant C_Cpp.default.* settings set, + // and there is only 1 registered custom config provider, default to using that provider. + const providers: CustomConfigurationProviderCollection = getCustomConfigProviders(); + const hasEmptyConfiguration: boolean = !this.propertiesFile + && !settings.defaultCompilerPath + && settings.defaultCompilerPath !== "" + && !settings.defaultIncludePath + && !settings.defaultDefines + && !settings.defaultMacFrameworkPath + && settings.defaultWindowsSdkVersion === "" + && !settings.defaultForcedInclude + && settings.defaultCompileCommands === "" + && !settings.defaultCompilerArgs + && settings.defaultCStandard === "" + && settings.defaultCppStandard === "" + && settings.defaultIntelliSenseMode === "" + && settings.defaultConfigurationProvider === ""; + + // Only keep a cached custom browse config if there is an emptry configuration, + // or if a specified provider ID has not changed. + let keepCachedBrowseConfig: boolean = true; + if (hasEmptyConfiguration) { + if (providers.size === 1) { + providers.forEach(provider => { configuration.configurationProvider = provider.extensionId; }); + if (this.lastCustomBrowseConfigurationProviderId !== undefined) { + keepCachedBrowseConfig = configuration.configurationProvider === this.lastCustomBrowseConfigurationProviderId.Value; + } + } else if (this.lastCustomBrowseConfigurationProviderId !== undefined + && !!this.lastCustomBrowseConfigurationProviderId.Value) { + // Use the last configure provider we received a browse config from as the provider ID. + configuration.configurationProvider = this.lastCustomBrowseConfigurationProviderId.Value; + } + } else if (this.lastCustomBrowseConfigurationProviderId !== undefined) { + keepCachedBrowseConfig = configuration.configurationProvider === this.lastCustomBrowseConfigurationProviderId.Value; + } + if (!keepCachedBrowseConfig && this.lastCustomBrowseConfiguration !== undefined) { + this.lastCustomBrowseConfiguration.Value = undefined; + } } } From f7c1b157ea0e255bdc745a7f282fa5c6a91d6ba3 Mon Sep 17 00:00:00 2001 From: Colen Garoutte-Carson Date: Thu, 20 May 2021 16:51:43 -0700 Subject: [PATCH 2/2] Fix spelling/grammar issue --- Extension/src/LanguageServer/configurations.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Extension/src/LanguageServer/configurations.ts b/Extension/src/LanguageServer/configurations.ts index 9a1fa578a..d861a47e2 100644 --- a/Extension/src/LanguageServer/configurations.ts +++ b/Extension/src/LanguageServer/configurations.ts @@ -881,7 +881,7 @@ export class CppProperties { } } else if (this.lastCustomBrowseConfigurationProviderId !== undefined && !!this.lastCustomBrowseConfigurationProviderId.Value) { - // Use the last configure provider we received a browse config from as the provider ID. + // Use the last configuration provider we received a browse config from as the provider ID. configuration.configurationProvider = this.lastCustomBrowseConfigurationProviderId.Value; } } else if (this.lastCustomBrowseConfigurationProviderId !== undefined) {