diff options
| author | James Walmsley <james@fullfat-fs.co.uk> | 2021-04-14 16:31:22 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-14 18:31:22 +0300 |
| commit | 07d518105cdd6778919181ebf3502cd6f4e9f493 (patch) | |
| tree | 8c34b887d876fb9f749a8e098f9263ea95d29e7f | |
| parent | c5f0d05835f70f4bce15168d949563ef4c842e4d (diff) | |
picker(live_grep): add option to grep only over open files (#666)
| -rw-r--r-- | README.md | 8 | ||||
| -rw-r--r-- | lua/telescope/builtin/files.lua | 34 |
2 files changed, 37 insertions, 5 deletions
@@ -374,6 +374,14 @@ Built-in functions. Ready to be bound to any key you like. :smile: | `builtin.live_grep` | Searches in current directory files. (respecting .gitignore) | | `builtin.file_browser` | Ivy-like file explorer. Creates files by typing in filename and pressing `<C-e>`. Press `<C-e>` without prompt for more info | +#### Options for builtin.live_grep + +| Keys | Description | Options | +|------------------------|-------------------------------------------------------|--------------| +| `grep_open_files` | Restrict live_grep to currently open files. | boolean | +| `search_dirs` | List of directories to search in. | list | + + ### Vim Pickers | Functions | Description | diff --git a/lua/telescope/builtin/files.lua b/lua/telescope/builtin/files.lua index 23ceaa7..a30f693 100644 --- a/lua/telescope/builtin/files.lua +++ b/lua/telescope/builtin/files.lua @@ -13,6 +13,7 @@ local Path = require('plenary.path') local os_sep = Path.path.sep local flatten = vim.tbl_flatten +local filter = vim.tbl_filter local files = {} @@ -29,10 +30,28 @@ 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 - opts.cwd = opts.cwd and vim.fn.expand(opts.cwd) + 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 + + local tele_path = require'telescope.path' + for _, bufnr in ipairs(bufnrs) do + local file = vim.api.nvim_buf_get_name(bufnr) + table.insert(filelist, tele_path.make_relative(file, opts.cwd)) + end + end if search_dirs then for i, path in ipairs(search_dirs) do @@ -49,15 +68,19 @@ files.live_grep = function(opts) prompt = escape_chars(prompt) - local args = flatten { vimgrep_arguments, prompt } + local search_list = {} if search_dirs then - table.insert(args, search_dirs) + table.insert(search_list, search_dirs) elseif os_sep == '\\' then - table.insert(args, '.') + table.insert(search_list, '.') end - return args + if grep_open_files then + search_list = filelist + end + + return flatten { vimgrep_arguments, prompt, search_list } end, opts.entry_maker or make_entry.gen_from_vimgrep(opts), opts.max_results, @@ -72,6 +95,7 @@ files.live_grep = function(opts) }):find() end + -- Special keys: -- opts.search -- the string to search. -- opts.search_dirs -- list of directory to search in |
