From 18d96bf9a83cb56b3596c7ed4f62affdf761ed49 Mon Sep 17 00:00:00 2001 From: fm39hz-laptop Date: Wed, 30 Oct 2024 14:02:51 +0700 Subject: [PATCH] feat: implement ufo properly --- lua/config/options.lua | 6 ++++ lua/plugins/visualize/fold.lua | 57 +++++++++++++++++++++++++++++----- 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/lua/config/options.lua b/lua/config/options.lua index c10d5bd..c6be94f 100644 --- a/lua/config/options.lua +++ b/lua/config/options.lua @@ -5,3 +5,9 @@ vim.opt.swapfile = false vim.opt.backup = false vim.opt.writebackup = false vim.opt.spelllang = {} + +-- fold config +vim.o.foldcolumn = "1" -- '0' is not bad +vim.o.foldlevel = 99 -- Using ufo provider need a large value, feel free to decrease the value +vim.o.foldlevelstart = 99 +vim.o.foldenable = true diff --git a/lua/plugins/visualize/fold.lua b/lua/plugins/visualize/fold.lua index 49c7025..896a720 100644 --- a/lua/plugins/visualize/fold.lua +++ b/lua/plugins/visualize/fold.lua @@ -1,10 +1,51 @@ +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 return { - "kevinhwang91/nvim-ufo", - dependencies = "kevinhwang91/promise-async", - event = "BufReadPre", - opts = { - provider_selector = function(bufnr, filetype, buftype) - return { "treesitter", "indent" } - end, - }, + "kevinhwang91/nvim-ufo", + dependencies = "kevinhwang91/promise-async", + event = "BufReadPre", + config = function() + local capabilities = vim.lsp.protocol.make_client_capabilities() + capabilities.textDocument.foldingRange = { + dynamicRegistration = false, + lineFoldingOnly = true, + } + local language_servers = require("lspconfig").util.available_servers() + for _, ls in ipairs(language_servers) do + require("lspconfig")[ls].setup({ + capabilities = capabilities, + }) + end + end, + opts = { + fold_virt_text_handler = handler, + provider_selector = function(bufnr, filetype, buftype) + return { "treesitter", "indent" } + end, + }, }