kevinhwang91 / nvim-ufo

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

Selected provider is always nil when session is recovered by auto-session #148

Closed tan-wei closed 10 months ago

tan-wei commented 10 months ago

Neovim version (nvim -v | head -n1)

NVIM v0.10.0-dev-900+g4a1fb37108

Operating system/version

archlinux

How to reproduce the issue

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

local status_ok, ufo = pcall(require, "ufo") if not status_ok then return end

local ftMap = { vim = "indent", python = { "indent" }, git = "", lua = { "lsp", "treesitter" }, rust = { "lsp", "treesitter" }, c = { "lsp", "treesitter" }, cpp = { "lsp", "treesitter" }, }

ufo.setup { open_fold_hl_timeout = 150, close_fold_kinds = { "imports", "comment" }, preview = { win_config = { border = { "", "─", "", "", "", "─", "", "" }, winhighlight = "Normal:Folded", winblend = 0, }, mappings = { scrollU = "", scrollD = "", jumpTop = "[", jumpBot = "]", }, }, provider_selector = function(bufnr, filetype, buftype) return ftMap[filetype] or { "treesitter", "indent" } end, }

vim.keymap.set("n", "zR", ufo.openAllFolds) vim.keymap.set("n", "zM", ufo.closeAllFolds) vim.keymap.set("n", "zr", ufo.openFoldsExceptKinds) vim.keymap.set("n", "zm", ufo.closeFoldsWith) -- closeAllFolds == closeFoldsWith(0) vim.keymap.set("n", "K", function() local winid = ufo.peekFoldedLinesUnderCursor() if not winid then vim.lsp.buf.hover() end end)

`nvim --clean +'so mini.lua'`

1. LSP rust-analyzer is installed correctly before
2. Treesitter is updated
3. `capabilities.textDocument.foldingRange` for LSP  is setted according to the document

### Expected behavior

ufo works well with Rust or C/C++ using LSP or Treesitter provider.

### Actual behavior

The provider is always nil, which means I can't do anything with ufo.
I find that the selected provider is always `nil` for my Rust project (actually, so does C/C++ project):

图片

rockyzhang24 commented 10 months ago

I tried your config above and couldn't reproduce (both rust and c++ work well). I believe it's not ufo's bug.

kevinhwang91 commented 10 months ago
  1. Enable ufo log: UFO_LOG=debug nvim;
  2. Get log from another terminal: tail -f ~/.cache/nvim/ufo.log;
tan-wei commented 10 months ago

Thanks. After several try with restart neovim and waiting for LSP complete parsing, it works. I think I should wait for it complete patiently.

Thanks for your reply. @kevinhwang91 's method is helpful in the future when I meet issue.

rockyzhang24 commented 10 months ago

Plugin like LSP progress indicator may be useful for a large repo.

tan-wei commented 10 months ago

Got it. So if LSP progess is not finished, the selected provider will be nil in nvim-ufo?

tan-wei commented 10 months ago

I find it behave strange on my other computer which runs on Windows. The configure is just the same, but after I open a Rust project, the log would be:

[23-08-22 12:38:42] [DEBUG] init.lua:128 : attach bufnr: 1
[23-08-22 12:38:43] [INFO] init.lua:89 : providers: { "treesitter", "indent" }
[23-08-22 12:38:44] [DEBUG] init.lua:128 : attach bufnr: 4
[23-08-22 12:38:44] [DEBUG] init.lua:128 : attach bufnr: 5
[23-08-22 12:38:45] [DEBUG] init.lua:53 : requestFoldingRange({ "treesitter", "indent" }, 1) has elapsed: 2199ms
[23-08-22 12:38:45] [DEBUG] init.lua:83 : update fold again for bufnr: 1

The buffer 4 is the opened Rust file, but it does not update fold, and after LSP finished, I can confirm that the selected provider is still nil: image

Seems that nvim-ufo does not update buffer 4 then. And I try to use command UfoEnable, UfoDisable, UfoEnableFold, UfoAttach, etc., no new log is logged.

kevinhwang91 commented 10 months ago

Got it. So if LSP progess is not finished, the selected provider will be nil in nvim-ufo?

https://github.com/kevinhwang91/nvim-ufo/blob/aebd36bf5447862671356fcd5d4a8708ce6c9470/lua/ufo/fold/init.lua#L90-L95

Only assign value in line 95. So nil is expected in the request.

I find it behave strange on my other computer which runs on Windows. The configure is just the same, but after I open a Rust project, the log would be:

From your log, no request for bufnr 4 which is attached, not sure if it is your complete log.

tan-wei commented 10 months ago

Yes, it is a complete log. I upload the whole one here, and you can see I've tried sever times by restart neovim with opening the same file: ufo.log

tan-wei commented 10 months ago

OK, I find a phenomenon:

So I think it is related to auto-session's recover session behave. When does nvim-ufo try to update the buffer?

Here is the latest log: ufo.log

kevinhwang91 commented 10 months ago

Look like BufDetach event is fired. Change log level and debug yourself, no idea about your ENV. UFO_LOG=trace nvim

tan-wei commented 10 months ago

OK, I'll try, hope I can find a workaround for this. Thanks.

But I still upload the trace log here: ufo.log

kevinhwang91 commented 10 months ago

Thanks, from the trace log I found a bug, please update the code, hope that commit should help you.

tan-wei commented 10 months ago

OK. Thanks again!