Skip to content

Commit a71cbad

Browse files
authored
Fix issues with remapping of .C files (#13476)
1 parent 17f149d commit a71cbad

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

Extension/src/LanguageServer/protocolFilter.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as util from '../common';
1111
import { logAndReturn } from '../Utility/Async/returns';
1212
import { Client } from './client';
1313
import { clients } from './extension';
14+
import { hasFileAssociation } from './settings';
1415
import { shouldChangeFromCToCpp } from './utils';
1516

1617
export const RequestCancelled: number = -32800;
@@ -30,21 +31,22 @@ export function createProtocolFilter(): Middleware {
3031
client.TrackedDocuments.set(uriString, document);
3132
// Work around vscode treating ".C" or ".H" as c, by adding this file name to file associations as cpp
3233
if (document.languageId === "c" && shouldChangeFromCToCpp(document)) {
33-
const baseFileName: string = path.basename(document.fileName);
34-
const mappingString: string = baseFileName + "@" + document.fileName;
35-
client.addFileAssociations(mappingString, "cpp");
36-
client.sendDidChangeSettings();
37-
// This will cause the file to be closed and reopened.
38-
void vscode.languages.setTextDocumentLanguage(document, "cpp");
39-
return;
34+
// Don't override the user's setting.
35+
if (!hasFileAssociation(path.basename(document.uri.fsPath))) {
36+
const baseFileName: string = path.basename(document.fileName);
37+
const mappingString: string = baseFileName + "@" + document.fileName;
38+
client.addFileAssociations(mappingString, "cpp");
39+
client.sendDidChangeSettings();
40+
// The following will cause the file to be closed and reopened.
41+
void vscode.languages.setTextDocumentLanguage(document, "cpp");
42+
return;
43+
}
4044
}
4145
// client.takeOwnership() will call client.TrackedDocuments.add() again, but that's ok. It's a Set.
4246
client.takeOwnership(document);
4347
void sendMessage(document);
44-
const editor: vscode.TextEditor | undefined = vscode.window.visibleTextEditors.find(editor => editor.document === document);
45-
if (editor) {
46-
client.onDidChangeVisibleTextEditors([editor]).catch(logAndReturn.undefined);
47-
}
48+
const cppEditors: vscode.TextEditor[] = vscode.window.visibleTextEditors.filter(e => util.isCpp(e.document));
49+
client.onDidChangeVisibleTextEditors(cppEditors).catch(logAndReturn.undefined);
4850
}
4951
}
5052
},

Extension/src/LanguageServer/settings.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,3 +1099,17 @@ export class OtherSettings {
10991099
public get searchExclude(): Excludes { return this.getAsExcludes("search", "exclude", this.defaultSearchExcludes, this.resource); }
11001100
public get workbenchSettingsEditor(): string { return this.getAsString("workbench.settings", "editor", this.resource, "ui"); }
11011101
}
1102+
1103+
export function hasFileAssociation(fileName: string): boolean {
1104+
const otherSettings: OtherSettings = new OtherSettings();
1105+
const associations: Associations = otherSettings.filesAssociations;
1106+
if (associations[fileName]) {
1107+
return true;
1108+
}
1109+
for (const pattern in associations) {
1110+
if (pattern.startsWith('*.') && fileName.endsWith(pattern.slice(1))) {
1111+
return true;
1112+
}
1113+
}
1114+
return false;
1115+
}

0 commit comments

Comments
 (0)