Skip to content

Commit 12e1d56

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
1 parent bdbdf3c commit 12e1d56

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

lua/fzf-lua/previewer/builtin.lua

Lines changed: 42 additions & 2 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
function Previewer.buffer_or_file:populate_preview_buf(entry_str)
@@ -974,9 +1003,10 @@ function Previewer.buffer_or_file:populate_preview_buf(entry_str)
9741003
return
9751004
end
9761005
do
977-
local lines = nil
1006+
---@type (fzf-lua.line|string)[], table
1007+
local lines, extmarks
9781008
if entry.debug then
979-
lines = { entry.debug }
1009+
lines = { { { entry.debug, "Error" } } }
9801010
elseif entry.content then
9811011
lines = entry.content
9821012
else
@@ -995,7 +1025,17 @@ function Previewer.buffer_or_file:populate_preview_buf(entry_str)
9951025
end
9961026
end
9971027
if lines then
1028+
lines, extmarks = parse_rich(lines)
1029+
extmarks = entry.extmarks or lines
9981030
pcall(api.nvim_buf_set_lines, tmpbuf, 0, -1, false, lines)
1031+
if extmarks and #extmarks > 0 then
1032+
local setmark = vim.F.nil_wrap(api.nvim_buf_set_extmark)
1033+
local ns = api.nvim_create_namespace("fzf-lua.preview.hl")
1034+
for _, extmark in ipairs(extmarks) do
1035+
setmark(tmpbuf, ns, extmark.row, extmark.col,
1036+
{ end_col = extmark.end_col, hl_group = extmark.hl_group })
1037+
end
1038+
end
9991039
-- swap preview buffer with new one
10001040
self:set_preview_buf(tmpbuf)
10011041
self:preview_buf_post(entry)

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)