From 02cf3e40703e25b49a8193a4159592576bbe530f Mon Sep 17 00:00:00 2001 From: Simon Hauser Date: Wed, 6 Jul 2022 08:26:22 +0200 Subject: [PATCH] feat: grep_open_files for builtin.grep_string (#2039) --- doc/telescope.txt | 6 ++- lua/telescope/builtin/__files.lua | 61 +++++++++++++++++-------------- lua/telescope/builtin/init.lua | 3 +- 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/doc/telescope.txt b/doc/telescope.txt index 6e9c638..eb980d1 100644 --- a/doc/telescope.txt +++ b/doc/telescope.txt @@ -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) diff --git a/lua/telescope/builtin/__files.lua b/lua/telescope/builtin/__files.lua index 0e26222..8eb373b 100644 --- a/lua/telescope/builtin/__files.lua +++ b/lua/telescope/builtin/__files.lua @@ -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 "" 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 diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua index f25d059..804b663 100644 --- a/lua/telescope/builtin/init.lua +++ b/lua/telescope/builtin/init.lua @@ -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