summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcristiansofronie <53446505+cristiansofronie@users.noreply.github.com>2023-02-19 14:41:28 +0200
committerGitHub <noreply@github.com>2023-02-19 13:41:28 +0100
commitf40e3e2304c633411ddf266075f7db5184b1db02 (patch)
treecfb77a4a555d593c325e25313c4a1f9ee491a15f
parent5d9658c39dca8dd8538dd3eb1ef767374cd9b88f (diff)
feat: support selection for grep_string (#2333)
-rw-r--r--README.md2
-rw-r--r--doc/telescope.txt3
-rw-r--r--lua/telescope/builtin/__files.lua36
-rw-r--r--lua/telescope/builtin/init.lua2
4 files changed, 32 insertions, 11 deletions
diff --git a/README.md b/README.md
index a2a6f63..ca75501 100644
--- a/README.md
+++ b/README.md
@@ -303,7 +303,7 @@ Built-in functions. Ready to be bound to any key you like.
|-------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `builtin.find_files` | Lists files in your current working directory, respects .gitignore |
| `builtin.git_files` | Fuzzy search through the output of `git ls-files` command, respects .gitignore |
-| `builtin.grep_string` | Searches for the string under your cursor in your current working directory |
+| `builtin.grep_string` | Searches for the string under your cursor or selection in your current working directory |
| `builtin.live_grep` | Search for a string in your current working directory and get results live as you type, respects .gitignore. (Requires [ripgrep](https://github.com/BurntSushi/ripgrep)) |
### Vim Pickers
diff --git a/doc/telescope.txt b/doc/telescope.txt
index 8135b0f..0c80c50 100644
--- a/doc/telescope.txt
+++ b/doc/telescope.txt
@@ -804,7 +804,8 @@ builtin.live_grep({opts}) *telescope.builtin.live_grep()*
builtin.grep_string({opts}) *telescope.builtin.grep_string()*
- 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
Parameters: ~
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