Builtin previewer for vim.fn.taglist() picker
#2426
-
|
Hey, so I want to create a simple picker which lets me select items from the list generated by This is what I have currently local fzf_lua = require("fzf-lua")
local builtin = require("fzf-lua.previewer.builtin")
local keyword = vim.fn.expand("<cword>")
local matches = vim.fn.taglist(string.format("^%s$", vim.fn.escape(keyword, "^$")), vim.fn.expand("%:p"))
local rows = {}
for _, v in ipairs(matches) do
table.insert(
rows,
string.format("%s\t%s %s\t%s", v.name, v.kind, vim.fn.fnamemodify(v.filename, ":~:."), v.cmd)
)
end
local CtagsPreviewer = builtin.buffer_or_file:extend()
function CtagsPreviewer:new(o, opts, fzf_win)
CtagsPreviewer.super.new(self, o, opts, fzf_win)
setmetatable(self, CtagsPreviewer)
return self
end
function CtagsPreviewer:parse_entry(entry_str)
local filename, excmd = entry_str:match(".*\t. (.*)\t(.*)$")
vim.api.nvim_win_call(self.win.preview_winid, function()
vim.fn.search(excmd:sub(2, -2), "cw")
end)
local cursor = vim.api.nvim_win_get_cursor(self.win.preview_winid)
return {
path = filename,
line = cursor[1],
col = cursor[2] + 1,
}
end
fzf_lua.fzf_exec(rows, {
prompt = string.format("%s> ", keyword),
previewer = CtagsPreviewer,
})This kinda works, but the problem I have is that sometimes the correct line is selected, sometimes not. I read some of the previewer code and I guess it's due to caching and What I need is to call Some pseudocode what I need: Previewer:on_window_change()
vim.api.nvim_win_call(self.win, function() vim.fn.search(self.parsed_entry.cmd) end)
endI know I can just use the builtin |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
|
Have you tried extending the preview class? |
Beta Was this translation helpful? Give feedback.
-
|
If anyone stumbles upon this discussion, this is what I have currently: local fzf_lua = require("fzf-lua")
local builtin = require("fzf-lua.previewer.builtin")
local ansi_codes = fzf_lua.utils.ansi_codes
local function parse_entry(entry_str)
local filename = entry_str:match("^.+ . (.+): /(.+)/$")
local ctag = require("fzf-lua.path").entry_to_ctag(entry_str, false)
return filename, ctag
end
local CtagsPreviewer = builtin.tags:extend()
function CtagsPreviewer:new(o, opts, fzf_win)
CtagsPreviewer.super.new(self, o, opts, fzf_win)
setmetatable(self, CtagsPreviewer)
return self
end
function CtagsPreviewer:parse_entry(entry_str)
local filename, ctag = parse_entry(entry_str)
return {
path = filename,
ctag = ctag,
}
end
local function goto_match(entry_str)
local filename, ctag = parse_entry(entry_str)
vim.cmd.e(filename)
vim.api.nvim_win_set_cursor(0, { 1, 0 })
vim.fn.search(ctag, "W")
end
vim.api.nvim_create_user_command("Taglist", function(ev)
if vim.bo.buftype ~= "" then
print("Taglist requires normal buffer")
return
end
local keyword = ev.fargs[1]
local matches = vim.fn.taglist(string.format("^%s$", vim.fn.escape(keyword, "^$")), vim.fn.expand("%:p"))
local rows = {}
for _, v in ipairs(matches) do
table.insert(
rows,
string.format(
"%s %s %s: %s",
ansi_codes.magenta(v.name),
ansi_codes.yellow(v.kind),
vim.fn.fnamemodify(v.filename, ":~:."),
ansi_codes.grey(v.cmd)
)
)
end
fzf_lua.fzf_exec(rows, {
prompt = string.format("%s> ", keyword),
previewer = CtagsPreviewer,
winopts = {
title = "Taglist",
},
actions = {
default = function(item)
goto_match(item[1])
end,
["ctrl-s"] = function(item)
vim.cmd("new")
goto_match(item[1])
end,
["ctrl-v"] = function(item)
vim.cmd("vertical new")
goto_match(item[1])
end,
},
})
end, {
nargs = 1,
})
|
Beta Was this translation helpful? Give feedback.

Why don’t you extend the tags previewer? IIRC The code for it also uses fn.search, maybe with a small modification it will do exactly what you want?