Pass a Context rather than a Window to highlighters

This commit is contained in:
Maxime Coste 2013-12-16 23:24:08 +00:00
parent ae75594d25
commit d1ac813f61
7 changed files with 57 additions and 49 deletions

View File

@ -66,7 +66,7 @@ void Client::redraw_ifn()
if (dimensions == DisplayCoord{0,0}) if (dimensions == DisplayCoord{0,0})
return; return;
context().window().set_dimensions(dimensions); context().window().set_dimensions(dimensions);
context().window().update_display_buffer();; context().window().update_display_buffer(context());
context().ui().draw(context().window().display_buffer(), context().ui().draw(context().window().display_buffer(),
m_status_line, generate_mode_line()); m_status_line, generate_mode_line());

View File

@ -13,6 +13,10 @@ Context::Context(InputHandler& input_handler, Editor& editor, String name)
: m_input_handler(&input_handler), m_editor(&editor), : m_input_handler(&input_handler), m_editor(&editor),
m_name(std::move(name)) {} m_name(std::move(name)) {}
Context::Context(Editor& editor, String name)
: m_editor(&editor),
m_name(std::move(name)) {}
Context::~Context() = default; Context::~Context() = default;
Buffer& Context::buffer() const Buffer& Context::buffer() const

View File

@ -26,6 +26,7 @@ class Context
public: public:
Context(); Context();
Context(InputHandler& input_handler, Editor& editor, String name = ""); Context(InputHandler& input_handler, Editor& editor, String name = "");
Context(Editor& editor, String name = "");
~Context(); ~Context();
Context(const Context&) = delete; Context(const Context&) = delete;

View File

