1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-22 02:51:32 +03:00

DisplayBuffer: add range and compute_range methods

This commit is contained in:
Maxime Coste 2012-07-12 23:51:13 +02:00
parent b1a087485c
commit 03d8efc249
4 changed files with 38 additions and 4 deletions

View File

@ -17,4 +17,26 @@ DisplayLine::iterator DisplayLine::split(iterator it, BufferIterator pos)
return m_atoms.insert(it, std::move(atom)); return m_atoms.insert(it, std::move(atom));
} }
void DisplayBuffer::compute_range()
{
m_range.first = BufferIterator();
m_range.second = BufferIterator();
for (auto& line : m_lines)
{
for (auto& atom : line)
{
if (not atom.content.has_buffer_range())
continue;
if (not m_range.first.is_valid() or m_range.first > atom.content.begin())
m_range.first = atom.content.begin();
if (not m_range.second.is_valid() or m_range.second < atom.content.end())
m_range.second = atom.content.end();
}
}
assert(m_range.first.is_valid() and m_range.second.is_valid());
assert(m_range.first <= m_range.second);
}
} }

View File

@ -144,6 +144,8 @@ private:
AtomList m_atoms; AtomList m_atoms;
}; };
using BufferRange = std::pair<BufferIterator, BufferIterator>;
class DisplayBuffer class DisplayBuffer
{ {
public: public:
@ -152,8 +154,15 @@ public:
LineList& lines() { return m_lines; } LineList& lines() { return m_lines; }
const LineList& lines() const { return m_lines; } const LineList& lines() const { return m_lines; }
// returns the smallest BufferIterator range which contains every DisplayAtoms
const BufferRange& range() const { return m_range; }
void compute_range();
private: private:
LineList m_lines; LineList m_lines;
BufferRange m_range;
}; };
} }

View File

@ -18,6 +18,9 @@ void highlight_range(DisplayBuffer& display_buffer,
BufferIterator begin, BufferIterator end, BufferIterator begin, BufferIterator end,
bool skip_replaced, T func) bool skip_replaced, T func)
{ {
if (end <= display_buffer.range().first or begin >= display_buffer.range().second)
return;
for (auto& line : display_buffer.lines()) for (auto& line : display_buffer.lines())
{ {
if (line.buffer_line() >= begin.line() and line.buffer_line() <= end.line()) if (line.buffer_line() >= begin.line() and line.buffer_line() <= end.line())
@ -50,11 +53,11 @@ void highlight_range(DisplayBuffer& display_buffer,
} }
void colorize_regex(DisplayBuffer& display_buffer, void colorize_regex(DisplayBuffer& display_buffer,
const Buffer& buffer,
const Regex& ex, const Regex& ex,
Color fg_color, Color bg_color = Color::Default) Color fg_color, Color bg_color = Color::Default)
{ {
RegexIterator re_it(buffer.begin(), buffer.end(), ex, boost::match_nosubs); const BufferRange& range = display_buffer.range();
RegexIterator re_it(range.first, range.second, ex, boost::match_nosubs);
RegexIterator re_end; RegexIterator re_end;
for (; re_it != re_end; ++re_it) for (; re_it != re_end; ++re_it)
{ {
@ -94,8 +97,7 @@ HighlighterAndId colorize_regex_factory(Window& window,
String id = "colre'" + params[0] + "'"; String id = "colre'" + params[0] + "'";
return HighlighterAndId(id, std::bind(colorize_regex, return HighlighterAndId(id, std::bind(colorize_regex,
_1, std::ref(window.buffer()), _1, ex, fg_color, bg_color));
ex, fg_color, bg_color));
} }
void expand_tabulations(Window& window, DisplayBuffer& display_buffer) void expand_tabulations(Window& window, DisplayBuffer& display_buffer)

View File

@ -61,6 +61,7 @@ void Window::update_display_buffer()
lines.back().push_back(DisplayAtom(AtomContent(pos,end))); lines.back().push_back(DisplayAtom(AtomContent(pos,end)));
} }
m_display_buffer.compute_range();
m_highlighters(m_display_buffer); m_highlighters(m_display_buffer);
} }