mrcjkb / rustaceanvim

πŸ¦€ Supercharge your Rust experience in Neovim! A heavily modified fork of rust-tools.nvim
GNU General Public License v2.0
1.54k stars 54 forks source link

When applying code action escape character is inserted before } #303

Closed AradiPatrik closed 5 months ago

AradiPatrik commented 5 months ago

Have you read the docs and searched existing issues?

Neovim version (nvim -v)

v0.9.5

Operating system/version

macOS 14.3.1 (23D60)

Output of :checkhealth rustaceanvim

rustaceanvim: require("rustaceanvim.health").check()

Checking for Lua dependencies ~
- OK [mfussenegger/nvim-dap](https://github.com/mfussenegger/nvim-dap) installed.

Checking external dependencies ~
- OK rust-analyzer: found rust-analyzer 0.0.0 (5346002d0 2024-02-25)
- OK Cargo: found cargo 1.76.0 (c84b36747 2024-01-18)
- OK rustc: found rustc 1.76.0 (07dca489a 2024-02-04)
- OK lldb: found lldb-1500.0.404.7

Checking config ~
- OK No errors found in config.

Checking for conflicting plugins ~
- OK No conflicting plugins detected.

Checking for tree-sitter parser ~
- OK tree-sitter parser for Rust detected.

How to reproduce the issue

mkdir -p /tmp/minimal/
NVIM_DATA_MINIMAL="/tmp/minimal" NVIM_APP_NAME="nvim-minimal" nvim -u NORC -u minimal.lua
:edit main.rs
/impl<CR>
:lua vim.lua.buf.code_action()<CR>
1<CR>

Expected behaviour

https://github.com/mrcjkb/rustaceanvim/assets/16306605/e00346dd-62c3-4d9d-848e-704e8c74060e

The expected behavior is to have regular closing braces generated }. Please check the video issue. It should be quite obvious.

Actual behaviour

Right now closing braces are escaped for some reason \} instead of } Please check the attached video.

The minimal config used to reproduce this issue.

The minimal config linked in the troubleshooting
AradiPatrik commented 5 months ago

The code to demonstrate the issue main.rs

trait Messenger {
    fn send(&self, message: &str);
}

struct LimitTracker<'a, T: Messenger> {
    messenger: &'a T,
    value: u32,
    limit: u32,
}

impl<'a, T: Messenger> LimitTracker<'a, T> {
    fn set_value(&mut self, value: u32) {
        if self.limit as f32 / value as f32 > 0.5f32 {
            self.messenger.send("You've consumed half of your quota");
        } else if self.limit as f32 / value as f32 > 0.75f32 {
            self.messenger
                .send("Warning! You've consumed 75% of your quota");
        } else if self.limit as f32 / value as f32 > 0.9f32 {
            self.messenger
                .send("Urgent! You've consumed 90% of your quota");
        }

        self.value = value
    }
}

fn main() {}
mrcjkb commented 5 months ago

Hey :wave:

Thanks for the detailed report. This is a rust-analyzer bug: https://github.com/rust-lang/rust-analyzer/issues/16607

rami3l commented 5 months ago

@mrcjkb Looks like the problem is more complicated than it seems...

Coming exactly from https://github.com/rust-lang/rust-analyzer/issues/16867, I made my own tests and saw that even on Neovim v0.10 I can still reproduce this issue with this plugin enabled. However, if I disable this plugin, then the completion works fine.

mrcjkb commented 5 months ago

@mrcjkb Looks like the problem is more complicated than it seems...

Coming exactly from rust-lang/rust-analyzer#16867, I made my own tests and saw that even on Neovim v0.10 I can still reproduce this issue with this plugin enabled. However, if I disable this plugin, then the completion works fine.

πŸ€” vim.lua.buf.code_action() isn't implemented by this plugin. It's a core neovim function. So that sounds very fishy.

Are you sure you're using the same rust-analyzer version in both cases? Many neovim distributions use mason.nvim to install it.

rami3l commented 5 months ago

@mrcjkb Thanks a lot for your timely reply! I did some more research and it did point me to a different direction.

Are you sure you're using the same rust-analyzer version in both cases?

I'm quite sure about this one. Initially I thought it was an r-a problem so that was my first thing to look into. However I do think the latest version of r-a is now generating the correct output.

πŸ€” vim.lua.buf.code_action() isn't implemented by this plugin. It's a core neovim function. So that sounds very fishy.

That's why I didn't think of disabling this plugin at first. However I'm not suggesting it's directly causing the problem.

