kevinhwang91 / nvim-ufo

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

UnhandledPromiseRejection when opening Telescope #17

Closed wilriker closed 2 years ago

wilriker commented 2 years ago

Neovim version (nvim -v | head -n1)

nvim v0.7.0

Operating system/version

Arch Linux

How to reproduce the issue

  1. Open Telescope

Expected behavior

No errors

Actual behavior

When I open Telescope (git-files in this case) I see at least sometimes:

Error executing vim.schedule lua callback: UnhandledPromiseRejection with the reason:
...vim/site/pack/packer/start/nvim-ufo/lua/ufo/provider.lua:48: ...site/pack/packer/start/nvim-ufo/lua/ufo/provider/lsp.lua:58: attempt to call method 'match' (a nil value)
stack traceback:
        ...re/nvim/site/pack/packer/start/nvim-ufo/lua/ufo/fold.lua:51: in function <Anonymous:40>
kevinhwang91 commented 2 years ago

What's lsp client?

wilriker commented 2 years ago

In this case it is gopls.

Also the error seems to go away after I opened a fold for the first time (closing folds does not make it go away).

kevinhwang91 commented 2 years ago

Not a server, I mean coc.nvim or nvim lsp. It's weird that throw a nil reason by error()

wilriker commented 2 years ago

I'm sorry, I use nvim LSP client.

wilriker commented 2 years ago

Update: I now also got it after closing and opening a fold again, so no reliable way to get it solved (that I know of).

kevinhwang91 commented 2 years ago

I can swallow the nil reason in the fallback function for the nvimlsp client, but it's fragile, need to handle it inside nvimlsp, need time to explore the source code.

kevinhwang91 commented 2 years ago

Update: I now also got it after closing and opening a fold again, so no reliable way to get it solved (that I know of).

Give a context please, what buffer content and filetype, what's UfoInspect output?

wilriker commented 2 years ago

I meant that I previously wrote

Also the error seems to go away after I opened a fold for the first time (closing folds does not make it go away).

but that was most likely a coincidence. I could also produce the error after closing and reopening a fold.

Nevertheless, the base buffer outputs for UfoInspect:

Buffer: 7
Fold Status: start
Main provider: lsp
Fallback provider: indent
Selected provider: lsp

but that is (at least seems to me) irrelevant since the error only occurs once I try to open Telescope and there it states

Buffer: 15
Fold Status: start
Main provider: lsp
Fallback provider: indent
Selected provider: nil

What I forgot to mention this far: I use vim sessions heavily, so after starting nvim in 99.9% of the times I load a session. When I do that I sometimes (but not always) also get this message:

Error executing vim.schedule lua callback: UnhandledPromiseRejection with the reason:
...vim/site/pack/packer/start/nvim-ufo/lua/ufo/provider.lua:48: ...site/pack/packer/start/nvim-ufo/lua/ufo/provider/lsp.lua:61: /usr/share/nvim/runtime/lua/vim/uri.lua:84: Invalid buffer id: 1
stack traceback:
        ...re/nvim/site/pack/packer/start/nvim-ufo/lua/ufo/fold.lua:51: in function <Anonymous:40>

Might be related.

The error though seems to go away after some time at least sometimes. Very hard spurious.

kevinhwang91 commented 2 years ago

Please update the code, I have checked buffer valid before request server.

wilriker commented 2 years ago

Updated, problem still present. I even deleted my session and created a new one.

When I open Telescope I still get the exact same error message as in the issue description. UfoInspect now has a slightly different output for the telescope buffer though:

Buffer: 14
Fold Status: start
Main provider: lsp
Fallback provider: indent
Selected provider: indent
kevinhwang91 commented 2 years ago

try try-fix-nvimlsp branch please, and post the message if has any errors.

wilriker commented 2 years ago

Error also present and still exactly the same:

Error executing vim.schedule lua callback: UnhandledPromiseRejection with the reason:
...vim/site/pack/packer/start/nvim-ufo/lua/ufo/provider.lua:48: ...site/pack/packer/start/nvim-ufo/lua/ufo/provider/lsp.lua:58: attempt to call method 'match' (a nil value)
stack traceback:
        ...re/nvim/site/pack/packer/start/nvim-ufo/lua/ufo/fold.lua:51: in function <Anonymous:40>
kevinhwang91 commented 2 years ago

I think I have known the reason, please update the code and post the error again, I want to confirm that.

wilriker commented 2 years ago

Error message changed now:

Error executing vim.schedule lua callback: UnhandledPromiseRejection with the reason:
{ ["message"] = read /: is a directory, ["code"] =  }
stack traceback:
        ...re/nvim/site/pack/packer/start/nvim-ufo/lua/ufo/fold.lua:51: in function <Anonymous:40>
kevinhwang91 commented 2 years ago

Look like sending a buffer is a directory, can you confirm that?

wilriker commented 2 years ago

Sorry, I don't understand what you mean by that.

What I do is simply opening a Telescope window. It does not matter whether that is showing files (:Telescope git_files), help (:Telescope help_tags) or git branches (:Telescope git_branches). I always get the same error message. I have no idea where the root directory / could come into play here.

zbindenren commented 2 years ago

I have a similar issue if I commit via neogit:

Error executing vim.schedule lua callback: UnhandledPromiseRejection with the reason:                                                                                                                                                                                                         
...vim/site/pack/packer/start/nvim-ufo/lua/ufo/provider.lua:48: ...site/pack/packer/start/nvim-ufo/lua/ufo/provider/lsp.lua:58: attempt to call method 'match' (a nil value)                                                                                                                  
stack traceback:                                                                                                                                                                                                                                                                              
        ...re/nvim/site/pack/packer/start/nvim-ufo/lua/ufo/fold.lua:51: in function <Anonymous:40>                                                                                                                                                                                            
Press ENTER or type command to continue       

The filetype is: NeogitStatus and it does not happen every time.

kevinhwang91 commented 2 years ago

I have a similar issue if I commit via neogit:

Error executing vim.schedule lua callback: UnhandledPromiseRejection with the reason:                                                                                                                                                                                                         
...vim/site/pack/packer/start/nvim-ufo/lua/ufo/provider.lua:48: ...site/pack/packer/start/nvim-ufo/lua/ufo/provider/lsp.lua:58: attempt to call method 'match' (a nil value)                                                                                                                  
stack traceback:                                                                                                                                                                                                                                                                              
        ...re/nvim/site/pack/packer/start/nvim-ufo/lua/ufo/fold.lua:51: in function <Anonymous:40>                                                                                                                                                                                            
Press ENTER or type command to continue       

The filetype is: NeogitStatus and it does not happen every time.

Please update the code from main branch, will show the true error.

kevinhwang91 commented 2 years ago

Sorry, I don't understand what you mean by that.

What I do is simply opening a Telescope window. It does not matter whether that is showing files (:Telescope git_files), help (:Telescope help_tags) or git branches (:Telescope git_branches). I always get the same error message. I have no idea where the root directory / could come into play here.

The message looks like returning by the language server, we should avoid sending invalid buffers, need the condition to filter them out.

kevinhwang91 commented 2 years ago

Telescope and Neogit buffers are all nofile buftype, I am not sure whether should skip nofile at ufo,

    require('ufo').setup({
        provider_selector = function(bufnr, filetype)
            if vim.bo[bufnr].bt == 'nofile' then
                return ''
            end
        end,
    })

should work.

If someone master nvim lsp please tell me, need help.

wilriker commented 2 years ago

Telescope and Neogit buffers are all nofile buftype, I am not sure whether should skip nofile at ufo,

    require('ufo').setup({
        provider_selector = function(bufnr, filetype)
            if vim.bo[bufnr].bt == 'nofile' then
                return ''
            end
        end,
    })

should work.

