Skip to content

Commit d429beb

Browse files
committed
Refactor LanguageClientConsumer to wait properly
This makes a lot more sense and fixes a long-standing bug. Also cleaned up a lot while at it. 1. `SessionManager` is created and the features are injected into the session manager with the intention of them being able to "register" their language client event handlers in the period between when the client is created and when it is started. 2. For each feature, if it gets to a point where it needs the language client, we want them to wait on a promise for the "started" client to be provided from the session manager before continuing. 3. Once it is started, the session manager passes the client to `LanguageClientConsumer` which resolves the promise for the features so they can continue.
1 parent 2b00cc4 commit d429beb

16 files changed

+318
-424
lines changed

src/features/CodeActions.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@ import vscode = require("vscode");
55
import { ILogger } from "../logging";
66

77
export class CodeActionsFeature implements vscode.Disposable {
8-
private showDocumentationCommand: vscode.Disposable;
8+
private command: vscode.Disposable;
99

1010
constructor(private log: ILogger) {
1111
// NOTE: While not exposed to the user via package.json, this is
1212
// required as the server's code action sends across a command name.
1313
//
1414
// TODO: In the far future with LSP 3.19 the server can just set a URL
1515
// and this can go away. See https://github.com/microsoft/language-server-protocol/issues/1548
16-
this.showDocumentationCommand =
16+
this.command =
1717
vscode.commands.registerCommand("PowerShell.ShowCodeActionDocumentation", async (ruleName: string) => {
1818
await this.showRuleDocumentation(ruleName);
1919
});
2020
}
2121

2222
public dispose(): void {
23-
this.showDocumentationCommand.dispose();
23+
this.command.dispose();
2424
}
2525

26-
public async showRuleDocumentation(ruleId: string): Promise<void> {
26+
private async showRuleDocumentation(ruleId: string): Promise<void> {
2727
const pssaDocBaseURL = "https://docs.microsoft.com/powershell/utility-modules/psscriptanalyzer/rules/";
2828

2929
if (!ruleId) {

src/features/Console.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ export class ConsoleFeature extends LanguageClientConsumer {
200200
} else {
201201
selectionRange = editor.document.lineAt(editor.selection.start.line).range;
202202
}
203-
204-
await this.languageClient?.sendRequest(EvaluateRequestType, {
203+
const client = await LanguageClientConsumer.getLanguageClient();
204+
await client.sendRequest(EvaluateRequestType, {
205205
expression: editor.document.getText(selectionRange),
206206
});
207207

@@ -217,19 +217,19 @@ export class ConsoleFeature extends LanguageClientConsumer {
217217
for (const command of this.commands) {
218218
command.dispose();
219219
}
220+
220221
for (const handler of this.handlers) {
221222
handler.dispose();
222223
}
223224
}
224225

225-
public override setLanguageClient(languageClient: LanguageClient): void {
226-
this.languageClient = languageClient;
226+
public override onLanguageClientSet(languageClient: LanguageClient): void {
227227
this.handlers = [
228-
this.languageClient.onRequest(
228+
languageClient.onRequest(
229229
ShowChoicePromptRequestType,
230230
(promptDetails) => showChoicePrompt(promptDetails)),
231231

232-
this.languageClient.onRequest(
232+
languageClient.onRequest(
233233
ShowInputPromptRequestType,
234234
(promptDetails) => showInputPrompt(promptDetails)),
235235
];

0 commit comments

Comments
 (0)