2012-11-23 13:40:20 +01:00
|
|
|
#ifndef function_registry_h_INCLUDED
|
|
|
|
#define function_registry_h_INCLUDED
|
|
|
|
|
|
|
|
#include "completion.hh"
|
2013-11-18 23:24:31 +01:00
|
|
|
#include "id_map.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
#include "string.hh"
|
2012-11-23 13:40:20 +01:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
struct function_not_found : runtime_error
|
|
|
|
{
|
|
|
|
function_not_found(const String& name)
|
|
|
|
: runtime_error("'" + name + "' not found") {}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename FunctionType>
|
|
|
|
class FunctionRegistry
|
|
|
|
{
|
|
|
|
public:
|
2014-04-21 11:50:09 +02:00
|
|
|
void register_func(StringView name, const FunctionType& function)
|
2012-11-23 13:40:20 +01:00
|
|
|
{
|
2013-11-18 23:17:50 +01:00
|
|
|
kak_assert(not m_functions.contains(name));
|
|
|
|
m_functions.append(std::make_pair(name, function));
|
2012-11-23 13:40:20 +01:00
|
|
|
}
|
|
|
|
|
2014-04-21 11:50:09 +02:00
|
|
|
const FunctionType& operator[](StringView name) const
|
2012-11-23 13:40:20 +01:00
|
|
|
{
|
|
|
|
auto it = m_functions.find(name);
|
|
|
|
if (it == m_functions.end())
|
|
|
|
throw function_not_found(name);
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2014-04-18 15:02:14 +02:00
|
|
|
CandidateList complete_name(StringView prefix, ByteCount cursor_pos)
|
2012-11-23 13:40:20 +01:00
|
|
|
{
|
2013-11-18 23:17:50 +01:00
|
|
|
return m_functions.complete_id(prefix, cursor_pos);
|
2012-11-23 13:40:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-11-18 23:24:31 +01:00
|
|
|
id_map<FunctionType> m_functions;
|
2012-11-23 13:40:20 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // function_registry_h_INCLUDED
|