From 43766fdffebf8b5b93f2c67ce99687418277c284 Mon Sep 17 00:00:00 2001 From: Colen Garoutte-Carson Date: Tue, 8 Apr 2025 17:16:01 -0700 Subject: [PATCH 1/7] Fix an issue if a user explicitly sets a `.C` file to C --- Extension/src/LanguageServer/protocolFilter.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Extension/src/LanguageServer/protocolFilter.ts b/Extension/src/LanguageServer/protocolFilter.ts index 2cdc38d53..38ed8c598 100644 --- a/Extension/src/LanguageServer/protocolFilter.ts +++ b/Extension/src/LanguageServer/protocolFilter.ts @@ -34,9 +34,8 @@ export function createProtocolFilter(): Middleware { const mappingString: string = baseFileName + "@" + document.fileName; client.addFileAssociations(mappingString, "cpp"); client.sendDidChangeSettings(); - // This will cause the file to be closed and reopened. + // This 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); From cfa068c8af1c847d7a89b25dfb8a8e2150849ad3 Mon Sep 17 00:00:00 2001 From: Colen Garoutte-Carson Date: Tue, 8 Apr 2025 17:29:30 -0700 Subject: [PATCH 2/7] Update comment --- Extension/src/LanguageServer/protocolFilter.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Extension/src/LanguageServer/protocolFilter.ts b/Extension/src/LanguageServer/protocolFilter.ts index 38ed8c598..5853cf73c 100644 --- a/Extension/src/LanguageServer/protocolFilter.ts +++ b/Extension/src/LanguageServer/protocolFilter.ts @@ -34,7 +34,8 @@ export function createProtocolFilter(): Middleware { const mappingString: string = baseFileName + "@" + document.fileName; client.addFileAssociations(mappingString, "cpp"); client.sendDidChangeSettings(); - // This cause the file to be closed and reopened. + // Changes to the languageId in the editor take precendence over setTextDocumentLanguage. + // So, this may or may not cause the file to be closed and reopened. void vscode.languages.setTextDocumentLanguage(document, "cpp"); } // client.takeOwnership() will call client.TrackedDocuments.add() again, but that's ok. It's a Set. From 0ffea100007b2a147fe4672ee8de8f2bfac38659 Mon Sep 17 00:00:00 2001 From: Colen Garoutte-Carson Date: Tue, 8 Apr 2025 17:37:13 -0700 Subject: [PATCH 3/7] Fix bug in prior PR --- Extension/src/LanguageServer/protocolFilter.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Extension/src/LanguageServer/protocolFilter.ts b/Extension/src/LanguageServer/protocolFilter.ts index 5853cf73c..a725f64d3 100644 --- a/Extension/src/LanguageServer/protocolFilter.ts +++ b/Extension/src/LanguageServer/protocolFilter.ts @@ -41,10 +41,8 @@ export function createProtocolFilter(): Middleware { // 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); } } }, From fee31b43cf60c2646f6b3458ef1692af124b427c Mon Sep 17 00:00:00 2001 From: Colen Garoutte-Carson Date: Tue, 8 Apr 2025 17:41:51 -0700 Subject: [PATCH 4/7] Fix a bug --- Extension/src/LanguageServer/protocolFilter.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Extension/src/LanguageServer/protocolFilter.ts b/Extension/src/LanguageServer/protocolFilter.ts index a725f64d3..e17d62ad5 100644 --- a/Extension/src/LanguageServer/protocolFilter.ts +++ b/Extension/src/LanguageServer/protocolFilter.ts @@ -34,9 +34,10 @@ export function createProtocolFilter(): Middleware { const mappingString: string = baseFileName + "@" + document.fileName; client.addFileAssociations(mappingString, "cpp"); client.sendDidChangeSettings(); - // Changes to the languageId in the editor take precendence over setTextDocumentLanguage. - // So, this may or may not cause the file to be closed and reopened. + // This will definitely cause the file to be closed and reopened. + // setTextDocumentLanguage takes precedence over setting the languageId in UI. 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); From d39939c78754f25f27edd6eb130e54a8b1f890fa Mon Sep 17 00:00:00 2001 From: Colen Garoutte-Carson Date: Tue, 8 Apr 2025 18:25:10 -0700 Subject: [PATCH 5/7] More fixes for '.C' --- .../src/LanguageServer/protocolFilter.ts | 19 +++++++++++-------- Extension/src/LanguageServer/settings.ts | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/Extension/src/LanguageServer/protocolFilter.ts b/Extension/src/LanguageServer/protocolFilter.ts index e17d62ad5..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,14 +31,16 @@ 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 definitely cause the file to be closed and reopened. - // setTextDocumentLanguage takes precedence over setting the languageId in UI. - 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); diff --git a/Extension/src/LanguageServer/settings.ts b/Extension/src/LanguageServer/settings.ts index fb8b3c458..c78b6f741 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 associations: Associations = new OtherSettings().filesAssociations; + if (associations[fileName]) { + return true; + } else { + for (const pattern in associations) { + if (pattern.startsWith('*.') && fileName.endsWith(pattern.slice(1))) { + return true; + } + } + } + return false; +} From bd1db71fa0041413204d24adaf949896835ab6f0 Mon Sep 17 00:00:00 2001 From: Colen Garoutte-Carson Date: Tue, 8 Apr 2025 18:41:06 -0700 Subject: [PATCH 6/7] clean up --- Extension/src/LanguageServer/settings.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Extension/src/LanguageServer/settings.ts b/Extension/src/LanguageServer/settings.ts index c78b6f741..b2c2d90c5 100644 --- a/Extension/src/LanguageServer/settings.ts +++ b/Extension/src/LanguageServer/settings.ts @@ -1101,14 +1101,14 @@ export class OtherSettings { } export function hasFileAssociation(fileName: string): boolean { - const associations: Associations = new OtherSettings().filesAssociations; + const otherSettings: OtherSettings = new OtherSettings() + const associations: Associations = otherSettings.filesAssociations; if (associations[fileName]) { return true; - } else { - for (const pattern in associations) { - if (pattern.startsWith('*.') && fileName.endsWith(pattern.slice(1))) { - return true; - } + } + for (const pattern in associations) { + if (pattern.startsWith('*.') && fileName.endsWith(pattern.slice(1))) { + return true; } } return false; From 08b64f8e043d5f77c7cc1316f0e3da1f179bdb62 Mon Sep 17 00:00:00 2001 From: Colen Garoutte-Carson Date: Tue, 8 Apr 2025 18:44:23 -0700 Subject: [PATCH 7/7] Fix lint --- Extension/src/LanguageServer/settings.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Extension/src/LanguageServer/settings.ts b/Extension/src/LanguageServer/settings.ts index b2c2d90c5..999e8114e 100644 --- a/Extension/src/LanguageServer/settings.ts +++ b/Extension/src/LanguageServer/settings.ts @@ -1101,7 +1101,7 @@ export class OtherSettings { } export function hasFileAssociation(fileName: string): boolean { - const otherSettings: OtherSettings = new OtherSettings() + const otherSettings: OtherSettings = new OtherSettings(); const associations: Associations = otherSettings.filesAssociations; if (associations[fileName]) { return true;