diff options
| author | cristiansofronie <53446505+cristiansofronie@users.noreply.github.com> | 2023-02-19 14:41:28 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-19 13:41:28 +0100 |
| commit | f40e3e2304c633411ddf266075f7db5184b1db02 (patch) | |
| tree | cfb77a4a555d593c325e25313c4a1f9ee491a15f /lua/telescope/builtin | |
| parent | 5d9658c39dca8dd8538dd3eb1ef767374cd9b88f (diff) | |
feat: support selection for grep_string (#2333)
Diffstat (limited to 'lua/telescope/builtin')
| -rw-r--r-- | lua/telescope/builtin/__files.lua | 36 | ||||
| -rw-r--r-- | lua/telescope/builtin/init.lua | 2 |
2 files changed, 29 insertions, 9 deletions
diff --git a/lua/telescope/builtin/__files.lua b/lua/telescope/builtin/__files.lua index 7005b21..afb3d5a 100644 --- a/lua/telescope/builtin/__files.lua +++ b/lua/telescope/builtin/__files.lua @@ -162,10 +162,20 @@ files.live_grep = function(opts) end files.grep_string = function(opts) - -- TODO: This should probably check your visual selection as well, if you've got one opts.cwd = opts.cwd and vim.fn.expand(opts.cwd) or vim.loop.cwd() local vimgrep_arguments = vim.F.if_nil(opts.vimgrep_arguments, conf.vimgrep_arguments) - local word = vim.F.if_nil(opts.search, vim.fn.expand "<cword>") + local word + local visual = vim.fn.mode() == "v" + + if visual == true then + local saved_reg = vim.fn.getreg "v" + vim.cmd [[noautocmd sil norm "vy]] + local sele = vim.fn.getreg "v" + vim.fn.setreg("v", saved_reg) + word = vim.F.if_nil(opts.search, sele) + else + word = vim.F.if_nil(opts.search, vim.fn.expand "<cword>") + end local search = opts.use_regex and word or escape_chars(word) local additional_args = {} @@ -183,12 +193,22 @@ files.grep_string = function(opts) search = { "--", search } end - local args = flatten { - vimgrep_arguments, - additional_args, - opts.word_match, - search, - } + local args + if visual == true then + args = flatten { + vimgrep_arguments, + additional_args, + search, + } + else + args = flatten { + vimgrep_arguments, + additional_args, + opts.word_match, + search, + } + end + opts.__inverted, opts.__matches = opts_contain_invert(args) if opts.grep_open_files then diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua index e74cdf4..d0d669d 100644 --- a/lua/telescope/builtin/init.lua +++ b/lua/telescope/builtin/init.lua @@ -57,7 +57,7 @@ end ---@field disable_coordinates boolean: don't show the line & row numbers (default: false) builtin.live_grep = require_on_exported_call("telescope.builtin.__files").live_grep ---- Searches for the string under your cursor in your current working directory +--- Searches for the string under your cursor or the visual selection in your current working directory ---@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 |
