jinzhongjia / LspUI.nvim

A modern and useful UI plugin that wraps lsp operations.
MIT License
158 stars 4 forks source link

fix: change code_action line diagnostic fetcher function #8

Closed rmoretto closed 11 months ago

rmoretto commented 11 months ago

When using the code_action function with Rust LSP I got the following error message:

{ {
    error = {
      code = -32602,
      message = 'Failed to deserialize textDocument/codeAction: missing field `range`; {"range":{"end":{"line":3,"character":4},"start":{"line":3,"character":4}},"textDocument":{"uri":"file:///home/rodrigo/programations/pixel_prog/lets-
go/src/main.rs"},"context":{"diagnostics":[{"col":4,"message":"cannot find value `Instance` in this scope\\nnot found in this scope","lnum":3,"end_col":12,"bufnr":1,"namespace":21,"source":"rustc","severity":1,"user_data":{"lsp":{"codeD
escription":{"href":"https://doc.rust-lang.org/error-index.html#E0425"},"relatedInformation":[{"message":"consider importing this unit variant: `use wgpu::VertexStepMode::Instance;\\n`","location":{"uri":"file:///home/rodrigo/programati
ons/pixel_prog/lets-go/src/main.rs","range":{"end":{"line":0,"character":0},"start":{"line":0,"character":0}}}}],"code":"E0425","data":{"rendered":"error[E0425]: cannot find value `Instance` in this scope\\n --> src/main.rs:4:5\\n  |\\n
4 |     Instance;\\n  |     ^^^^^^^^ not found in this scope\\n  |\\nhelp: consider importing this unit variant\\n  |\\n1 + use wgpu::VertexStepMode::Instance;\\n  |\\n\\n"}}},"code":"E0425","end_lnum":3}]}}',
      <metatable> = {
        __tostring = <function 1>
      }
    }
  } }

Looking at the code I saw that the implementation of the logic to get the diagnostic from the current line was not align with the lsputil code, which use:

context.diagnostics = vim.lsp.diagnostic.get_line_diagnostics(bufnr)

Changing the diagnostic fetcher logic to use the vim.lsp.diagnostic.get_line_diagnostics(bufnr) function fixes the problem.

This PR simply changes the way that the code_action get the line diagnostic.

jinzhongjia commented 11 months ago

Can you give an example code of what you are using? I didn't get an error when testing locally

According to the official documentation, this function is deprecated

jinzhongjia commented 11 months ago

Maybe we can just replace https://github.com/jinzhongjia/LspUI.nvim/blob/b2662a1be2fdc95299637aebf18bc1fa53d98bd8/lua/LspUI/code_action/init.lua#L54-L56

fn.line(".") - 1 with

vim.api.nvim_win_get_cursor(0)[1] - 1

I looked at the implementation of get_line_diagnostics,it just will use vim.api.nvim_win_get_cursor and a function called diagnostic_vim_to_lsp

The following is my slightly modified diagnostic_vim_to_lsp

function(diagnostics)
    return vim.tbl_map(function(diagnostic)
        return vim.tbl_extend("keep", {
            range = {
                start = {
                    line = diagnostic.lnum,
                    character = diagnostic.col,
                },
                ["end"] = {
                    line = diagnostic.end_lnum,
                    character = diagnostic.end_col,
                },
            },
            severity = type(diagnostic.severity) == "string" and vim.diagnostic.severity[diagnostic.severity]
                or diagnostic.severity,
            message = diagnostic.message,
            source = diagnostic.source,
            code = diagnostic.code,
        }, diagnostic.user_data and (diagnostic.user_data.lsp or {}) or {})
    end, diagnostics)
end

Use this function to wrap vim.api.nvim_win_get_cursor(0)[1] - 1, I test it ok

rmoretto commented 11 months ago

The code I was using is a simple main function, like:

fn main() {
    io::Error;
}

and I was running the code_action with the cursor on top of the io::Error.

Maybe we can just replace

https://github.com/jinzhongjia/LspUI.nvim/blob/b2662a1be2fdc95299637aebf18bc1fa53d98bd8/lua/LspUI/code_action/init.lua#L54-L56

fn.line(".") - 1 with

vim.api.nvim_win_get_cursor(0)[1] - 1

Just implemented the change you proposed and the code_action works correctly and updated the PR.

jinzhongjia commented 11 months ago

The code I was using is a simple main function, like:

fn main() {
    io::Error;
}

and I was running the code_action with the cursor on top of the io::Error.

Maybe we can just replace https://github.com/jinzhongjia/LspUI.nvim/blob/b2662a1be2fdc95299637aebf18bc1fa53d98bd8/lua/LspUI/code_action/init.lua#L54-L56

fn.line(".") - 1 with

vim.api.nvim_win_get_cursor(0)[1] - 1

Just implemented the change you proposed and the code_action works correctly and updated the PR.

Note that I re-edited this comment

I suggest you add a function called M.diagnostic_vim_to_lsp to code_action/util.lua, and use it like this:

local diagnostics = util.diagnostic_vim_to_lsp(vim.diagnostic.get(current_buffer, {
        lnum = fn.line(".") - 1,
    }))
jinzhongjia commented 11 months ago

Interestingly, I didn't get the error you meet, it's just that there is no option for lsp in code_action,your neovim version is nigthly?

jinzhongjia commented 11 months ago

If you test code_action and everything is normal, then I can merge pr Please pay attention to clearing the print information for your debugging

rmoretto commented 11 months ago

Note that I re-edited this comment

Sorry didn't catch the comment 😅

I suggest you add a function called M.diagnostic_vim_to_lsp to code_action/util.lua, and use it like this:

Created the new function as you suggested, and updated the PR! It worked for me.

rmoretto commented 11 months ago

Interestingly, I didn't get the error you meet, it's just that there is no option for lsp in code_action,your neovim version is nigthly?

I'm using the 0.9.1 version:

❯ nvim --version
NVIM v0.9.1
Build type: RelWithDebInfo
LuaJIT 2.1.0-beta3
Compilation: /usr/bin/cc -O2 -g -Og -g -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion -Wvla -Wdouble-promotion -Wmissing-noreturn -Wmissing-format-attribute -Wmissing-prototypes -fno-common -Wno-unused-result -Wimplicit-fallthrough -fdiagnostics-color=always -fstack-protector-strong -DUNIT_TESTING -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -I/home/rodrigo/programations/misc/neovim/.deps/usr/include/luajit-2.1 -I/usr/include -I/home/rodrigo/programations/misc/neovim/.deps/usr/include -I/home/rodrigo/programations/misc/neovim/build/src/nvim/auto -I/home/rodrigo/programations/misc/neovim/build/include -I/home/rodrigo/programations/misc/neovim/build/cmake.config -I/home/rodrigo/programations/misc/neovim/src -I/usr/include -I/home/rodrigo/programations/misc/neovim/.deps/usr/include -I/home/rodrigo/programations/misc/neovim/.deps/usr/include -I/home/rodrigo/programations/misc/neovim/.deps/usr/include -I/home/rodrigo/programations/misc/neovim/.deps/usr/include -I/home/rodrigo/programations/misc/neovim/.deps/usr/include -I/home/rodrigo/programations/misc/neovim/.deps/usr/include

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/local/share/nvim"

Run :checkhealth for more info
jinzhongjia commented 11 months ago

Everything is ok now, thanks for your PR