Use a sorted IdMap for aliases

This commit is contained in:
Maxime Coste 2015-09-16 22:32:02 +01:00
parent bab95491c8
commit e7a8f64722
3 changed files with 13 additions and 13 deletions

View File

@ -9,21 +9,21 @@ void AliasRegistry::add_alias(String alias, String command)
{
kak_assert(not alias.empty());
kak_assert(CommandManager::instance().command_defined(command));
m_aliases[alias] = std::move(command);
m_aliases.append({std::move(alias), std::move(command) }, true);
}
void AliasRegistry::remove_alias(const String& alias)
void AliasRegistry::remove_alias(StringView alias)
{
auto it = m_aliases.find(alias);
if (it != m_aliases.end())
m_aliases.erase(it);
}
StringView AliasRegistry::operator[](const String& alias) const
StringView AliasRegistry::operator[](StringView alias) const
{
auto it = m_aliases.find(alias);
if (it != m_aliases.end())
return it->second;
return it->value;
else if (m_parent)
return (*m_parent)[alias];
else
@ -38,8 +38,8 @@ Vector<StringView> AliasRegistry::aliases_for(StringView command) const
for (auto& alias : m_aliases)
{
if (alias.second == command)
res.push_back(alias.first);
if (alias.value == command)
res.push_back(alias.key);
}
return res;

View File

@ -3,7 +3,7 @@
#include "safe_ptr.hh"
#include "string.hh"
#include "unordered_map.hh"
#include "id_map.hh"
namespace Kakoune
{
@ -13,10 +13,10 @@ class AliasRegistry : public SafeCountable
public:
AliasRegistry(AliasRegistry& parent) : m_parent(&parent) {}
void add_alias(String alias, String command);
void remove_alias(const String& alias);
StringView operator[](const String& name) const;
void remove_alias(StringView alias);
StringView operator[](StringView name) const;
using AliasMap = UnorderedMap<String, String, MemoryDomain::Aliases>;
using AliasMap = IdMap<String, MemoryDomain::Aliases>;
using iterator = AliasMap::const_iterator;
iterator begin() const { return m_aliases.begin(); }
iterator end() const { return m_aliases.end(); }
@ -28,7 +28,7 @@ private:
AliasRegistry() {}
SafePtr<AliasRegistry> m_parent;
UnorderedMap<String, String, MemoryDomain::Aliases> m_aliases;
AliasMap m_aliases;
};
}

View File

@ -572,8 +572,8 @@ Completions CommandManager::complete(const Context& context,
}
for (auto& alias : context.aliases())
{
if (prefix_match(alias.first, prefix))
result.candidates.push_back(alias.first);
if (prefix_match(alias.key, prefix))
result.candidates.push_back(alias.key);
}
std::sort(result.candidates.begin(), result.candidates.end());
return result;