From 6f647763a7fea03d78f24609b1fc78ca91536318 Mon Sep 17 00:00:00 2001 From: Tarik Brown Date: Thu, 5 Sep 2024 12:44:28 -0700 Subject: [PATCH 1/2] Remove autoAddFileAssociations setting and persist the document language of a file if goto def or FAR is used. --- Extension/src/LanguageServer/client.ts | 54 +++++++++++++++++------- Extension/src/LanguageServer/settings.ts | 1 - Extension/src/LanguageServer/utils.ts | 6 +-- 3 files changed, 40 insertions(+), 21 deletions(-) diff --git a/Extension/src/LanguageServer/client.ts b/Extension/src/LanguageServer/client.ts index 05a500ee50..aecba3c97f 100644 --- a/Extension/src/LanguageServer/client.ts +++ b/Extension/src/LanguageServer/client.ts @@ -57,7 +57,7 @@ import { DataBinding } from './dataBinding'; import { cachedEditorConfigSettings, getEditorConfigSettings } from './editorConfig'; import { CppSourceStr, clients, configPrefix, updateLanguageConfigurations, usesCrashHandler, watchForCrashes } from './extension'; import { LocalizeStringParams, getLocaleId, getLocalizedString } from './localization'; -import { PersistentFolderState, PersistentWorkspaceState } from './persistentState'; +import { PersistentFolderState, PersistentState, PersistentWorkspaceState } from './persistentState'; import { createProtocolFilter } from './protocolFilter'; import * as refs from './references'; import { CppSettings, OtherSettings, SettingsParams, WorkspaceFolderSettingsParams } from './settings'; @@ -277,6 +277,11 @@ interface IntelliSenseDiagnostic { relatedInformation?: IntelliSenseDiagnosticRelatedInformation[]; } +interface textDocumentLanguageInformation{ + uri: string; + languageId: string; +} + interface RefactorDiagnostic { range: Range; code?: number; @@ -472,6 +477,7 @@ interface SetTemporaryTextDocumentLanguageParams { uri: string; isC: boolean; isCuda: boolean; + isPersistent: boolean; } enum CodeAnalysisScope { @@ -610,7 +616,6 @@ const RequestCustomConfig: NotificationType = new NotificationType = new NotificationType('cpptools/publishRefactorDiagnostics'); const ShowMessageWindowNotification: NotificationType = new NotificationType('cpptools/showMessageWindow'); const ShowWarningNotification: NotificationType = new NotificationType('cpptools/showWarning'); -const ReportTextDocumentLanguage: NotificationType = new NotificationType('cpptools/reportTextDocumentLanguage'); const IntelliSenseSetupNotification: NotificationType = new NotificationType('cpptools/IntelliSenseSetup'); const SetTemporaryTextDocumentLanguageNotification: NotificationType = new NotificationType('cpptools/setTemporaryTextDocumentLanguage'); const ReportCodeAnalysisProcessedNotification: NotificationType = new NotificationType('cpptools/reportCodeAnalysisProcessed'); @@ -1758,9 +1763,20 @@ export class DefaultClient implements Client { } } - public onDidOpenTextDocument(document: vscode.TextDocument): void { + public async onDidOpenTextDocument(document: vscode.TextDocument): Promise { + if (document.uri.scheme === "file") { + console.log(document.languageId); const uri: string = document.uri.toString(); + const textDocumentLanguagePersistentState: PersistentState = new PersistentState("CPP.textDocumentLanguage", undefined); + const persistentLanguage: string | undefined = textDocumentLanguagePersistentState.Value?.languageId; + const persistentFile: string | undefined = textDocumentLanguagePersistentState.Value?.uri; + + if (persistentFile == uri && persistentLanguage) { + await vscode.languages.setTextDocumentLanguage(document, persistentLanguage); + + } + openFileVersions.set(uri, document.version); void SessionState.buildAndDebugIsSourceFile.set(util.isCppOrCFile(document.uri)); void SessionState.buildAndDebugIsFolderOpen.set(util.isFolderOpen(document.uri)); @@ -1771,6 +1787,14 @@ export class DefaultClient implements Client { public onDidCloseTextDocument(document: vscode.TextDocument): void { const uri: string = document.uri.toString(); + const textDocumentLanguagePersistentState: PersistentState = new PersistentState("CPP.textDocumentLanguage", undefined); + const persistentFile: string | undefined = textDocumentLanguagePersistentState.Value?.uri; + const persistentLanguage: string | undefined = textDocumentLanguagePersistentState.Value?.languageId; + console.log(document.languageId); + // If the file being closed has changed its language from the one we have stored, clear the stored language. + if (persistentFile == uri && persistentLanguage !== document.languageId) { + textDocumentLanguagePersistentState.Value = undefined; + } if (this.semanticTokensProvider) { this.semanticTokensProvider.removeFile(uri); } @@ -2365,7 +2389,6 @@ export class DefaultClient implements Client { RegisterCodeAnalysisNotifications(this.languageClient); this.languageClient.onNotification(ShowMessageWindowNotification, showMessageWindow); this.languageClient.onNotification(ShowWarningNotification, showWarning); - this.languageClient.onNotification(ReportTextDocumentLanguage, (e) => this.setTextDocumentLanguage(e)); this.languageClient.onNotification(IntelliSenseSetupNotification, (e) => this.logIntelliSenseSetupTime(e)); this.languageClient.onNotification(SetTemporaryTextDocumentLanguageNotification, (e) => void this.setTemporaryTextDocumentLanguage(e)); this.languageClient.onNotification(ReportCodeAnalysisProcessedNotification, (e) => this.updateCodeAnalysisProcessed(e)); @@ -2426,23 +2449,24 @@ export class DefaultClient implements Client { diagnosticsCollectionIntelliSense.set(realUri, diagnosticsIntelliSense); clients.timeTelemetryCollector.setUpdateRangeTime(realUri); - } - - private setTextDocumentLanguage(languageStr: string): void { - const cppSettings: CppSettings = new CppSettings(); - if (cppSettings.autoAddFileAssociations) { - const is_c: boolean = languageStr.startsWith("c;"); - const is_cuda: boolean = languageStr.startsWith("cu;"); - languageStr = languageStr.substring(is_c ? 2 : is_cuda ? 3 : 1); - this.addFileAssociations(languageStr, is_c ? "c" : is_cuda ? "cuda-cpp" : "cpp"); - } - } + } private async setTemporaryTextDocumentLanguage(params: SetTemporaryTextDocumentLanguageParams): Promise { const languageId: string = params.isC ? "c" : params.isCuda ? "cuda-cpp" : "cpp"; const uri: vscode.Uri = vscode.Uri.parse(params.uri); + const client: Client = clients.getClientFor(uri); const document: vscode.TextDocument | undefined = client.TrackedDocuments.get(params.uri); + if (params.isPersistent){ + let textDocumentLanguagePersistentState: PersistentState = new PersistentState("CPP.textDocumentLanguage", undefined); + textDocumentLanguagePersistentState.Value = undefined; + const doc: vscode.TextDocument | undefined = await vscode.workspace.openTextDocument(params.uri); + await vscode.languages.setTextDocumentLanguage(doc, languageId); + console.log(textDocumentLanguagePersistentState.Value); + if (!textDocumentLanguagePersistentState.Value){ + textDocumentLanguagePersistentState.Value = {uri: doc.uri.toString(), languageId: languageId}; + } + } if (!!document && document.languageId !== languageId) { if (document.languageId === "cpp" && languageId === "c") { handleChangedFromCppToC(document); diff --git a/Extension/src/LanguageServer/settings.ts b/Extension/src/LanguageServer/settings.ts index 5171f27793..c2723b3fe6 100644 --- a/Extension/src/LanguageServer/settings.ts +++ b/Extension/src/LanguageServer/settings.ts @@ -377,7 +377,6 @@ export class CppSettings extends Settings { public get autocomplete(): string { return this.getAsString("autocomplete"); } public get autocompleteAddParentheses(): boolean { return this.getAsBoolean("autocompleteAddParentheses"); } public get loggingLevel(): string { return this.getAsString("loggingLevel"); } - public get autoAddFileAssociations(): boolean { return this.getAsBoolean("autoAddFileAssociations"); } public get workspaceParsingPriority(): string { return this.getAsString("workspaceParsingPriority"); } public get workspaceSymbols(): string { return this.getAsString("workspaceSymbols"); } public get exclusionPolicy(): string { return this.getAsString("exclusionPolicy"); } diff --git a/Extension/src/LanguageServer/utils.ts b/Extension/src/LanguageServer/utils.ts index da3d29b693..7dc018c99b 100644 --- a/Extension/src/LanguageServer/utils.ts +++ b/Extension/src/LanguageServer/utils.ts @@ -8,7 +8,6 @@ import * as vscode from 'vscode'; import { Range } from 'vscode-languageclient'; import { SessionState } from '../sessionState'; import { Location, TextEdit } from './commonTypes'; -import { CppSettings } from './settings'; export function makeLspRange(vscRange: vscode.Range): Range { return { @@ -37,10 +36,7 @@ export function rangeEquals(range1: vscode.Range | Range, range2: vscode.Range | // Check this before attempting to switch a document from C to C++. export function shouldChangeFromCToCpp(document: vscode.TextDocument): boolean { if (document.fileName.endsWith(".C") || document.fileName.endsWith(".H")) { - const cppSettings: CppSettings = new CppSettings(); - if (cppSettings.autoAddFileAssociations) { - return !docsChangedFromCppToC.has(document.fileName); - } + return !docsChangedFromCppToC.has(document.fileName); // We could potentially add a new setting to enable switching to cpp even when files.associations isn't changed. } return false; From 376b3b90f8c371b468c5ad21b56c427c7848eb77 Mon Sep 17 00:00:00 2001 From: Tarik Brown Date: Thu, 5 Sep 2024 12:51:17 -0700 Subject: [PATCH 2/2] Fix lint errors. --- Extension/src/LanguageServer/client.ts | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/Extension/src/LanguageServer/client.ts b/Extension/src/LanguageServer/client.ts index aecba3c97f..9aec0e9d7b 100644 --- a/Extension/src/LanguageServer/client.ts +++ b/Extension/src/LanguageServer/client.ts @@ -277,7 +277,7 @@ interface IntelliSenseDiagnostic { relatedInformation?: IntelliSenseDiagnosticRelatedInformation[]; } -interface textDocumentLanguageInformation{ +interface TextDocumentLanguageInformation { uri: string; languageId: string; } @@ -1764,17 +1764,16 @@ export class DefaultClient implements Client { } public async onDidOpenTextDocument(document: vscode.TextDocument): Promise { - + if (document.uri.scheme === "file") { console.log(document.languageId); const uri: string = document.uri.toString(); - const textDocumentLanguagePersistentState: PersistentState = new PersistentState("CPP.textDocumentLanguage", undefined); + const textDocumentLanguagePersistentState: PersistentState = new PersistentState("CPP.textDocumentLanguage", undefined); const persistentLanguage: string | undefined = textDocumentLanguagePersistentState.Value?.languageId; - const persistentFile: string | undefined = textDocumentLanguagePersistentState.Value?.uri; + const persistentFile: string | undefined = textDocumentLanguagePersistentState.Value?.uri; - if (persistentFile == uri && persistentLanguage) { + if (persistentFile === uri && persistentLanguage) { await vscode.languages.setTextDocumentLanguage(document, persistentLanguage); - } openFileVersions.set(uri, document.version); @@ -1787,12 +1786,12 @@ export class DefaultClient implements Client { public onDidCloseTextDocument(document: vscode.TextDocument): void { const uri: string = document.uri.toString(); - const textDocumentLanguagePersistentState: PersistentState = new PersistentState("CPP.textDocumentLanguage", undefined); - const persistentFile: string | undefined = textDocumentLanguagePersistentState.Value?.uri; + const textDocumentLanguagePersistentState: PersistentState = new PersistentState("CPP.textDocumentLanguage", undefined); + const persistentFile: string | undefined = textDocumentLanguagePersistentState.Value?.uri; const persistentLanguage: string | undefined = textDocumentLanguagePersistentState.Value?.languageId; console.log(document.languageId); // If the file being closed has changed its language from the one we have stored, clear the stored language. - if (persistentFile == uri && persistentLanguage !== document.languageId) { + if (persistentFile === uri && persistentLanguage !== document.languageId) { textDocumentLanguagePersistentState.Value = undefined; } if (this.semanticTokensProvider) { @@ -2449,7 +2448,7 @@ export class DefaultClient implements Client { diagnosticsCollectionIntelliSense.set(realUri, diagnosticsIntelliSense); clients.timeTelemetryCollector.setUpdateRangeTime(realUri); - } + } private async setTemporaryTextDocumentLanguage(params: SetTemporaryTextDocumentLanguageParams): Promise { const languageId: string = params.isC ? "c" : params.isCuda ? "cuda-cpp" : "cpp"; @@ -2457,13 +2456,13 @@ export class DefaultClient implements Client { const client: Client = clients.getClientFor(uri); const document: vscode.TextDocument | undefined = client.TrackedDocuments.get(params.uri); - if (params.isPersistent){ - let textDocumentLanguagePersistentState: PersistentState = new PersistentState("CPP.textDocumentLanguage", undefined); + if (params.isPersistent) { + const textDocumentLanguagePersistentState: PersistentState = new PersistentState("CPP.textDocumentLanguage", undefined); textDocumentLanguagePersistentState.Value = undefined; const doc: vscode.TextDocument | undefined = await vscode.workspace.openTextDocument(params.uri); await vscode.languages.setTextDocumentLanguage(doc, languageId); console.log(textDocumentLanguagePersistentState.Value); - if (!textDocumentLanguagePersistentState.Value){ + if (!textDocumentLanguagePersistentState.Value) { textDocumentLanguagePersistentState.Value = {uri: doc.uri.toString(), languageId: languageId}; } }