@ -13,20 +13,20 @@ namespace Kakoune
{ {
class DisplayBuffer; class DisplayBuffer;
class Window; class Context;
// An Highlighter is a function which mutates a DisplayBuffer in order to // An Highlighter is a function which mutates a DisplayBuffer in order to
// change the visual representation of a file. It could be changing text // change the visual representation of a file. It could be changing text
// color, adding information text (line numbering for example) or replacing // color, adding information text (line numbering for example) or replacing
// buffer content (folding for example) // buffer content (folding for example)
typedef std::function<void (const Window& window, DisplayBuffer& display_buffer)> HighlighterFunc; typedef std::function<void (const Context& context, 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 (HighlighterParameters params)>; using HighlighterFactory = std::function<HighlighterAndId (HighlighterParameters params)>;
using HighlighterGroup = FunctionGroup<const Window&, DisplayBuffer&>; using HighlighterGroup = FunctionGroup<const Context&, DisplayBuffer&>;
struct HighlighterRegistry : FunctionRegistry<HighlighterFactory>, struct HighlighterRegistry : FunctionRegistry<HighlighterFactory>,
Singleton<HighlighterRegistry> Singleton<HighlighterRegistry>

View File

@ -3,12 +3,12 @@
#include "assert.hh" #include "assert.hh"
#include "color_registry.hh" #include "color_registry.hh"
#include "context.hh" #include "context.hh"
#include "display_buffer.hh"
#include "option_types.hh" #include "option_types.hh"
#include "register_manager.hh" #include "register_manager.hh"
#include "string.hh" #include "string.hh"
#include "utf8.hh" #include "utf8.hh"
#include "utf8_iterator.hh" #include "utf8_iterator.hh"
#include "window.hh"
#include <sstream> #include <sstream>
#include <locale> #include <locale>
@ -62,7 +62,7 @@ void highlight_range(DisplayBuffer& display_buffer,
} }
template<typename T> template<typename T>
void apply_highlighter(const Window& window, void apply_highlighter(const Context& context,
DisplayBuffer& display_buffer, DisplayBuffer& display_buffer,
BufferCoord begin, BufferCoord end, BufferCoord begin, BufferCoord end,
T&& highlighter) T&& highlighter)
@ -132,7 +132,7 @@ void apply_highlighter(const Window& window,
} }
region_display.compute_range(); region_display.compute_range();
highlighter(window, region_display); highlighter(context, region_display);
for (size_t i = 0; i < region_lines.size(); ++i) 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 (auto& match : cache.m_matches)
{ {
for (size_t n = 0; n < match.size(); ++n) for (size_t n = 0; n < match.size(); ++n)
@ -253,9 +253,9 @@ public:
DynamicRegexHighlighter(const ColorSpec& colors, RegexGetter getter) DynamicRegexHighlighter(const ColorSpec& colors, RegexGetter getter)
: m_regex_getter(getter), m_colors(colors), m_colorizer(Regex(), m_colors) {} : 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) if (regex != m_last_regex)
{ {
m_last_regex = regex; m_last_regex = regex;
@ -263,7 +263,7 @@ public:
m_colorizer = RegexColorizer{m_last_regex, m_colors}; m_colorizer = RegexColorizer{m_last_regex, m_colors};
} }
if (not m_last_regex.empty()) if (not m_last_regex.empty())
m_colorizer(window, display_buffer); m_colorizer(context, display_buffer);
} }
private: private:
@ -280,7 +280,7 @@ HighlighterAndId highlight_search_factory(HighlighterParameters params)
try try
{ {
ColorSpec colors { { 0, &get_color(params[0]) } }; ColorSpec colors { { 0, &get_color(params[0]) } };
auto get_regex = [](const Window&){ auto get_regex = [](const Context&){
auto s = RegisterManager::instance()['/'].values(Context{}); auto s = RegisterManager::instance()['/'].values(Context{});
return s.empty() ? Regex{} : Regex{s[0].begin(), s[0].end()}; 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 // verify option type now
GlobalOptions::instance()[option_name].get<Regex>(); GlobalOptions::instance()[option_name].get<Regex>();
auto get_regex = [option_name](const Window& window){ return window.options()[option_name].get<Regex>(); }; auto get_regex = [option_name](const Context& context){ return context.options()[option_name].get<Regex>(); };
return {"hloption_" + option_name, DynamicRegexHighlighter<decltype(get_regex)>{colors, get_regex}}; return {"hloption_" + option_name, DynamicRegexHighlighter<decltype(get_regex)>{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<int>(); const int tabstop = context.options()["tabstop"].get<int>();
auto& buffer = window.buffer(); auto& buffer = context.buffer();
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)
@ -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; int digit_count = 0;
for (LineCount c = last_line; c > 0; c /= 10) for (LineCount c = last_line; c > 0; c /= 10)
++digit_count; ++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(); const auto& buffer = context.buffer();
for (size_t i = 0; i < window.selections().size(); ++i) 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(); const bool forward = sel.first() <= sel.last();
BufferCoord begin = forward ? sel.first() : buffer.char_next(sel.last()); BufferCoord begin = forward ? sel.first() : buffer.char_next(sel.last());
BufferCoord end = forward ? sel.last() : buffer.char_next(sel.first()); 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"); ColorPair sel_colors = get_color(primary ? "PrimarySelection" : "SecondarySelection");
highlight_range(display_buffer, begin, end, false, highlight_range(display_buffer, begin, end, false,
[&](DisplayAtom& atom) { atom.colors = sel_colors; }); [&](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& 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)
@ -436,9 +436,9 @@ HighlighterAndId flag_lines_factory(HighlighterParameters params)
GlobalOptions::instance()[option_name].get<std::vector<LineAndFlag>>(); GlobalOptions::instance()[option_name].get<std::vector<LineAndFlag>>();
return {"hlflags_" + params[1], 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<std::vector<LineAndFlag>>(); auto& lines = lines_opt.get<std::vector<LineAndFlag>>();
CharCount width = 0; CharCount width = 0;
@ -460,7 +460,7 @@ HighlighterAndId flag_lines_factory(HighlighterParameters params)
}}; }};
} }
template<void (*highlighter_func)(const Window&, DisplayBuffer&)> template<void (*highlighter_func)(const Context&, DisplayBuffer&)>
class SimpleHighlighterFactory class SimpleHighlighterFactory
{ {
public: public:
@ -493,8 +493,8 @@ HighlighterAndId reference_factory(HighlighterParameters params)
DefinedHighlighters::instance().get_group(name, '/'); DefinedHighlighters::instance().get_group(name, '/');
return HighlighterAndId(name, return HighlighterAndId(name,
[name](const Window& window, DisplayBuffer& display_buffer) [name](const Context& context, DisplayBuffer& display_buffer)
{ DefinedHighlighters::instance().get_group(name, '/')(window, display_buffer); }); { DefinedHighlighters::instance().get_group(name, '/')(context, display_buffer); });
} }
template<typename HighlightFunc> template<typename HighlightFunc>
@ -507,11 +507,11 @@ public:
m_func(std::move(func)) 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) for (auto& pair : cache.regions)
m_func(window, display_buffer, pair.first, pair.second); m_func(context, display_buffer, pair.first, pair.second);
} }
private: private:
Regex m_begin; Regex m_begin;
@ -570,7 +570,7 @@ HighlighterAndId region_factory(HighlighterParameters params)
Regex end{params[1]}; Regex end{params[1]};
const ColorPair colors = get_color(params[2]); 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) BufferCoord begin, BufferCoord end)
{ {
highlight_range(display_buffer, begin, end, true, highlight_range(display_buffer, begin, end, true,
@ -597,11 +597,11 @@ HighlighterAndId region_ref_factory(HighlighterParameters params)
Regex end{params[1]}; Regex end{params[1]};
const String& name = params[2]; 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) BufferCoord begin, BufferCoord end)
{ {
HighlighterGroup& ref = DefinedHighlighters::instance().get_group(name, '/'); 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 + ")", return HighlighterAndId("regionref(" + params[0] + "," + params[1] + "," + name + ")",

View File

@ -13,9 +13,9 @@ namespace Kakoune
{ {
// Implementation in highlighters.cc // Implementation in highlighters.cc
void highlight_selections(const Window& window, DisplayBuffer& display_buffer); void highlight_selections(const Context& context, DisplayBuffer& display_buffer);
void expand_tabulations(const Window& window, DisplayBuffer& display_buffer); void expand_tabulations(const Context& context, DisplayBuffer& display_buffer);
void expand_unprintable(const Window& window, DisplayBuffer& display_buffer); void expand_unprintable(const Context& context, DisplayBuffer& display_buffer);
Window::Window(Buffer& buffer) Window::Window(Buffer& buffer)
: Editor(buffer), : Editor(buffer),
@ -63,9 +63,10 @@ void Window::scroll(CharCount offset)
m_position.column = std::max(0_char, m_position.column + 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(); DisplayBuffer::LineList& lines = m_display_buffer.lines();
lines.clear(); lines.clear();
@ -79,8 +80,8 @@ void Window::update_display_buffer()
} }
m_display_buffer.compute_range(); m_display_buffer.compute_range();
m_highlighters(*this, m_display_buffer); m_highlighters(context, m_display_buffer);
m_builtin_highlighters(*this, m_display_buffer); m_builtin_highlighters(context, m_display_buffer);
// cut the start of the line before m_position.column // cut the start of the line before m_position.column
for (auto& line : lines) for (auto& line : lines)
@ -142,10 +143,10 @@ static CharCount adapt_view_pos(const DisplayBuffer& display_buffer,
return view_pos; 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& first = selection.first();
const auto& last = selections().main().last(); const auto& last = selection.last();
const LineCount offset = std::min<LineCount>(options()["scrolloff"].get<int>(), const LineCount offset = std::min<LineCount>(options()["scrolloff"].get<int>(),
(m_dimensions.line - 1) / 2); (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(), coord.line, coord.line+1} });
lines.emplace_back(AtomList{ {buffer(), line, line+1} }); lines.emplace_back(AtomList{ {buffer(), line, line+1} });
display_buffer.compute_range(); display_buffer.compute_range();
Context context(*this);
m_highlighters(*this, display_buffer); m_highlighters(*this, display_buffer);
m_builtin_highlighters(*this, display_buffer); m_builtin_highlighters(*this, display_buffer);

View File

@ -37,7 +37,7 @@ public:
void display_line_at(LineCount buffer_line, LineCount display_line); void display_line_at(LineCount buffer_line, LineCount display_line);
void scroll(LineCount offset); void scroll(LineCount offset);
void scroll(CharCount offset); void scroll(CharCount offset);
void update_display_buffer(); void update_display_buffer(const Context& context);
DisplayCoord display_position(BufferCoord coord); DisplayCoord display_position(BufferCoord coord);
@ -60,7 +60,7 @@ private:
void on_option_changed(const Option& option) override; 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_position;
DisplayCoord m_dimensions; DisplayCoord m_dimensions;