Skip to content

Commit d7f9cf4

Browse files
committed
feat(highlights): improved picker and preview
- Picker shows same output of `:hi` making the definition searchable - Preview shows recursive details of the highlight (inc links) as well a "quick brown fox" preview of the highlight
1 parent 23fca9a commit d7f9cf4

2 files changed

Lines changed: 37 additions & 60 deletions

File tree

lua/fzf-lua/previewer/builtin.lua

Lines changed: 26 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,67 +1597,39 @@ function Previewer.highlights:new(o, opts)
15971597
return self
15981598
end
15991599

1600-
function Previewer.highlights:close()
1601-
-- Remove our scratch buffer from "listed" so it gets wiped
1602-
self.listed_buffers[tostring(self.tmpbuf)] = nil
1603-
Previewer.highlights.super.close(self)
1604-
self.tmpbuf = nil
1605-
end
1606-
16071600
function Previewer.highlights:populate_preview_buf(entry_str)
1608-
if not self.tmpbuf then
1609-
local output = vim.split(vim.fn.execute "highlight", "\n")
1610-
local hl_groups = {}
1611-
for _, v in ipairs(output) do
1612-
if v ~= "" then
1613-
if v:sub(1, 1) == " " then
1614-
local part_of_old = v:match "%s+(.*)"
1615-
hl_groups[#hl_groups] = hl_groups[#hl_groups] .. part_of_old
1616-
else
1617-
table.insert(hl_groups, v)
1618-
end
1619-
end
1620-
end
1621-
1622-
-- Get a scratch buffer that doesn't wipe on hide (vs `self:get_tmp_buffer`)
1623-
-- and mark it as "listed" so it doesn't get cleared on fzf's zero event
1624-
self.tmpbuf = api.nvim_create_buf(false, true)
1625-
self.listed_buffers[tostring(self.tmpbuf)] = true
1626-
1627-
pcall(vim.api.nvim_buf_clear_namespace, self.tmpbuf, self.ns_previewer, 0, -1)
1628-
vim.api.nvim_buf_set_lines(self.tmpbuf, 0, -1, false, hl_groups)
1629-
for k, v in ipairs(hl_groups) do
1630-
local startPos = string.find(v, "xxx", 1, true) - 1
1631-
local endPos = startPos + 3
1632-
local hlgroup = string.match(v, "([^ ]*)%s+.*")
1633-
vim.api.nvim_buf_set_extmark(self.tmpbuf, self.ns_previewer, k - 1, startPos, {
1634-
end_line = k - 1,
1635-
end_col = endPos,
1636-
hl_group = hlgroup,
1637-
hl_mode = "combine",
1638-
})
1639-
end
1640-
end
1641-
16421601
-- Preview buffer isn't set on init and after fzf's zero event
16431602
if not self.preview_bufnr then
1644-
self:set_preview_buf(self.tmpbuf)
1603+
local buf = self:get_tmp_buffer()
1604+
vim.bo[buf].ft = "lua"
1605+
ts_attach(buf, "lua")
1606+
self:set_preview_buf(buf)
16451607
end
16461608

1647-
local selected_hl = "^" .. utils.strip_ansi_coloring(entry_str) .. "\\>"
1648-
pcall(api.nvim_win_call, self.win.preview_winid, function()
1649-
-- start searching at line 1 in case we
1650-
-- didn't reload the buffer (same file)
1651-
api.nvim_win_set_cursor(0, { 1, 0 })
1652-
fn.clearmatches()
1653-
fn.search(selected_hl, "W")
1654-
if self.win.hls.search then
1655-
fn.matchadd(self.win.hls.search, selected_hl)
1609+
local serpent = require "fzf-lua.lib.serpent"
1610+
local buf = self.preview_bufnr
1611+
local hl = entry_str:match("^[^%s]+")
1612+
local hlgroup = hl
1613+
local lines = {}
1614+
repeat
1615+
local hl_def = vim.api.nvim_get_hl(0, { name = hl, link = true })
1616+
local block = utils.strsplit(serpent.block(hl_def, { comment = false, sortkeys = false }), "\n")
1617+
block[1] = string.format("%s = %s", hl, block[1])
1618+
vim.tbl_map(function(l) table.insert(lines, l) end, block)
1619+
hl = hl_def.link
1620+
until not hl
1621+
table.insert(lines, [[]])
1622+
table.insert(lines, [["THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG"]])
1623+
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
1624+
self.win:update_preview_title(hl)
1625+
self.win:update_preview_scrollbar()
1626+
vim.api.nvim_win_call(self.win.preview_winid, function()
1627+
if self.match_id then
1628+
pcall(fn.matchdelete, self.match_id)
1629+
self.match_id = nil
16561630
end
1657-
self.orig_pos = api.nvim_win_get_cursor(0)
1658-
utils.zz()
1631+
self.match_id = fn.matchaddpos(hlgroup, { { #lines } }, self.ns_previewer)
16591632
end)
1660-
self.win:update_preview_scrollbar()
16611633
end
16621634

16631635
---@class fzf-lua.previewer.Quickfix : fzf-lua.previewer.Builtin,{}

lua/fzf-lua/providers/colorschemes.lua

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,17 @@ M.highlights = function(opts)
9393
if not opts then return end
9494

9595
local contents = function(cb)
96-
local highlights = vim.fn.getcompletion("", "highlight")
97-
98-
local function add_entry(hl, co)
99-
-- translate the highlight using ansi escape sequences
100-
local x = utils.ansi_from_hl(hl, hl)
101-
cb(x, function(err)
96+
local _, hl_dir = utils.ansi_from_hl("Directory", "foo")
97+
local highlights =
98+
utils.strsplit(vim.api.nvim_exec2("highlight", { output = true }).output, "\n")
99+
100+
local function add_entry(line, co)
101+
local hl = line:match("^[^%s]+")
102+
local xxx = utils.ansi_from_hl(hl, "xxx")
103+
line = line:gsub("xxx", xxx)
104+
line = line:gsub("links to", hl_dir .. "%0" .. utils.ansi_escseq.clear)
105+
line = line:gsub("%s([^%s]+=)", hl_dir .. "%0" .. utils.ansi_escseq.clear)
106+
cb(line, function(err)
102107
if co then coroutine.resume(co) end
103108
if err then
104109
-- error, close fzf pipe

0 commit comments

Comments
 (0)