Skip to content
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

Support local source changes for rust-analyzer #1124

Closed
JeanMertz opened this issue Aug 28, 2019 · 9 comments
Closed

Support local source changes for rust-analyzer #1124

JeanMertz opened this issue Aug 28, 2019 · 9 comments

Comments

@JeanMertz
Copy link

I'm looking into making the rust-analyzer LSP work as much as possible with Coc.

Everything mostly works out of the box, except that the server does not support workspace/executeCommand, and so code actions don't work.

Instead, the response of a textDocument/codeAction request returns a set of actions, each having their command set to rust-analyzer.applySourceChange, and arguments containing the data needed to manipulate the code locally to apply the source change.

I've implemented something similar before for LanguageClient-neovim.

Given that this doesn't fit into an "extension" (as far as I know), I suspect support for something like this will have to be hard-coded into coc.nvim itself?

Are you open to such a change, or do you have any pointers for me to make this work as expected?

@JeanMertz
Copy link
Author

Actually, looking at the code, it seems like this can be handled by registering the rust-analyzer.applySourceChange command to the CommandManager.

The only question is if there's already shared code that supports easy local text manipulation based on span information and text.

I'll have to dig into this some more sometime this weekend, but if you have any other pointers, feel free to let me know.

@JeanMertz
Copy link
Author

Okay, that was fairly easy to implement. I'll see about making this into a proper extension, but for starters, this will make sure local edits are applied as expected:

const { commands, workspace } = require('coc.nvim')

exports.activate = context => {
  context.subscriptions.push(commands.registerCommand('rust-analyzer.applySourceChange', async({ _cursorPosition, _label, workspaceEdit }) => {
    await workspace.applyEdit(workspaceEdit)
  }, null, true))
}

For anyone finding this issue: add this to a rust-analyzer.js file in $VIMCONFIG/coc-extensions, and things should work as expected.

@fannheyward
Copy link
Member

I'm porting rust-analyzer-vscode to a coc extension, https://www.npmjs.com/package/coc-rust-analyzer, give it a try.

rust-analyzer.applySourceChange is available now.

@JeanMertz
Copy link
Author

@fannheyward great! Are you going to host the code somewhere so others can contribute?

@fannheyward
Copy link
Member

@JeanMertz yes, I'll open it on GitHub later.

@fannheyward
Copy link
Member

These commands are works!

  1. rust-analyzer.analyzerStatus
  2. rust-analyzer.applySourceChange
  3. rust-analyzer.collectGarbage
  4. rust-analyzer.joinLines
  5. rust-analyzer.matchingBrace
  6. rust-analyzer.parentModule
  7. rust-analyzer.reload
  8. rust-analyzer.run
  9. rust-analyzer.runSingle

@Avi-D-coder
Copy link
Contributor

@fannheyward after you have a repo, I'll add a note in Avi-D-coder/coc-rust-analyzer pointing to it, since I never got around to finishing it.

@JeanMertz
Copy link
Author

@fannheyward great work so far. Two things I've noticed, but I'm not sure if it's a limitation of the plugin, coc itself, or my configuration:

  1. Autocompletion works, but it doesn't expand the method signature with snippet-support enabled, even though this does work in VSCode.
  2. Diagnostics (and code lens) only show up once a change is made in a document, not when the document is first loaded.

@fannheyward
Copy link
Member

@Avi-D-coder @JeanMertz I make it open: https://github.com/fannheyward/coc-rust-analyzer

  1. cargo-watch is WIP.
  2. method signature is supported by default, also diagnostics/codelens, but in some unknown case, it won't work properly, I'm looking on this.

This issue can be closed, any issue/feature/feedback can be post in coc-rust-analyzer repo, thanks.

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

No branches or pull requests

3 participants