WindowHighlighterFactory class which passes the Window to the higlighter function

This commit is contained in:
Maxime Coste 2012-04-03 13:22:07 +00:00
parent d1cc86d95d
commit 30bc1bad8f

View File

@ -178,35 +178,12 @@ void show_line_numbers(DisplayBuffer& display_buffer)
} }
} }
template<void (*highlighter_func)(DisplayBuffer&)> void highlight_selections(Window& window, DisplayBuffer& display_buffer)
class SimpleHighlighterFactory
{
public:
SimpleHighlighterFactory(const std::string& id) : m_id(id) {}
HighlighterAndId operator()(Window& window,
const HighlighterParameters& params) const
{
return HighlighterAndId(m_id, HighlighterFunc(highlighter_func));
}
private:
std::string m_id;
};
class SelectionsHighlighter
{
public:
SelectionsHighlighter(Window& window)
: m_window(window)
{
}
void operator()(DisplayBuffer& display_buffer)
{ {
typedef std::pair<BufferIterator, BufferIterator> BufferRange; typedef std::pair<BufferIterator, BufferIterator> BufferRange;
std::vector<BufferRange> selections; std::vector<BufferRange> selections;
for (auto& sel : m_window.selections()) for (auto& sel : window.selections())
selections.push_back(BufferRange(sel.begin(), sel.end())); selections.push_back(BufferRange(sel.begin(), sel.end()));
std::sort(selections.begin(), selections.end(), std::sort(selections.begin(), selections.end(),
@ -270,7 +247,7 @@ public:
} }
// invert selection last char // invert selection last char
for (auto& sel : m_window.selections()) for (auto& sel : window.selections())
{ {
const BufferIterator& last = sel.last(); const BufferIterator& last = sel.last();
@ -285,18 +262,38 @@ public:
atom_it->attribute() |= Attributes::Reverse; atom_it->attribute() |= Attributes::Reverse;
} }
} }
static HighlighterAndId create(Window& window,
const HighlighterParameters& params) template<void (*highlighter_func)(DisplayBuffer&)>
class SimpleHighlighterFactory
{ {
return HighlighterAndId("highlight_selections", public:
SelectionsHighlighter(window)); SimpleHighlighterFactory(const std::string& id) : m_id(id) {}
}
HighlighterAndId operator()(Window& window,
const HighlighterParameters& params) const
{
return HighlighterAndId(m_id, HighlighterFunc(highlighter_func));
}
private: private:
const Window& m_window; std::string m_id;
};
template<void (*highlighter_func)(Window&, DisplayBuffer&)>
class WindowHighlighterFactory
{
public:
WindowHighlighterFactory(const std::string& id) : m_id(id) {}
HighlighterAndId operator()(Window& window,
const HighlighterParameters& params) const
{
using namespace std::placeholders;
return HighlighterAndId(m_id, std::bind(highlighter_func, std::ref(window), _1));
}
private:
std::string m_id;
}; };
HighlighterAndId highlighter_group_factory(Window& window, HighlighterAndId highlighter_group_factory(Window& window,
@ -312,7 +309,7 @@ void register_highlighters()
{ {
HighlighterRegistry& registry = HighlighterRegistry::instance(); HighlighterRegistry& registry = HighlighterRegistry::instance();
registry.register_factory("highlight_selections", SelectionsHighlighter::create); registry.register_factory("highlight_selections", WindowHighlighterFactory<highlight_selections>("highlight_selections"));
registry.register_factory("expand_tabs", SimpleHighlighterFactory<expand_tabulations>("expand_tabs")); registry.register_factory("expand_tabs", SimpleHighlighterFactory<expand_tabulations>("expand_tabs"));
registry.register_factory("number_lines", SimpleHighlighterFactory<show_line_numbers>("number_lines")); registry.register_factory("number_lines", SimpleHighlighterFactory<show_line_numbers>("number_lines"));
registry.register_factory("regex", colorize_regex_factory); registry.register_factory("regex", colorize_regex_factory);