From 1d646c03f563e79ee3ea972e6b739a35c7b70740 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 24 Mar 2016 00:05:40 +0000 Subject: [PATCH] Add completion support for alias/unalias commands --- src/command_manager.cc | 22 +++++++++++++--------- src/command_manager.hh | 4 ++-- src/commands.cc | 24 ++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/command_manager.cc b/src/command_manager.cc index 4902f068..ff4b589a 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -513,16 +513,20 @@ CommandInfo CommandManager::command_info(const Context& context, StringView comm } Completions CommandManager::complete_command_name(const Context& context, - StringView query) const + StringView query, bool with_aliases) const { - auto candidates = Kakoune::complete( - query, query.length(), concatenated( - m_commands + auto commands = m_commands | filter([](const CommandMap::value_type& cmd) { return not (cmd.second.flags & CommandFlags::Hidden); }) - | transform([](const CommandMap::value_type& cmd) { return StringView{cmd.first}; }), - context.aliases().flatten_aliases() - | transform([](AliasRegistry::AliasDesc alias) { return alias.first; }))); + | transform([](const CommandMap::value_type& cmd) { return StringView{cmd.first}; }); + if (not with_aliases) + return {0, query.length(), Kakoune::complete(query, query.length(), commands)}; + + auto candidates = Kakoune::complete(query, query.length(), + concatenated(commands, + context.aliases().flatten_aliases() + | transform([](AliasRegistry::AliasDesc alias) + { return alias.first; }))); return {0, query.length(), std::move(candidates)}; } @@ -556,7 +560,7 @@ Completions CommandManager::complete(const Context& context, { auto cmd_start = is_last_token ? cursor_pos : tokens[tok_idx].begin(); StringView query = command_line.substr(cmd_start, cursor_pos - cmd_start); - return offset_pos(complete_command_name(context, query), cmd_start); + return offset_pos(complete_command_name(context, query, true), cmd_start); } kak_assert(not tokens.empty()); @@ -625,7 +629,7 @@ Completions CommandManager::complete(const Context& context, StringView prefix = params[token_to_complete].substr(0, pos_in_token); if (token_to_complete == 0) - return complete_command_name(context, prefix); + return complete_command_name(context, prefix, true); else { const String& command_name = params[0]; diff --git a/src/command_manager.hh b/src/command_manager.hh index 090ed0fb..4a1c4c01 100644 --- a/src/command_manager.hh +++ b/src/command_manager.hh @@ -124,14 +124,14 @@ public: CommandHelper helper = CommandHelper(), CommandCompleter completer = CommandCompleter()); + Completions complete_command_name(const Context& context, StringView query, bool with_aliases) const; + private: void execute_single_command(CommandParameters params, Context& context, const ShellContext& shell_context, CharCoord pos) const; - Completions complete_command_name(const Context& context, StringView query) const; - struct CommandDescriptor { Command command; diff --git a/src/commands.cc b/src/commands.cc index d92f3ee9..a483573b 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -867,6 +867,21 @@ const CommandDesc define_command_cmd = { define_command }; +static Completions complete_scope(const Context&, CompletionFlags, + const String& prefix, ByteCount cursor_pos) +{ + auto scopes = {"global", "buffer", "window"}; + return { 0_byte, cursor_pos, complete(prefix, cursor_pos, scopes) }; +} + + +static Completions complete_command_name(const Context& context, CompletionFlags, + const String& prefix, ByteCount cursor_pos) +{ + return CommandManager::instance().complete_command_name( + context, prefix.substr(0, cursor_pos), false); +} + const CommandDesc alias_cmd = { "alias", nullptr, @@ -874,7 +889,9 @@ const CommandDesc alias_cmd = { ParameterDesc{{}, ParameterDesc::Flags::None, 3, 3}, CommandFlags::None, CommandHelper{}, - CommandCompleter{}, + PerArgumentCommandCompleter({ + complete_scope, complete_nothing, complete_command_name + }), [](const ParametersParser& parser, Context& context, const ShellContext&) { if (not CommandManager::instance().command_defined(parser[2])) @@ -893,7 +910,10 @@ const CommandDesc unalias_cmd = { ParameterDesc{{}, ParameterDesc::Flags::None, 2, 3}, CommandFlags::None, CommandHelper{}, - CommandCompleter{}, + PerArgumentCommandCompleter({ + complete_scope, complete_nothing, complete_command_name + + }), [](const ParametersParser& parser, Context& context, const ShellContext&) { AliasRegistry& aliases = get_scope(parser[0], context).aliases();