Skip to content

Commit dacfffc

Browse files
committed
feat(previewer): rich content
entry.content can be `(string|fzf-lua.line)[]` ---@alias fzf-lua.line (string|[string,string])[] The second part is hlgroup chore: simplify highlight previewer
1 parent 51c2ea8 commit dacfffc

2 files changed

Lines changed: 44 additions & 7 deletions

File tree

lua/fzf-lua/previewer/builtin.lua

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,35 @@ function Previewer.buffer_or_file:check_bcache(entry)
881881
return cached
882882
end
883883

884+
---@alias fzf-lua.line (string|[string,string])[]
885+
---@param content (string|fzf-lua.line)[]
886+
---@return string[], table
887+
local parse_rich = function(content)
888+
local lines, extmarks = {}, {}
889+
for i, line in ipairs(content) do
890+
-- If the line_content is a string, just add it directly.
891+
-- Otherwise, it's a table of parts (rich text line)
892+
if type(line) == "string" then
893+
lines[#lines + 1] = line
894+
else
895+
local parts = {}
896+
local col = 0
897+
for _, part in ipairs(line) do
898+
local norm_part = type(part) == "string" and { part } or part
899+
local text, hl = norm_part[1] or "", norm_part[2]
900+
parts[#parts + 1] = text
901+
local end_col = col + #text
902+
if hl then
903+
extmarks[#extmarks + 1] = { row = i - 1, col = col, end_col = end_col, hl_group = hl }
904+
end
905+
col = end_col
906+
end
907+
lines[#lines + 1] = table.concat(parts, "")
908+
end
909+
end
910+
return lines, extmarks
911+
end
912+
884913
---@async
885914
---@param entry_str string
886915
---@return false? no preview
@@ -975,9 +1004,10 @@ function Previewer.buffer_or_file:populate_preview_buf(entry_str)
9751004
return
9761005
end
9771006
do
978-
local lines = nil
1007+
---@type (fzf-lua.line|string)[], table
1008+
local lines, extmarks
9791009
if entry.debug then
980-
lines = { entry.debug }
1010+
lines = { { { entry.debug, "Error" } } }
9811011
elseif entry.content then
9821012
lines = entry.content
9831013
else
@@ -996,7 +1026,17 @@ function Previewer.buffer_or_file:populate_preview_buf(entry_str)
9961026
end
9971027
end
9981028
if lines then
1029+
lines, extmarks = parse_rich(lines)
1030+
extmarks = entry.extmarks or extmarks
9991031
pcall(api.nvim_buf_set_lines, tmpbuf, 0, -1, false, lines)
1032+
if extmarks and #extmarks > 0 then
1033+
local setmark = vim.F.nil_wrap(api.nvim_buf_set_extmark)
1034+
local ns = api.nvim_create_namespace("fzf-lua.preview.hl")
1035+
for _, extmark in ipairs(extmarks) do
1036+
setmark(tmpbuf, ns, extmark.row, extmark.col,
1037+
{ end_col = extmark.end_col, hl_group = extmark.hl_group })
1038+
end
1039+
end
10001040
-- swap preview buffer with new one
10011041
self:set_preview_buf(tmpbuf)
10021042
self:preview_buf_post(entry)
@@ -1670,15 +1710,11 @@ function Previewer.highlights:parse_entry(entry_str)
16701710
hl = hl_def.link
16711711
until not hl
16721712
table.insert(lines, [[]])
1673-
table.insert(lines, [["THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG"]])
1713+
table.insert(lines, { { [["THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG"]], hlgroup } })
16741714
return {
16751715
filetype = "lua",
16761716
title = hl,
16771717
content = lines,
1678-
line = #lines,
1679-
col = 1,
1680-
end_col = 1 + #lines[#lines],
1681-
hlgroup = hlgroup
16821718
}
16831719
end
16841720

lua/fzf-lua/types.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ local FzfLua = require("fzf-lua")
1818
---@field title? string
1919
---@field hlgroup? any
2020
---@field debug? string debug information
21+
---@field extmarks? table
2122

2223
---@class fzf-lua.buffer_or_file.Entry : fzf-lua.path.Entry,{}
2324
---@field do_not_cache? boolean

0 commit comments

Comments
 (0)