diff --git a/lua/ucw/keys.lua b/lua/ucw/keys.lua index 969e424..fe12150 100644 --- a/lua/ucw/keys.lua +++ b/lua/ucw/keys.lua @@ -1,4 +1,5 @@ local map = require('ucw.utils').map +local actions = require('ucw.keys.actions') vim.g.mapleader = " " @@ -29,6 +30,9 @@ end, { expr = true }) map('n', 'gS', [[set opfunc=v:lua.require'ucw.keys.actions'.opfunc_textobj_go_startg@]], opts) map('n', 'gE', [[set opfunc=v:lua.require'ucw.keys.actions'.opfunc_textobj_go_endg@]], opts) +-- folding vs lsp +vim.keymap.set('n', 'K', actions.hoverK, { desc = "Hover over symbol" }) + -- For mouse map({'n', 'i', 'v'}, '', '', opts) map({'n', 'i', 'v'}, '', '', opts) diff --git a/lua/ucw/keys/actions.lua b/lua/ucw/keys/actions.lua index fb9191e..c894810 100644 --- a/lua/ucw/keys/actions.lua +++ b/lua/ucw/keys/actions.lua @@ -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 diff --git a/lua/ucw/lsp/init.lua b/lua/ucw/lsp/init.lua index c99e6c8..2fd7069 100644 --- a/lua/ucw/lsp/init.lua +++ b/lua/ucw/lsp/init.lua @@ -48,7 +48,6 @@ local function on_attach(client, bufnr) H = { [[lua vim.lsp.declaration()]], "Go to declaration"}, r = { [[Telescope lsp_references]], "Find references"}, }, - K = { 'lua vim.lsp.buf.hover()', "Hover over symbol" }, [''] = { 'lua vim.diagnostic.open_float()', "Show diagnostics on the current line" }, [''] = { [[lua vim.lsp.buf.rename()]], "Rename the symbol under cursor" }, }, { buffer = bufnr }) diff --git a/lua/ucw/units/thirdparty/ufo.lua b/lua/ucw/units/thirdparty/ufo.lua index 60fdef5..0d0b861 100644 --- a/lua/ucw/units/thirdparty/ufo.lua +++ b/lua/ucw/units/thirdparty/ufo.lua @@ -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() @@ -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()