Skip to content

Commit

Permalink
telescope: add custom rg for fw to enable regex like searching
Browse files Browse the repository at this point in the history
Signed-off-by: aserowy <serowy@hotmail.com>
  • Loading branch information
aserowy committed Dec 11, 2024
1 parent 1a06ccf commit 86ef6ad
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lua/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ local function navigations()
["<C-f><C-s>"] = [[<cmd>lua require'telescope.builtin'.lsp_document_symbols()<cr>]],
["<C-f>t"] = [[<cmd>lua require'theming.theme_picker'.open_picker()<cr>]],
["<C-f><C-u>"] = [[<cmd>Telescope undo<cr>]],
["<C-f><C-w>"] = [[<cmd>lua require'telescope.builtin'.live_grep()<cr>]],
["<C-f><C-w>"] = [[<cmd>lua require'nvim.search'.filtered_grep()<cr>]],
["<C-e><C-e>"] = [[<cmd>lua require'nvim.terminal'.open_file_manager_tui('')<cr>]],
["<C-e><C-t>"] = [[<cmd>lua require'nvim.terminal'.open_file_manager_tui('tabnew')<cr>]],
["<C-e><C-v>"] = [[<cmd>lua require'nvim.terminal'.open_file_manager_tui('vsplit')<cr>]],
Expand Down
69 changes: 69 additions & 0 deletions lua/nvim/search.lua
Original file line number Diff line number Diff line change
@@ -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 = {}

Expand All @@ -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

0 comments on commit 86ef6ad

Please sign in to comment.