diff --git a/Extension/src/LanguageServer/protocolFilter.ts b/Extension/src/LanguageServer/protocolFilter.ts index 2cdc38d53..0a166df83 100644 --- a/Extension/src/LanguageServer/protocolFilter.ts +++ b/Extension/src/LanguageServer/protocolFilter.ts @@ -11,6 +11,7 @@ import * as util from '../common'; import { logAndReturn } from '../Utility/Async/returns'; import { Client } from './client'; import { clients } from './extension'; +import { hasFileAssociation } from './settings'; import { shouldChangeFromCToCpp } from './utils'; export const RequestCancelled: number = -32800; @@ -30,21 +31,22 @@ export function createProtocolFilter(): Middleware { client.TrackedDocuments.set(uriString, document); // Work around vscode treating ".C" or ".H" as c, by adding this file name to file associations as cpp if (document.languageId === "c" && shouldChangeFromCToCpp(document)) { - const baseFileName: string = path.basename(document.fileName); - const mappingString: string = baseFileName + "@" + document.fileName; - client.addFileAssociations(mappingString, "cpp"); - client.sendDidChangeSettings(); - // This will cause the file to be closed and reopened. - void vscode.languages.setTextDocumentLanguage(document, "cpp"); - return; + // Don't override the user's setting. + if (!hasFileAssociation(path.basename(document.uri.fsPath))) { + const baseFileName: string = path.basename(document.fileName); + const mappingString: string = baseFileName + "@" + document.fileName; + client.addFileAssociations(mappingString, "cpp"); + client.sendDidChangeSettings(); + // The following will cause the file to be closed and reopened. + void vscode.languages.setTextDocumentLanguage(document, "cpp"); + return; + } } // client.takeOwnership() will call client.TrackedDocuments.add() again, but that's ok. It's a Set. client.takeOwnership(document); void sendMessage(document); - const editor: vscode.TextEditor | undefined = vscode.window.visibleTextEditors.find(editor => editor.document === document); - if (editor) { - client.onDidChangeVisibleTextEditors([editor]).catch(logAndReturn.undefined); - } + const cppEditors: vscode.TextEditor[] = vscode.window.visibleTextEditors.filter(e => util.isCpp(e.document)); + client.onDidChangeVisibleTextEditors(cppEditors).catch(logAndReturn.undefined); } } }, diff --git a/Extension/src/LanguageServer/settings.ts b/Extension/src/LanguageServer/settings.ts index fb8b3c458..999e8114e 100644 --- a/Extension/src/LanguageServer/settings.ts +++ b/Extension/src/LanguageServer/settings.ts @@ -1099,3 +1099,17 @@ export class OtherSettings { public get searchExclude(): Excludes { return this.getAsExcludes("search", "exclude", this.defaultSearchExcludes, this.resource); } public get workbenchSettingsEditor(): string { return this.getAsString("workbench.settings", "editor", this.resource, "ui"); } } + +export function hasFileAssociation(fileName: string): boolean { + const otherSettings: OtherSettings = new OtherSettings(); + const associations: Associations = otherSettings.filesAssociations; + if (associations[fileName]) { + return true; + } + for (const pattern in associations) { + if (pattern.startsWith('*.') && fileName.endsWith(pattern.slice(1))) { + return true; + } + } + return false; +}