From 98b661865856ea7a6ed74a922197b506b1eae8da Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 22 Oct 2012 13:20:02 +0200 Subject: [PATCH] merge contiguous DisplayAtoms after highlighting DisplayBuffer --- src/display_buffer.cc | 31 +++++++++++++++++++++++++++++++ src/display_buffer.hh | 3 ++- src/window.cc | 1 + 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/display_buffer.cc b/src/display_buffer.cc index 576e5a14..bdd7d68a 100644 --- a/src/display_buffer.cc +++ b/src/display_buffer.cc @@ -20,6 +20,32 @@ DisplayLine::iterator DisplayLine::split(iterator it, BufferIterator pos) return m_atoms.insert(it, std::move(atom)); } +void DisplayLine::optimize() +{ + for (auto atom_it = m_atoms.begin(); atom_it != m_atoms.end(); ++atom_it) + { + decltype(atom_it) next_atom_it; + while ((next_atom_it = atom_it + 1) != m_atoms.end()) + { + auto& atom = *atom_it; + auto& next_atom = *next_atom_it; + + if (atom.fg_color == next_atom.fg_color and + atom.bg_color == next_atom.bg_color and + atom.attribute == next_atom.attribute and + atom.content.type() == AtomContent::BufferRange and + next_atom.content.type() == AtomContent::BufferRange and + next_atom.content.begin() == atom.content.end()) + { + atom.content.m_end = next_atom.content.end(); + atom_it = m_atoms.erase(next_atom_it) - 1; + } + else + break; + } + } +} + void DisplayBuffer::compute_range() { m_range.first = BufferIterator(); @@ -42,4 +68,9 @@ void DisplayBuffer::compute_range() assert(m_range.first <= m_range.second); } +void DisplayBuffer::optimize() +{ + for (auto& line : m_lines) + line.optimize(); +} } diff --git a/src/display_buffer.hh b/src/display_buffer.hh index 884d45b0..385ad94b 100644 --- a/src/display_buffer.hh +++ b/src/display_buffer.hh @@ -144,6 +144,7 @@ public: iterator insert(iterator it, DisplayAtom atom) { return m_atoms.insert(it, std::move(atom)); } void push_back(DisplayAtom atom) { m_atoms.push_back(std::move(atom)); } + void optimize(); private: LineCount m_buffer_line; AtomList m_atoms; @@ -162,11 +163,11 @@ public: // returns the smallest BufferIterator range which contains every DisplayAtoms const BufferRange& range() const { return m_range; } + void optimize(); void compute_range(); private: LineList m_lines; - BufferRange m_range; }; diff --git a/src/window.cc b/src/window.cc index 45485dd8..fd3be30a 100644 --- a/src/window.cc +++ b/src/window.cc @@ -63,6 +63,7 @@ void Window::update_display_buffer() m_display_buffer.compute_range(); m_highlighters(m_display_buffer); + m_display_buffer.optimize(); } void Window::set_dimensions(const DisplayCoord& dimensions)