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

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

Open
sagehane opened this issue Oct 20, 2024 · 2 comments

Comments

@sagehane
Copy link

sagehane commented Oct 20, 2024

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
Copy link
Member

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
Copy link
Author

sagehane commented Oct 21, 2024

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?

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

No branches or pull requests

2 participants