feat(picker): syntax highlighting for command and search history

This commit is contained in:
Folke Lemaitre 2025-01-21 22:21:16 +01:00
parent f7d07bcbc2
commit efb6d1f8b8
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
3 changed files with 15 additions and 7 deletions

View file

@ -118,6 +118,9 @@ local defaults = {
ui_select = true, -- replace `vim.ui.select` with the snacks picker ui_select = true, -- replace `vim.ui.select` with the snacks picker
---@class snacks.picker.formatters.Config ---@class snacks.picker.formatters.Config
formatters = { formatters = {
text = {
ft = nil, ---@type string? filetype for highlighting
},
file = { file = {
filename_first = false, -- display filename before the file path filename_first = false, -- display filename before the file path
}, },

View file

@ -71,6 +71,7 @@ M.command_history = {
preset = "vscode", preset = "vscode",
}, },
confirm = "cmd", confirm = "cmd",
formatters = { text = { ft = "vim" } },
} }
-- Neovim commands -- Neovim commands
@ -549,10 +550,9 @@ M.search_history = {
name = "search", name = "search",
format = "text", format = "text",
preview = "none", preview = "none",
layout = { layout = { preset = "vscode" },
preset = "vscode",
},
confirm = "search", confirm = "search",
formatters = { text = { ft = "regex" } },
} }
---@class snacks.picker.smart.Config: snacks.picker.Config ---@class snacks.picker.smart.Config: snacks.picker.Config

View file

@ -275,10 +275,15 @@ function M.lines(item)
return ret return ret
end end
function M.text(item) function M.text(item, picker)
return { local ret = {} ---@type snacks.picker.Highlight[]
{ item.text }, local ft = item.ft or picker.opts.formatters.text.ft
} if ft then
Snacks.picker.highlight.format(item, item.text, ret, { lang = ft })
else
ret[#ret + 1] = { item.text }
end
return ret
end end
function M.command(item) function M.command(item)