From 6d111d4bd78f340b397a5f45dc8c22a245f676e0 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 20 Feb 2018 22:09:03 +1100 Subject: [PATCH] Re-introduce aliases in command name completion Aliases are considered again in command name completion, but only if they are more than 3 charactes long. This should prevent cluttering with aliases while still letting long ones being completed. --- src/alias_registry.hh | 16 +++++++++++++++- src/command_manager.cc | 6 +++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/alias_registry.hh b/src/alias_registry.hh index 49571c1e..952669a0 100644 --- a/src/alias_registry.hh +++ b/src/alias_registry.hh @@ -18,12 +18,26 @@ public: Vector aliases_for(StringView command) const; + auto flatten_aliases() const + { + auto merge = [](auto&& first, const AliasMap& second) { + return concatenated(std::forward(first) + | filter([&second](auto& i) { return not second.contains(i.key); }), + second); + }; + static const AliasMap empty; + auto& parent = m_parent ? m_parent->m_aliases : empty; + auto& grand_parent = (m_parent and m_parent->m_parent) ? m_parent->m_parent->m_aliases : empty; + return merge(merge(grand_parent, parent), m_aliases); + } + private: friend class Scope; AliasRegistry() {} SafePtr m_parent; - HashMap m_aliases; + using AliasMap = HashMap; + AliasMap m_aliases; }; } diff --git a/src/command_manager.cc b/src/command_manager.cc index ed489183..45200e11 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -540,7 +540,11 @@ Completions CommandManager::complete_command_name(const Context& context, String | filter([](const CommandMap::Item& cmd) { return not (cmd.value.flags & CommandFlags::Hidden); }) | transform(std::mem_fn(&CommandMap::Item::key)); - return {0, query.length(), Kakoune::complete(query, query.length(), commands)}; + auto aliases = context.aliases().flatten_aliases() + | transform(std::mem_fn(&HashItem::key)) + | filter([](auto& alias) { return alias.length() > 3; }); + + return {0, query.length(), Kakoune::complete(query, query.length(), concatenated(commands, aliases))}; } Completions CommandManager::complete(const Context& context,