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

"Add #[derive]" missing from code actions with rust-analyzer #1907

Closed
miikkas opened this issue Jul 13, 2020 · 11 comments · Fixed by #1926
Closed

"Add #[derive]" missing from code actions with rust-analyzer #1907

miikkas opened this issue Jul 13, 2020 · 11 comments · Fixed by #1926
Assignees

Comments

@miikkas
Copy link

miikkas commented Jul 13, 2020

Bug description
Code actions for structs don't have the "Add `#[derive]`" item, which used to be available when using older versions of rust-analyzer. The "Add `#[derive]`" item is available in vscode, though, but I can't seem to get it to appear in Emacs even when using the same version of rust-analyzer as with vscode.

To reproduce

  1. cargo new lsp-ra-bug-repro
  2. Use the following code for lsp-ra-bug-repro/src/main.rs:
    struct WantToPrint {
        x: i32,
        y: i32,
    }
    
    fn main() {
        let wtp = WantToPrint { x: 1234, y: 5678, };
        println!("{:?}", wtp);
    }
  3. Move cursor/caret to the beginning of the first line
  4. Invoke lsp-mode's execution of code actions (I pressed C-c l a a having configured it thus)
  5. An option to "Add `#[derive]`" is missing. Here's how it appears in a very bare-bones Emacs with only rust-mode and lsp-mode manually installed, after pressing TAB a couple of times after the previous step:
    emacs_no_derive
  6. Additionally, pressing RET here gives the error "Wrong type argument: hash-table-p, nil". I don't remember this happening when using an older version of rust-analyzer, but I suppose it's somehow linked to how I seem to get an empty(?) item at the beginning of the minibuffer when using Ido mode on my default configuration. Here's how the empty item looks in that case – the { | at the beginning without any text between those characters, none of the actually usable code actions being currently selected:
    emacs_empty_item
    To be clear, pressing RET on that empty item in Ido mode will also yield the same wrong type argument error message.

Expected behavior
An option to "Add `#[derive]`" should be available. Here's how it appears in vscode (the item is highlighted because I had my mouse cursor on it, which isn't visible in the screenshot):
vscode_derive_exists

Environment
OS: Ubuntu 20.04
Emacs: GNU Emacs 26.3 (build 2, x86_64-pc-linux-gnu, GTK+ Version 3.24.14) of 2020-03-26, modified by Debian
lsp-mode: 20200713.506 from MELPA (I've also tested on several other versions during the past month)
rust-analyzer: 5ca7cd9 (included with vscode), 4b1c372 (manually built today, 2020-07-13), also several others during the past month

LSP Mode workspace log
I captured this workspace log from pressing C-c l a a with the above file open and the cursor where stated in the above reproduction instructions.

@miikkas miikkas changed the title "Add [#derive]" missing from code actions with rust-analyzer "Add #[derive]" missing from code actions with rust-analyzer Jul 13, 2020
@yyoncho
Copy link
Member

yyoncho commented Jul 13, 2020

Thank you for detailed report, I will take a look.

@yyoncho yyoncho self-assigned this Jul 13, 2020
@yyoncho
Copy link
Member

yyoncho commented Jul 14, 2020

@flodiebold I spent quite some time looking into this issue and in rust analyzer code and it seems like RA is just failing on calculating this quick fix and it is related to something else...

@flodiebold
Copy link
Contributor

flodiebold commented Jul 14, 2020

I think the code action isn't generated because lsp-mode doesn't support the snippet TextEdit extension yet. Most code actions just return a non-snippet edit in that case, but this one doesn't generate, probably because it doesn't make that much sense if it can't position the cursor.

@yyoncho
Copy link
Member

yyoncho commented Jul 14, 2020

I think the code action isn't generated because lsp-mode doesn't support the snippet TextEdit extension yet. Most code actions just return a non-snippet edit in that case, but this one doesn't generate, probably because it doesn't make that much sense if it can't position the cursor.

Thank you for the pointer, I will add support for that.

yyoncho added a commit to yyoncho/lsp-mode that referenced this issue Jul 16, 2020
@yyoncho
Copy link
Member

yyoncho commented Jul 16, 2020

proposed fix at #1926

yyoncho added a commit to yyoncho/lsp-mode that referenced this issue Jul 17, 2020
yyoncho added a commit that referenced this issue Jul 18, 2020
@miikkas
Copy link
Author

miikkas commented Jul 18, 2020

I was traveling and couldn't get back to you immediately – thanks for working on this. I just tested yyoncho's proposed fix: I first upgraded lsp-mode to the latest version 20200716.2052 on MELPA, deleted the .elc files and replaced the three files changed in the pull request. I then went through the same steps I originally reported for reproducing the issue. Looks like I'm still not getting the option for adding #[derive], though.

@yyoncho
Copy link
Member

yyoncho commented Jul 18, 2020

@miikkas seems like melpa is not picking lsp-mode, the fix was submitted on 0717 but still there is no lsp-mode version with these changes.

@miikkas
Copy link
Author

miikkas commented Jul 18, 2020

Sorry, I actually meant I applied your patch manually to the latest lsp-mode at the moment of writing my previous comment.

@miikkas seems like melpa is not picking lsp-mode, the fix was submitted on 0717 but still there is no lsp-mode version with these changes.

Looks like MELPA just finished with the latest update, and lsp-mode 20200718.1111 is now up.

I tried that version now, but the code action for adding #[derive] still wasn't available. I dug around the workspace log from around where rust-analyzer is started and noticed the following under the "capabilities" section:

    "experimental": {
      "snippetTextEdit": [
        ",",
        "lsp-enable-snippet"
      ]
    }

Seems like the lsp-enable-snippet variable is not evaluated here:

:custom-capabilities '((experimental . ((snippetTextEdit . ,lsp-enable-snippet ))))))

I tried changing ,lsp-enable-snippet to t instead and the code action became available. Thanks for adding the feature! I don't know enough elisp, however, to fix it such that it would use that variable. :)

@kiennq
Copy link
Member

kiennq commented Jul 18, 2020

@miikkas You just need to change the first quote ' into back-quote `

@yyoncho
Copy link
Member

yyoncho commented Jul 18, 2020

@miikkas sorry about that, I pushed a fix about that - I changed t to lsp-enable-snippet just before pushing which is not expanded.

@miikkas
Copy link
Author

miikkas commented Jul 18, 2020

@miikkas sorry about that, I pushed a fix about that - I changed t to lsp-enable-snippet just before pushing which is not expanded.

Thank you!

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 a pull request may close this issue.

4 participants