I can confirm that this removes the error message for me.

zbindenren commented 2 years ago
 provider_selector = function(bufnr, filetype)
            if vim.bo[bufnr].bt == 'nofile' then
                return ''
            end
        end,

I still get the error for neogit:

Error executing vim.schedule lua callback: UnhandledPromiseRejection with the reason:                                                                                                                                                                                                         
{ ["code"] = 0, ["message"] = stat /home/rz/repos/gitlab.com/linux/ces/ces-operator/NeogitStatus: no such file or director }                                                                                                                                                              
stack traceback:                                                                                                                                                                                                                                                                              
        ...re/nvim/site/pack/packer/start/nvim-ufo/lua/ufo/fold.lua:51: in function <Anonymous:40>

And I have the following ufo config:

local status_ok, ufo = pcall(require, "ufo")
if not status_ok then
    vim.notify("ufo (fold) plugin not found!")
    return
end

vim.wo.foldlevel = 99
vim.wo.foldenable = true

local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities.textDocument.foldingRange = {
    dynamicRegistration = false,
    lineFoldingOnly = true,
}

local handler = function(virtText, lnum, endLnum, width, truncate)
    local newVirtText = {}
    local suffix = ("  %d "):format(endLnum - lnum)
    local sufWidth = vim.fn.strdisplaywidth(suffix)
    local targetWidth = width - sufWidth
    local curWidth = 0
    for _, chunk in ipairs(virtText) do
        local chunkText = chunk[1]
        local chunkWidth = vim.fn.strdisplaywidth(chunkText)
        if targetWidth > curWidth + chunkWidth then
            table.insert(newVirtText, chunk)
        else
            chunkText = truncate(chunkText, targetWidth - curWidth)
            local hlGroup = chunk[2]
            table.insert(newVirtText, { chunkText, hlGroup })
            chunkWidth = vim.fn.strdisplaywidth(chunkText)
            -- str width returned from truncate() may less than 2nd argument, need padding
            if curWidth + chunkWidth < targetWidth then
                suffix = suffix .. (" "):rep(targetWidth - curWidth - chunkWidth)
            end
            break
        end
        curWidth = curWidth + chunkWidth
    end
    table.insert(newVirtText, { suffix, "MoreMsg" })
    return newVirtText
end

-- global handler
ufo.setup({
    fold_virt_text_handler = handler,
    provider_selector = function(bufnr, filetype)
        if vim.bo[bufnr].bt == "nofile" then
            return ""
        end
    end,
})
kevinhwang91 commented 2 years ago
 provider_selector = function(bufnr, filetype)
            if vim.bo[bufnr].bt == 'nofile' then
                return ''
            end
        end,

I still get the error for neogit:

Error executing vim.schedule lua callback: UnhandledPromiseRejection with the reason:                                                                                                                                                                                                         
{ ["code"] = 0, ["message"] = stat /home/rz/repos/gitlab.com/linux/ces/ces-operator/NeogitStatus: no such file or director }                                                                                                                                                              
stack traceback:                                                                                                                                                                                                                                                                              
        ...re/nvim/site/pack/packer/start/nvim-ufo/lua/ufo/fold.lua:51: in function <Anonymous:40>

And I have the following ufo config:

local status_ok, ufo = pcall(require, "ufo")
if not status_ok then
  vim.notify("ufo (fold) plugin not found!")
  return
end

vim.wo.foldlevel = 99
vim.wo.foldenable = true

local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities.textDocument.foldingRange = {
  dynamicRegistration = false,
  lineFoldingOnly = true,
}

