2014-10-30 00:22:54 +01:00
|
|
|
#ifndef alias_registry_hh_INCLUDED
|
|
|
|
#define alias_registry_hh_INCLUDED
|
|
|
|
|
|
|
|
#include "safe_ptr.hh"
|
|
|
|
#include "string.hh"
|
2017-03-07 02:12:37 +01:00
|
|
|
#include "hash_map.hh"
|
2014-10-30 00:22:54 +01:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
class AliasRegistry : public SafeCountable
|
|
|
|
{
|
|
|
|
public:
|
2017-08-04 06:38:04 +02:00
|
|
|
AliasRegistry(AliasRegistry& parent) : SafeCountable{}, m_parent(&parent) {}
|
2014-10-30 00:22:54 +01:00
|
|
|
void add_alias(String alias, String command);
|
2015-09-16 23:32:02 +02:00
|
|
|
void remove_alias(StringView alias);
|
2017-03-17 00:08:10 +01:00
|
|
|
StringView operator[](StringView alias) const;
|
2014-10-30 00:22:54 +01:00
|
|
|
|
2015-01-12 14:58:41 +01:00
|
|
|
Vector<StringView> aliases_for(StringView command) const;
|
2014-10-30 00:22:54 +01:00
|
|
|
|
2018-02-20 12:09:03 +01:00
|
|
|
auto flatten_aliases() const
|
|
|
|
{
|
|
|
|
auto merge = [](auto&& first, const AliasMap& second) {
|
|
|
|
return concatenated(std::forward<decltype(first)>(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);
|
|
|
|
}
|
|
|
|
|
2014-10-30 00:22:54 +01:00
|
|
|
private:
|
2014-10-30 15:00:42 +01:00
|
|
|
friend class Scope;
|
2018-04-07 07:36:39 +02:00
|
|
|
AliasRegistry() = default;
|
2014-10-30 00:22:54 +01:00
|
|
|
|
2015-02-19 14:58:25 +01:00
|
|
|
SafePtr<AliasRegistry> m_parent;
|
2018-02-20 12:09:03 +01:00
|
|
|
using AliasMap = HashMap<String, String, MemoryDomain::Aliases>;
|
|
|
|
AliasMap m_aliases;
|
2014-10-30 00:22:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // alias_registry_hh_INCLUDED
|