From 0bfdaf7d3e79e7ffc8465bd382119141afdc9164 Mon Sep 17 00:00:00 2001 From: Delapouite Date: Mon, 4 Jun 2018 17:05:40 +0200 Subject: [PATCH] Improve unmap command key-completer --- src/commands.cc | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/commands.cc b/src/commands.cc index 213926f7..c5c8e08c 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -1131,6 +1131,8 @@ KeymapMode parse_keymap_mode(StringView str, const KeymapManager::UserModeList& return (KeymapMode)(std::distance(user_modes.begin(), it) + offset); } +static constexpr auto modes = { "normal", "insert", "menu", "prompt", "goto", "view", "user", "object" }; + const CommandDesc debug_cmd = { "debug", nullptr, @@ -1203,8 +1205,6 @@ const CommandDesc debug_cmd = { else if (parser[0] == "mappings") { auto& keymaps = context.keymaps(); - auto modes = {"normal", "insert", "prompt", "menu", - "goto", "view", "user", "object"}; auto user_modes = keymaps.user_modes(); write_to_debug_buffer("Mappings:"); for (auto& mode : concatenated(modes, user_modes) | gather>()) @@ -1448,7 +1448,8 @@ const CommandDesc declare_option_cmd = { } }; -auto map_key_completer = +template +static auto map_key_completer = [](const Context& context, CompletionFlags flags, CommandParameters params, size_t token_to_complete, ByteCount pos_in_token) -> Completions @@ -1456,14 +1457,23 @@ auto map_key_completer = if (token_to_complete == 0) return { 0_byte, params[0].length(), complete(params[0], pos_in_token, scopes) }; - if (token_to_complete == 1) { - static constexpr auto modes = { "normal", "insert", "menu", "prompt", "goto", "view", "user", "object" }; auto& user_modes = get_scope(params[0], context).keymaps().user_modes(); return { 0_byte, params[1].length(), complete(params[1], pos_in_token, concatenated(modes, user_modes) | gather>()) }; } + if (unmap and token_to_complete == 2) + { + KeymapManager& keymaps = get_scope(params[0], context).keymaps(); + KeymapMode keymap_mode = parse_keymap_mode(params[1], keymaps.user_modes()); + KeyList keys = keymaps.get_mapped_keys(keymap_mode); + + return { 0_byte, params[2].length(), + complete(params[2], pos_in_token, + keys | transform([](Key k) { return key_to_str(k); }) + | gather>()) }; + } return {}; }; @@ -1486,7 +1496,7 @@ const CommandDesc map_key_cmd = { }, CommandFlags::None, CommandHelper{}, - map_key_completer, + map_key_completer, [](const ParametersParser& parser, Context& context, const ShellContext&) { KeymapManager& keymaps = get_scope(parser[0], context).keymaps(); @@ -1519,7 +1529,7 @@ const CommandDesc unmap_key_cmd = { ParameterDesc{{}, ParameterDesc::Flags::None, 3, 4}, CommandFlags::None, CommandHelper{}, - map_key_completer, + map_key_completer, [](const ParametersParser& parser, Context& context, const ShellContext&) { KeymapManager& keymaps = get_scope(parser[0], context).keymaps();