I'm thinking in theory this plugin should do no harm to the whole code action thing, except it somehow puts r-a into an output mode Neovim cannot handle properly.

After some more research I've found https://github.com/fannheyward/coc-rust-analyzer/commit/5059573c3e6bd8b31315f1abec6007da14f77fd4 to support this theory. Looks like it has something to do with:

https://github.com/mrcjkb/rustaceanvim/blob/597c8d6c7b4433ec4577b73bced1df41c132b077/lua/rustaceanvim/lsp.lua#L138

If this is the case, probably there're some problems remaining even with the updated Neovim after all.

mrcjkb commented 5 months ago

@rami3l wow thanks for the thorough research! πŸ˜€

If I have some time in the next days, I'll see if can find the issue in neovim.

In the meantime, you should be able to work around this by explicitly setting the capability to false via vim.g.rustaceanvim.server.capabilities.

rami3l commented 5 months ago

@rami3l wow thanks for the thorough research! πŸ˜€

If I have some time in the next days, I'll see if can find the issue in neovim.

In the meantime, you should be able to work around this by explicitly setting the capability to false via vim.g.rustaceanvim.server.capabilities.

@mrcjkb Thanks for your pointers! Now I can 100% confirm that this problem can be worked around by setting vim.g.rustaceanvim.server.capabilities.experimental.snippetTextEdit to false.

mrcjkb commented 5 months ago

:thinking: I can't reproduce this with my personal config, but I can with a minimal one. I wonder if there's a neovim setting or plugin that is fixing it in my config.

elithrade commented 5 months ago

@rami3l wow thanks for the thorough research! πŸ˜€ If I have some time in the next days, I'll see if can find the issue in neovim. In the meantime, you should be able to work around this by explicitly setting the capability to false via vim.g.rustaceanvim.server.capabilities.

@mrcjkb Thanks for your pointers! Now I can 100% confirm that this problem can be worked around by setting vim.g.rustaceanvim.server.capabilities.experimental.snippetTextEdit to false.

@rami3l can I please ask how do you set vim.g.rustaceanvim.server.capabilities.experimental.snippetTextEdit to false? I am using LazyVim.

mrcjkb commented 5 months ago

@rami3l wow thanks for the thorough research! πŸ˜€ If I have some time in the next days, I'll see if can find the issue in neovim. In the meantime, you should be able to work around this by explicitly setting the capability to false via vim.g.rustaceanvim.server.capabilities.

@mrcjkb Thanks for your pointers! Now I can 100% confirm that this problem can be worked around by setting vim.g.rustaceanvim.server.capabilities.experimental.snippetTextEdit to false.

@rami3l can I please ask how do you set vim.g.rustaceanvim.server.capabilities.experimental.snippetTextEdit to false? I am using LazyVim.

See :h rustaceanvim.config and the LazyVim documentation on the rust extension pack.

rami3l commented 5 months ago

@rami3l wow thanks for the thorough research! πŸ˜€ If I have some time in the next days, I'll see if can find the issue in neovim. In the meantime, you should be able to work around this by explicitly setting the capability to false via vim.g.rustaceanvim.server.capabilities.

@mrcjkb Thanks for your pointers! Now I can 100% confirm that this problem can be worked around by setting vim.g.rustaceanvim.server.capabilities.experimental.snippetTextEdit to false.

@rami3l can I please ask how do you set vim.g.rustaceanvim.server.capabilities.experimental.snippetTextEdit to false? I am using LazyVim.

@elithrade You seem to be using lazy.nvim to install this plugin, so you should be good to go after simply adding that assignment to this plugin's config function.

ctx: https://github.com/folke/lazy.nvim?tab=readme-ov-file#-plugin-spec

rami3l commented 5 months ago

πŸ€” I can't reproduce this with my personal config, but I can with a minimal one. I wonder if there's a neovim setting or plugin that is fixing it in my config.

@mrcjkb Thanks again for your efforts! Would you like to share the minimal reproduction with Neovim? I guess it's still a problem to be addressed on their side...

mrcjkb commented 5 months ago

πŸ€” I can't reproduce this with my personal config, but I can with a minimal one. I wonder if there's a neovim setting or plugin that is fixing it in my config.

@mrcjkb Thanks again for your efforts! Would you like to share the minimal reproduction with Neovim? I guess it's still a problem to be addressed on their side...

I am planning to look into it, but haven't had the time yet πŸ˜ƒ Since it's related to an experimental rust-analyzer feature, I don't think it's worth opening an issue with Neovim yet.