Skip to content

Commit

Permalink
Add command to show LSP's internal state for diagnosing issues (#3195)
Browse files Browse the repository at this point in the history
### 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
  • Loading branch information
vinistock committed Feb 14, 2025
1 parent 45ae6f6 commit 2860c05
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
5 changes: 5 additions & 0 deletions vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions vscode/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions vscode/src/documentProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
46 changes: 46 additions & 0 deletions vscode/src/rubyLsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 2860c05

Please sign in to comment.