zed icon indicating copy to clipboard operation
zed copied to clipboard

"Sort JSON" code action does nothing

Open bitfield opened this issue 1 year ago • 1 comments

Check for existing issues

  • [X] Completed

Describe the bug / provide steps to reproduce it

When I'm editing any JSON file, the lightning bolt icon appears in the left margin to indicate a code action. If I click on it, it offers 'Sort JSON' as the only action. Selecting this has no effect; the JSON is unchanged.

I would expect this to either sort the JSON, or not suggest the "Sort JSON" code action if it's not available.

Environment

Zed: v0.149.3 (Zed) OS: macOS 10.15.7 Memory: 16 GiB Architecture: x86_64

If applicable, add mockups / screenshots to help explain present your vision of the feature

Screenshot 2024-08-23 at 13 21 22

If applicable, attach your Zed.log file to this issue.

Zed.log

2024-08-23T13:17:17.688422+01:00 [INFO] downloading language server "json-language-server"
2024-08-23T13:17:17.862503+01:00 [INFO] Node runtime install_if_needed
2024-08-23T13:17:18.218317+01:00 [INFO] starting language server. binary path: "/Users/john/Library/Application Support/Zed/node/node-v22.5.1-darwin-x64/bin/node", working directory: "/Users/john/Documents", args: ["/Users/john/Library/Application Support/Zed/languages/json-language-server/node_modules/vscode-langservers-extracted/bin/vscode-json-language-server", "--stdio"]
2024-08-23T13:17:26.43264+01:00 [ERROR] Unhandled method workspace/executeCommand
2024-08-23T13:17:40.772022+01:00 [ERROR] Unhandled method workspace/executeCommand
2024-08-23T13:18:49.934046+01:00 [ERROR] Unhandled method workspace/executeCommand
2024-08-23T13:19:12.995784+01:00 [ERROR] Unhandled method workspace/executeCommand
2024-08-23T13:19:17.514419+01:00 [ERROR] Unhandled method workspace/executeCommand
2024-08-23T13:22:26.242182+01:00 [ERROR] Unhandled method workspace/executeCommand

bitfield avatar Aug 23 '24 12:08 bitfield

Can reproduce this issue which triggers this log event on Sort:

2024-08-23T13:18:08.492764Z [ERROR] Unhandled method workspace/executeCommand

notpeter avatar Aug 23 '24 13:08 notpeter

looks like sorting isn't implemented server-side for the json language server: https://github.com/microsoft/vscode/blob/main/extensions/json-language-features/client/src/jsonClient.ts#L215-L232

new to the project and not sure what the preferred path here would be but @notpeter if you want to give me a nudge in the right direction i can take a stab at this one. if im understanding right seems like options would be

  • some flag to disable this code action in the language server
  • add a server-side sorting implementation

thejchap avatar Feb 26 '25 04:02 thejchap

@notpeter following up on this one, lmk if i can help here

thejchap avatar Mar 06 '25 00:03 thejchap

You are right, looking at rpc logs under debug: language server logs it shows this code action available:

{"jsonrpc":"2.0","id":7,"result":[{"title":"Sort JSON","kind":"source.sort.json","command":{"command":"json.sort","title":"Sort JSON"}}]}

But when Zed sends it json-language-server returns an error:

// Send:
{"jsonrpc":"2.0","id":8,"method":"workspace/executeCommand","params":{"command":"json.sort","arguments":[]}}
// Receive:
{"jsonrpc":"2.0","id":8,"error":{"code":-32601,"message":"Unhandled method workspace/executeCommand"}}

The whole thing is ruse.

I would love to support this functionality, and creating an action which would parse and sort the buffer in Rust would be relatively trivial (e.g. see the implementation of Editor::SortLinesCaseSensitive): https://github.com/zed-industries/zed/blob/5a19810cc77366fd47f8c9f6b8c731b2b37da302/crates/editor/src/editor.rs#L7752-L7759

Conceivably we could:

  1. implement a command registry (e.g. including json.sort)
  2. instead of sending workspace/executeCommand to the language server, zed checks the command registry for a match
  3. if found, it dispatches to the corresponding function

I don't know if there are other workspace/executeCommand commands we should support with other language servers, but this seems like one that would certainly see a performance benefit from using native code, so I kind of understand why json-language-server relies on vscode to sort rather than implementing it in js/ts.

notpeter avatar Mar 06 '25 05:03 notpeter

@notpeter makes sense to me/seems similar to how these commands are implemented in vscode

would it make sense to add a function to the LspAdapter trait for the registry - something like

fn commands(&self) -> HashMap<String, Fn>;

that is a mapping of command -> function and defaults to empty, where JsonLspAdapter could register json.sort?

thejchap avatar Mar 06 '25 06:03 thejchap