diff --git a/src/commands.cc b/src/commands.cc index 51e917f51..82d30840d 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -446,7 +446,7 @@ Completions add_highlighter_completer( if (token_to_complete == 1 and params[0] == "-group") 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")) - 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{}; } @@ -495,7 +495,10 @@ const CommandDesc add_highlighter_cmd = { auto& group = (parser.has_option("group")) ? get_highlighter(context, parser.option_value("group")) : 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)); } }; diff --git a/src/function_registry.hh b/src/function_registry.hh deleted file mode 100644 index cf0ea46e5..000000000 --- a/src/function_registry.hh +++ /dev/null @@ -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 -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::get_id); - return complete(prefix, cursor_pos, c); - } - -private: - id_map m_functions; -}; - -} - -#endif // function_registry_h_INCLUDED diff --git a/src/highlighter.hh b/src/highlighter.hh index bf67cbcb9..7d7f5a1d3 100644 --- a/src/highlighter.hh +++ b/src/highlighter.hh @@ -2,7 +2,9 @@ #define highlighter_hh_INCLUDED #include "coord.hh" -#include "function_registry.hh" +#include "completion.hh" +#include "exception.hh" +#include "id_map.hh" #include "memoryview.hh" #include "string.hh" #include "utils.hh" @@ -65,7 +67,7 @@ std::unique_ptr> make_simple_highlighter(T func) using HighlighterParameters = memoryview; using HighlighterFactory = std::function; -struct HighlighterRegistry : FunctionRegistry, +struct HighlighterRegistry : id_map, Singleton {}; diff --git a/src/highlighters.cc b/src/highlighters.cc index a60f70f39..cb4eb7c37 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -1122,18 +1122,18 @@ void register_highlighters() { HighlighterRegistry& registry = HighlighterRegistry::instance(); - registry.register_func("number_lines", simple_factory("number_lines", show_line_numbers)); - registry.register_func("show_matching", simple_factory("show_matching", show_matching_char)); - registry.register_func("show_whitespaces", simple_factory("show_whitespaces", show_whitespaces)); - registry.register_func("fill", create_fill_highlighter); - registry.register_func("regex", RegexHighlighter::create); - registry.register_func("regex_option", create_regex_option_highlighter); - registry.register_func("search", create_search_highlighter); - registry.register_func("group", create_highlighter_group); - registry.register_func("flag_lines", create_flag_lines_highlighter); - registry.register_func("line_option", create_line_option_highlighter); - registry.register_func("ref", create_reference_highlighter); - registry.register_func("regions", RegionsHighlighter::create); + registry.append({ "number_lines", simple_factory("number_lines", show_line_numbers) }); + registry.append({ "show_matching", simple_factory("show_matching", show_matching_char) }); + registry.append({ "show_whitespaces", simple_factory("show_whitespaces", show_whitespaces) }); + registry.append({ "fill", create_fill_highlighter }); + registry.append({ "regex", RegexHighlighter::create }); + registry.append({ "regex_option", create_regex_option_highlighter }); + registry.append({ "search", create_search_highlighter }); + registry.append({ "group", create_highlighter_group }); + registry.append({ "flag_lines", create_flag_lines_highlighter }); + registry.append({ "line_option", create_line_option_highlighter }); + registry.append({ "ref", create_reference_highlighter }); + registry.append({ "regions", RegionsHighlighter::create }); } }