From 84604a549b1b257bcd390563201141d4090d0797 Mon Sep 17 00:00:00 2001 From: Lucy Crockett <58056722+lcrockett@users.noreply.github.com> Date: Wed, 3 Sep 2025 23:01:42 +0000 Subject: [PATCH] feat(foldtext): allow function() for virtual text components --- lua/origami/config.lua | 3 +++ lua/origami/features/foldtext.lua | 22 +++++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lua/origami/config.lua b/lua/origami/config.lua index 7a2e9fa..2e430a6 100644 --- a/lua/origami/config.lua +++ b/lua/origami/config.lua @@ -10,10 +10,13 @@ local defaultConfig = { enabled = true, padding = 3, lineCount = { + ---@type string | fun():string template = "%d lines", -- `%d` is replaced with the number of folded lines hlgroup = "Comment", }, + ---@type boolean | fun():boolean diagnosticsCount = true, -- uses hlgroups and icons from `vim.diagnostic.config().signs` + ---@type boolean | fun():boolean gitsignsCount = true, -- requires `gitsigns.nvim` }, autoFold = { diff --git a/lua/origami/features/foldtext.lua b/lua/origami/features/foldtext.lua index c726561..8101996 100644 --- a/lua/origami/features/foldtext.lua +++ b/lua/origami/features/foldtext.lua @@ -97,17 +97,33 @@ local function renderFoldedSegments(win, buf, foldstart) local foldend = vim.fn.foldclosedend(foldstart) -- get virtual text components - local lineCountText = config.foldtext.lineCount.template:format(foldend - foldstart) + local resolvedTemplate = config.foldtext.lineCount.template + if type(resolvedTemplate) == 'function' then + resolvedTemplate = resolvedTemplate(buf, foldstart, foldend) + end + local lineCountText = resolvedTemplate:format(foldend - foldstart) local virtText = { ---@type Origami.VirtTextChunk[] { (" "):rep(config.foldtext.padding) }, { lineCountText, { config.foldtext.lineCount.hlgroup } }, } - if config.foldtext.diagnosticsCount then + local show_diagnostics + if type(config.foldtext.diagnosticsCount) == "function" then + show_diagnostics = config.foldtext.diagnosticsCount() + else + show_diagnostics = config.foldtext.diagnosticsCount + end + if show_diagnostics then local diagnostics = getDiagnosticsInFold(buf, foldstart, foldend) if #diagnostics > 0 then table.insert(virtText, { " " }) end vim.list_extend(virtText, diagnostics) end - if config.foldtext.gitsignsCount then + local show_gitsigns + if type(config.foldtext.gitsignsCount) == "function" then + show_gitsigns = config.foldtext.gitsignsCount(buf, foldstart, foldend) + else + show_gitsigns = config.foldtext.gitsignsCount + end + if show_gitsigns then local hunks = getGitHunksInFold(buf, foldstart, foldend) if #hunks > 0 then table.insert(virtText, { " " }) end vim.list_extend(virtText, hunks)