fix(grep_string): cast search value to string (#3319)

When using the vim command API and passing a number to the `search`
option of `grep_string` (eg. `:Telescope grep_string search=3`), the
number is passed as a number. This eventually causes a type error at
`string.gsub` as the `search` value is expected to be a string.

We'll just cast `search` value to a string.
This commit is contained in:
James Trew
2024-10-06 02:11:05 +00:00
committed by GitHub
parent eae0d8fbde
commit dc6fc321a5

View File

@@ -17,8 +17,11 @@ local filter = vim.tbl_filter
local files = {} local files = {}
local escape_chars = function(string) ---@param s string
return string.gsub(string, "[%(|%)|\\|%[|%]|%-|%{%}|%?|%+|%*|%^|%$|%.]", { ---@return string
local escape_chars = function(s)
return (
s:gsub("[%(|%)|\\|%[|%]|%-|%{%}|%?|%+|%*|%^|%$|%.]", {
["\\"] = "\\\\", ["\\"] = "\\\\",
["-"] = "\\-", ["-"] = "\\-",
["("] = "\\(", ["("] = "\\(",
@@ -34,6 +37,7 @@ local escape_chars = function(string)
["$"] = "\\$", ["$"] = "\\$",
["."] = "\\.", ["."] = "\\.",
}) })
)
end end
local has_rg_program = function(picker_name, program) local has_rg_program = function(picker_name, program)
@@ -202,7 +206,10 @@ files.grep_string = function(opts)
else else
word = vim.F.if_nil(opts.search, vim.fn.expand "<cword>") word = vim.F.if_nil(opts.search, vim.fn.expand "<cword>")
end end
word = tostring(word)
local search = opts.use_regex and word or escape_chars(word) local search = opts.use_regex and word or escape_chars(word)
local search_args = search == "" and { "-v", "--", "^[[:space:]]*$" } or { "--", search }
local additional_args = {} local additional_args = {}
if opts.additional_args ~= nil then if opts.additional_args ~= nil then
@@ -217,32 +224,26 @@ files.grep_string = function(opts)
additional_args[#additional_args + 1] = "--encoding=" .. opts.file_encoding additional_args[#additional_args + 1] = "--encoding=" .. opts.file_encoding
end end
if search == "" then
search = { "-v", "--", "^[[:space:]]*$" }
else
search = { "--", search }
end
local args local args
if visual == true then if visual == true then
args = flatten { args = flatten {
vimgrep_arguments, vimgrep_arguments,
additional_args, additional_args,
search, search_args,
} }
else else
args = flatten { args = flatten {
vimgrep_arguments, vimgrep_arguments,
additional_args, additional_args,
opts.word_match, opts.word_match,
search, search_args,
} }
end end
opts.__inverted, opts.__matches = opts_contain_invert(args) opts.__inverted, opts.__matches = opts_contain_invert(args)
if opts.grep_open_files then if opts.grep_open_files then
for _, file in ipairs(get_open_filelist(opts.grep_open_files, opts.cwd)) do for _, file in ipairs(get_open_filelist(opts.grep_open_files, opts.cwd) or {}) do
table.insert(args, file) table.insert(args, file)
end end
elseif opts.search_dirs then elseif opts.search_dirs then