Skip to content

Commit 9415370

Browse files
committed
Add "smart" autoquote mode
the part before -- is taken as is, and the part after is splitted. For example: multi word search -- -flag --another-flag => "multi word search", "-flag", "--another-flag"
1 parent 7de3bae commit 9415370

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

lua/telescope-live-grep-args/prompt_parser.lua

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,16 @@ local non_autoquote_chars = {
8484

8585
--- Parses prompt shell like and returns a table containing the arguments
8686
--- If autoquote is true (default) and promt does not start with ', " or - then { prompt } will be returned.
87+
--- If autoquote is "smart", only the part before "--" will be autoquoted.
8788
M.parse = function(prompt, autoquote)
8889
if string.len(prompt) == 0 then return {} end
8990

90-
autoquote = autoquote or autoquote == nil
91+
autoquote = autoquote or true
9192
local first_char = string.sub(prompt, 1, 1)
92-
if autoquote and non_autoquote_chars[first_char] == nil then return { prompt } end
93+
if non_autoquote_chars[first_char] ~= nil then
94+
autoquote = false;
95+
end
96+
if autoquote == true then return { prompt } end
9397

9498
local str = {
9599
chars = prompt,
@@ -100,6 +104,27 @@ M.parse = function(prompt, autoquote)
100104
local parts = {}
101105
local current_arg = nil
102106

107+
if autoquote == "smart" then
108+
local sep_begin, sep_end = string.find(prompt, "%-%-");
109+
110+
-- potentially ignore one space before seperator
111+
if sep_begin and sep_begin > 1 and string.sub(prompt, sep_begin - 1, sep_begin - 1) == " " then
112+
sep_begin = sep_begin - 1
113+
end
114+
115+
sep_begin = sep_begin or string.len(prompt) + 1
116+
sep_end = sep_end or string.len(prompt) + 1
117+
118+
local before = string.sub(prompt, 1, sep_begin - 1)
119+
local after = string.sub(prompt, sep_end + 1)
120+
parts = { before }
121+
str = {
122+
chars = after,
123+
pos = 1,
124+
len = string.len(after)
125+
}
126+
end
127+
103128
while str["pos"] <= str["len"] do
104129
local safeguard = str["pos"]
105130

0 commit comments

Comments
 (0)