kevinhwang91 / nvim-ufo

Not UFO in the sky, but an ultra fold in Neovim.
BSD 3-Clause "New" or "Revised" License
2.23k stars 44 forks source link

LSP Hover mapped to <CR> when leaving a fold preview leads to an UnhandledPromiseRejection exception #53

Closed soifou closed 2 years ago

soifou commented 2 years ago

Neovim version (nvim -v | head -n1)

NVIM v0.8.0-dev+595-g95c65a6b2

Operating system/version

Debian testing

How to reproduce the issue

cat mini.lua

vim.cmd([[set runtimepath=$VIMRUNTIME]])
vim.cmd([[set packpath=/tmp/nvim/site]])

local package_root = "/tmp/nvim/site/pack"
local packer_install_path = package_root .. "/packer/start/packer.nvim"

local function load_plugins()
    require("packer").startup({
        {
            "wbthomason/packer.nvim",
            "williamboman/nvim-lsp-installer",
            "neovim/nvim-lspconfig",
            "kevinhwang91/nvim-ufo",
            "kevinhwang91/promise-async",
        },
        config = {
            package_root = package_root,
            compile_path = packer_install_path .. "/plugin/packer_compiled.lua",
            display = { non_interactive = true },
        },
    })
end

_G.load_config = function()
    require("nvim-lsp-installer").setup({
        install_root_dir = "/tmp/nvim/lsp_servers" ,
        automatic_installation = true
    })
    local lsp_init = function()
        require("lspconfig").sumneko_lua.setup({
            settings = { Lua = { cmd = { "/tmp/nvim/lsp_servers/sumneko_lua/extension/server/bin/lua-language-server" } } },
            on_attach = function(_, bufnr)
                vim.keymap.set("n", "<CR>", vim.lsp.buf.hover,{ buffer = bufnr })
            end
        })
    end
    lsp_init()
    vim.api.nvim_create_autocmd("ModeChanged", {
        once = true,
        pattern = { "n:i" },
        callback = function()
            package.loaded.lsp = nil
            lsp_init()
        end,
    })

    vim.o.foldcolumn = '1'
    vim.o.foldlevel = 1
    vim.o.foldlevelstart = -1
    vim.o.foldenable = true

    local ufo = require('ufo')
    ufo.setup()
    vim.keymap.set("n", "K", function() ufo.peekFoldedLinesUnderCursor() end)
end

if vim.fn.isdirectory(packer_install_path) == 0 then
    print("Installing plugins and dependencies...")
    vim.fn.system({
        "git",  "clone",   "--depth=1",
        "https://github.com/wbthomason/packer.nvim",
        packer_install_path,
    })
end

load_plugins()
require("packer").sync()
vim.cmd([[autocmd User PackerComplete ++once echo "Ready!" | lua load_config()]])

nvim -nu mini.lua mini.lua

  1. Wait until the code has been folded
  2. Preview a fold with K and leave the preview with any key, notice everything is normal.
  3. Enter Insert mode then go back to normal, LSP starts, diagnostics appears
  4. Hover any part of the code (ie vim), hit a LSP popup appear
  5. Now repeat step 2, an exception is thrown each time we leave a folded preview and step 4 does not work anymore

Expected behavior

I can map vim.lsp.buf.hover to <CR> with no exception thrown when leaving a fold preview. My mapping should be preserved after leaving the preview.

Actual behavior

An exception is thrown each time we leave a folded code preview

Error executing vim.schedule lua callback: UnhandledPromiseRejection with the reason:
...te/pack/packer/start/nvim-ufo/lua/ufo/preview/keymap.lua:33: Expected lua string

The current mapping to vim.lsp.buf.hover is lost.


Note that using the recommended mapping K for vim.lsp.buf.hover fix the issue described.

Some additional notes, commit 27c466886959fad03a09dd62814e126ff8178a1a introduce this issue and replacing k.lhs by k.rhs on this line seems to fix the exception but the mapping is still lost.

Not related but thank you for your very nice plugin :)

kevinhwang91 commented 2 years ago

I can't reproduce. Please use the mini.lua template. You can add log.error() or print(vim.inspect()) to debug yourself.

soifou commented 2 years ago

Sorry for the template, I thought providing one embedding all in /tmp would be more reproductible.

I tried to debug myself but I don't understand why vim.api.nvim_buf_get_keymap return no rhs for my mapping

    if capabilities.hoverProvider then
        keymap.set("n", "<CR>", lsp.buf.hover, { buffer = bufnr, desc = "LSP Hover" })
    end
:verbose map <CR>
n  <CR>        *@<Lua function 611>
                 LSP Hover
        Last set from Lua
Press ENTER or type command to continue
:lua = vim.api.nvim_buf_get_keymap(vim.api.nvim_get_current_buf(), 'n')
{ {
    buffer = 10,
    callback = <function 1>,
    desc = "LSP Hover",
    expr = 0,
    lhs = "<CR>",
    lnum = 0,
    mode = "n",
    noremap = 1,
    nowait = 0,
    script = 0,
    sid = -8,
    silent = 0
  }, {
...
kevinhwang91 commented 2 years ago

Thanks, callback is lua ref. Fixed now