[mini.pick] pick "buffer lines" in current buffer and include treesitter syntax highlighting #988
abeldekat
started this conversation in
Show and tell
Replies: 3 comments 31 replies
-
Oh, that's a nice one! Couple of remarks:
|
Beta Was this translation helpful? Give feedback.
5 replies
-
I’m very excited to try this later as it was something that I was going to look into for myself at some point! |
Beta Was this translation helpful? Give feedback.
0 replies
-
I think I now have a version where the highlighting is complete. The code:local Pick = require("mini.pick")
local H = {} -- Helper functions for custom pickers
---@type table<string,function> event MiniPickStart
H.start_hooks = {}
---@type table<string,function> event MiniPickStop
H.stop_hooks = {}
H.setup_autocommands = function()
local group = vim.api.nvim_create_augroup("minipick-hooks", { clear = true })
local function au(pattern, desc, hooks)
vim.api.nvim_create_autocmd({ "User" }, {
pattern = pattern,
group = group,
desc = desc,
callback = function(...)
local opts = MiniPick.get_picker_opts() or {}
if opts and opts.source then
local hook = hooks[opts.source.name] or function(...) end
hook(...)
end
end,
})
end
au("MiniPickStart", "Picker start hook for source.name", H.start_hooks)
au("MiniPickStop", "Picker stop hook for source.name", H.stop_hooks)
end
Pick.registry.buffer_lines_current = function()
local ns = vim.api.nvim_create_namespace("ak")
local show = function(buf_id, items, query, opts)
local find_linenr = function(text)
local _, _, linenr, _ = string.find(text, "(%d+):(.*)")
return linenr
end
local remove_linenrs = function(replacement)
return vim.tbl_map(function(item)
local linenr = find_linenr(item)
if linenr then item = string.sub(item, #linenr + 2) end
return item
end, replacement)
end
local add_extmark = function(buffer, idx, text)
vim.api.nvim_buf_set_extmark(buffer, ns, idx - 1, 0, {
virt_text = { { text } },
virt_text_pos = "inline",
priority = 1000, -- higher than treesitter's
})
end
if not items or #items == 0 then Pick.default_show(buf_id, items, query, opts) end
local set_lines_orig = vim.api.nvim_buf_set_lines
---@diagnostic disable-next-line: duplicate-set-field
vim.api.nvim_buf_set_lines = function(buffer, start, end_, strict_indexing, replacement)
set_lines_orig(buffer, start, end_, strict_indexing, remove_linenrs(replacement))
for idx, _ in ipairs(replacement) do
local linenr = find_linenr(replacement[idx])
if linenr then add_extmark(buffer, idx, linenr .. ": ") end
end
end
Pick.default_show(buf_id, items, query, opts)
vim.api.nvim_buf_set_lines = set_lines_orig -- restore original function
end
local name = "Buffer lines (current)"
local src_buffer_filetype = vim.bo.filetype -- remember ft of buffer to search in
H.start_hooks[name] = function() vim.bo.filetype = src_buffer_filetype end -- set same ft in picker buffer
H.stop_hooks[name] = function() vim.api.nvim_buf_clear_namespace(0, ns, 0, -1) end -- clean up
MiniExtra.pickers.buf_lines({ scope = "current" }, { source = { show = show } })
end
H.setup_autocommands()
EDIT 20240820: Latest version |
Beta Was this translation helpful? Give feedback.
26 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
For the current buffer, I modified the
buf_lines
picker to show the lines with treesitter syntax highlighting.The code:
I use this quite often to fuzzy search a function while the
lsp
is starting up.EDIT 20240820: Latest version
Beta Was this translation helpful? Give feedback.
All reactions