feat: grep_open_files for builtin.grep_string (#2039)

This commit is contained in:
Simon Hauser
2022-07-06 08:26:22 +02:00
committed by GitHub
parent 6ea106329a
commit 02cf3e4070
3 changed files with 41 additions and 29 deletions

View File

@@ -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)

View File

@@ -36,6 +36,29 @@ local escape_chars = function(string)
}) })
end 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: -- Special keys:
-- opts.search_dirs -- list of directory to search in -- opts.search_dirs -- list of directory to search in
-- opts.grep_open_files -- boolean to restrict search to open files -- 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 local grep_open_files = opts.grep_open_files
opts.cwd = opts.cwd and vim.fn.expand(opts.cwd) or vim.loop.cwd() opts.cwd = opts.cwd and vim.fn.expand(opts.cwd) or vim.loop.cwd()
local filelist = {} local filelist = get_open_filelist(grep_open_files, opts.cwd)
if search_dirs then
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
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

View File

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