kevinhwang91 / nvim-ufo

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

Disable UFO in certain buffer types #33

Open hisbaan opened 2 years ago

hisbaan commented 2 years ago

Feature description

Using something like a disabled table in the configuration function, one should be able to disable nvim-ufo in certain buffer types. An example of where this is useful is .org documents. The nvim-orgmode/orgmode plugin has built in fold support, but nvim-ufo overrides that with incorrect behaviour (intent based). The ability to disable it in certain buffer types would allow for the best of both worlds.

One distinction is that when switching to another buffer of a different type, nvim-ufo should be able to work again.

Describe the solution you'd like

Something similar to how LSP servers handle enabling and disabling filetypes would be best. An example configuration would be something like:

require('ufo').setup({
    open_fold_h1_timeout = 100,
    disabled = { 'org' },
})

Additional context

No response

kevinhwang91 commented 2 years ago

Not support for now, as a workaround.

  1. Add au FileType org lua require('ufo').detach() or au FileType org UfoDetach
  2. Add vim.cmd('UfoDetach') under ~/.config/nvim/ftplugin/org.vim or ~/.config/nvim/ftplugin/org.lua
  3. Correct ufo virt text for org buffer: https://github.com/kevinhwang91/nvim-ufo/blob/9331576ce02b41c687309f8217eb6830e0a2ae19/doc/example.lua#L68-L73

If there're many buffers that want to detach ufo, ufo will add an option to be convenient for the users.

Leave this issue for others.

akinsho commented 2 years ago

@kevinhwang91 the README makes it sound like returning '' from provider_selector should disable folds but that doesn't work. Is that something that should be working.


  local filetypes = { org = '' }

  ufo.setup({
    fold_virt_text_handler = handler,
    provider_selector = function(_, ft)
      return filetypes[ft] or { 'treesitter', 'indent' }
    end,
  })
kevinhwang91 commented 2 years ago

provider_selector return '' only disables the providers to get folds. Disable virtual text must run UfoDetach

Edit: Return a {} for fold_virt_text_handler or handler in require('ufo').setFoldVirtTextHandler(bufnr, handler) will only clear the color folded line.

jghauser commented 2 years ago

Is this still working for others? It used to work for me, but not anymore... I need to manually do UfoDetach once the file is opened.

kevinhwang91 commented 2 years ago

It's broken after recently refactoring, I will fix it tomorrow.

Mange commented 1 year ago

Another case where this might be useful: When working with buffers that aren't even files, like opened preview windows like popups, etc.

I get UFO folding indicators in symbols-outline, for example.

ttytm commented 1 year ago
local ft = {
   vim = "indent",
   python = { "indent" },
}

local ignored_filetypes = { "markdown", "git", "NeogitStatus" }
for _, key in ipairs(ignored_filetypes) do
   ignored_filetypes[key] = true
   ft[key] = ""
end

require('ufo').setup({
-- ...
})

Inserting ignored filetypes from a dedicated list is what I do, to then reuse them in the keymaps. E.g., allows to keep ufos foldstyle for markdown but use preservim/vim-markdown's foldmethod.

nx.au({
   "BufEnter",
   callback = function()
      if ignored_filetypes[vim.bo.ft] or vim.bo.bt ~= "" then return end -- <--

      nx.map({
         { "zR", ufo.openAllFolds },
         { "zM", ufo.closeAllFolds },
         { "zr", ufo.openFoldsExceptKinds },
         { "zm", ufo.closeFoldsWith },
         {
            "K",
            function()
               local winid = ufo.peekFoldedLinesUnderCursor()
               if not winid then vim.lsp.buf.hover() end
            end,
         },
      }, { buffer = true })
   end,
})
liujoey commented 1 year ago

would love to disable ufo on all neogit* buffers

ashb commented 1 year ago

My approach (I just wanted to turn off folding altogether, rather than disabling UFO specifically):

vim.api.nvim_create_autocmd("FileType", {
  pattern = { "nvcheatsheet", "neo-tree" },
  callback = function()
    require("ufo").detach()
    vim.opt_local.foldenable = false
  end
})

Edit: seems that due to UFO's events you also need to do a detach to make it work.

AAlcainaFlexidao commented 1 year ago

Another case where this might be useful: When working with buffers that aren't even files, like opened preview windows like popups, etc.

I get UFO folding indicators in symbols-outline, for example.

I have the same issue with Neo-tree.nvim

cloud303-cholden commented 1 year ago

@ashb Works great for me, thanks!

waynerv commented 1 year ago

My approach (I just wanted to turn off folding altogether, rather than disabling UFO specifically):

vim.api.nvim_create_autocmd("FileType", {
  pattern = { "nvcheatsheet", "neo-tree" },
  callback = function()
    require("ufo").detach()
    vim.opt_local.foldenable = false
  end
})

Edit: seems that due to UFO's events you also need to do a detach to make it work.

You might also want to disable the extra fold column by append this to callback function:

vim.wo.foldcolumn = "0"
karamanliev commented 5 months ago

This doesn't work for me for Neogit.

My approach (I just wanted to turn off folding altogether, rather than disabling UFO specifically):

vim.api.nvim_create_autocmd("FileType", {
  pattern = { "nvcheatsheet", "neo-tree" },
  callback = function()
    require("ufo").detach()
    vim.opt_local.foldenable = false
  end
})

Edit: seems that due to UFO's events you also need to do a detach to make it work.

It works by adding this, but then if you open another buffer which is a proper file it doesn't have folds anymore...

vim.wo.foldcolumn = "0"

Adding vim.opt_local.foldcolumn = '0' instead actually solved it for me:

    vim.api.nvim_create_autocmd('FileType', {
        pattern = { 'NeogitStatus' },
        callback = function()
          require('ufo').detach()
          vim.opt_local.foldenable = false
          vim.opt_local.foldcolumn = '0'
        end,
      })
    end,
devsunb commented 5 months ago

My approach (I just wanted to turn off folding altogether, rather than disabling UFO specifically):

vim.api.nvim_create_autocmd("FileType", {
  pattern = { "nvcheatsheet", "neo-tree" },
  callback = function()
    require("ufo").detach()
    vim.opt_local.foldenable = false
  end
})

Edit: seems that due to UFO's events you also need to do a detach to make it work.

You might also want to disable the extra fold column by append this to callback function:

vim.wo.foldcolumn = "0"

With statuscol, you must also set the ft_ignore option.

require('statuscol').setup {
  ft_ignore = { 'neo-tree' },
  ...
}