Skip to content

Commit 9646722

Browse files
authored
Address issue with Attempting to use languageClient before initialized (#12959)
1 parent 911c39d commit 9646722

File tree

2 files changed

+7
-13
lines changed

2 files changed

+7
-13
lines changed

Diff for: Extension/src/LanguageServer/client.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,12 @@ export class DefaultClient implements Client {
12851285

12861286
// Listen for messages from the language server.
12871287
this.registerNotifications();
1288+
1289+
// If a file is already open when we activate, sometimes we don't get any notifications about visible
1290+
// or active text editors, visible ranges, or text selection. As a workaround, we trigger
1291+
// onDidChangeVisibleTextEditors here.
1292+
const cppEditors: vscode.TextEditor[] = vscode.window.visibleTextEditors.filter(e => util.isCpp(e.document));
1293+
await this.onDidChangeVisibleTextEditors(cppEditors);
12881294
}
12891295

12901296
// update all client configurations

Diff for: Extension/src/LanguageServer/protocolFilter.ts

+1-13
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@ import * as path from 'path';
88
import * as vscode from 'vscode';
99
import { Middleware } from 'vscode-languageclient';
1010
import * as util from '../common';
11-
import { logAndReturn } from '../Utility/Async/returns';
1211
import { Client } from './client';
1312
import { clients } from './extension';
1413
import { shouldChangeFromCToCpp } from './utils';
1514

1615
export const RequestCancelled: number = -32800;
1716
export const ServerCancelled: number = -32802;
1817

19-
let anyFileOpened: boolean = false;
20-
2118
export function createProtocolFilter(): Middleware {
2219
return {
2320
didOpen: async (document, sendMessage) => {
@@ -43,16 +40,7 @@ export function createProtocolFilter(): Middleware {
4340
// client.takeOwnership() will call client.TrackedDocuments.add() again, but that's ok. It's a Set.
4441
client.onDidOpenTextDocument(document);
4542
client.takeOwnership(document);
46-
void sendMessage(document).then(() => {
47-
// For a file already open when we activate, sometimes we don't get any notifications about visible
48-
// or active text editors, visible ranges, or text selection. As a workaround, we trigger
49-
// onDidChangeVisibleTextEditors here, only for the first file opened.
50-
if (!anyFileOpened) {
51-
anyFileOpened = true;
52-
const cppEditors: vscode.TextEditor[] = vscode.window.visibleTextEditors.filter(e => util.isCpp(e.document));
53-
client.onDidChangeVisibleTextEditors(cppEditors).catch(logAndReturn.undefined);
54-
}
55-
});
43+
void sendMessage(document);
5644
}
5745
}
5846
},

0 commit comments

Comments
 (0)