Skip to content

Commit

Permalink
Address issue with `Attempting to use languageClient before initializ…
Browse files Browse the repository at this point in the history
…ed` (#12959)
  • Loading branch information
Colengms authored Nov 13, 2024
1 parent 911c39d commit 9646722
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
6 changes: 6 additions & 0 deletions Extension/src/LanguageServer/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 1 addition & 13 deletions Extension/src/LanguageServer/protocolFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ 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';

export const RequestCancelled: number = -32800;
export const ServerCancelled: number = -32802;

let anyFileOpened: boolean = false;

export function createProtocolFilter(): Middleware {
return {
didOpen: async (document, sendMessage) => {
Expand All @@ -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);
}
}
},
Expand Down

0 comments on commit 9646722

Please sign in to comment.