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

Telescope from Alpha.nvim enters files in insert mode when Lazy loading nvim-ufo #179

Closed ravibrock closed 7 months ago

ravibrock commented 7 months ago

Neovim version (nvim -v | head -n1)

NVIM v0.9.4

Operating system/version

MacOS 14.1.1

How to reproduce the issue

Lazy.nvim:

{
        "kevinhwang91/nvim-ufo",
        event = { "BufReadPost", "BufNewFile" },
        dependencies = {
            "kevinhwang91/promise-async",
            "neovim/nvim-lspconfig",
        },
        config = function()
            require(prefix .. "nvim-ufo")
        end,
},
{
        "goolord/alpha-nvim",
        event = "VimEnter",
        opts = function()
            return require(prefix .. "alpha-nvim").opts()
        end,
        config = function(_, dashboard)
            require(prefix .. "alpha-nvim").config(_, dashboard)
        end,
}

Nvim-ufo config:

vim.o.foldcolumn = "1"
vim.o.foldlevel = 99
vim.o.foldlevelstart = 99
vim.o.foldenable = true

local M = {}

function M.fold_col()
    local Cfold_info = M.ffi.C.fold_info
    local wp = M.ffi.C.find_window_by_handle(vim.g.statusline_winid, M.ffi.new("Error"))
    local foldinfo = Cfold_info(wp, vim.v.lnum)
    if foldinfo.start == vim.v.lnum then
        if vim.fn.foldclosed(vim.v.lnum) ~= -1 then
            return [[%#StatusColumnFoldClosed#]] .. [[▶]] .. [[%*]]
        end
    end
    return ""
end

vim.keymap.set("n", "zR", require("ufo").openAllFolds, { desc = "Open all folds" })
vim.keymap.set("n", "zM", require("ufo").closeAllFolds, { desc = "Close all folds" })

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

local language_servers = require("lspconfig").util.available_servers()
for _, ls in ipairs(language_servers) do
    require("lspconfig")[ls].setup({ capabilities = capabilities })
end

require("ufo").setup({
    provider_selector = function(_, ft, _)
        local lspWithOutFolding = { "markdown", "sh", "css", "html", "python" }
        if vim.tbl_contains(lspWithOutFolding, ft) then return { "treesitter", "indent" } end
        return { "lsp", "indent" }
    end,
})

Alpha config:

local all = {}
all.opts = function()
    local dashboard = require("alpha.themes.dashboard")
    dashboard.section.header.val = require("ascii").art.text.neovim.dos_rebel
    dashboard.section.buttons.val = {
        dashboard.button("n", " " .. " New file", "<CMD>ene <BAR> startinsert<CR>"),
        dashboard.button("r", " " .. " Recent files", "<CMD>Telescope oldfiles<CR>"),
        dashboard.button("f", "󰍉 " .. " Find file", "<CMD>Telescope find_files<CR>"),
        dashboard.button("g", " " .. " Find text", "<CMD>Telescope live_grep<CR>"),
        dashboard.button("s", " " .. " Restore Session", "<CMD>SessionLoadLast<CR>"),
        dashboard.button("c", " " .. " Config", "<CMD>e $MYVIMRC<CR>"),
        dashboard.button("m", "󱊍 " .. " Mason", "<CMD>Mason<CR>"),
        dashboard.button("l", "󰒲 " .. " Lazy", "<CMD>Lazy<CR>"),
        dashboard.button("q", " " .. " Quit", "<CMD>qa<CR>"),
    }
    for _, button in ipairs(dashboard.section.buttons.val) do
        button.opts.hl = "AlphaButtons"
        button.opts.hl_shortcut = "AlphaShortcut"
    end
    dashboard.section.header.opts.hl = "AlphaHeader"
    dashboard.section.buttons.opts.hl = "AlphaButtons"
    dashboard.section.footer.opts.hl = "AlphaFooter"
    dashboard.opts.layout[1].val = 8
    return dashboard
end
all.config = function(_, dashboard)
    if vim.o.filetype == "lazy" then
        vim.cmd.close()
        vim.api.nvim_create_autocmd("User", {
            pattern = "AlphaReady",
            callback = function()
                require("lazy").show()
            end,
        })
    end
    require("alpha").setup(dashboard.opts)
    vim.api.nvim_create_autocmd("User", {
        pattern = "LazyVimStarted",
        callback = function()
            local stats = require("lazy").stats()
            local ms = (math.floor(stats.startuptime * 100 + 0.5) / 100)
            local version = vim.version()
            local v = version.major .. "." .. version.minor .. "." .. version.patch
            dashboard.section.footer.val = "⚡ Neovim v" .. v .. " loaded " .. stats.count .. " plugins in " .. ms .. "ms"
            pcall(vim.cmd.AlphaRedraw)
        end,
    })
end
return all

I install the plugins with Lazy.nvim as shown above, and then they each source the code I have listed below for configuration.

Expected behavior

I would expect to enter buffers from telescope in normal mode rather than in insert mode. I would also expect :UfoInspect to say

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

but as I said, I could be mistaken on this.

These aren't huge issues, so it's not a big rush. Any help is appreciated. Thank you!

Actual behavior

I noticed that when I type, for example, r on my Alpha dashboard (to search recent files as indicated in the config), it enters the selected recent file's buffer in insert mode when I'm lazy-loading nvim-ufo on BufReadPost and BufNewFile but not when I load it eagerly.

I also noticed, when debugging this, that :UfoInspect always says

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

when on a file that should be using a LSP. I would think that it should be Selected provider: lsp but I could be mistaken. My config for the LSP is in the example as well.

rockyzhang24 commented 7 months ago

Not sure if it is related to the old bug (https://github.com/nvim-telescope/telescope.nvim/issues/2501) reported in telescope.nvim. At least it can be a reference. Also, this one https://github.com/nvim-telescope/telescope.nvim/issues/2766 maybe helpful to track.

ravibrock commented 7 months ago

Yep, that worked to fix it. Thank you for the help!

Would you be able to say if that is expected behavior for the lsp folding option? I can also open another issue for that if it would work better, but it seemed very minor.

rockyzhang24 commented 7 months ago

Selected provider should show lsp instead of indent. I think the issue is caused by the lazy loading of lazy.nvim. I don't use lazy.nvim so I have no hints. Seems it's not attached normally. Probably the event should be BufRead instead of BufReadPost. Hmm, I don't know. Check this https://github.com/kevinhwang91/nvim-ufo/issues/117 that might be useful.

ravibrock commented 7 months ago

Ok, I got it working now. Thank you for all the help! Turns out the issue was how the language servers were being configured - I fixed it in my mason-lspconfig setup.