tags: auto-detect line numbers in tags file

main
bhagwan 2 years ago
parent ca4dc395d8
commit fcaa3d5b24

@ -323,11 +323,13 @@ M.tag = function(opts, x)
if not file or not name or not text then return x end
text = text:match('(.*);"') or text -- remove ctag comments
local line, tag = text:gsub("\\/", "/"):match("(%d-);?(/.*/)")
line = line and #line>0 and tonumber(line)
return ("%s%s: %s %s"):format(
M.file(opts, file),
(not line or #line==0) and "" or ":"..utils.ansi_codes.green(tostring(line)),
not line and "" or ":"..utils.ansi_codes.green(tostring(line)),
utils.ansi_codes.magenta(name),
utils.ansi_codes.green(tag))
, line
end
return M

@ -6,7 +6,7 @@ local make_entry = require "fzf-lua.make_entry"
local M = {}
local function get_tags_cmd(opts)
local function get_tags_cmd(opts, flags)
local query = nil
local cmd = "grep"
if vim.fn.executable("rg") == 1 then
@ -20,7 +20,7 @@ local function get_tags_cmd(opts)
else
query = "-v '^!_TAG_'"
end
return ("%s %s %s"):format(cmd, query,
return ("%s %s %s %s"):format(cmd, flags or '', query,
vim.fn.shellescape(opts._ctags_file))
end
@ -40,6 +40,21 @@ local function tags(opts)
return
end
if opts.line_field_index == nil then
-- if caller did not specify the line field index
-- grep the first tag with '-m 1' and test for line presence
local cmd = get_tags_cmd({ _ctags_file = opts._ctags_file }, "-m 1")
local ok, lines, err = pcall(utils.io_systemlist, cmd)
if ok and err == 0 and lines and not vim.tbl_isempty(lines) then
local tag, line = make_entry.tag(opts, lines[1])
if tag and not line then
-- tags file does not contain lines
-- remove preview offset field index
opts.line_field_index = 0
end
end
end
-- prevents 'file|git_icons=false' from overriding processing
opts.requires_processing = true
opts._fn_transform = make_entry.tag -- multiprocess=false

Loading…
Cancel
Save