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

Fold not working in files open with Telescope #39

Closed anuvyklack closed 2 years ago

anuvyklack commented 2 years ago

Neovim version (nvim -v | head -n1)

NVIM 0.7.0

Operating system/version

Fedora 36

How to reproduce the issue

  1. Open any lua file in nvim;
  2. Folds working
  3. :Telescope find_files or vim builtin gf
  4. Pick any other lua file
  5. Folds not applied

Expected behavior

Folds should work in the buffer open with Telescope also.

Actual behavior

Folds not applied. UfoInspect returns

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

UfoEnableFold has no effect.

If open file with :edit path/to/file folds are working.

kevinhwang91 commented 2 years ago

I can't reproduce it, please provide a minimal config.

anuvyklack commented 2 years ago

I reproduced this problem with very minimal init.lua:

local fn = vim.fn
local install_path = fn.stdpath('data')..'/site/pack/packer/start/packer.nvim'
if fn.empty(fn.glob(install_path)) > 0 then
   packer_bootstrap = fn.system({'git', 'clone', '--depth', '1', 'https://github.com/wbthomason/packer.nvim', install_path})
end

return require('packer').startup(function(use)
   use 'wbthomason/packer.nvim'

   use { 'neovim/nvim-lspconfig', as = 'lspconfig',
      requires = { 'williamboman/nvim-lsp-installer', 'folke/lua-dev.nvim' },
      config = function()
         require('nvim-lsp-installer').setup {}

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

         require('lspconfig').sumneko_lua.setup({
            capabilities = create_capabilities()
         })
      end
   }

   use { 'nvim-telescope/telescope.nvim',
      requires = { 'nvim-lua/plenary.nvim' },
      config = function()
         require('telescope').setup{}
      end
   }

   use { 'kevinhwang91/nvim-ufo',
      requires = 'kevinhwang91/promise-async',
      config = function()
         vim.o.foldcolumn = '1'
         vim.o.foldlevel = 99
         vim.o.foldlevelstart = -1

         local ufo = require('ufo')
         ufo.setup()
         vim.keymap.set('n', 'zR', ufo.openAllFolds)
         vim.keymap.set('n', 'zM', ufo.closeAllFolds)
      end
   }

   if packer_bootstrap then
      require('packer').sync()
   end
end)

-- vim: ts=3 sts=3
kevinhwang91 commented 2 years ago

This is not a minal config. Please using like this:


vim.o.packpath = '~/.local/share/nvim/site'
vim.o.foldcolumn = '1'
vim.o.foldlevel = 99
vim.o.foldlevelstart = -1

-- promise-async under start
-- nvim-lspconfig under opt
-- plenary.nvim under opt
-- telescope.nvim under opt
-- nvim-ufo under opt
vim.cmd('pa nvim-lspconfig')
vim.cmd('pa plenary.nvim')
vim.cmd('pa telescope.nvim')
vim.cmd('pa nvim-ufo')

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

require('lspconfig').sumneko_lua.setup({
    capabilities = create_capabilities(),
    cmd = {'/usr/bin/lua-language-server'},
    on_attach = function(...)
        print('attach', ...)
    end
})
local ufo = require('ufo')
ufo.setup()
vim.keymap.set('n', 'zR', ufo.openAllFolds)
vim.keymap.set('n', 'zM', ufo.closeAllFolds)

nvim --clean -u mini.lua your_file.lua

anuvyklack commented 2 years ago

It happens to sumneko-lua when you open with telescope Lua file that not belongs to any project and opens in single file mode.

kevinhwang91 commented 2 years ago

I'm not familiar with single file mode, could you provide lsp config and a directory tree to explain?

ranjithshegde commented 2 years ago

This is a known telescope problem. Nothing to do with this plugin.

If you do not want to wait on an official fix, you could try setting custom picker maps like this

local foldMaps = function(_)
    require("telescope.actions.set").select:enhance {
        post = function()
            vim.cmd ":normal! zx"
        end,
    }
    return true
end

require("telescope").setup {
    pickers = {
        find_files = { attach_mappings = foldMaps },
        old_files = { attach_mappings = foldMaps},
    }
}
anuvyklack commented 2 years ago

No, it is not this problem. I am aware about it and even suggested another temporary fix and this problem is not that, zx does not fix it.

anuvyklack commented 2 years ago

I'm not familiar with single file mode, could you provide lsp config and a directory tree to explain?

The single file support for Sumneko_lua turn on by default in lspconfig plugin. So you need just open from telescope, any Lua file that doesn't have any of the next files somewhere downstairs in the file tree:

".luarc.json", ".luacheckrc", ".stylua.toml", "selene.toml", ".git"

You can check if lsp is in single file mode with :LspInfo command

anuvyklack commented 2 years ago

I found another case where this problem appears:

I do not appear folds, however UfoInspect says that ufo is attached.

kevinhwang91 commented 2 years ago

Can't reproduce all of the cases you said even LspInfo tell me root directory: Running in single file mode.

kevinhwang91 commented 2 years ago

ping me if has new info