WIP: added `tags`, `btags` (need to fix previewer line)

main
bhagwan 3 years ago
parent cea8e27991
commit ecdc79da2e

@ -174,6 +174,8 @@ nnoremap <c-P> <cmd>lua require('fzf-lua').files()<CR>
|`registers`|:registers|
|`keymaps`|key mappings|
|`spell_suggest`|spelling suggestions|
|`tags`|project tags|
|`btags`|buffer tags|
## Customization
@ -432,7 +434,8 @@ EOF
* [x] ~~keymaps~~ (2021-08-14)
* [x] ~~spelling suggestions~~ (2021-08-14)
* [x] ~~marks~~ (2021-08-14)
+ [ ] tags
+ [x] ~~tags~~ (2021-08-15)
- [ ] Fix previewer for `tags`, `btags` (WIP)
- [ ] Improve previewer for `buffers`, `marks`
- [ ] Built-in previewer with treesitter support
- [ ] Add built-in plugin documentation

@ -258,6 +258,34 @@ M.globals.buffers = {
["ctrl-x"] = actions.buf_del,
},
}
M.globals.tags = {
prompt = 'Tags> ',
ctags_file = "tags",
file_icons = true and M._has_devicons,
git_icons = true,
color_icons = true,
actions = {
["default"] = actions.file_edit,
["ctrl-s"] = actions.file_split,
["ctrl-v"] = actions.file_vsplit,
["ctrl-t"] = actions.file_tabedit,
["ctrl-q"] = actions.file_sel_to_qf,
},
}
M.globals.btags = {
prompt = 'BTags> ',
ctags_file = "tags",
file_icons = true and M._has_devicons,
git_icons = true,
color_icons = true,
actions = {
["default"] = actions.file_edit,
["ctrl-s"] = actions.file_split,
["ctrl-v"] = actions.file_vsplit,
["ctrl-t"] = actions.file_tabedit,
["ctrl-q"] = actions.file_sel_to_qf,
},
}
M.globals.colorschemes = {
prompt = 'Colorschemes> ',
live_preview = true,

@ -68,6 +68,8 @@ M.help_tags = require'fzf-lua.providers.helptags'.helptags
M.man_pages = require'fzf-lua.providers.manpages'.manpages
M.colorschemes = require'fzf-lua.providers.colorschemes'.colorschemes
M.tags = require'fzf-lua.providers.tags'.tags
M.btags = require'fzf-lua.providers.tags'.btags
M.marks = require'fzf-lua.providers.nvim'.marks
M.keymaps = require'fzf-lua.providers.nvim'.keymaps
M.registers = require'fzf-lua.providers.nvim'.registers

@ -0,0 +1,114 @@
if not pcall(require, "fzf") then
return
end
local core = require "fzf-lua.core"
local path = require "fzf-lua.path"
local utils = require "fzf-lua.utils"
local config = require "fzf-lua.config"
local M = {}
local grep_cmd = nil
local get_grep_cmd = function()
if vim.fn.executable("rg") == 1 then
return "rg --line-number"
end
return "grep -n -P"
end
local fzf_tags = function(opts)
opts.ctags_file = opts.ctags_file or "tags"
if not vim.loop.fs_open(vim.fn.expand(opts.ctags_file, true), "r", 438) then
utils.info("Tags file does not exists. Create one with ctags -R")
return
end
-- get these here before we open fzf
local cwd = vim.fn.expand(opts.cwd or vim.fn.getcwd())
local current_file = vim.api.nvim_buf_get_name(0)
local fzf_function = function (cb)
local getlinenumber = function(t)
if not grep_cmd then grep_cmd = get_grep_cmd() end
local line = 1
local filepath = path.join({cwd, t.file})
local pattern = utils.rg_escape(t.text:match("/(.*)/"))
-- do not escape '$' if it's the last pattern char
-- as ctags uses '$' at the end of short patterns
pattern = pattern:gsub("\\%$$", "%$")
if not pattern or not filepath then return line end
local cmd = string.format('%s "%s" "%s"',
grep_cmd,
pattern,
filepath)
-- TODO: why is this causing the function to crash?
-- vim.fn.shellescape(filepath or ""))
--[[ local out = vim.fn.system(cmd)
if not utils.shell_error() then
line = out:match("[^:]+")
end ]]
-- print(line, cmd)
return line
end
local add_tag = function(t, fzf_cb, co)
local line = getlinenumber(t)
local tag = string.format("%s:%s: %s %s",
core.make_entry_file(opts, t.file),
utils.ansi_codes.green(tostring(line)),
utils.ansi_codes.magenta(t.name),
utils.ansi_codes.green(t.text))
fzf_cb(tag, function()
coroutine.resume(co)
end)
end
coroutine.wrap(function ()
local co = coroutine.running()
local delimiter = string.char(9)
local lines = vim.split(utils.read_file(opts.ctags_file), '\n', true)
for _, line in ipairs(lines) do
if not line:match'^!_TAG_' then
local fields = vim.split(line, delimiter, true)
if #fields >= 3 then
if not opts.current_buffer_only or
current_file == path.join({cwd, fields[2]}) then
add_tag({
name = fields[1],
file = fields[2],
text = fields[3],
}, cb, co)
-- pause here until we call coroutine.resume()
coroutine.yield()
end
end
end
end
-- done, we can't call utils.delayed_cb here
-- because sleep() messes up the coroutine
-- cb(nil, function() coroutine.resume(co) end)
utils.delayed_cb(cb, function() coroutine.resume(co) end)
coroutine.yield()
end)()
end
opts.fzf_fn = fzf_function
return core.fzf_files(opts)
end
M.tags = function(opts)
opts = config.normalize_opts(opts, config.globals.tags)
return fzf_tags(opts)
end
M.btags = function(opts)
opts = config.normalize_opts(opts, config.globals.btags)
opts.current_buffer_only = true
return fzf_tags(opts)
end
return M
Loading…
Cancel
Save