nvim/lua/plugins/ufo.lua

86 lines
3.1 KiB
Lua

-- Phase 6.7: nvim-ufo for better folding with Treesitter and LSP support
return {
'kevinhwang91/nvim-ufo',
dependencies = {
'kevinhwang91/promise-async',
},
event = { 'BufReadPost', 'BufNewFile' },
keys = {
{ 'zR', function() require('ufo').openAllFolds() end, desc = 'UFO: Open all folds' },
{ 'zM', function() require('ufo').closeAllFolds() end, desc = 'UFO: Close all folds' },
{ 'zr', function() require('ufo').openFoldsExceptKinds() end, desc = 'UFO: Open folds except kinds' },
{ 'zm', function() require('ufo').closeFoldsWith() end, desc = 'UFO: Close folds with' },
{ 'K', function()
local winid = require('ufo').peekFoldedLinesUnderCursor()
if not winid then
-- Fall back to LSP hover if not on a fold
vim.lsp.buf.hover()
end
end, desc = 'UFO: Peek fold or LSP hover' },
},
opts = {
provider_selector = function(bufnr, filetype, buftype)
-- Use Treesitter for supported languages, LSP for others, indent as fallback
return { 'treesitter', 'indent' }
end,
-- Open folds when searching
open_fold_hl_timeout = 150,
close_fold_kinds_for_ft = {
default = { 'imports', 'comment' },
},
preview = {
win_config = {
border = { '', '', '', '', '', '', '', '' },
winhighlight = 'Normal:Folded',
winblend = 0,
},
mappings = {
scrollU = '<C-u>',
scrollD = '<C-d>',
jumpTop = '[',
jumpBot = ']',
},
},
-- Custom fold text showing line count
fold_virt_text_handler = function(virtText, lnum, endLnum, width, truncate)
local newVirtText = {}
local suffix = (' %d lines '):format(endLnum - lnum)
local sufWidth = vim.fn.strdisplaywidth(suffix)
local targetWidth = width - sufWidth
local curWidth = 0
for _, chunk in ipairs(virtText) do
local chunkText = chunk[1]
local chunkWidth = vim.fn.strdisplaywidth(chunkText)
if targetWidth > curWidth + chunkWidth then
table.insert(newVirtText, chunk)
else
chunkText = truncate(chunkText, targetWidth - curWidth)
local hlGroup = chunk[2]
table.insert(newVirtText, {chunkText, hlGroup})
chunkWidth = vim.fn.strdisplaywidth(chunkText)
if curWidth + chunkWidth < targetWidth then
suffix = suffix .. (' '):rep(targetWidth - curWidth - chunkWidth)
end
break
end
curWidth = curWidth + chunkWidth
end
table.insert(newVirtText, {suffix, 'MoreMsg'})
return newVirtText
end,
},
config = function(_, opts)
-- Set fold settings for nvim-ufo
vim.o.foldcolumn = '0' -- Hide fold column (we don't need it)
vim.o.foldlevel = 99 -- Open all folds by default
vim.o.foldlevelstart = 99 -- Open all folds when opening a file
vim.o.foldenable = true -- Enable folding
-- Use simple characters for fold indicators (though foldcolumn is hidden)
vim.o.fillchars = [[eob: ,fold: ,foldopen:▼,foldsep: ,foldclose:▶]]
require('ufo').setup(opts)
end,
}