feat(picker.diff): native diff now also highlights the language of the diffed code in the diff

This commit is contained in:
Folke Lemaitre 2025-10-27 23:36:03 +01:00
parent e39478daac
commit 726095723d
No known key found for this signature in database
GPG key ID: 9B52594D560070AB

View file

@ -24,6 +24,7 @@ M.__index = M
local ns = vim.api.nvim_create_namespace("snacks.picker.preview")
local ns_loc = vim.api.nvim_create_namespace("snacks.picker.preview.loc")
local did_diff_setup = false
-- HACK: work-around for buffer-local window options mess. From the docs:
-- > When editing a buffer that has been edited before, the options from the window
@ -281,6 +282,9 @@ function M:highlight(opts)
end
self:check_big()
local lang = Snacks.util.get_lang(opts.lang or ft)
if lang == "diff" then
self:diff()
end
if not (lang and pcall(vim.treesitter.start, self.win.buf, lang)) and ft then
vim.bo[self.win.buf].syntax = ft
end
@ -411,4 +415,20 @@ function M:notify(msg, level, opts)
self:highlight({ lang = "lua" })
end
function M:diff()
if did_diff_setup then
return
end
did_diff_setup = true
vim.treesitter.query.set(
"diff",
"injections",
[[; extends
(block
(new_file (filename) @injection.filename)
(hunks
(hunk changes: (changes) @injection.content
(#set! injection.include-children))))]]
)
end
return M