summaryrefslogtreecommitdiff
path: root/src/alias_registry.cc
blob: 4263c317a0822d6f43303845565699613f177a54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "alias_registry.hh"

#include "command_manager.hh"

namespace Kakoune
{

void AliasRegistry::add_alias(String alias, String command)
{
    kak_assert(not alias.empty());
    kak_assert(CommandManager::instance().command_defined(command));
    auto it = m_aliases.find(alias);
    if (it == m_aliases.end())
        m_aliases.insert({std::move(alias), std::move(command) });
    else
        it->value = std::move(command);
}

void AliasRegistry::remove_alias(StringView alias)
{
    m_aliases.remove(alias);
}

StringView AliasRegistry::operator[](StringView alias) const
{
    auto it = m_aliases.find(alias);
    if (it != m_aliases.end())
        return it->value;
    else if (m_parent)
        return (*m_parent)[alias];
    else
        return StringView{};
}

Vector<StringView> AliasRegistry::aliases_for(StringView command) const
{
    Vector<StringView> res;
    if (m_parent)
        res = m_parent->aliases_for(command);

    for (auto& alias : m_aliases)
    {
        if (alias.value == command)
            res.emplace_back(alias.key);
    }

    return res;
}

}