Skip to content

Commit

Permalink
better ufo
Browse files Browse the repository at this point in the history
  • Loading branch information
mosheavni committed Aug 17, 2024
1 parent 4b8499b commit 5768757
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion .config/nvim/lua/plugins/ufo.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
local handler = function(virtText, lnum, endLnum, width, truncate)
local log = require 'ufo.lib.log'
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
log.error('chunk', chunk)
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
local _end = endLnum - 1
local final_text = vim.trim(vim.api.nvim_buf_get_text(0, _end, 0, _end, -1, {})[1])
table.insert(newVirtText, { '' .. final_text })
table.insert(newVirtText, { suffix, 'MoreMsg' })
return newVirtText
end

local M = {
'kevinhwang91/nvim-ufo',
dependencies = { 'kevinhwang91/promise-async' },
Expand All @@ -7,7 +40,18 @@ local M = {
{ '<leader>fc', '<cmd>lua require("ufo").closeAllFolds()<cr>' },
{ '<leader>fp', '<cmd>lua require("ufo").peekFoldedLinesUnderCursor()<cr>' },
},
opts = {},
opts = {
close_fold_kinds_for_ft = {
default = { 'imports', 'comment' },
json = { 'array' },
c = { 'comment', 'region' },
},
open_fold_hl_timeout = 0,
provider_selector = function()
return { 'treesitter', 'indent' }
end,
fold_virt_text_handler = handler,
},

init = function()
---@diagnostic disable-next-line: inject-field
Expand Down

0 comments on commit 5768757

Please sign in to comment.