Do not use a factory for window builtins highlighters

This helps cleanup up the code, as factories no longer takes
a window as parameter.
This commit is contained in:
Maxime Coste 2013-03-01 19:15:05 +01:00
parent 8ef79c41bd
commit d23694e31f
4 changed files with 19 additions and 40 deletions

View File

@ -280,7 +280,7 @@ void add_highlighter(const CommandParameters& params, Context& context)
: window.highlighters(); : window.highlighters();
auto& factory = registry[name]; auto& factory = registry[name];
group.append(factory(window, highlighter_params)); group.append(factory(highlighter_params));
} }
void rm_highlighter(const CommandParameters& params, Context& context) void rm_highlighter(const CommandParameters& params, Context& context)

View File

@ -23,8 +23,7 @@ typedef std::function<void (DisplayBuffer& display_buffer)> HighlighterFunc;
typedef std::pair<String, HighlighterFunc> HighlighterAndId; typedef std::pair<String, HighlighterFunc> HighlighterAndId;
typedef memoryview<String> HighlighterParameters; typedef memoryview<String> HighlighterParameters;
using HighlighterFactory = std::function<HighlighterAndId (Window& window, using HighlighterFactory = std::function<HighlighterAndId (const HighlighterParameters& params)>;
const HighlighterParameters& params)>;
struct HighlighterRegistry : FunctionRegistry<HighlighterFactory>, struct HighlighterRegistry : FunctionRegistry<HighlighterFactory>,
Singleton<HighlighterRegistry> Singleton<HighlighterRegistry>

View File

@ -1,6 +1,5 @@
#include "highlighters.hh" #include "highlighters.hh"
#include "assert.hh" #include "assert.hh"
#include "window.hh"
#include "color_registry.hh" #include "color_registry.hh"
#include "highlighter_group.hh" #include "highlighter_group.hh"
#include "register_manager.hh" #include "register_manager.hh"
@ -120,8 +119,7 @@ private:
} }
}; };
HighlighterAndId colorize_regex_factory(Window& window, HighlighterAndId colorize_regex_factory(const HighlighterParameters params)
const HighlighterParameters params)
{ {
if (params.size() < 2) if (params.size() < 2)
throw runtime_error("wrong parameter count"); throw runtime_error("wrong parameter count");
@ -184,8 +182,7 @@ private:
RegexColorizer m_colorizer; RegexColorizer m_colorizer;
}; };
HighlighterAndId highlight_search_factory(Window& window, HighlighterAndId highlight_search_factory(const HighlighterParameters params)
const HighlighterParameters params)
{ {
if (params.size() != 1) if (params.size() != 1)
throw runtime_error("wrong parameter count"); 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& line : display_buffer.lines())
{ {
for (auto atom_it = line.begin(); atom_it != line.end(); ++atom_it) 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, highlight_range(display_buffer, sel.begin(), sel.end(), false,
[](DisplayAtom& atom) { atom.attribute |= Attributes::Underline; }); [](DisplayAtom& atom) { atom.attribute |= Attributes::Underline; });
@ -278,7 +275,7 @@ void highlight_selections(Window& window, DisplayBuffer& display_buffer)
[](DisplayAtom& atom) { atom.attribute |= Attributes::Reverse; [](DisplayAtom& atom) { atom.attribute |= Attributes::Reverse;
atom.attribute &= ~Attributes::Underline; }); atom.attribute &= ~Attributes::Underline; });
} }
const Selection& back = window.selections().back(); const Selection& back = selections.back();
highlight_range(display_buffer, back.begin(), back.end(), false, highlight_range(display_buffer, back.begin(), back.end(), false,
[](DisplayAtom& atom) { atom.attribute |= Attributes::Bold; }); [](DisplayAtom& atom) { atom.attribute |= Attributes::Bold; });
} }
@ -318,8 +315,7 @@ class SimpleHighlighterFactory
public: public:
SimpleHighlighterFactory(const String& id) : m_id(id) {} SimpleHighlighterFactory(const String& id) : m_id(id) {}
HighlighterAndId operator()(Window& window, HighlighterAndId operator()(const HighlighterParameters& params) const
const HighlighterParameters& params) const
{ {
return HighlighterAndId(m_id, HighlighterFunc(highlighter_func)); return HighlighterAndId(m_id, HighlighterFunc(highlighter_func));
} }
@ -327,23 +323,7 @@ private:
String m_id; String m_id;
}; };
template<void (*highlighter_func)(Window&, DisplayBuffer&)> HighlighterAndId highlighter_group_factory(const HighlighterParameters& params)
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)
{ {
if (params.size() != 1) if (params.size() != 1)
throw runtime_error("wrong parameter count"); throw runtime_error("wrong parameter count");
@ -355,9 +335,6 @@ void register_highlighters()
{ {
HighlighterRegistry& registry = HighlighterRegistry::instance(); HighlighterRegistry& registry = HighlighterRegistry::instance();
registry.register_func("highlight_selections", WindowHighlighterFactory<highlight_selections>("highlight_selections"));
registry.register_func("expand_tabs", WindowHighlighterFactory<expand_tabulations>("expand_tabs"));
registry.register_func("expand_unprintable", SimpleHighlighterFactory<expand_unprintable>("expand_unprintable"));
registry.register_func("number_lines", SimpleHighlighterFactory<show_line_numbers>("number_lines")); registry.register_func("number_lines", SimpleHighlighterFactory<show_line_numbers>("number_lines"));
registry.register_func("regex", colorize_regex_factory); registry.register_func("regex", colorize_regex_factory);
registry.register_func("search", highlight_search_factory); registry.register_func("search", highlight_search_factory);

View File

@ -11,20 +11,23 @@
namespace Kakoune 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) Window::Window(Buffer& buffer)
: Editor(buffer), : Editor(buffer),
m_hooks(buffer.hooks()), m_hooks(buffer.hooks()),
m_options(buffer.options()) m_options(buffer.options())
{ {
HighlighterRegistry& registry = HighlighterRegistry::instance();
Context hook_context{*this}; Context hook_context{*this};
m_hooks.run_hook("WinCreate", buffer.name(), hook_context); m_hooks.run_hook("WinCreate", buffer.name(), hook_context);
m_options.register_watcher(*this); m_options.register_watcher(*this);
m_builtin_highlighters.append(registry["expand_tabs"](*this, {})); m_builtin_highlighters.append({"tabulations", [this](DisplayBuffer& db) { expand_tabulations(m_options, db); }});
m_builtin_highlighters.append(registry["expand_unprintable"](*this, {})); m_builtin_highlighters.append({"unprintable", expand_unprintable});
m_builtin_highlighters.append(registry["highlight_selections"](*this, {})); m_builtin_highlighters.append({"selections", [this](DisplayBuffer& db) { highlight_selections(selections(), db); }});
for (auto& option : m_options.flatten_options()) for (auto& option : m_options.flatten_options())
on_option_changed(option.first, option.second); on_option_changed(option.first, option.second);