From 3eee006f5583c507a41d367cdfd15977109e4914 Mon Sep 17 00:00:00 2001 From: Yuan Chiu Date: Thu, 8 May 2025 16:37:29 +0800 Subject: [PATCH] =?UTF-8?q?feat=20nvim:=20fold=E7=A8=8B=E5=BC=8F=E7=A2=BC?= =?UTF-8?q?=E6=8A=98=E7=96=8A=E5=8A=9F=E8=83=BD(=E8=A3=9C=E4=B8=8A?= =?UTF-8?q?=E6=9B=B4=E5=A4=9A=E5=AE=98=E6=96=B9=E7=AF=84=E4=BE=8B)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exact_plugins/exact_editor/fold.lua | 82 ++++++++++++++----- 1 file changed, 60 insertions(+), 22 deletions(-) diff --git a/dot_config/nvim/exact_lua/exact_plugins/exact_editor/fold.lua b/dot_config/nvim/exact_lua/exact_plugins/exact_editor/fold.lua index 8ab0634..22ff129 100644 --- a/dot_config/nvim/exact_lua/exact_plugins/exact_editor/fold.lua +++ b/dot_config/nvim/exact_lua/exact_plugins/exact_editor/fold.lua @@ -1,3 +1,9 @@ +local ftMap = { + vim = 'indent', + python = {'indent'}, + git = '' +} + return { { 'kevinhwang91/nvim-ufo', @@ -6,29 +12,32 @@ return { }, event = "VeryLazy", opts = { - open_fold_hl_timeout = 400, - provider_selector = function(bufnr, filetype, buftype) - return {'treesitter', 'indent'} - end, - close_fold_kinds_for_ft = { - default = {'imports', 'comment'} - }, - fold_virt_text_handler = nil, - enable_get_fold_virt_text = false, - preview = { - win_config = { - border = 'rounded', - winblend = 12, - winhighlight = 'Normal:Normal', - maxheight = 20 + open_fold_hl_timeout = 150, + close_fold_kinds_for_ft = { + default = {'imports', 'comment'}, + json = {'array'}, + c = {'comment', 'region'} }, - mappings = { - -- 這裡你可以加上對應的快捷鍵,如: - -- scrollB = '', - -- scrollF = '', - -- 或留空(等你有需要再補) - } - } + preview = { + win_config = { + border = {'', '─', '', '', '', '─', '', ''}, + winhighlight = 'Normal:Folded', + winblend = 0 + }, + mappings = { + scrollU = '', + scrollD = '', + jumpTop = '[', + jumpBot = ']' + } + }, + provider_selector = function(bufnr, filetype, buftype) + -- if you prefer treesitter provider rather than lsp, + -- return ftMap[filetype] or {'treesitter', 'indent'} + return ftMap[filetype] + + -- refer to ./doc/example.lua for detail + end }, init = function() vim.o.foldcolumn = '1' -- '0' is not bad @@ -37,6 +46,35 @@ return { vim.o.foldenable = true end, config = function(_, opts) + local handler = function(virtText, lnum, endLnum, width, truncate) + local newVirtText = {} + local suffix = (' 󰁂 %d '):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) + -- str width returned from truncate() may less than 2nd argument, need padding + 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 + opts["fold_virt_text_handler"] = handler + -- ufo官方範例: -- Using ufo provider need remap `zR` and `zM`. If Neovim is 0.6.1, remap yourself vim.keymap.set('n', 'zR', require('ufo').openAllFolds)