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
|
cwd, use utils.buffer_dir() to
|
||||||
search relative to open buffer)
|
search relative to open buffer)
|
||||||
{search} (string) the query to search
|
{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_dirs} (table) directory/directories/files to
|
||||||
search
|
search, mutually exclusive with
|
||||||
|
`grep_open_files`
|
||||||
{use_regex} (boolean) if true, special characters won't be
|
{use_regex} (boolean) if true, special characters won't be
|
||||||
escaped, allows for using regex
|
escaped, allows for using regex
|
||||||
(default: false)
|
(default: false)
|
||||||
|
|||||||
@@ -36,18 +36,11 @@ local escape_chars = function(string)
|
|||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Special keys:
|
local get_open_filelist = function(grep_open_files, cwd)
|
||||||
-- opts.search_dirs -- list of directory to search in
|
if not grep_open_files then
|
||||||
-- opts.grep_open_files -- boolean to restrict search to open files
|
return nil
|
||||||
files.live_grep = function(opts)
|
end
|
||||||
local vimgrep_arguments = opts.vimgrep_arguments or conf.vimgrep_arguments
|
|
||||||
local search_dirs = opts.search_dirs
|
|
||||||
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)
|
local bufnrs = filter(function(b)
|
||||||
if 1 ~= vim.fn.buflisted(b) then
|
if 1 ~= vim.fn.buflisted(b) then
|
||||||
return false
|
return false
|
||||||
@@ -58,11 +51,25 @@ files.live_grep = function(opts)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local filelist = {}
|
||||||
for _, bufnr in ipairs(bufnrs) do
|
for _, bufnr in ipairs(bufnrs) do
|
||||||
local file = vim.api.nvim_buf_get_name(bufnr)
|
local file = vim.api.nvim_buf_get_name(bufnr)
|
||||||
table.insert(filelist, Path:new(file):make_relative(opts.cwd))
|
table.insert(filelist, Path:new(file):make_relative(cwd))
|
||||||
end
|
end
|
||||||
elseif search_dirs then
|
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
|
||||||
|
files.live_grep = function(opts)
|
||||||
|
local vimgrep_arguments = opts.vimgrep_arguments or conf.vimgrep_arguments
|
||||||
|
local search_dirs = opts.search_dirs
|
||||||
|
local grep_open_files = opts.grep_open_files
|
||||||
|
opts.cwd = opts.cwd and vim.fn.expand(opts.cwd) or vim.loop.cwd()
|
||||||
|
|
||||||
|
local filelist = get_open_filelist(grep_open_files, opts.cwd)
|
||||||
|
if search_dirs then
|
||||||
for i, path in ipairs(search_dirs) do
|
for i, path in ipairs(search_dirs) do
|
||||||
search_dirs[i] = vim.fn.expand(path)
|
search_dirs[i] = vim.fn.expand(path)
|
||||||
end
|
end
|
||||||
@@ -94,11 +101,9 @@ files.live_grep = function(opts)
|
|||||||
|
|
||||||
local search_list = {}
|
local search_list = {}
|
||||||
|
|
||||||
if search_dirs then
|
|
||||||
table.insert(search_list, search_dirs)
|
|
||||||
end
|
|
||||||
|
|
||||||
if grep_open_files then
|
if grep_open_files then
|
||||||
|
table.insert(search_list, search_dirs)
|
||||||
|
elseif search_dirs then
|
||||||
search_list = filelist
|
search_list = filelist
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -119,10 +124,6 @@ files.live_grep = function(opts)
|
|||||||
}):find()
|
}):find()
|
||||||
end
|
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)
|
files.grep_string = function(opts)
|
||||||
-- TODO: This should probably check your visual selection as well, if you've got one
|
-- 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 word = opts.search or vim.fn.expand "<cword>"
|
||||||
local search = opts.use_regex and word or escape_chars(word)
|
local search = opts.use_regex and word or escape_chars(word)
|
||||||
local word_match = opts.word_match
|
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.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")
|
local title_word = word:gsub("\n", "\\n")
|
||||||
|
|
||||||
@@ -148,7 +151,11 @@ files.grep_string = function(opts)
|
|||||||
search,
|
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
|
for _, path in ipairs(search_dirs) do
|
||||||
table.insert(args, vim.fn.expand(path))
|
table.insert(args, vim.fn.expand(path))
|
||||||
end
|
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
|
---@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 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 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 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 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
|
---@field additional_args function: function(opts) which returns a table of additional arguments to be passed on
|
||||||
|
|||||||
Reference in New Issue
Block a user