Skip to content

Show document with server debug info #694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 18, 2025

Conversation

dhruvmanila
Copy link
Member

@dhruvmanila dhruvmanila commented Feb 18, 2025

Summary

This PR is inspired by rust-analyzer/status request to show the debug information in a separate window.

Reference: https://github.com/rust-lang/rust-analyzer/blob/e865b249e63a9c73e4741b32fd1c977a98d3c556/editors/code/src/commands.ts#L37-L71

It has been adopted to fit out needs:

  • The language client could be undefined
  • When not using the native server
  • Avoid showing the document if it's empty
  • Return an empty string for older Ruff version which doesn't return the information

Test Plan

Screen.Recording.2025-02-18.at.4.03.37.PM.mov

dhruvmanila added a commit to astral-sh/ruff that referenced this pull request Feb 18, 2025
## Summary

This PR updates the `ruff.printDebugInformation` command to return the
info as string in the response. Currently, we send a `window/logMessage`
request with the info but that has the disadvantage that it's not
visible to the user directly.

What `rust-analyzer` does with it's `rust-analyzer/status` request which
returns it as a string which then the client can just display it in a
separate window. This is what I'm thinking of doing as well.

Other editors can also benefit from it by directly opening a temporary
file with this information that the user can see directly.

There are couple of options here:
1. Keep using the command, keep the log request and return the string
2. Keep using the command, remove the log request and return the string
3. Create a new request similar to `rust-analyzer/status` which returns
a string

This PR implements (1) but I'd want to move towards (2) and remove the
log request completely. We haven't advertised it as such so this would
only require updating the VS Code extension to handle it by opening a
new document with the debug content.

## Test plan

For VS Code, refer to astral-sh/ruff-vscode#694.

For Neovim, one could do:
```lua
local function execute_ruff_command(command)
  local client = vim.lsp.get_clients({ 
    bufnr = vim.api.nvim_get_current_buf(), 
    name = name,
    method = 'workspace/executeCommand',
  })[1]
  if not client then
    return
  end
  client.request('workspace/executeCommand', {
    command = command,
    arguments = {
      { uri = vim.uri_from_bufnr(0) }
    },
    function(err, result)
      if err then
        -- log error
        return
      end
      vim.print(result)
      -- Or, open a new window with the `result` content
    end
  }
```
@dhruvmanila dhruvmanila force-pushed the dhruv/show-doc-with-debug-info branch from 2a347c8 to 0627873 Compare February 18, 2025 10:08
Base automatically changed from dhruv/print-debug-info-param to main February 18, 2025 10:08
@dhruvmanila dhruvmanila force-pushed the dhruv/show-doc-with-debug-info branch from 0627873 to 3d3fae7 Compare February 18, 2025 10:11
@dhruvmanila dhruvmanila force-pushed the dhruv/show-doc-with-debug-info branch from 3d3fae7 to 7022bbc Compare February 18, 2025 10:32
Comment on lines +70 to +83
const textEditor = vscode.window.activeTextEditor;
const notebookEditor = vscode.window.activeNotebookEditor;
const params = {
command: `${serverId}.printDebugInformation`,
arguments: [
{
textDocument: notebookEditor
? { uri: notebookEditor.notebook.uri.toString() }
: textEditor
? { uri: textEditor.document.uri.toString() }
: undefined,
},
],
};
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated this in this PR from main to check if we're in a notebook context and then use text document. I don't think there's any specific information for a notebook cell so using a notebook for them should be fine.

@dhruvmanila dhruvmanila marked this pull request as ready for review February 18, 2025 10:38
Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Comment on lines +200 to +203
registerCommand(
`${serverId}.debugInformation`,
createDebugInformationProvider(getClient, serverId, context),
),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to avoid creating the command if it is the native server?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. Looking at https://code.visualstudio.com/api/references/contribution-points#contributes.commands, we need to add it to the UI via package.json which makes this not possible (

ruff-vscode/package.json

Lines 434 to 438 in b4e0698

{
"title": "Print debug information (native server only)",
"category": "Ruff",
"command": "ruff.debugInformation"
},
).

@dhruvmanila dhruvmanila merged commit 01f76c1 into main Feb 18, 2025
6 checks passed
@dhruvmanila dhruvmanila deleted the dhruv/show-doc-with-debug-info branch February 18, 2025 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants