kevinhwang91 / nvim-ufo

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

Cache buffer folds and re-apply when event is triggered #133

Closed rohit-kumar-j closed 1 year ago

rohit-kumar-j commented 1 year ago

Feature description

A way to cache the folds and re-apply them upon an event is triggered like in an autocmd.

Describe the solution you'd like

Perhaps this is/needs to be a nvim core feature, but if there can be a function like ufo.refold(), which can be called for all the buffers that are being saved/written to.

(Maybe this is a very specific request 😅)

Additional context

the function vim.lsp.buf.format(), when we do BufWritePost for auto-format before save is so annoying as it reopens folds upon format. A potential solution can be to cache the folds, which can be done upon BufWritePre and re-apply can be done upon BufWritePost. (Hope that is the correct order of events 😅)

kevinhwang91 commented 1 year ago

https://github.com/kevinhwang91/nvim-ufo/blob/45aede6f5c4981156df3ddeeae01c4290e43e104/doc/example.lua#L113-L125 Modify above snippet inside your event.

mBaratta96 commented 1 year ago

@rohit-kumar-j have you managed to solve this issue? Having problems integrating the snippet in null-ls, and wondering what your solution has been eventually.

rohit-kumar-j commented 1 year ago

@mBaratta96, unfortunately the above snippet closes all the folds. Instead I put the following my settings.

This works for me, but is somewhat buggy: (Works as expected in lua files, but not in c++ (clangd lsp server))

init.lua:

require(user.settings)
require(user.plugins) -- Plugins after settings.

Do not source this snippet after plugins, source it before plugins. I don't know why it doesn't work well if you source if after the plugins

Settings.lua:

_G.User = {}

User.autoformat = false

vim.api.nvim_create_autocmd({ 'BufWritePre' }, {
    group = vim.api.nvim_create_augroup('User_Group', { clear = true }),
    pattern = "*",
    callback = function()
        if User.autoformat == true then
            if vim.o.filetype == 'help' then
                return
            else
                vim.cmd([[ :mkview ]])
                vim.lsp.buf.format()
                -- print("Buf Write Pre! autoFormatting") -- This message will not be seen by default
            end
        else
            -- print("Saving file... Auto format disabled!")
            return
        end
    end
})

vim.api.nvim_create_autocmd({ 'BufWritePost' }, {
    group = vim.api.nvim_create_augroup('User_Group', { clear = false }),
    pattern = "*",
    callback = function()
        if User.autoformat == true then
            if vim.o.filetype == 'help' then
                return
            else
                vim.cmd([[ :loadview ]])
                -- print("Buf Write Post! autoFormatting")
            end
        else
            -- print("Saving file... Auto format disabled!")
            return
        end
    end
})

function _G.toggleAutoformat()
    if User.autoformat == true then
        User.autoformat = not User.autoformat
        print("Disabled format on save")
    else
        User.autoformat = not User.autoformat
        print("Enabled format on save")
    end
end

-- Toggle Format on save
vim.api.nvim_set_keymap('n', '<leader>F', ':lua toggleAutoformat()<CR>',
    { noremap = true, silent = true, desc = "Toggle Format on Save" })

-- Fold close: Closes only the 1st layer of folds. Not all of them :D
vim.api.nvim_set_keymap('n', ',f', ':%foldclose<CR>',
    { noremap = true, silent = true, desc = "Fold Close" })