Skip to content

Commit dc6fc32

Browse files
authored
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.
1 parent eae0d8f commit dc6fc32

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

lua/telescope/builtin/__files.lua

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,27 @@ local filter = vim.tbl_filter
1717

1818
local files = {}
1919

20-
local escape_chars = function(string)
21-
return string.gsub(string, "[%(|%)|\\|%[|%]|%-|%{%}|%?|%+|%*|%^|%$|%.]", {
22-
["\\"] = "\\\\",
23-
["-"] = "\\-",
24-
["("] = "\\(",
25-
[")"] = "\\)",
26-
["["] = "\\[",
27-
["]"] = "\\]",
28-
["{"] = "\\{",
29-
["}"] = "\\}",
30-
["?"] = "\\?",
31-
["+"] = "\\+",
32-
["*"] = "\\*",
33-
["^"] = "\\^",
34-
["$"] = "\\$",
35-
["."] = "\\.",
36-
})
20+
---@param s string
21+
---@return string
22+
local escape_chars = function(s)
23+
return (
24+
s:gsub("[%(|%)|\\|%[|%]|%-|%{%}|%?|%+|%*|%^|%$|%.]", {
25+
["\\"] = "\\\\",
26+
["-"] = "\\-",
27+
["("] = "\\(",
28+
[")"] = "\\)",
29+
["["] = "\\[",
30+
["]"] = "\\]",
31+
["{"] = "\\{",
32+
["}"] = "\\}",
33+
["?"] = "\\?",
34+
["+"] = "\\+",
35+
["*"] = "\\*",
36+
["^"] = "\\^",
37+
["$"] = "\\$",
38+
["."] = "\\.",
39+
})
40+
)
3741
end
3842

3943
local has_rg_program = function(picker_name, program)
@@ -202,7 +206,10 @@ files.grep_string = function(opts)
202206
else
203207
word = vim.F.if_nil(opts.search, vim.fn.expand "<cword>")
204208
end
209+
210+
word = tostring(word)
205211
local search = opts.use_regex and word or escape_chars(word)
212+
local search_args = search == "" and { "-v", "--", "^[[:space:]]*$" } or { "--", search }
206213

207214
local additional_args = {}
208215
if opts.additional_args ~= nil then
@@ -217,32 +224,26 @@ files.grep_string = function(opts)
217224
additional_args[#additional_args + 1] = "--encoding=" .. opts.file_encoding
218225
end
219226

220-
if search == "" then
221-
search = { "-v", "--", "^[[:space:]]*$" }
222-
else
223-
search = { "--", search }
224-
end
225-
226227
local args
227228
if visual == true then
228229
args = flatten {
229230
vimgrep_arguments,
230231
additional_args,
231-
search,
232+
search_args,
232233
}
233234
else
234235
args = flatten {
235236
vimgrep_arguments,
236237
additional_args,
237238
opts.word_match,
238-
search,
239+
search_args,
239240
}
240241
end
241242

242243
opts.__inverted, opts.__matches = opts_contain_invert(args)
243244

244245
if opts.grep_open_files then
245-
for _, file in ipairs(get_open_filelist(opts.grep_open_files, opts.cwd)) do
246+
for _, file in ipairs(get_open_filelist(opts.grep_open_files, opts.cwd) or {}) do
246247
table.insert(args, file)
247248
end
248249
elseif opts.search_dirs then

0 commit comments

Comments
 (0)