Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable UFO in certain buffer types #33

Open
hisbaan opened this issue Jul 1, 2022 · 14 comments
Open

Disable UFO in certain buffer types #33

hisbaan opened this issue Jul 1, 2022 · 14 comments
Labels
enhancement New feature or request

Comments

@hisbaan
Copy link

hisbaan commented Jul 1, 2022

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 hisbaan added the enhancement New feature or request label Jul 1, 2022
@hisbaan hisbaan changed the title Disable UFO in certain buffers Disable UFO in certain buffer types Jul 1, 2022
@kevinhwang91
Copy link
Owner

kevinhwang91 commented Jul 2, 2022

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:

    nvim-ufo/doc/example.lua

    Lines 68 to 73 in 9331576

    local function customizeBufFoldText()
    -- buffer scope handler
    -- will override global handler if it is existed
    local bufnr = vim.api.nvim_get_current_buf()
    require('ufo').setFoldVirtTextHandler(bufnr, handler)
    end

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
Copy link

akinsho commented Jul 6, 2022

@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
Copy link
Owner

kevinhwang91 commented Jul 6, 2022

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

This comment was marked as resolved.

@kevinhwang91

This comment was marked as resolved.

@Mange
Copy link

Mange commented Feb 2, 2023

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
Copy link

ttytm commented Mar 21, 2023

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
Copy link

liujoey commented Jul 26, 2023

would love to disable ufo on all neogit* buffers

@ashb
Copy link

ashb commented Aug 3, 2023

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
Copy link

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
Copy link

@ashb Works great for me, thanks!

@waynerv
Copy link

waynerv commented Sep 26, 2023

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
Copy link

karamanliev commented May 30, 2024

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
Copy link

devsunb commented Jun 20, 2024

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' },
  ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests