From d9ea62666b2671bc1ee4ef2dd7debd6fe8965dcf Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 26 May 2022 19:42:15 +1000 Subject: [PATCH] Filter out switches when completing commands Fixes #4625 Fixes #4209 Fixes #4040 --- src/command_manager.cc | 6 ++++-- src/commands.cc | 20 ++++++++++---------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/command_manager.cc b/src/command_manager.cc index 266b832d..c051a4bf 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -760,7 +760,9 @@ Completions CommandManager::complete(const Context& context, auto& command = command_it->value; - if (token.content.substr(0_byte, 1_byte) == "-") + auto is_switch = [](StringView s) { return s.substr(0_byte, 1_byte) == "-"; }; + + if (is_switch(token.content)) { auto switches = Kakoune::complete(token.content.substr(1_byte), pos_in_token, command.param_desc.switches | @@ -771,7 +773,7 @@ Completions CommandManager::complete(const Context& context, if (not command.completer) return Completions{}; - auto params = tokens | skip(1) | transform(&Token::content) | gather(); + auto params = tokens | skip(1) | transform(&Token::content) | filter(std::not_fn(is_switch)) | gather(); auto index = params.size() - 1; return offset_pos(requote(command.completer(context, flags, params, index, pos_in_token), token.type), start); diff --git a/src/commands.cc b/src/commands.cc index 3cfd8b73..3cd5c3f5 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -1655,18 +1655,18 @@ const CommandDesc set_option_cmd = { static constexpr auto scopes = { "global", "buffer", "window", "current" }; - if (token_to_complete == start) - return { 0_byte, params[start].length(), - complete(params[start], pos_in_token, scopes) }; - else if (token_to_complete == start + 1) - return { 0_byte, params[start + 1].length(), - GlobalScope::instance().option_registry().complete_option_name(params[start + 1], pos_in_token) }; - else if (not add and token_to_complete == start + 2 and params[start + 2].empty() and - GlobalScope::instance().option_registry().option_exists(params[start + 1])) + if (token_to_complete == 0) + return { 0_byte, params[0].length(), + complete(params[0], pos_in_token, scopes) }; + else if (token_to_complete == 1) + return { 0_byte, params[1].length(), + GlobalScope::instance().option_registry().complete_option_name(params[1], pos_in_token) }; + else if (token_to_complete == 2 and params[2].empty() and + GlobalScope::instance().option_registry().option_exists(params[1])) { OptionManager& options = get_scope(params[start], context).options(); - return {0_byte, params[start + 2].length(), - {options[params[start + 1]].get_as_string(Quoting::Kakoune)}, + return {0_byte, params[2].length(), + {options[params[1]].get_as_string(Quoting::Kakoune)}, Completions::Flags::Quoted}; } return Completions{};