Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ Call live grep args:
If the prompt value does not begin with `'`, `"` or `-` the entire prompt is treated as a single argument.
This behaviour can be turned off by setting the `auto_quoting` option to `false`.

Setting the `auto_quoting` option to `smart` allows using `--` as a seperator before flags.
The part before the seperator is taken as a single argument, and the part after is split.
Again, if the prompt begins with `'`, `"` or `-`, this is disabled and control is manual.

| prompt | args (smart mode) |
| --- | --- |
| `foo bar` | `foo bar` |
| `foo bar -- --flag` | `foo bar`, `--flag` |
| `"foo bar" baz --flag` | `foo bar`, `baz`, `--flag` |


## Configuration

Expand Down
29 changes: 27 additions & 2 deletions lua/telescope-live-grep-args/prompt_parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,16 @@ local non_autoquote_chars = {

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

autoquote = autoquote or autoquote == nil
autoquote = autoquote or true
local first_char = string.sub(prompt, 1, 1)
if autoquote and non_autoquote_chars[first_char] == nil then return { prompt } end
if non_autoquote_chars[first_char] ~= nil then
autoquote = false;
end
if autoquote == true then return { prompt } end

local str = {
chars = prompt,
Expand All @@ -100,6 +104,27 @@ M.parse = function(prompt, autoquote)
local parts = {}
local current_arg = nil

if autoquote == "smart" then
local sep_begin, sep_end = string.find(prompt, "%-%-");

-- potentially ignore one space before seperator
if sep_begin and sep_begin > 1 and string.sub(prompt, sep_begin - 1, sep_begin - 1) == " " then
sep_begin = sep_begin - 1
end

sep_begin = sep_begin or string.len(prompt) + 1
sep_end = sep_end or string.len(prompt) + 1

local before = string.sub(prompt, 1, sep_begin - 1)
local after = string.sub(prompt, sep_end + 1)
parts = { before }
str = {
chars = after,
pos = 1,
len = string.len(after)
}
end

while str["pos"] <= str["len"] do
local safeguard = str["pos"]

Expand Down