Use an id_map directly for HighlighterRegistry rather than the FunctionRegistry class

This commit is contained in:
Maxime Coste 2014-12-23 22:15:53 +00:00
parent c17fa7be14
commit fb611e2f62
4 changed files with 21 additions and 65 deletions

View File

@ -446,7 +446,7 @@ Completions add_highlighter_completer(
if (token_to_complete == 1 and params[0] == "-group") if (token_to_complete == 1 and params[0] == "-group")
return complete_highlighter(context, params[1], pos_in_token, true); return complete_highlighter(context, params[1], pos_in_token, true);
else if (token_to_complete == 0 or (token_to_complete == 2 and params[0] == "-group")) else if (token_to_complete == 0 or (token_to_complete == 2 and params[0] == "-group"))
return { 0_byte, arg.length(), HighlighterRegistry::instance().complete_name(arg, pos_in_token) }; return { 0_byte, arg.length(), complete(arg, pos_in_token, transformed(HighlighterRegistry::instance(), HighlighterRegistry::get_id)) };
return Completions{}; return Completions{};
} }
@ -495,7 +495,10 @@ const CommandDesc add_highlighter_cmd = {
auto& group = (parser.has_option("group")) ? auto& group = (parser.has_option("group")) ?
get_highlighter(context, parser.option_value("group")) get_highlighter(context, parser.option_value("group"))
: context.window().highlighters(); : context.window().highlighters();
group.add_child(registry[name](highlighter_params)); auto it = registry.find(name);
if (it == registry.end())
throw runtime_error("No such highlighter factory '" + name + "'");
group.add_child(it->second(highlighter_params));
} }
}; };

View File

@ -1,49 +0,0 @@
#ifndef function_registry_h_INCLUDED
#define function_registry_h_INCLUDED
#include "completion.hh"
#include "id_map.hh"
#include "string.hh"
#include "exception.hh"
#include "containers.hh"
namespace Kakoune
{
struct function_not_found : runtime_error
{
function_not_found(StringView name)
: runtime_error("'" + name + "' not found") {}
};
template<typename FunctionType>
class FunctionRegistry
{
public:
void register_func(StringView name, const FunctionType& function)
{
kak_assert(not m_functions.contains(name));
m_functions.append(std::make_pair(name, function));
}
const FunctionType& operator[](StringView name) const
{
auto it = m_functions.find(name);
if (it == m_functions.end())
throw function_not_found(name);
return it->second;
}
CandidateList complete_name(StringView prefix, ByteCount cursor_pos)
{
auto c = transformed(m_functions, id_map<FunctionType>::get_id);
return complete(prefix, cursor_pos, c);
}
private:
id_map<FunctionType> m_functions;
};
}
#endif // function_registry_h_INCLUDED

View File

@ -2,7 +2,9 @@
#define highlighter_hh_INCLUDED #define highlighter_hh_INCLUDED
#include "coord.hh" #include "coord.hh"
#include "function_registry.hh" #include "completion.hh"
#include "exception.hh"
#include "id_map.hh"
#include "memoryview.hh" #include "memoryview.hh"
#include "string.hh" #include "string.hh"
#include "utils.hh" #include "utils.hh"
@ -65,7 +67,7 @@ std::unique_ptr<SimpleHighlighter<T>> make_simple_highlighter(T func)
using HighlighterParameters = memoryview<String>; using HighlighterParameters = memoryview<String>;
using HighlighterFactory = std::function<HighlighterAndId (HighlighterParameters params)>; using HighlighterFactory = std::function<HighlighterAndId (HighlighterParameters params)>;
struct HighlighterRegistry : FunctionRegistry<HighlighterFactory>, struct HighlighterRegistry : id_map<HighlighterFactory>,
Singleton<HighlighterRegistry> Singleton<HighlighterRegistry>
{}; {};

View File

@ -1122,18 +1122,18 @@ void register_highlighters()
{ {
HighlighterRegistry& registry = HighlighterRegistry::instance(); HighlighterRegistry& registry = HighlighterRegistry::instance();
registry.register_func("number_lines", simple_factory("number_lines", show_line_numbers)); registry.append({ "number_lines", simple_factory("number_lines", show_line_numbers) });
registry.register_func("show_matching", simple_factory("show_matching", show_matching_char)); registry.append({ "show_matching", simple_factory("show_matching", show_matching_char) });
registry.register_func("show_whitespaces", simple_factory("show_whitespaces", show_whitespaces)); registry.append({ "show_whitespaces", simple_factory("show_whitespaces", show_whitespaces) });
registry.register_func("fill", create_fill_highlighter); registry.append({ "fill", create_fill_highlighter });
registry.register_func("regex", RegexHighlighter::create); registry.append({ "regex", RegexHighlighter::create });
registry.register_func("regex_option", create_regex_option_highlighter); registry.append({ "regex_option", create_regex_option_highlighter });
registry.register_func("search", create_search_highlighter); registry.append({ "search", create_search_highlighter });
registry.register_func("group", create_highlighter_group); registry.append({ "group", create_highlighter_group });
registry.register_func("flag_lines", create_flag_lines_highlighter); registry.append({ "flag_lines", create_flag_lines_highlighter });
registry.register_func("line_option", create_line_option_highlighter); registry.append({ "line_option", create_line_option_highlighter });
registry.register_func("ref", create_reference_highlighter); registry.append({ "ref", create_reference_highlighter });
registry.register_func("regions", RegionsHighlighter::create); registry.append({ "regions", RegionsHighlighter::create });
} }
} }