ShinKage / idris2-nvim

Simple configuration and extra tools for NVIM + LSP + Idris2
MIT License
46 stars 7 forks source link

Code actions slightly broken with other LSPs when using the save hook from README #33

Open sagehane opened 2 days ago

sagehane commented 2 days ago

Sorry beforehand if this is a dumb question and I'm just using the LSP wrong.

I'm using https://www.lazyvim.org/ as my package manager for Neovim and here's my ~/.config/nvim/lua/plugins/idris2.lua file:

return {
        "ShinKage/idris2-nvim",
        dependencies = { 'neovim/nvim-lspconfig', 'MunifTanjim/nui.nvim' },

        config = function()
                local code_action = require('idris2.code_action')

                local filters = code_action.filters
                local introspect = code_action.introspect_filter
                local function save_hook(action)
                        if introspect(action) == filters.MAKE_CASE
                            or introspect(action) == filters.MAKE_WITH then
                                return
                        end
                        vim.cmd('silent write')
                end

                require('idris2').setup({
                        code_action_post_hook = save_hook, -- Commenting this out solves the issue
                })
        end,
}

Now, when trying some other LSP code action with :lua vim.lsp.buf.code_action(), I get an error.

Sample Haskell file with haskell-language-server:

-- Code action suggests: x = 1 : [1]
x = [1] ++ [1]

Error message as reported to :messages:

"test.hs" [New] 2L, 52B written                                                                                  
Error executing vim.schedule lua callback: ...l/share/nvim/lazy/idris2-nvim/lua/idris2/code_action.lua:21: attempt to index
 local 'action' (a nil value)                                                                                              
stack traceback:                                                                                                           
        ...l/share/nvim/lazy/idris2-nvim/lua/idris2/code_action.lua:21: in function 'introspect'                           
        /home/plumeus/.config/nvim/lua/plugins/idris2.lua:11: in function 'custom_handler'                                 
        ...l/share/nvim/lazy/idris2-nvim/lua/idris2/code_action.lua:171: in function 'on_choice'                           
        ...eovim-unwrapped-0.10.2/share/nvim/runtime/lua/vim/ui.lua:54: in function 'ui_select'                            
        ...l/share/nvim/lazy/idris2-nvim/lua/idris2/code_action.lua:174: in function 'select'                              
        ...-unwrapped-0.10.2/share/nvim/runtime/lua/vim/lsp/buf.lua:821: in function 'on_code_action_results'              
        ...-unwrapped-0.10.2/share/nvim/runtime/lua/vim/lsp/buf.lua:865: in function 'handler'                             
        ...wrapped-0.10.2/share/nvim/runtime/lua/vim/lsp/client.lua:687: in function ''                                    
        vim/_editor.lua: in function <vim/_editor.lua:0>      
mattpolzin commented 1 day ago

I can reproduce this, though I surprisingly never have before just by chance.

The hook this plugin installs is not specific to the type of file being read. I am not sure (having not looked into it) if there would be a more appropriate way to install the hook that was more targeted, but I am sure this can be worked around in your implementation of that callback at least.

Try changing your save hook to something like:

local function save_hook(action)
  if not action or not action.title then
    return
  end
  local code_action = require('idris2.code_action')

  local filters = code_action.filters
  local introspect = code_action.introspect_filter
  ...
end
sagehane commented 1 day ago

Due to my ignorance of pretty much everything involved (Lua, Neovim, this repo), I have no idea what that's supposed to do. But hey, at least it's definitely work for me. Both this LSP and haskell-language-server code actions work now as I would expect.

New config:

                local code_action = require('idris2.code_action')

                local filters = code_action.filters
                local introspect = code_action.introspect_filter
                local function save_hook(action)
                        -- https://github.com/ShinKage/idris2-nvim/issues/33#issuecomment-2425178847
                        if not action or not action.title then
                                return
                        end

                        if introspect(action) == filters.MAKE_CASE
                            or introspect(action) == filters.MAKE_WITH then
                                return
                        end
                        vim.cmd('silent write')
                end

Edit: I presume I should keep this issue open until the README is fixed?