From 2860c054ac794b3928d94471f744168e8ed78bde Mon Sep 17 00:00:00 2001 From: vinistock <18742907+vinistock@users.noreply.github.com> Date: Fri, 14 Feb 2025 14:09:56 +0000 Subject: [PATCH] Add command to show LSP's internal state for diagnosing issues (#3195) ### Motivation Add a command to trigger the new custom diagnose request, so that it's easy to see the information at a glance. ### Implementation It's the same implementation as show syntax tree, we use a custom resource to show a read-only document containing the relevant information. ### Manual Tests 1. Launch the extension on this branch 2. On the second VS Code window, open the Ruby LSP code itself (you need the server changes to verify all pieces together) 3. Invoke the new command 4. Verify you see the internal state of the server --- vscode/package.json | 5 ++++ vscode/src/common.ts | 1 + vscode/src/documentProvider.ts | 3 +++ vscode/src/rubyLsp.ts | 46 ++++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+) diff --git a/vscode/package.json b/vscode/package.json index bdac55e77..53b7fed20 100644 --- a/vscode/package.json +++ b/vscode/package.json @@ -138,6 +138,11 @@ "title": "Show syntax tree", "category": "Ruby LSP" }, + { + "command": "rubyLsp.diagnoseState", + "title": "Diagnose language server state", + "category": "Ruby LSP" + }, { "command": "rubyLsp.railsGenerate", "title": "Rails generate", diff --git a/vscode/src/common.ts b/vscode/src/common.ts index 32ef885e2..805db65f1 100644 --- a/vscode/src/common.ts +++ b/vscode/src/common.ts @@ -20,6 +20,7 @@ export enum Command { RunTestInTerminal = "rubyLsp.runTestInTerminal", DebugTest = "rubyLsp.debugTest", ShowSyntaxTree = "rubyLsp.showSyntaxTree", + DiagnoseState = "rubyLsp.diagnoseState", DisplayAddons = "rubyLsp.displayAddons", RunTask = "rubyLsp.runTask", BundleInstall = "rubyLsp.bundleInstall", diff --git a/vscode/src/documentProvider.ts b/vscode/src/documentProvider.ts index 4d413a43a..ca9df743b 100644 --- a/vscode/src/documentProvider.ts +++ b/vscode/src/documentProvider.ts @@ -12,6 +12,9 @@ export default class DocumentProvider case "show-syntax-tree": response = uri.query; break; + case "show-diagnose-state": + response = uri.query; + break; } return response; diff --git a/vscode/src/rubyLsp.ts b/vscode/src/rubyLsp.ts index 88266bc2c..c4a373aa7 100644 --- a/vscode/src/rubyLsp.ts +++ b/vscode/src/rubyLsp.ts @@ -264,6 +264,10 @@ export class RubyLsp { Command.ShowSyntaxTree, this.showSyntaxTree.bind(this), ), + vscode.commands.registerCommand( + Command.DiagnoseState, + this.diagnoseState.bind(this), + ), vscode.commands.registerCommand(Command.ShowServerChangelog, () => { const version = this.currentActiveWorkspace()?.lspClient?.serverVersion; @@ -779,6 +783,48 @@ export class RubyLsp { return this.getWorkspace(workspaceFolder.uri); } + private async diagnoseState() { + const workspace = await this.showWorkspacePick(); + + const response: + | { + workerAlive: boolean; + backtrace: string[]; + documents: { uri: string; source: string }; + incomingQueueSize: number; + } + | null + | undefined = await workspace?.lspClient?.sendRequest( + "rubyLsp/diagnoseState", + ); + + if (response) { + const documentData = Object.entries(response.documents); + const information = [ + `Worker alive: ${response.workerAlive}`, + `Incoming queue size: ${response.incomingQueueSize}`, + `Backtrace:\n${response.backtrace.join("\n")}\n`, + `=========== Documents (${documentData.length}) ===========`, + ...documentData.map( + ([uri, source]) => `URI: ${uri}\n\n${source}\n===========`, + ), + ].join("\n"); + + const document = await vscode.workspace.openTextDocument( + vscode.Uri.from({ + scheme: "ruby-lsp", + path: "show-diagnose-state", + query: information, + }), + ); + + await vscode.window.showTextDocument(document, { + viewColumn: vscode.ViewColumn.Beside, + preserveFocus: true, + }); + } + } + // Show syntax tree command private async showSyntaxTree() { const activeEditor = vscode.window.activeTextEditor;