From 9646722501d18fb0f64dcac608576593afa80a59 Mon Sep 17 00:00:00 2001 From: Colen Garoutte-Carson <49173979+Colengms@users.noreply.github.com> Date: Tue, 12 Nov 2024 16:25:04 -0800 Subject: [PATCH] Address issue with `Attempting to use languageClient before initialized` (#12959) --- Extension/src/LanguageServer/client.ts | 6 ++++++ Extension/src/LanguageServer/protocolFilter.ts | 14 +------------- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/Extension/src/LanguageServer/client.ts b/Extension/src/LanguageServer/client.ts index e46743815e..fdc7d8ec0e 100644 --- a/Extension/src/LanguageServer/client.ts +++ b/Extension/src/LanguageServer/client.ts @@ -1285,6 +1285,12 @@ export class DefaultClient implements Client { // Listen for messages from the language server. this.registerNotifications(); + + // If a file is already open when we activate, sometimes we don't get any notifications about visible + // or active text editors, visible ranges, or text selection. As a workaround, we trigger + // onDidChangeVisibleTextEditors here. + const cppEditors: vscode.TextEditor[] = vscode.window.visibleTextEditors.filter(e => util.isCpp(e.document)); + await this.onDidChangeVisibleTextEditors(cppEditors); } // update all client configurations diff --git a/Extension/src/LanguageServer/protocolFilter.ts b/Extension/src/LanguageServer/protocolFilter.ts index b292c67b16..9829f0a138 100644 --- a/Extension/src/LanguageServer/protocolFilter.ts +++ b/Extension/src/LanguageServer/protocolFilter.ts @@ -8,7 +8,6 @@ import * as path from 'path'; import * as vscode from 'vscode'; import { Middleware } from 'vscode-languageclient'; import * as util from '../common'; -import { logAndReturn } from '../Utility/Async/returns'; import { Client } from './client'; import { clients } from './extension'; import { shouldChangeFromCToCpp } from './utils'; @@ -16,8 +15,6 @@ import { shouldChangeFromCToCpp } from './utils'; export const RequestCancelled: number = -32800; export const ServerCancelled: number = -32802; -let anyFileOpened: boolean = false; - export function createProtocolFilter(): Middleware { return { didOpen: async (document, sendMessage) => { @@ -43,16 +40,7 @@ export function createProtocolFilter(): Middleware { // client.takeOwnership() will call client.TrackedDocuments.add() again, but that's ok. It's a Set. client.onDidOpenTextDocument(document); client.takeOwnership(document); - void sendMessage(document).then(() => { - // For a file already open when we activate, sometimes we don't get any notifications about visible - // or active text editors, visible ranges, or text selection. As a workaround, we trigger - // onDidChangeVisibleTextEditors here, only for the first file opened. - if (!anyFileOpened) { - anyFileOpened = true; - const cppEditors: vscode.TextEditor[] = vscode.window.visibleTextEditors.filter(e => util.isCpp(e.document)); - client.onDidChangeVisibleTextEditors(cppEditors).catch(logAndReturn.undefined); - } - }); + void sendMessage(document); } } },