t0vim/lua/custom/fzf_lua.lua

241 lines
7.3 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

local fzf_lua = require("fzf-lua")
fzf_lua.setup({
winopts = {
height = 0.85,
width = 0.80,
},
fzf_opts = {
['--layout'] = 'reverse-list',
},
files = {
fd_opts = "--color=never --type f",
rg_opts = "--color=never --files",
},
keymap = {
fzf = {
["ctrl-q"] = "select-all+accept",
},
builtin = {
["<C-j>"] = "down",
["<C-k>"] = "up",
["<S-tab>"] = "toggle+up",
["<tab>"] = "toggle+down",
},
},
})
-- Glob pattern searcher
local function live_grep_with_glob()
vim.ui.input({
prompt = "Glob pattern: ",
default = "/home/user/Work/root/ivideon/audio_storage/",
}, function(input)
if input then
fzf_lua.live_grep({
cwd = input,
})
end
end)
end
local function live_grep_in_current_dir()
local current_file = vim.fn.expand('%:p')
local current_dir = current_file:match("(.*/)")
vim.ui.input({
prompt = "Glob pattern: ",
default = current_dir or vim.fn.getcwd(),
}, function(input)
if input then
fzf_lua.live_grep({
cwd = input,
})
end
end)
end
local function copy_current_file_path()
local filepath = vim.fn.expand('%:p')
vim.fn.setreg("+", filepath)
print("Copied: " .. filepath)
end
local function copy_file_path_and_position()
local filepath = vim.fn.expand('%:p')
local cursor_pos = vim.api.nvim_win_get_cursor(0)
local line = cursor_pos[1]
local col = cursor_pos[2] + 1 -- fzf-lua uses 1-indexed columns
local output = string.format("%s:%d:%d", filepath, line, col)
vim.fn.setreg("+", output)
print("Copied: " .. output)
end
local function live_grep_in_quick_fix_list_files()
local qf_items = vim.fn.getqflist()
local files = {}
for _, item in ipairs(qf_items) do
if item.bufnr > 0 then
local filename = vim.api.nvim_buf_get_name(item.bufnr)
if filename ~= "" then
table.insert(files, filename)
end
end
end
if #files > 0 then
fzf_lua.live_grep({
search_paths = files,
})
else
print("No files in quickfix list")
end
end
-- Two-step search in cwd:
-- 1) type a literal file name prefix (e.g. `Go - `, case-insensitive), <Tab> multi-select
-- or <C-q> to take all matches
-- 2) live_grep restricted to the files picked in step 1
local function grep_in_filtered_files()
local cwd = vim.fn.getcwd()
-- rg matches the glob against the file name only; escape glob metacharacters
local function files_cmd(args)
local prefix = (args[1] or ""):gsub("[%*%?%[%]{}\\]", "\\%0")
return string.format("rg --files --color=never --iglob %s", vim.fn.shellescape(prefix .. "*"))
end
---@diagnostic disable-next-line: missing-fields
fzf_lua.fzf_live(files_cmd, {
cwd = cwd,
prompt = "Filename starts with❯ ",
exec_empty_query = true,
fzf_opts = { ["--multi"] = true },
actions = {
["default"] = function(selected)
if not selected or #selected == 0 then
return
end
local files = {}
for _, name in ipairs(selected) do
table.insert(files, cwd .. "/" .. name)
end
fzf_lua.live_grep({
cwd = cwd,
prompt = string.format("Grep in %d files❯ ", #files),
search_paths = files,
})
end,
},
})
end
local function find_all_files_including_hidden()
fzf_lua.files(
{
fd_opts = "--type f --hidden --no-ignore"
}
)
end
-- Function for getting filename and copy it to + buffer
local function copy_filename()
local filepath = vim.fn.expand('%:t')
local output = string.format("[[%s]]", filepath)
vim.fn.setreg("+", output)
print("Copied: " .. output)
end
vim.api.nvim_create_user_command('FzfLuaGrepGlob', live_grep_with_glob, {})
local keymap = vim.keymap.set
keymap("n", "<leader>ff", function() fzf_lua.files() end, { desc = "FZF: find files in cwd" })
keymap("n", "<leader>fg", function() fzf_lua.live_grep() end, { desc = "FZF: find string in cwd" })
keymap("n", "<leader>fs", function() fzf_lua.grep_cword() end, { desc = "FZF: find string under cursor" })
keymap("n", "<leader>fb", function() fzf_lua.buffers() end, { desc = "FZF: display opened buffers" })
keymap("n", "<leader>fh", function() fzf_lua.help_tags() end, { desc = "FZF: display help tags" })
-- Your custom commands
keymap("n", "<leader>fa", find_all_files_including_hidden, { desc = "FZF: Find all files" })
keymap('n', '<leader>fp', live_grep_with_glob, { desc = 'Live grep with glob pattern' })
keymap('n', '<leader>fcp', live_grep_in_current_dir, { desc = 'Live grep in current directory' })
keymap('n', '<leader>fv', grep_in_filtered_files, { desc = "Filter files by name, then grep inside them" })
keymap('n', '<leader>ccp', copy_file_path_and_position, { desc = "Copy cursor position" })
keymap('n', '<leader>ccd', copy_current_file_path, { desc = 'Copy current file path' })
keymap('n', '<leader>ccf', copy_filename, { desc = 'Copy current file path' })
keymap("n", "<leader>fcgb", function()
fzf_lua.git_blame({
winopts = {
preview = { hidden = "hidden" }
}
})
end, { desc = "Grep: Document symbols" })
keymap('n', '<leader>fq', live_grep_in_quick_fix_list_files, { desc = "Live grep in quickfix files" })
keymap('n', '<leader>fcf', function()
fzf_lua.live_grep({
cwd = vim.fn.expand('%:p:h'),
search = '',
fzf_cli_args = '--no-sort',
})
end, { desc = "Live grep in current file" })
keymap("n", "<leader>fml", function() fzf_lua.marks() end, { desc = "FZF: find marks list" })
keymap("n", "<leader>fmp", function() fzf_lua.man_pages() end, { desc = "FZF: find available man pages" })
keymap("n", "<leader>fds", function() fzf_lua.lsp_document_symbols() end, { desc = "Grep: Document symbols" })
-- keymap("n", "<leader>fgb", function() fzf_lua.git_blame() end, { desc = "Grep: Document symbols" })
keymap("n", "<leader>ft", function() fzf_lua.tabs() end, { desc = "Pick opened tabs" })
keymap("n", "<leader>fn", function()
fzf_lua.grep({
prompt = 'Todo❯ ',
search = 'TODO|FIXME|XXX|HACK|BUG|NOTE|PERF|OPTIMIZE',
no_esc = true,
})
end, { desc = "FZF: display TODO comments" })
keymap("n", "<leader>fap", function()
fzf_lua.grep_curbuf({
prompt = 'Problems (file)❯ ',
search = '━ Problem',
})
end, { desc = "FZF: find problems in current file" })
keymap("n", "<leader>faq", function()
fzf_lua.grep_curbuf({
prompt = 'Questions (file)❯ ',
search = '━ Question',
})
end, { desc = "FZF: find questions in current file" })
-- Unresolved questions/problems in current buffer (header ends with " 0 ")
keymap("n", "<leader>fdq", function()
fzf_lua.grep_curbuf({
prompt = 'Open Questions (file)❯ ',
search = '┏━+ Question 0 ',
no_esc = true,
})
end, { desc = "FZF: unresolved questions in current buffer" })
keymap("n", "<leader>fdp", function()
fzf_lua.grep_curbuf({
prompt = 'Open Problems (file)❯ ',
search = '┏━+ Problem 0 ',
no_esc = true,
})
end, { desc = "FZF: unresolved problems in current buffer" })
-- Exact search of text placed right after a ``` fence (code snippet first line) in cwd
keymap("n", "<leader>fcs", function()
fzf_lua.live_grep({
prompt = 'Code snippets❯ ',
search = '(?<=```\\n)',
no_esc = true,
rg_opts = "--column --line-number --no-heading --color=always --smart-case -U --pcre2 -g '*.md'",
})
end, { desc = "FZF: search first line of code blocks in cwd" })