diff --git a/src/commands.cc b/src/commands.cc index c28985b5..76d4054f 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -9,8 +9,8 @@ #include "file.hh" #include "input_handler.hh" #include "string.hh" -#include "highlighter_registry.hh" -#include "filter_registry.hh" +#include "highlighter.hh" +#include "filter.hh" #include "register_manager.hh" #include "completion.hh" #include "shell_manager.hh" @@ -453,8 +453,8 @@ void add_highlighter(const CommandParameters& params, Context& context) window.highlighters().get_group(parser.option_value("group")) : window.highlighters(); - registry.add_highlighter_to_group(window, group, name, - highlighter_params); + auto& factory = registry[name]; + group.append(factory(window, highlighter_params)); } void rm_highlighter(const CommandParameters& params, Context& context) @@ -490,7 +490,8 @@ void add_filter(const CommandParameters& params, Context& context) editor.filters().get_group(parser.option_value("group")) : editor.filters(); - registry.add_filter_to_group(group, name, filter_params); + auto& factory = registry[name]; + group.append(factory(filter_params)); } void rm_filter(const CommandParameters& params, Context& context) @@ -809,7 +810,7 @@ void register_commands() if (token_to_complete == 1 and params[0] == "-group") return w.highlighters().complete_group_id(arg, pos_in_token); else if (token_to_complete == 0 or (token_to_complete == 2 and params[0] == "-group")) - return HighlighterRegistry::instance().complete_highlighter(arg, pos_in_token); + return HighlighterRegistry::instance().complete_name(arg, pos_in_token); else return CandidateList(); }); @@ -837,7 +838,7 @@ void register_commands() if (token_to_complete == 1 and params[0] == "-group") return w.filters().complete_group_id(arg, pos_in_token); else if (token_to_complete == 0 or (token_to_complete == 2 and params[0] == "-group")) - return FilterRegistry::instance().complete_filter(arg, pos_in_token); + return FilterRegistry::instance().complete_name(arg, pos_in_token); else return CandidateList(); }); diff --git a/src/filter.hh b/src/filter.hh index 6f54d496..28fa0202 100644 --- a/src/filter.hh +++ b/src/filter.hh @@ -1,10 +1,14 @@ #ifndef filter_hh_INCLUDED #define filter_hh_INCLUDED -#include "string.hh" -#include "selection.hh" #include +#include "string.hh" +#include "utils.hh" +#include "memoryview.hh" +#include "selection.hh" +#include "function_registry.hh" + namespace Kakoune { @@ -18,6 +22,13 @@ class BufferIterator; using FilterFunc = std::function; using FilterAndId = std::pair; +using FilterParameters = memoryview; +using FilterFactory = std::function; + +struct FilterRegistry : FunctionRegistry, + Singleton +{}; + } #endif // filter_hh_INCLUDED diff --git a/src/filter_registry.cc b/src/filter_registry.cc deleted file mode 100644 index 1a2a1754..00000000 --- a/src/filter_registry.cc +++ /dev/null @@ -1,39 +0,0 @@ -#include "filter_registry.hh" - -#include "exception.hh" -#include "filter_group.hh" - -namespace Kakoune -{ - -struct factory_not_found : public runtime_error -{ - factory_not_found(const String& name) - : runtime_error("filter factory not found '" + name + "'") {} -}; - -void FilterRegistry::register_factory(const String& name, - const FilterFactory& factory) -{ - assert(not m_factories.contains(name)); - m_factories.append(std::make_pair(name, factory)); -} - -void FilterRegistry::add_filter_to_group(FilterGroup& group, - const String& name, - const FilterParameters& parameters) -{ - auto it = m_factories.find(name); - if (it == m_factories.end()) - throw factory_not_found(name); - - group.append(it->second(parameters)); -} - -CandidateList FilterRegistry::complete_filter(const String& prefix, - ByteCount cursor_pos) -{ - return m_factories.complete_id(prefix, cursor_pos); -} - -} diff --git a/src/filter_registry.hh b/src/filter_registry.hh deleted file mode 100644 index 0756091c..00000000 --- a/src/filter_registry.hh +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef filter_registry_h_INCLUDED -#define filter_registry_h_INCLUDED - -#include - -#include "string.hh" -#include "filter.hh" -#include "utils.hh" -#include "completion.hh" -#include "memoryview.hh" -#include "idvaluemap.hh" - -namespace Kakoune -{ - -class FilterGroup; - -typedef memoryview FilterParameters; - -typedef std::function FilterFactory; - -class FilterRegistry : public Singleton -{ -public: - void register_factory(const String& name, - const FilterFactory& factory); - - void add_filter_to_group(FilterGroup& group, - const String& factory_name, - const FilterParameters& parameters); - - CandidateList complete_filter(const String& prefix, - ByteCount cursor_pos); - -private: - idvaluemap m_factories; -}; - -} - -#endif // filter_registry_h_INCLUDED - diff --git a/src/filters.cc b/src/filters.cc index bd24fe23..4503aecf 100644 --- a/src/filters.cc +++ b/src/filters.cc @@ -1,5 +1,4 @@ #include "filters.hh" -#include "filter_registry.hh" #include "buffer.hh" #include "filter_group.hh" @@ -136,11 +135,11 @@ void register_filters() { FilterRegistry& registry = FilterRegistry::instance(); - registry.register_factory("preserve_indent", SimpleFilterFactory("preserve_indent")); - registry.register_factory("cleanup_whitespaces", SimpleFilterFactory("cleanup_whitespaces")); - registry.register_factory("expand_tabulations", SimpleFilterFactory("expand_tabulations")); - registry.register_factory("regex", regex_filter_factory); - registry.register_factory("group", filter_group_factory); + registry.register_func("preserve_indent", SimpleFilterFactory("preserve_indent")); + registry.register_func("cleanup_whitespaces", SimpleFilterFactory("cleanup_whitespaces")); + registry.register_func("expand_tabulations", SimpleFilterFactory("expand_tabulations")); + registry.register_func("regex", regex_filter_factory); + registry.register_func("group", filter_group_factory); } } diff --git a/src/filters.hh b/src/filters.hh index 0d88da46..4242b013 100644 --- a/src/filters.hh +++ b/src/filters.hh @@ -1,6 +1,8 @@ #ifndef filters_hh_INCLUDED #define filters_hh_INCLUDED +#include "filter.hh" + namespace Kakoune { diff --git a/src/function_registry.hh b/src/function_registry.hh new file mode 100644 index 00000000..058c4aa3 --- /dev/null +++ b/src/function_registry.hh @@ -0,0 +1,46 @@ +#ifndef function_registry_h_INCLUDED +#define function_registry_h_INCLUDED + +#include "string.hh" +#include "completion.hh" +#include "idvaluemap.hh" + +namespace Kakoune +{ + +struct function_not_found : runtime_error +{ + function_not_found(const String& name) + : runtime_error("'" + name + "' not found") {} +}; + +template +class FunctionRegistry +{ +public: + void register_func(const String& name, const FunctionType& function) + { + assert(not m_functions.contains(name)); + m_functions.append(std::make_pair(name, function)); + } + + 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) + { + return m_functions.complete_id(prefix, cursor_pos); + } + +private: + idvaluemap m_functions; +}; + +} + +#endif // function_registry_h_INCLUDED diff --git a/src/highlighter.hh b/src/highlighter.hh index 5f6d58d9..926224d2 100644 --- a/src/highlighter.hh +++ b/src/highlighter.hh @@ -1,14 +1,18 @@ #ifndef highlighter_hh_INCLUDED #define highlighter_hh_INCLUDED -#include "string.hh" #include + +#include "string.hh" +#include "utils.hh" #include "memoryview.hh" +#include "function_registry.hh" namespace Kakoune { class DisplayBuffer; +class Window; // An Highlighter is a function which mutates a DisplayBuffer in order to // change the visual representation of a file. It could be changing text @@ -19,6 +23,12 @@ typedef std::function HighlighterFunc; typedef std::pair HighlighterAndId; typedef memoryview HighlighterParameters; +using HighlighterFactory = std::function; + +struct HighlighterRegistry : FunctionRegistry, + Singleton +{}; } diff --git a/src/highlighter_registry.cc b/src/highlighter_registry.cc deleted file mode 100644 index 90cbe36f..00000000 --- a/src/highlighter_registry.cc +++ /dev/null @@ -1,41 +0,0 @@ -#include "highlighter_registry.hh" - -#include "exception.hh" -#include "window.hh" -#include "highlighters.hh" - -namespace Kakoune -{ - -struct factory_not_found : public runtime_error -{ - factory_not_found(const String& name) - : runtime_error("highlighter factory not found '" + name + "'") {} -}; - -void HighlighterRegistry::register_factory(const String& name, - const HighlighterFactory& factory) -{ - assert(not m_factories.contains(name)); - m_factories.append(std::make_pair(name, factory)); -} - -void HighlighterRegistry::add_highlighter_to_group(Window& window, - HighlighterGroup& group, - const String& name, - const HighlighterParameters& parameters) -{ - auto it = m_factories.find(name); - if (it == m_factories.end()) - throw factory_not_found(name); - - group.append(it->second(window, parameters)); -} - -CandidateList HighlighterRegistry::complete_highlighter(const String& prefix, - ByteCount cursor_pos) -{ - return m_factories.complete_id(prefix, cursor_pos); -} - -} diff --git a/src/highlighter_registry.hh b/src/highlighter_registry.hh deleted file mode 100644 index 9690318d..00000000 --- a/src/highlighter_registry.hh +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef highlighter_registry_h_INCLUDED -#define highlighter_registry_h_INCLUDED - -#include "string.hh" -#include - -#include "highlighter.hh" -#include "utils.hh" -#include "completion.hh" -#include "idvaluemap.hh" - -namespace Kakoune -{ - -class Window; -class HighlighterGroup; - -typedef std::function HighlighterFactory; - -class HighlighterRegistry : public Singleton -{ -public: - void register_factory(const String& name, - const HighlighterFactory& factory); - - void add_highlighter_to_group(Window& window, - HighlighterGroup& group, - const String& factory_name, - const HighlighterParameters& parameters); - - CandidateList complete_highlighter(const String& prefix, - ByteCount cursor_pos); - -private: - idvaluemap m_factories; -}; - -} - -#endif // highlighter_registry_h_INCLUDED diff --git a/src/highlighters.cc b/src/highlighters.cc index ac66e50f..ba2a74af 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -1,7 +1,6 @@ #include "highlighters.hh" #include "assert.hh" #include "window.hh" -#include "highlighter_registry.hh" #include "color_registry.hh" #include "highlighter_group.hh" #include "string.hh" @@ -272,11 +271,11 @@ void register_highlighters() { HighlighterRegistry& registry = HighlighterRegistry::instance(); - registry.register_factory("highlight_selections", WindowHighlighterFactory("highlight_selections")); - registry.register_factory("expand_tabs", WindowHighlighterFactory("expand_tabs")); - registry.register_factory("number_lines", WindowHighlighterFactory("number_lines")); - registry.register_factory("regex", colorize_regex_factory); - registry.register_factory("group", highlighter_group_factory); + registry.register_func("highlight_selections", WindowHighlighterFactory("highlight_selections")); + registry.register_func("expand_tabs", WindowHighlighterFactory("expand_tabs")); + registry.register_func("number_lines", WindowHighlighterFactory("number_lines")); + registry.register_func("regex", colorize_regex_factory); + registry.register_func("group", highlighter_group_factory); } } diff --git a/src/highlighters.hh b/src/highlighters.hh index 53738e94..83c9dbd7 100644 --- a/src/highlighters.hh +++ b/src/highlighters.hh @@ -1,6 +1,8 @@ #ifndef highlighters_hh_INCLUDED #define highlighters_hh_INCLUDED +#include "highlighter.hh" + namespace Kakoune { diff --git a/src/main.cc b/src/main.cc index d0606f21..25357773 100644 --- a/src/main.cc +++ b/src/main.cc @@ -9,9 +9,7 @@ #include "assert.hh" #include "debug.hh" #include "highlighters.hh" -#include "highlighter_registry.hh" #include "filters.hh" -#include "filter_registry.hh" #include "hook_manager.hh" #include "option_manager.hh" #include "event_manager.hh" diff --git a/src/window.cc b/src/window.cc index ae3e0ca5..25dc5497 100644 --- a/src/window.cc +++ b/src/window.cc @@ -1,7 +1,7 @@ #include "window.hh" #include "assert.hh" -#include "highlighter_registry.hh" +#include "highlighter.hh" #include "hook_manager.hh" #include "context.hh" @@ -21,8 +21,8 @@ Window::Window(Buffer& buffer) m_hooks.run_hook("WinCreate", buffer.name(), Context(*this)); m_options.register_watcher(*this); - registry.add_highlighter_to_group(*this, m_highlighters, "expand_tabs", HighlighterParameters()); - registry.add_highlighter_to_group(*this, m_highlighters, "highlight_selections", HighlighterParameters()); + m_highlighters.append(registry["expand_tabs"](*this, {})); + m_highlighters.append(registry["highlight_selections"](*this, {})); for (auto& option : m_options.flatten_options()) on_option_changed(option.first, option.second);