2011-09-02 20:51:20 +04:00
|
|
|
#include "display_buffer.hh"
|
|
|
|
|
2011-09-29 13:10:27 +04:00
|
|
|
#include "assert.hh"
|
2011-09-29 12:55:08 +04:00
|
|
|
|
2011-09-02 20:51:20 +04:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2013-07-24 16:55:57 +04:00
|
|
|
void DisplayAtom::trim_begin(CharCount count)
|
2013-06-28 02:03:11 +04:00
|
|
|
{
|
|
|
|
if (m_type == BufferRange)
|
|
|
|
m_begin = utf8::advance(m_buffer->iterator_at(m_begin),
|
|
|
|
m_buffer->iterator_at(m_end), count).coord();
|
|
|
|
else
|
|
|
|
m_text = m_text.substr(count);
|
|
|
|
}
|
|
|
|
|
2013-07-24 16:55:57 +04:00
|
|
|
void DisplayAtom::trim_end(CharCount count)
|
2013-06-28 02:03:11 +04:00
|
|
|
{
|
|
|
|
if (m_type == BufferRange)
|
|
|
|
m_end = utf8::advance(m_buffer->iterator_at(m_end),
|
|
|
|
m_buffer->iterator_at(m_begin), -count).coord();
|
|
|
|
else
|
|
|
|
m_text = m_text.substr(0, m_text.char_length() - count);
|
|
|
|
}
|
|
|
|
|
2013-07-23 22:11:26 +04:00
|
|
|
DisplayLine::DisplayLine(AtomList atoms)
|
|
|
|
: m_atoms(std::move(atoms))
|
|
|
|
{
|
|
|
|
compute_range();
|
|
|
|
}
|
|
|
|
|
2013-05-23 15:59:33 +04:00
|
|
|
DisplayLine::iterator DisplayLine::split(iterator it, BufferCoord pos)
|
2011-10-15 08:45:49 +04:00
|
|
|
{
|
2013-07-24 16:55:57 +04:00
|
|
|
kak_assert(it->type() == DisplayAtom::BufferRange);
|
|
|
|
kak_assert(it->begin() < pos);
|
|
|
|
kak_assert(it->end() > pos);
|
2011-10-15 08:45:49 +04:00
|
|
|
|
2012-07-13 01:19:10 +04:00
|
|
|
DisplayAtom atom = *it;
|
2013-07-24 16:55:57 +04:00
|
|
|
atom.m_end = pos;
|
|
|
|
it->m_begin = pos;
|
2012-07-13 01:19:10 +04:00
|
|
|
return m_atoms.insert(it, std::move(atom));
|
2011-09-29 13:10:27 +04:00
|
|
|
}
|
|
|
|
|
2013-07-23 22:11:26 +04:00
|
|
|
DisplayLine::iterator DisplayLine::insert(iterator it, DisplayAtom atom)
|
|
|
|
{
|
2013-07-24 16:55:57 +04:00
|
|
|
if (atom.has_buffer_range())
|
2013-07-23 22:11:26 +04:00
|
|
|
{
|
2013-07-24 16:55:57 +04:00
|
|
|
m_range.first = std::min(m_range.first, atom.begin());
|
|
|
|
m_range.second = std::max(m_range.second, atom.end());
|
2013-07-23 22:11:26 +04:00
|
|
|
}
|
|
|
|
return m_atoms.insert(it, std::move(atom));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DisplayLine::push_back(DisplayAtom atom)
|
|
|
|
{
|
2013-07-24 16:55:57 +04:00
|
|
|
if (atom.has_buffer_range())
|
2013-07-23 22:11:26 +04:00
|
|
|
{
|
2013-07-24 16:55:57 +04:00
|
|
|
m_range.first = std::min(m_range.first, atom.begin());
|
|
|
|
m_range.second = std::max(m_range.second, atom.end());
|
2013-07-23 22:11:26 +04:00
|
|
|
}
|
|
|
|
m_atoms.push_back(std::move(atom));
|
|
|
|
}
|
|
|
|
|
2013-12-12 01:38:43 +04:00
|
|
|
DisplayLine::iterator DisplayLine::erase(iterator beg, iterator end)
|
|
|
|
{
|
|
|
|
iterator res = m_atoms.erase(beg, end);
|
|
|
|
compute_range();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-10-22 15:20:02 +04:00
|
|
|
void DisplayLine::optimize()
|
|
|
|
{
|
2013-05-02 20:57:15 +04:00
|
|
|
if (m_atoms.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto atom_it = m_atoms.begin();
|
|
|
|
auto next_atom_it = atom_it + 1;
|
|
|
|
while (next_atom_it != m_atoms.end())
|
2012-10-22 15:20:02 +04:00
|
|
|
{
|
2013-05-02 20:57:15 +04:00
|
|
|
auto& atom = *atom_it;
|
|
|
|
auto& next_atom = *next_atom_it;
|
|
|
|
bool merged = false;
|
2012-10-22 15:20:02 +04:00
|
|
|
|
2013-05-02 20:57:15 +04:00
|
|
|
if (atom.colors == next_atom.colors and
|
|
|
|
atom.attribute == next_atom.attribute and
|
2013-07-24 16:55:57 +04:00
|
|
|
atom.type() == next_atom.type())
|
2013-05-02 20:57:15 +04:00
|
|
|
{
|
2013-07-24 16:55:57 +04:00
|
|
|
auto type = atom.type();
|
|
|
|
if ((type == DisplayAtom::BufferRange or
|
|
|
|
type == DisplayAtom::ReplacedBufferRange) and
|
|
|
|
next_atom.begin() == atom.end())
|
2012-10-22 15:20:02 +04:00
|
|
|
{
|
2013-07-24 16:55:57 +04:00
|
|
|
atom.m_end = next_atom.end();
|
|
|
|
if (type == DisplayAtom::ReplacedBufferRange)
|
|
|
|
atom.m_text += next_atom.m_text;
|
2013-05-02 20:57:15 +04:00
|
|
|
merged = true;
|
|
|
|
}
|
2013-07-24 16:55:57 +04:00
|
|
|
if (type == DisplayAtom::Text)
|
2013-05-02 20:57:15 +04:00
|
|
|
{
|
2013-07-24 16:55:57 +04:00
|
|
|
atom.m_text += next_atom.m_text;
|
2013-05-02 20:57:15 +04:00
|
|
|
merged = true;
|
2012-10-22 15:20:02 +04:00
|
|
|
}
|
|
|
|
}
|
2013-05-02 20:57:15 +04:00
|
|
|
if (merged)
|
|
|
|
next_atom_it = m_atoms.erase(next_atom_it);
|
|
|
|
else
|
|
|
|
atom_it = next_atom_it++;
|
2012-10-22 15:20:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-04 20:50:00 +04:00
|
|
|
CharCount DisplayLine::length() const
|
|
|
|
{
|
|
|
|
CharCount len = 0;
|
|
|
|
for (auto& atom : m_atoms)
|
2013-07-24 16:55:57 +04:00
|
|
|
len += atom.length();
|
2013-04-04 20:50:00 +04:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2013-06-28 02:03:11 +04:00
|
|
|
void DisplayLine::trim(CharCount first_char, CharCount char_count)
|
|
|
|
{
|
|
|
|
for (auto it = begin(); first_char > 0 and it != end(); )
|
|
|
|
{
|
2013-07-24 16:55:57 +04:00
|
|
|
if (not it->has_buffer_range())
|
2013-06-28 02:03:11 +04:00
|
|
|
{
|
|
|
|
++it;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-07-24 16:55:57 +04:00
|
|
|
auto len = it->length();
|
2013-06-28 02:03:11 +04:00
|
|
|
if (len <= first_char)
|
|
|
|
{
|
|
|
|
m_atoms.erase(it);
|
|
|
|
first_char -= len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-24 16:55:57 +04:00
|
|
|
it->trim_begin(first_char);
|
2013-06-28 02:03:11 +04:00
|
|
|
first_char = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto it = begin();
|
|
|
|
for (; it != end() and char_count > 0; ++it)
|
2013-07-24 16:55:57 +04:00
|
|
|
char_count -= it->length();
|
2013-06-28 02:03:11 +04:00
|
|
|
|
|
|
|
if (char_count < 0)
|
2013-07-24 16:55:57 +04:00
|
|
|
(it-1)->trim_end(-char_count);
|
2013-06-28 02:03:11 +04:00
|
|
|
m_atoms.erase(it, end());
|
2013-07-23 22:11:26 +04:00
|
|
|
|
|
|
|
compute_range();
|
|
|
|
}
|
|
|
|
|
2013-12-12 01:38:43 +04:00
|
|
|
constexpr BufferRange init_range{ {INT_MAX, INT_MAX}, {INT_MIN, INT_MIN} };
|
|
|
|
|
2013-07-23 22:11:26 +04:00
|
|
|
void DisplayLine::compute_range()
|
|
|
|
{
|
2013-12-12 01:38:43 +04:00
|
|
|
m_range = init_range;
|
2013-07-23 22:11:26 +04:00
|
|
|
for (auto& atom : m_atoms)
|
|
|
|
{
|
2013-07-24 16:55:57 +04:00
|
|
|
if (not atom.has_buffer_range())
|
2013-07-23 22:11:26 +04:00
|
|
|
continue;
|
2013-07-24 16:55:57 +04:00
|
|
|
m_range.first = std::min(m_range.first, atom.begin());
|
|
|
|
m_range.second = std::max(m_range.second, atom.end());
|
2013-07-23 22:11:26 +04:00
|
|
|
}
|
2013-12-12 01:38:43 +04:00
|
|
|
if (m_range == init_range)
|
|
|
|
m_range = { { 0, 0 }, { 0, 0 } };
|
|
|
|
kak_assert(m_range.first <= m_range.second);
|
2013-06-28 02:03:11 +04:00
|
|
|
}
|
|
|
|
|
2012-07-13 01:51:13 +04:00
|
|
|
void DisplayBuffer::compute_range()
|
|
|
|
{
|
2013-12-12 01:38:43 +04:00
|
|
|
m_range = init_range;
|
2012-07-13 01:51:13 +04:00
|
|
|
for (auto& line : m_lines)
|
|
|
|
{
|
2013-07-23 22:11:26 +04:00
|
|
|
m_range.first = std::min(line.range().first, m_range.first);
|
|
|
|
m_range.second = std::max(line.range().second, m_range.second);
|
2012-07-13 01:51:13 +04:00
|
|
|
}
|
2013-12-12 01:38:43 +04:00
|
|
|
if (m_range == init_range)
|
|
|
|
m_range = { { 0, 0 }, { 0, 0 } };
|
2013-04-09 22:04:11 +04:00
|
|
|
kak_assert(m_range.first <= m_range.second);
|
2012-07-13 01:51:13 +04:00
|
|
|
}
|
|
|
|
|
2012-10-22 15:20:02 +04:00
|
|
|
void DisplayBuffer::optimize()
|
|
|
|
{
|
|
|
|
for (auto& line : m_lines)
|
|
|
|
line.optimize();
|
|
|
|
}
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|