Tweak completion quoting behaviour once again

Quote by wrapping in quotes if we are replacing the whole token,
using backspaces if the completion only adds to it.

This ensure that the inserted completion will be correctly parsed
once validated.

Fixes #4121
This commit is contained in:
Maxime Coste 2021-03-31 17:15:31 +11:00
parent c507863a00
commit d1e19727ff

View File

@ -669,16 +669,18 @@ Completions CommandManager::complete(const Context& context,
const auto& token = tokens.back();
auto requote = [](Completions completions, Token::Type token_type) {
if ((completions.flags & Completions::Flags::Quoted) or
completions.start != 0)
if (completions.flags & Completions::Flags::Quoted)
return completions;
if (token_type == Token::Type::Raw)
{
for (auto& c : completions.candidates)
const bool at_token_start = completions.start == 0;
for (auto& candidate : completions.candidates)
{
if (c.substr(0_byte, 1_byte) == "%" or any_of(c, [](auto i) { return contains("; \t'\"", i); }))
c = quote(c);
const StringView to_escape = ";\n \t";
if ((at_token_start and candidate.substr(0_byte, 1_byte) == "%") or
any_of(candidate, [&](auto c) { return contains(to_escape, c); }))
candidate = at_token_start ? quote(candidate) : escape(candidate, to_escape, '\\');
}
}
else if (token_type == Token::Type::RawQuoted)