From cb108b248a8458bf2d36869de86084d87fe08492 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 17 Nov 2015 13:55:27 +0000 Subject: [PATCH] Fix alias completion and overwritting --- src/alias_registry.cc | 20 +++++++++++++++++++- src/alias_registry.hh | 3 +-- src/command_manager.cc | 6 +++--- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/alias_registry.cc b/src/alias_registry.cc index bcb6b9fd..6e92c006 100644 --- a/src/alias_registry.cc +++ b/src/alias_registry.cc @@ -1,6 +1,7 @@ #include "alias_registry.hh" #include "command_manager.hh" +#include "containers.hh" namespace Kakoune { @@ -9,7 +10,11 @@ void AliasRegistry::add_alias(String alias, String command) { kak_assert(not alias.empty()); kak_assert(CommandManager::instance().command_defined(command)); - m_aliases.append({std::move(alias), std::move(command) }); + auto it = m_aliases.find(alias); + if (it == m_aliases.end()) + m_aliases.append({std::move(alias), std::move(command) }); + else + it->value = std::move(command); } void AliasRegistry::remove_alias(StringView alias) @@ -45,4 +50,17 @@ Vector AliasRegistry::aliases_for(StringView command) const return res; } +Vector> AliasRegistry::flatten_aliases() const +{ + Vector> res; + if (m_parent) + res = m_parent->flatten_aliases(); + for (auto& alias : m_aliases) + { + if (not contains(transformed(res, [](const std::pair& val) { return val.first; }), alias.key)) + res.emplace_back(alias.key, alias.value); + } + return res; +} + } diff --git a/src/alias_registry.hh b/src/alias_registry.hh index 4b9239b5..7f688e08 100644 --- a/src/alias_registry.hh +++ b/src/alias_registry.hh @@ -18,10 +18,9 @@ public: using AliasMap = IdMap; using iterator = AliasMap::const_iterator; - iterator begin() const { return m_aliases.begin(); } - iterator end() const { return m_aliases.end(); } Vector aliases_for(StringView command) const; + Vector> flatten_aliases() const; private: friend class Scope; diff --git a/src/command_manager.cc b/src/command_manager.cc index 03621184..0f689fa7 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -571,10 +571,10 @@ Completions CommandManager::complete(const Context& context, if (prefix_match(command.first, prefix)) result.candidates.push_back(command.first); } - for (auto& alias : context.aliases()) + for (auto& alias : context.aliases().flatten_aliases()) { - if (prefix_match(alias.key, prefix)) - result.candidates.push_back(alias.key); + if (prefix_match(alias.first, prefix)) + result.candidates.push_back(alias.first.str()); } std::sort(result.candidates.begin(), result.candidates.end()); return result;