rachartier / tiny-code-action.nvim

A Neovim plugin that provides a simple way to run and visualize code actions with Telescope.
MIT License
154 stars 2 forks source link

Incorrect diff with rust analyzer #7

Closed ouuan closed 3 months ago

ouuan commented 3 months ago

Original code:

pub fn foo() -> i32 {
    let x = 1;
    x + x
}

Plugin diff:

 pub fn foo() -> i32 {
-    let x = 1;
-    x + x
+    1
+ + x1
 }

Actual result:

pub fn foo() -> i32 {
    1 + 1
}

Plugin diff:

 pub fn foo() -> i32 {
-    let x = 1;
-    x + x
+    consX:i32  x = 1;
+    X + X
 }

Actual result:

pub fn foo() -> i32 {
    const X: i32 = 1;
    X + X
}

actions-preview.nvim works as expected.

rachartier commented 3 months ago

Hello, Which LSP do you use?

ouuan commented 3 months ago

Which LSP do you use?

In the title, rust analyzer.

rachartier commented 3 months ago

Maybe did you configure your LSP ? I have the default config, and it did not shows the same code actions as yours:

image

image

ouuan commented 3 months ago

I'm not sure, but the problem also exists in your second screenshot.

rachartier commented 3 months ago

It should be good now. Can you test it please ?

ouuan commented 3 months ago

image

image

There is an extra new line in the diff.

rachartier commented 3 months ago

It should now be ok (I hope) as of commit https://github.com/rachartier/tiny-code-action.nvim/commit/8fb55da53c5ed3f5a25ad07916d40f50b0b59172:

image

ouuan commented 3 months ago

image

image

There is an extra new line in the diff.

This one is still the same. I'm not sure why you are seeing a different list of available code actions. I'm using rust-analyzer 1 (4afe0d5393 2024-07-21) and cargo 1.80.0 (376290515 2024-07-16).

ouuan commented 3 months ago

I see that this plugin uses custom logic to parse the language server outputs. Can we use vim.lsp.util.apply_text_edits instead? See https://github.com/aznhe21/actions-preview.nvim/blob/9f52a01c374318e91337697ebed51c6fae57f8a4/lua/actions-preview/action.lua#L59-L66

rachartier commented 3 months ago

apply_text_edits

Unfortunately, LSPs send back textChanges for some actions, which is not supported (I think, I haven't found anything about it) with native LSP in Neovim, so that's why I used custom logic. But yes, for some actions, it can work with apply_text_edits

I really can't have the same actions as yours. I do not understand why, I did install rust-analyzer (2024-07-22) (as 2024-07-21 was not in mason) and cargo 1.80.0 (376290515 2024-07-16). I've tried to get code actions for every characters in the demo file, and I can't have the same as yours... Do you have some specifics configurations? It will made my life easier if I can debug a real example.

EDIT: But I use apply_workspace_edit/execute_command when applying the code action selected by the user onto the buffer

ouuan commented 3 months ago

Do you have some specifics configurations?

I don't think so. I just use the default configuration from nvim-lspconfig.

rachartier commented 3 months ago

Okay I did reproduce the issue.

You probably have rustacean installed. I've modified the code to merge lines if needed, so you shouldn't have the issue anymore.