nvim-ufo icon indicating copy to clipboard operation
nvim-ufo copied to clipboard

Disable UFO in certain buffer types

Open hisbaan opened this issue 2 years ago • 12 comments

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

hisbaan avatar Jul 01 '22 17:07 hisbaan

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.

kevinhwang91 avatar Jul 02 '22 04:07 kevinhwang91

@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,
  })

akinsho avatar Jul 06 '22 10:07 akinsho

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.

kevinhwang91 avatar Jul 06 '22 11:07 kevinhwang91

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.

jghauser avatar Jul 20 '22 18:07 jghauser

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

kevinhwang91 avatar Jul 20 '22 19:07 kevinhwang91

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.

Mange avatar Feb 02 '23 21:02 Mange

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,
})

ttytm avatar Mar 21 '23 15:03 ttytm

would love to disable ufo on all neogit* buffers

liujoey avatar Jul 26 '23 19:07 liujoey

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.

ashb avatar Aug 03 '23 21:08 ashb

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

AAlcainaFlexidao avatar Sep 11 '23 08:09 AAlcainaFlexidao

@ashb Works great for me, thanks!

cloud303-cholden avatar Sep 25 '23 16:09 cloud303-cholden

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"

waynerv avatar Sep 26 '23 03:09 waynerv