2012-11-23 16:40:20 +04:00
|
|
|
#ifndef function_registry_h_INCLUDED
|
|
|
|
#define function_registry_h_INCLUDED
|
|
|
|
|
|
|
|
#include "completion.hh"
|
2013-04-09 22:05:40 +04:00
|
|
|
#include "string.hh"
|
2012-11-23 16:40:20 +04:00
|
|
|
|
2013-11-18 02:48:29 +04:00
|
|
|
#include <unordered_map>
|
|
|
|
|
2012-11-23 16:40:20 +04: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:
|
|
|
|
void register_func(const String& name, const FunctionType& function)
|
|
|
|
{
|
2013-11-18 02:48:29 +04:00
|
|
|
kak_assert(m_functions.find(name) == m_functions.end());
|
|
|
|
m_functions[name] = function;
|
2012-11-23 16:40:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
const FunctionType& operator[](const String& name) const
|
|
|
|
{
|
|
|
|
auto it = m_functions.find(name);
|
|
|
|
if (it == m_functions.end())
|
|
|
|
throw function_not_found(name);
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
CandidateList complete_name(const String& prefix, ByteCount cursor_pos)
|
|
|
|
{
|
2013-11-18 02:48:29 +04:00
|
|
|
return complete_key(m_functions, prefix, cursor_pos);
|
2012-11-23 16:40:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-11-18 02:48:29 +04:00
|
|
|
std::unordered_map<String, FunctionType> m_functions;
|
2012-11-23 16:40:20 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // function_registry_h_INCLUDED
|