From d23694e31fe4837cd9cfe6e8d529d91bd078c327 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Fri, 1 Mar 2013 19:15:05 +0100 Subject: [PATCH] Do not use a factory for window builtins highlighters This helps cleanup up the code, as factories no longer takes a window as parameter. --- src/commands.cc | 2 +- src/highlighter.hh | 3 +-- src/highlighters.cc | 41 +++++++++-------------------------------- src/window.cc | 13 ++++++++----- 4 files changed, 19 insertions(+), 40 deletions(-) diff --git a/src/commands.cc b/src/commands.cc index 7abf89743..e143accad 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -280,7 +280,7 @@ void add_highlighter(const CommandParameters& params, Context& context) : window.highlighters(); auto& factory = registry[name]; - group.append(factory(window, highlighter_params)); + group.append(factory(highlighter_params)); } void rm_highlighter(const CommandParameters& params, Context& context) diff --git a/src/highlighter.hh b/src/highlighter.hh index 926224d2a..29992c995 100644 --- a/src/highlighter.hh +++ b/src/highlighter.hh @@ -23,8 +23,7 @@ typedef std::function HighlighterFunc; typedef std::pair HighlighterAndId; typedef memoryview HighlighterParameters; -using HighlighterFactory = std::function; +using HighlighterFactory = std::function; struct HighlighterRegistry : FunctionRegistry, Singleton diff --git a/src/highlighters.cc b/src/highlighters.cc index ff6236012..5c58abe95 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -1,6 +1,5 @@ #include "highlighters.hh" #include "assert.hh" -#include "window.hh" #include "color_registry.hh" #include "highlighter_group.hh" #include "register_manager.hh" @@ -120,8 +119,7 @@ private: } }; -HighlighterAndId colorize_regex_factory(Window& window, - const HighlighterParameters params) +HighlighterAndId colorize_regex_factory(const HighlighterParameters params) { if (params.size() < 2) throw runtime_error("wrong parameter count"); @@ -184,8 +182,7 @@ private: RegexColorizer m_colorizer; }; -HighlighterAndId highlight_search_factory(Window& window, - const HighlighterParameters params) +HighlighterAndId highlight_search_factory(const HighlighterParameters params) { if (params.size() != 1) throw runtime_error("wrong parameter count"); @@ -201,9 +198,9 @@ HighlighterAndId highlight_search_factory(Window& window, } }; -void expand_tabulations(Window& window, DisplayBuffer& display_buffer) +void expand_tabulations(const OptionManager& options, DisplayBuffer& display_buffer) { - const int tabstop = window.options()["tabstop"].as_int(); + const int tabstop = options["tabstop"].as_int(); for (auto& line : display_buffer.lines()) { for (auto atom_it = line.begin(); atom_it != line.end(); ++atom_it) @@ -266,9 +263,9 @@ void show_line_numbers(DisplayBuffer& display_buffer) } } -void highlight_selections(Window& window, DisplayBuffer& display_buffer) +void highlight_selections(const SelectionList& selections, DisplayBuffer& display_buffer) { - for (auto& sel : window.selections()) + for (auto& sel : selections) { highlight_range(display_buffer, sel.begin(), sel.end(), false, [](DisplayAtom& atom) { atom.attribute |= Attributes::Underline; }); @@ -278,7 +275,7 @@ void highlight_selections(Window& window, DisplayBuffer& display_buffer) [](DisplayAtom& atom) { atom.attribute |= Attributes::Reverse; atom.attribute &= ~Attributes::Underline; }); } - const Selection& back = window.selections().back(); + const Selection& back = selections.back(); highlight_range(display_buffer, back.begin(), back.end(), false, [](DisplayAtom& atom) { atom.attribute |= Attributes::Bold; }); } @@ -318,8 +315,7 @@ class SimpleHighlighterFactory public: SimpleHighlighterFactory(const String& id) : m_id(id) {} - HighlighterAndId operator()(Window& window, - const HighlighterParameters& params) const + HighlighterAndId operator()(const HighlighterParameters& params) const { return HighlighterAndId(m_id, HighlighterFunc(highlighter_func)); } @@ -327,23 +323,7 @@ private: String m_id; }; -template -class WindowHighlighterFactory -{ -public: - WindowHighlighterFactory(const String& id) : m_id(id) {} - - HighlighterAndId operator()(Window& window, - const HighlighterParameters& params) const - { - return HighlighterAndId(m_id, std::bind(highlighter_func, std::ref(window), _1)); - } -private: - String m_id; -}; - -HighlighterAndId highlighter_group_factory(Window& window, - const HighlighterParameters& params) +HighlighterAndId highlighter_group_factory(const HighlighterParameters& params) { if (params.size() != 1) throw runtime_error("wrong parameter count"); @@ -355,9 +335,6 @@ void register_highlighters() { HighlighterRegistry& registry = HighlighterRegistry::instance(); - registry.register_func("highlight_selections", WindowHighlighterFactory("highlight_selections")); - registry.register_func("expand_tabs", WindowHighlighterFactory("expand_tabs")); - registry.register_func("expand_unprintable", SimpleHighlighterFactory("expand_unprintable")); registry.register_func("number_lines", SimpleHighlighterFactory("number_lines")); registry.register_func("regex", colorize_regex_factory); registry.register_func("search", highlight_search_factory); diff --git a/src/window.cc b/src/window.cc index ea2c8d369..e38900a74 100644 --- a/src/window.cc +++ b/src/window.cc @@ -11,20 +11,23 @@ namespace Kakoune { +// Implementation in highlighters.cc +void highlight_selections(const SelectionList& selections, DisplayBuffer& display_buffer); +void expand_tabulations(const OptionManager& options, DisplayBuffer& display_buffer); +void expand_unprintable(DisplayBuffer& display_buffer); + Window::Window(Buffer& buffer) : Editor(buffer), m_hooks(buffer.hooks()), m_options(buffer.options()) { - HighlighterRegistry& registry = HighlighterRegistry::instance(); - Context hook_context{*this}; m_hooks.run_hook("WinCreate", buffer.name(), hook_context); m_options.register_watcher(*this); - m_builtin_highlighters.append(registry["expand_tabs"](*this, {})); - m_builtin_highlighters.append(registry["expand_unprintable"](*this, {})); - m_builtin_highlighters.append(registry["highlight_selections"](*this, {})); + m_builtin_highlighters.append({"tabulations", [this](DisplayBuffer& db) { expand_tabulations(m_options, db); }}); + m_builtin_highlighters.append({"unprintable", expand_unprintable}); + m_builtin_highlighters.append({"selections", [this](DisplayBuffer& db) { highlight_selections(selections(), db); }}); for (auto& option : m_options.flatten_options()) on_option_changed(option.first, option.second);