From d1ac813f61e4f9e78077c43f8bf5bbda9dcef55f Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 16 Dec 2013 23:24:08 +0000 Subject: [PATCH] Pass a Context rather than a Window to highlighters --- src/client.cc | 2 +- src/context.cc | 4 +++ src/context.hh | 1 + src/highlighter.hh | 6 ++--- src/highlighters.cc | 66 ++++++++++++++++++++++----------------------- src/window.cc | 23 +++++++++------- src/window.hh | 4 +-- 7 files changed, 57 insertions(+), 49 deletions(-) diff --git a/src/client.cc b/src/client.cc index c9599403..a03c2e29 100644 --- a/src/client.cc +++ b/src/client.cc @@ -66,7 +66,7 @@ void Client::redraw_ifn() if (dimensions == DisplayCoord{0,0}) return; context().window().set_dimensions(dimensions); - context().window().update_display_buffer();; + context().window().update_display_buffer(context()); context().ui().draw(context().window().display_buffer(), m_status_line, generate_mode_line()); diff --git a/src/context.cc b/src/context.cc index 163beb16..a638a927 100644 --- a/src/context.cc +++ b/src/context.cc @@ -13,6 +13,10 @@ Context::Context(InputHandler& input_handler, Editor& editor, String name) : m_input_handler(&input_handler), m_editor(&editor), m_name(std::move(name)) {} +Context::Context(Editor& editor, String name) + : m_editor(&editor), + m_name(std::move(name)) {} + Context::~Context() = default; Buffer& Context::buffer() const diff --git a/src/context.hh b/src/context.hh index fd82b8f9..54c79490 100644 --- a/src/context.hh +++ b/src/context.hh @@ -26,6 +26,7 @@ class Context public: Context(); Context(InputHandler& input_handler, Editor& editor, String name = ""); + Context(Editor& editor, String name = ""); ~Context(); Context(const Context&) = delete; diff --git a/src/highlighter.hh b/src/highlighter.hh index bed5d235..beba2403 100644 --- a/src/highlighter.hh +++ b/src/highlighter.hh @@ -13,20 +13,20 @@ namespace Kakoune { class DisplayBuffer; -class Window; +class Context; // An Highlighter is a function which mutates a DisplayBuffer in order to // change the visual representation of a file. It could be changing text // color, adding information text (line numbering for example) or replacing // buffer content (folding for example) -typedef std::function HighlighterFunc; +typedef std::function HighlighterFunc; typedef std::pair HighlighterAndId; typedef memoryview HighlighterParameters; using HighlighterFactory = std::function; -using HighlighterGroup = FunctionGroup; +using HighlighterGroup = FunctionGroup; struct HighlighterRegistry : FunctionRegistry, Singleton diff --git a/src/highlighters.cc b/src/highlighters.cc index 3b8b273b..9fe1c9d9 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -3,12 +3,12 @@ #include "assert.hh" #include "color_registry.hh" #include "context.hh" +#include "display_buffer.hh" #include "option_types.hh" #include "register_manager.hh" #include "string.hh" #include "utf8.hh" #include "utf8_iterator.hh" -#include "window.hh" #include #include @@ -62,7 +62,7 @@ void highlight_range(DisplayBuffer& display_buffer, } template -void apply_highlighter(const Window& window, +void apply_highlighter(const Context& context, DisplayBuffer& display_buffer, BufferCoord begin, BufferCoord end, T&& highlighter) @@ -132,7 +132,7 @@ void apply_highlighter(const Window& window, } region_display.compute_range(); - highlighter(window, region_display); + highlighter(context, region_display); for (size_t i = 0; i < region_lines.size(); ++i) { @@ -154,9 +154,9 @@ public: { } - void operator()(const Window& window, DisplayBuffer& display_buffer) + void operator()(const Context& context, DisplayBuffer& display_buffer) { - auto& cache = update_cache_ifn(window.buffer(), display_buffer.range()); + auto& cache = update_cache_ifn(context.buffer(), display_buffer.range()); for (auto& match : cache.m_matches) { for (size_t n = 0; n < match.size(); ++n) @@ -253,9 +253,9 @@ public: DynamicRegexHighlighter(const ColorSpec& colors, RegexGetter getter) : m_regex_getter(getter), m_colors(colors), m_colorizer(Regex(), m_colors) {} - void operator()(const Window& window, DisplayBuffer& display_buffer) + void operator()(const Context& context, DisplayBuffer& display_buffer) { - Regex regex = m_regex_getter(window); + Regex regex = m_regex_getter(context); if (regex != m_last_regex) { m_last_regex = regex; @@ -263,7 +263,7 @@ public: m_colorizer = RegexColorizer{m_last_regex, m_colors}; } if (not m_last_regex.empty()) - m_colorizer(window, display_buffer); + m_colorizer(context, display_buffer); } private: @@ -280,7 +280,7 @@ HighlighterAndId highlight_search_factory(HighlighterParameters params) try { ColorSpec colors { { 0, &get_color(params[0]) } }; - auto get_regex = [](const Window&){ + auto get_regex = [](const Context&){ auto s = RegisterManager::instance()['/'].values(Context{}); return s.empty() ? Regex{} : Regex{s[0].begin(), s[0].end()}; }; @@ -302,14 +302,14 @@ HighlighterAndId highlight_regex_option_factory(HighlighterParameters params) // verify option type now GlobalOptions::instance()[option_name].get(); - auto get_regex = [option_name](const Window& window){ return window.options()[option_name].get(); }; + auto get_regex = [option_name](const Context& context){ return context.options()[option_name].get(); }; return {"hloption_" + option_name, DynamicRegexHighlighter{colors, get_regex}}; } -void expand_tabulations(const Window& window, DisplayBuffer& display_buffer) +void expand_tabulations(const Context& context, DisplayBuffer& display_buffer) { - const int tabstop = window.options()["tabstop"].get(); - auto& buffer = window.buffer(); + const int tabstop = context.options()["tabstop"].get(); + auto& buffer = context.buffer(); for (auto& line : display_buffer.lines()) { for (auto atom_it = line.begin(); atom_it != line.end(); ++atom_it) @@ -351,9 +351,9 @@ void expand_tabulations(const Window& window, DisplayBuffer& display_buffer) } } -void show_line_numbers(const Window& window, DisplayBuffer& display_buffer) +void show_line_numbers(const Context& context, DisplayBuffer& display_buffer) { - LineCount last_line = window.buffer().line_count(); + LineCount last_line = context.buffer().line_count(); int digit_count = 0; for (LineCount c = last_line; c > 0; c /= 10) ++digit_count; @@ -371,17 +371,17 @@ void show_line_numbers(const Window& window, DisplayBuffer& display_buffer) } } -void highlight_selections(const Window& window, DisplayBuffer& display_buffer) +void highlight_selections(const Context& context, DisplayBuffer& display_buffer) { - const auto& buffer = window.buffer(); - for (size_t i = 0; i < window.selections().size(); ++i) + const auto& buffer = context.buffer(); + for (size_t i = 0; i < context.selections().size(); ++i) { - auto& sel = window.selections()[i]; + auto& sel = context.selections()[i]; const bool forward = sel.first() <= sel.last(); BufferCoord begin = forward ? sel.first() : buffer.char_next(sel.last()); BufferCoord end = forward ? sel.last() : buffer.char_next(sel.first()); - const bool primary = (i == window.selections().main_index()); + const bool primary = (i == context.selections().main_index()); ColorPair sel_colors = get_color(primary ? "PrimarySelection" : "SecondarySelection"); highlight_range(display_buffer, begin, end, false, [&](DisplayAtom& atom) { atom.colors = sel_colors; }); @@ -391,9 +391,9 @@ void highlight_selections(const Window& window, DisplayBuffer& display_buffer) } } -void expand_unprintable(const Window& window, DisplayBuffer& display_buffer) +void expand_unprintable(const Context& context, DisplayBuffer& display_buffer) { - auto& buffer = window.buffer(); + auto& buffer = context.buffer(); for (auto& line : display_buffer.lines()) { for (auto atom_it = line.begin(); atom_it != line.end(); ++atom_it) @@ -436,9 +436,9 @@ HighlighterAndId flag_lines_factory(HighlighterParameters params) GlobalOptions::instance()[option_name].get>(); return {"hlflags_" + params[1], - [=](const Window& window, DisplayBuffer& display_buffer) + [=](const Context& context, DisplayBuffer& display_buffer) { - auto& lines_opt = window.options()[option_name]; + auto& lines_opt = context.options()[option_name]; auto& lines = lines_opt.get>(); CharCount width = 0; @@ -460,7 +460,7 @@ HighlighterAndId flag_lines_factory(HighlighterParameters params) }}; } -template +template class SimpleHighlighterFactory { public: @@ -493,8 +493,8 @@ HighlighterAndId reference_factory(HighlighterParameters params) DefinedHighlighters::instance().get_group(name, '/'); return HighlighterAndId(name, - [name](const Window& window, DisplayBuffer& display_buffer) - { DefinedHighlighters::instance().get_group(name, '/')(window, display_buffer); }); + [name](const Context& context, DisplayBuffer& display_buffer) + { DefinedHighlighters::instance().get_group(name, '/')(context, display_buffer); }); } template @@ -507,11 +507,11 @@ public: m_func(std::move(func)) {} - void operator()(const Window& window, DisplayBuffer& display_buffer) + void operator()(const Context& context, DisplayBuffer& display_buffer) { - auto& cache = update_cache_ifn(window.buffer()); + auto& cache = update_cache_ifn(context.buffer()); for (auto& pair : cache.regions) - m_func(window, display_buffer, pair.first, pair.second); + m_func(context, display_buffer, pair.first, pair.second); } private: Regex m_begin; @@ -570,7 +570,7 @@ HighlighterAndId region_factory(HighlighterParameters params) Regex end{params[1]}; const ColorPair colors = get_color(params[2]); - auto func = [colors](const Window&, DisplayBuffer& display_buffer, + auto func = [colors](const Context&, DisplayBuffer& display_buffer, BufferCoord begin, BufferCoord end) { highlight_range(display_buffer, begin, end, true, @@ -597,11 +597,11 @@ HighlighterAndId region_ref_factory(HighlighterParameters params) Regex end{params[1]}; const String& name = params[2]; - auto func = [name](const Window& window, DisplayBuffer& display_buffer, + auto func = [name](const Context& context, DisplayBuffer& display_buffer, BufferCoord begin, BufferCoord end) { HighlighterGroup& ref = DefinedHighlighters::instance().get_group(name, '/'); - apply_highlighter(window, display_buffer, begin, end, ref); + apply_highlighter(context, display_buffer, begin, end, ref); }; return HighlighterAndId("regionref(" + params[0] + "," + params[1] + "," + name + ")", diff --git a/src/window.cc b/src/window.cc index ef3e303d..b7e4b0c8 100644 --- a/src/window.cc +++ b/src/window.cc @@ -13,9 +13,9 @@ namespace Kakoune { // Implementation in highlighters.cc -void highlight_selections(const Window& window, DisplayBuffer& display_buffer); -void expand_tabulations(const Window& window, DisplayBuffer& display_buffer); -void expand_unprintable(const Window& window, DisplayBuffer& display_buffer); +void highlight_selections(const Context& context, DisplayBuffer& display_buffer); +void expand_tabulations(const Context& context, DisplayBuffer& display_buffer); +void expand_unprintable(const Context& context, DisplayBuffer& display_buffer); Window::Window(Buffer& buffer) : Editor(buffer), @@ -63,9 +63,10 @@ void Window::scroll(CharCount offset) m_position.column = std::max(0_char, m_position.column + offset); } -void Window::update_display_buffer() +void Window::update_display_buffer(const Context& context) { - scroll_to_keep_cursor_visible_ifn(); + kak_assert(&buffer() == &context.buffer()); + scroll_to_keep_selection_visible_ifn(context.selections().main()); DisplayBuffer::LineList& lines = m_display_buffer.lines(); lines.clear(); @@ -79,8 +80,8 @@ void Window::update_display_buffer() } m_display_buffer.compute_range(); - m_highlighters(*this, m_display_buffer); - m_builtin_highlighters(*this, m_display_buffer); + m_highlighters(context, m_display_buffer); + m_builtin_highlighters(context, m_display_buffer); // cut the start of the line before m_position.column for (auto& line : lines) @@ -142,10 +143,10 @@ static CharCount adapt_view_pos(const DisplayBuffer& display_buffer, return view_pos; } -void Window::scroll_to_keep_cursor_visible_ifn() +void Window::scroll_to_keep_selection_visible_ifn(const Range& selection) { - const auto& first = selections().main().first(); - const auto& last = selections().main().last(); + const auto& first = selection.first(); + const auto& last = selection.last(); const LineCount offset = std::min(options()["scrolloff"].get(), (m_dimensions.line - 1) / 2); @@ -243,6 +244,8 @@ BufferCoord Window::offset_coord(BufferCoord coord, LineCount offset) lines.emplace_back(AtomList{ {buffer(), coord.line, coord.line+1} }); lines.emplace_back(AtomList{ {buffer(), line, line+1} }); display_buffer.compute_range(); + + Context context(*this); m_highlighters(*this, display_buffer); m_builtin_highlighters(*this, display_buffer); diff --git a/src/window.hh b/src/window.hh index d95105e6..33fed537 100644 --- a/src/window.hh +++ b/src/window.hh @@ -37,7 +37,7 @@ public: void display_line_at(LineCount buffer_line, LineCount display_line); void scroll(LineCount offset); void scroll(CharCount offset); - void update_display_buffer(); + void update_display_buffer(const Context& context); DisplayCoord display_position(BufferCoord coord); @@ -60,7 +60,7 @@ private: void on_option_changed(const Option& option) override; - void scroll_to_keep_cursor_visible_ifn(); + void scroll_to_keep_selection_visible_ifn(const Range& selection); DisplayCoord m_position; DisplayCoord m_dimensions;