feat: grep_open_files for builtin.grep_string (#2039)
This commit is contained in:
@@ -866,8 +866,12 @@ builtin.grep_string({opts}) *telescope.builtin.grep_string()*
|
||||
cwd, use utils.buffer_dir() to
|
||||
search relative to open buffer)
|
||||
{search} (string) the query to search
|
||||
{grep_open_files} (boolean) if true, restrict search to open
|
||||
files only, mutually exclusive with
|
||||
`search_dirs`
|
||||
{search_dirs} (table) directory/directories/files to
|
||||
search
|
||||
search, mutually exclusive with
|
||||
`grep_open_files`
|
||||
{use_regex} (boolean) if true, special characters won't be
|
||||
escaped, allows for using regex
|
||||
(default: false)
|
||||
|
||||
@@ -36,6 +36,29 @@ local escape_chars = function(string)
|
||||
})
|
||||
end
|
||||
|
||||
local get_open_filelist = function(grep_open_files, cwd)
|
||||
if not grep_open_files then
|
||||
return nil
|
||||
end
|
||||
|
||||
local bufnrs = filter(function(b)
|
||||
if 1 ~= vim.fn.buflisted(b) then
|
||||
return false
|
||||
end
|
||||
return true
|
||||
end, vim.api.nvim_list_bufs())
|
||||
if not next(bufnrs) then
|
||||
return
|
||||
end
|
||||
|
||||
local filelist = {}
|
||||
for _, bufnr in ipairs(bufnrs) do
|
||||
local file = vim.api.nvim_buf_get_name(bufnr)
|
||||
table.insert(filelist, Path:new(file):make_relative(cwd))
|
||||
end
|
||||
return filelist
|
||||
end
|
||||
|
||||
-- Special keys:
|
||||
-- opts.search_dirs -- list of directory to search in
|
||||
-- opts.grep_open_files -- boolean to restrict search to open files
|
||||
@@ -45,24 +68,8 @@ files.live_grep = function(opts)
|
||||
local grep_open_files = opts.grep_open_files
|
||||
opts.cwd = opts.cwd and vim.fn.expand(opts.cwd) or vim.loop.cwd()
|
||||
|
||||
local filelist = {}
|
||||
|
||||
if grep_open_files then
|
||||
local bufnrs = filter(function(b)
|
||||
if 1 ~= vim.fn.buflisted(b) then
|
||||
return false
|
||||
end
|
||||
return true
|
||||
end, vim.api.nvim_list_bufs())
|
||||
if not next(bufnrs) then
|
||||
return
|
||||
end
|
||||
|
||||
for _, bufnr in ipairs(bufnrs) do
|
||||
local file = vim.api.nvim_buf_get_name(bufnr)
|
||||
table.insert(filelist, Path:new(file):make_relative(opts.cwd))
|
||||
end
|
||||
elseif search_dirs then
|
||||
local filelist = get_open_filelist(grep_open_files, opts.cwd)
|
||||
if search_dirs then
|
||||
for i, path in ipairs(search_dirs) do
|
||||
search_dirs[i] = vim.fn.expand(path)
|
||||
end
|
||||
@@ -94,11 +101,9 @@ files.live_grep = function(opts)
|
||||
|
||||
local search_list = {}
|
||||
|
||||
if search_dirs then
|
||||
table.insert(search_list, search_dirs)
|
||||
end
|
||||
|
||||
if grep_open_files then
|
||||
table.insert(search_list, search_dirs)
|
||||
elseif search_dirs then
|
||||
search_list = filelist
|
||||
end
|
||||
|
||||
@@ -119,10 +124,6 @@ files.live_grep = function(opts)
|
||||
}):find()
|
||||
end
|
||||
|
||||
-- Special keys:
|
||||
-- opts.search -- the string to search.
|
||||
-- opts.search_dirs -- list of directory to search in
|
||||
-- opts.use_regex -- special characters won't be escaped
|
||||
files.grep_string = function(opts)
|
||||
-- TODO: This should probably check your visual selection as well, if you've got one
|
||||
|
||||
@@ -131,7 +132,9 @@ files.grep_string = function(opts)
|
||||
local word = opts.search or vim.fn.expand "<cword>"
|
||||
local search = opts.use_regex and word or escape_chars(word)
|
||||
local word_match = opts.word_match
|
||||
local grep_open_files = opts.grep_open_files
|
||||
opts.entry_maker = opts.entry_maker or make_entry.gen_from_vimgrep(opts)
|
||||
opts.cwd = opts.cwd and vim.fn.expand(opts.cwd) or vim.loop.cwd()
|
||||
|
||||
local title_word = word:gsub("\n", "\\n")
|
||||
|
||||
@@ -148,7 +151,11 @@ files.grep_string = function(opts)
|
||||
search,
|
||||
}
|
||||
|
||||
if search_dirs then
|
||||
if grep_open_files then
|
||||
for _, file in ipairs(get_open_filelist(grep_open_files, opts.cwd)) do
|
||||
table.insert(args, file)
|
||||
end
|
||||
elseif search_dirs then
|
||||
for _, path in ipairs(search_dirs) do
|
||||
table.insert(args, vim.fn.expand(path))
|
||||
end
|
||||
|
||||
@@ -66,7 +66,8 @@ builtin.live_grep = require_on_exported_call("telescope.builtin.__files").live_g
|
||||
---@param opts table: options to pass to the picker
|
||||
---@field cwd string: root dir to search from (default: cwd, use utils.buffer_dir() to search relative to open buffer)
|
||||
---@field search string: the query to search
|
||||
---@field search_dirs table: directory/directories/files to search
|
||||
---@field grep_open_files boolean: if true, restrict search to open files only, mutually exclusive with `search_dirs`
|
||||
---@field search_dirs table: directory/directories/files to search, mutually exclusive with `grep_open_files`
|
||||
---@field use_regex boolean: if true, special characters won't be escaped, allows for using regex (default: false)
|
||||
---@field word_match string: can be set to `-w` to enable exact word matches
|
||||
---@field additional_args function: function(opts) which returns a table of additional arguments to be passed on
|
||||
|
||||
Reference in New Issue
Block a user