Skip to content

Commit

Permalink
Update nvim-ufo config to fix fold close on insert leave
Browse files Browse the repository at this point in the history
  • Loading branch information
Aetf committed Jul 27, 2022
1 parent 1578133 commit 1c4c718
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lua/ucw/keys.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local map = require('ucw.utils').map
local actions = require('ucw.keys.actions')

vim.g.mapleader = " "

Expand Down Expand Up @@ -29,6 +30,9 @@ end, { expr = true })
map('n', 'gS', [[<cmd>set opfunc=v:lua.require'ucw.keys.actions'.opfunc_textobj_go_start<cr>g@]], opts)
map('n', 'gE', [[<cmd>set opfunc=v:lua.require'ucw.keys.actions'.opfunc_textobj_go_end<cr>g@]], opts)

-- folding vs lsp
vim.keymap.set('n', 'K', actions.hoverK, { desc = "Hover over symbol" })

-- For mouse
map({'n', 'i', 'v'}, '<X2Mouse>', '<c-i>', opts)
map({'n', 'i', 'v'}, '<X1Mouse>', '<c-o>', opts)
Expand Down
12 changes: 12 additions & 0 deletions lua/ucw/keys/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,16 @@ function M.opfunc_textobj_go_end()
vim.cmd 'normal! `]'
end

-- Invoke fold preview or lsp preview
function M.hoverK()
local winid = nil
local ok, ufo = pcall(require, 'ufo')
if ok then
winid = ufo.peekFoldedLinesUnderCursor()
end
if not winid then
vim.lsp.buf.hover()
end
end

return M
1 change: 0 additions & 1 deletion lua/ucw/lsp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ local function on_attach(client, bufnr)
H = { [[<cmd>lua vim.lsp.declaration()<cr>]], "Go to declaration"},
r = { [[<cmd>Telescope lsp_references<cr>]], "Find references"},
},
K = { '<cmd>lua vim.lsp.buf.hover()<cr>', "Hover over symbol" },
['<c-k>'] = { '<cmd>lua vim.diagnostic.open_float()<cr>', "Show diagnostics on the current line" },
['<M-S-r>'] = { [[<cmd>lua vim.lsp.buf.rename()<cr>]], "Rename the symbol under cursor" },
}, { buffer = bufnr })
Expand Down
19 changes: 18 additions & 1 deletion lua/ucw/units/thirdparty/ufo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ local function ufo_color()
end

function M.config()
-- UFO uses manual folding that unforunately doesn't play well with small foldlevel
-- TL'DR is vim will immediately close all folds to foldlevel whenever manual folding
-- is updated, which UFO does a lot, notably at InsertLeave.
-- See https://github.com/kevinhwang91/nvim-ufo/issues/7
vim.opt.foldlevel = 99
vim.opt.foldlevelstart = 99 -- useful when switching from a window with small foldlevel

-- Using ufo provider needs remap 'zR' and 'zM' to not let them change foldlevel
vim.keymap.set('n', 'zR', require('ufo').openAllFolds)
vim.keymap.set('n', 'zM', require('ufo').closeAllFolds)

-- Use a command to emulate foldlevel on bufenter
-- FUTURE: not possible yet, maybe after neovim#19155
-- au.group('ufo-foldlevel-emu', {
-- { 'BufEnter', '*', function() end},
-- })

-- tell any server that we support foldingRange
require('ucw.lsp').register_on_server_ready('.*', function(server, opts)
opts.capabilities = opts.capabilities or vim.lsp.protocol.make_client_capabilities()
Expand All @@ -66,7 +83,7 @@ function M.config()
-- timeout in ms to highlight the range when opening the folded line, 0 to disable
-- keep this the same as highlight on yank
open_fold_hl_timeout = 200,
fold_virt_text_handler = fold_virt_text_handler
fold_virt_text_handler = fold_virt_text_handler,
}

ufo_color()
Expand Down

0 comments on commit 1c4c718

Please sign in to comment.