Skip to content

Instantly share code, notes, and snippets.

@akemrir
Last active November 14, 2023 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akemrir/b1c791e46dccd119e855ed8e0938dfc9 to your computer and use it in GitHub Desktop.
Save akemrir/b1c791e46dccd119e855ed8e0938dfc9 to your computer and use it in GitHub Desktop.
Extend fzf-lua with tags with custom command
local function getPath(str)
return str:match("(.*[/\\])")
end
local function cutTagBasePath(tags, str)
for _, v in pairs(tags) do
print(v)
str = str:gsub(getPath(v), "")
end
return str
end
local function GetTableLng(tbl)
local getN = 0
for _ in pairs(tbl) do
getN = getN + 1
end
return getN
end
local function fn_transform_tbl(tbl)
-- format all entires as “fzf-lua compatible”
return vim.tbl_map(function(x)
-- 1 tag
-- 2 filename
-- 5 line number
-- 8 decorated tag line -- additional job
print("fn_transform_tbl")
local el = Split(x, "\t")
local tag = el[1]
local filename = el[2]
local path = getPath(el[GetTableLng(el)])
local no = Split(el[5], ":")[2]
local target = string.format("%s%s:%s:%s", path, filename, no, tag)
-- print(target)
return target
end, tbl)
end
function Split(s, delimiter)
local result = {};
for match in (s .. delimiter):gmatch("(.-)" .. delimiter) do
table.insert(result, match);
end
return result;
end
_G.fif_grep = function(opts)
local fzf_lua = require 'fzf-lua'
local tags = vim.opt.tags:get()
opts = opts or {}
if opts.buffer ~= "" and opts.buffer ~= nil and opts.search ~= "" and opts.search ~= nil then
local buf = cutTagBasePath(tags, opts.buffer)
opts.prompt = "fif  :'" .. buf .. "'  :'" .. opts.search .. "'❯ "
opts.fzf_cli_args = '-q ' .. vim.fn.shellescape(buf) .. ' ' .. vim.fn.shellescape(opts.search)
elseif opts.buffer ~= "" and opts.buffer ~= nil then
local buf = cutTagBasePath(tags, opts.buffer)
opts.prompt = "fif  :'" .. buf .. "'❯ "
opts.fzf_cli_args = '-q ' .. vim.fn.shellescape(buf)
elseif opts.search ~= "" and opts.search ~= nil then
opts.prompt = "fif  :'" .. opts.search .. "'❯ "
opts.fzf_cli_args = '-q ' .. vim.fn.shellescape(opts.search)
else
opts.prompt = "fif❯ "
end
print(opts.fzf_cli_args)
opts.git_icons = true
opts.file_icons = true
opts.color_icons = true
-- setup default actions for edit, quickfix, etc
opts.actions = fzf_lua.defaults.actions.files
-- see preview overview for more info on previewers
opts.previewer = "builtin"
opts.actions = {
-- ["<bind1>"] = { fn = function(selected) end, reload = true }
-- ["<o>"] = { fn = require'fzf-lua'.actions.file_edit, reload = true }
["default"] = function(selected, o)
require 'fzf-lua'.actions.file_edit_or_qf(fn_transform_tbl(selected), o)
end,
}
local cmdt = { "tag-dec" }
for _, value in pairs(tags) do
table.insert(cmdt, value)
end
for _, value in pairs({ "|", "rg", "-v", "'^!'" }) do
table.insert(cmdt, value)
end
local cmd = table.concat(cmdt, " ")
print(cmd)
return fzf_lua.fzf_exec(cmd, opts)
end
vim.keymap.set("n", ";t", function() _G.fif_grep({ search = "" }) end)
vim.keymap.set("n", "<leader>t", function() _G.fif_grep({ search = "" }) end)
vim.keymap.set("n", ";T", function() _G.fif_grep({ search = vim.fn.expand('<cword>') }) end)
vim.keymap.set("n", "<leader>T", function() _G.fif_grep({ search = vim.fn.expand('<cword>') }) end)
vim.keymap.set("n", ";o", function() _G.fif_grep({ search = "", buffer = vim.fn.expand('%') }) end)
vim.keymap.set("n", "<leader>o", function() _G.fif_grep({ search = "", buffer = vim.fn.expand('%') }) end)
vim.keymap.set("n", ";O", function() _G.fif_grep({ search = vim.fn.expand('<cword>'), buffer = vim.fn.expand('%') }) end)
vim.keymap.set("n", "<leader>O",
function() _G.fif_grep({ search = vim.fn.expand('<cword>'), buffer = vim.fn.expand('%') }) end)
@akemrir
Copy link
Author

akemrir commented Nov 14, 2023

Tags require to have absolute paths.
tag-dec could be replaced with perl script from junegunn https://github.com/junegunn/fzf.vim/blob/master/bin/tags.pl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment