diff --git a/lua/mappings.lua b/lua/mappings.lua index c562abf..335772e 100644 --- a/lua/mappings.lua +++ b/lua/mappings.lua @@ -158,7 +158,7 @@ local function navigations() [""] = [[lua require'telescope.builtin'.lsp_document_symbols()]], ["t"] = [[lua require'theming.theme_picker'.open_picker()]], [""] = [[Telescope undo]], - [""] = [[lua require'telescope.builtin'.live_grep()]], + [""] = [[lua require'nvim.search'.filtered_grep()]], [""] = [[lua require'nvim.terminal'.open_file_manager_tui('')]], [""] = [[lua require'nvim.terminal'.open_file_manager_tui('tabnew')]], [""] = [[lua require'nvim.terminal'.open_file_manager_tui('vsplit')]], diff --git a/lua/nvim/search.lua b/lua/nvim/search.lua index b94b607..8b8b935 100644 --- a/lua/nvim/search.lua +++ b/lua/nvim/search.lua @@ -1,4 +1,9 @@ local builtin = require("telescope.builtin") +local conf = require("telescope.config").values +local finders = require("telescope.finders") +local make_entry = require("telescope.make_entry") +local pickers = require("telescope.pickers") + local is_inside_work_tree = {} @@ -17,4 +22,68 @@ function M.git_or_local() end end +function M.filtered_grep(opts) + opts = opts or {} + opts.cwd = opts.cwd and vim.fn.expand(opts.cwd) or vim.loop.cwd() + opts.shortcuts = opts.shortcuts + or { + ["l"] = "*.lua", + ["c"] = "*.c*", + ["r"] = "*.rs", + ["g"] = "*.go", + } + opts.pattern = opts.pattern or "%s" + + local filtered_grep = finders.new_async_job({ + command_generator = function(prompt) + if not prompt or prompt == "" then + return nil + end + + local prompt_split = vim.split(prompt, " ") + + local args = { + "rg", + "--color=never", + "--no-heading", + "--with-filename", + "--line-number", + "--column", + "--smart-case", + } + if prompt_split[1] then + table.insert(args, "-e") + table.insert(args, prompt_split[1]) + end + + if prompt_split[2] then + table.insert(args, "-g") + + local pattern + if opts.shortcuts[prompt_split[2]] then + pattern = opts.shortcuts[prompt_split[2]] + else + pattern = prompt_split[2] + end + + table.insert(args, string.format(opts.pattern, pattern)) + end + + return args + end, + entry_maker = make_entry.gen_from_vimgrep(opts), + cwd = opts.cwd, + }) + + pickers + .new(opts, { + debounce = 100, + prompt_title = "filtered grep with ' '", + finder = filtered_grep, + previewer = conf.grep_previewer(opts), + sorter = require("telescope.sorters").empty(), + }) + :find() +end + return M