feat: support selection for grep_string (#2333)

This commit is contained in:
cristiansofronie
2023-02-19 14:41:28 +02:00
committed by GitHub
parent 5d9658c39d
commit f40e3e2304
4 changed files with 32 additions and 11 deletions

View File

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

View File

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

View File

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

View File

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