feat(picker.highlights): show color hash and highlight links

This commit is contained in:
Folke Lemaitre 2025-01-14 22:03:48 +01:00
parent 65ad0f6a23
commit 42762939b1
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
4 changed files with 18 additions and 1 deletions

View file

@ -1,6 +1,6 @@
local M = {}
---@alias snacks.picker.Extmark vim.api.keyset.set_extmark|{col:number}
---@alias snacks.picker.Extmark vim.api.keyset.set_extmark|{col:number, row?:number}
---@alias snacks.picker.Text {[1]:string, [2]:string?, virtual?:boolean}
---@alias snacks.picker.Highlight snacks.picker.Text|snacks.picker.Extmark
---@alias snacks.picker.format fun(item:snacks.picker.Item, picker:snacks.Picker):snacks.picker.Highlight[]

View file

@ -396,6 +396,7 @@ function M:_render(item, row)
for _, extmark in ipairs(extmarks) do
local col = extmark.col
extmark.col = nil
extmark.row = nil
vim.api.nvim_buf_set_extmark(self.win.buf, ns, row - 1, col, extmark)
end
end

View file

@ -47,6 +47,11 @@ function M.preview(ctx)
local lines = vim.split(ctx.item.preview.text, "\n")
vim.api.nvim_buf_set_lines(ctx.buf, 0, -1, false, lines)
ctx.preview:highlight({ ft = ctx.item.preview.ft })
for _, extmark in ipairs(ctx.item.preview.extmarks or {}) do
local e = vim.deepcopy(extmark)
e.col, e.row = nil, nil
vim.api.nvim_buf_set_extmark(ctx.buf, ns, (extmark.row or 1) - 1, extmark.col, e)
end
ctx.preview:loc()
end

View file

@ -167,8 +167,18 @@ function M.highlights()
end
end
local code = {} ---@type string[]
local extmarks = {} ---@type snacks.picker.Extmark[]
local row = 1
for _, def in ipairs(defs) do
for _, prop in ipairs({ "fg", "bg", "sp" }) do
local v = def.hl[prop]
if type(v) == "number" then
def.hl[prop] = ("#%06X"):format(v)
end
end
code[#code + 1] = ("%s = %s"):format(def.group, vim.inspect(def.hl))
extmarks[#extmarks + 1] = { row = row, col = 0, hl_group = def.group, end_col = #def.group }
row = row + #vim.split(code[#code], "\n") + 1
end
items[#items + 1] = {
text = vim.inspect(defs):gsub("\n", " "),
@ -176,6 +186,7 @@ function M.highlights()
preview = {
text = table.concat(code, "\n\n"),
ft = "lua",
extmarks = extmarks,
},
}
end