local handler = function(virtText, lnum, endLnum, width, truncate)
  local newVirtText = {}
  local suffix = ("  %d "):format(endLnum - lnum)
  local sufWidth = vim.fn.strdisplaywidth(suffix)
  local targetWidth = width - sufWidth
  local curWidth = 0
  for _, chunk in ipairs(virtText) do
      local chunkText = chunk[1]
      local chunkWidth = vim.fn.strdisplaywidth(chunkText)
      if targetWidth > curWidth + chunkWidth then
          table.insert(newVirtText, chunk)
      else
          chunkText = truncate(chunkText, targetWidth - curWidth)
          local hlGroup = chunk[2]
          table.insert(newVirtText, { chunkText, hlGroup })
          chunkWidth = vim.fn.strdisplaywidth(chunkText)
          -- str width returned from truncate() may less than 2nd argument, need padding
          if curWidth + chunkWidth < targetWidth then
              suffix = suffix .. (" "):rep(targetWidth - curWidth - chunkWidth)
          end
          break
      end
      curWidth = curWidth + chunkWidth
  end
  table.insert(newVirtText, { suffix, "MoreMsg" })
  return newVirtText
end

-- global handler
ufo.setup({
  fold_virt_text_handler = handler,
  provider_selector = function(bufnr, filetype)
      if vim.bo[bufnr].bt == "nofile" then
          return ""
      end
  end,
})

will work now, neogit init too late.

kevinhwang91 commented 2 years ago

maybe should stat a nofile buftype buffer before requesting foldingRange.

zbindenren commented 2 years ago
 provider_selector = function(bufnr, filetype)
            if vim.bo[bufnr].bt == 'nofile' then
                return ''
            end
        end,

I still get the error for neogit:

Error executing vim.schedule lua callback: UnhandledPromiseRejection with the reason:                                                                                                                                                                                                         
{ ["code"] = 0, ["message"] = stat /home/rz/repos/gitlab.com/linux/ces/ces-operator/NeogitStatus: no such file or director }                                                                                                                                                              
stack traceback:                                                                                                                                                                                                                                                                              
        ...re/nvim/site/pack/packer/start/nvim-ufo/lua/ufo/fold.lua:51: in function <Anonymous:40>

And I have the following ufo config:

local status_ok, ufo = pcall(require, "ufo")
if not status_ok then
    vim.notify("ufo (fold) plugin not found!")
    return
end

vim.wo.foldlevel = 99
vim.wo.foldenable = true

local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities.textDocument.foldingRange = {
    dynamicRegistration = false,
    lineFoldingOnly = true,
}

local handler = function(virtText, lnum, endLnum, width, truncate)
    local newVirtText = {}
    local suffix = ("  %d "):format(endLnum - lnum)
    local sufWidth = vim.fn.strdisplaywidth(suffix)
    local targetWidth = width - sufWidth
    local curWidth = 0
    for _, chunk in ipairs(virtText) do
        local chunkText = chunk[1]
        local chunkWidth = vim.fn.strdisplaywidth(chunkText)
        if targetWidth > curWidth + chunkWidth then
            table.insert(newVirtText, chunk)
        else
            chunkText = truncate(chunkText, targetWidth - curWidth)
            local hlGroup = chunk[2]
            table.insert(newVirtText, { chunkText, hlGroup })
            chunkWidth = vim.fn.strdisplaywidth(chunkText)
            -- str width returned from truncate() may less than 2nd argument, need padding
            if curWidth + chunkWidth < targetWidth then
                suffix = suffix .. (" "):rep(targetWidth - curWidth - chunkWidth)
            end
            break
        end
        curWidth = curWidth + chunkWidth
    end
    table.insert(newVirtText, { suffix, "MoreMsg" })
    return newVirtText
end

-- global handler
ufo.setup({
    fold_virt_text_handler = handler,
    provider_selector = function(bufnr, filetype)
        if vim.bo[bufnr].bt == "nofile" then
            return ""
        end
    end,
})

will work now, neogit init too late.

neogit works now. thanks. cool plugin.

wilriker commented 2 years ago

My issue is solved, so would be great to have it merged back into master branch. :+1:

kevinhwang91 commented 2 years ago

sure

kevinhwang91 commented 2 years ago

Have handled the buftype, no need to use provider_selector to handle it by yourself.