Skip to content

Fix issues with remapping of .C files #13476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions Extension/src/LanguageServer/protocolFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
},
Expand Down
14 changes: 14 additions & 0 deletions Extension/src/LanguageServer/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}