Skip to content

Commit d39939c

Browse files
committed
More fixes for '.C'
1 parent fee31b4 commit d39939c

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

Extension/src/LanguageServer/protocolFilter.ts

Lines changed: 11 additions & 8 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,14 +31,16 @@ 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 definitely cause the file to be closed and reopened.
38-
// setTextDocumentLanguage takes precedence over setting the languageId in UI.
39-
void vscode.languages.setTextDocumentLanguage(document, "cpp");
40-
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+
}
4144
}
4245
// client.takeOwnership() will call client.TrackedDocuments.add() again, but that's ok. It's a Set.
4346
client.takeOwnership(document);

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 associations: Associations = new OtherSettings().filesAssociations;
1105+
if (associations[fileName]) {
1106+
return true;
1107+
} else {
1108+
for (const pattern in associations) {
1109+
if (pattern.startsWith('*.') && fileName.endsWith(pattern.slice(1))) {
1110+
return true;
1111+
}
1112+
}
1113+
}
1114+
return false;
1115+
}

0 commit comments

Comments
 (0)