2011-09-02 18:51:20 +02:00
|
|
|
#ifndef display_buffer_hh_INCLUDED
|
|
|
|
#define display_buffer_hh_INCLUDED
|
|
|
|
|
2013-04-09 20:05:40 +02:00
|
|
|
#include "buffer.hh"
|
2014-07-11 01:27:04 +02:00
|
|
|
#include "face.hh"
|
2014-05-07 20:51:01 +02:00
|
|
|
#include "coord.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
#include "string.hh"
|
2012-10-11 00:41:48 +02:00
|
|
|
#include "utf8.hh"
|
2011-09-28 22:54:11 +02:00
|
|
|
|
2013-04-09 20:05:40 +02:00
|
|
|
#include <vector>
|
|
|
|
|
2011-09-02 18:51:20 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2013-07-24 14:55:57 +02:00
|
|
|
struct DisplayAtom
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
2012-07-12 23:19:10 +02:00
|
|
|
public:
|
|
|
|
enum Type { BufferRange, ReplacedBufferRange, Text };
|
|
|
|
|
2014-05-07 20:51:01 +02:00
|
|
|
DisplayAtom(const Buffer& buffer, ByteCoord begin, ByteCoord end)
|
2013-12-12 14:45:08 +01:00
|
|
|
: m_type(BufferRange), m_buffer(&buffer), m_begin(begin), m_end(end)
|
|
|
|
{ check_invariant(); }
|
2012-07-12 23:19:10 +02:00
|
|
|
|
2014-07-11 01:27:04 +02:00
|
|
|
DisplayAtom(String str, Face face = Face{})
|
|
|
|
: m_type(Text), m_text(std::move(str)), face(face)
|
2013-12-12 14:45:08 +01:00
|
|
|
{ check_invariant(); }
|
2012-07-12 23:19:10 +02:00
|
|
|
|
2014-05-16 20:29:39 +02:00
|
|
|
StringView content() const
|
2012-07-12 23:19:10 +02:00
|
|
|
{
|
|
|
|
switch (m_type)
|
|
|
|
{
|
|
|
|
case BufferRange:
|
2014-05-16 20:29:39 +02:00
|
|
|
{
|
|
|
|
auto& line = (*m_buffer)[m_begin.line];
|
|
|
|
if (m_begin.line == m_end.line)
|
|
|
|
return line.substr(m_begin.column, m_end.column - m_begin.column);
|
|
|
|
else if (m_begin.line+1 == m_end.line and m_end.column == 0)
|
|
|
|
return line.substr(m_begin.column);
|
|
|
|
break;
|
|
|
|
}
|
2012-07-12 23:19:10 +02:00
|
|
|
case Text:
|
|
|
|
case ReplacedBufferRange:
|
2014-05-16 20:29:39 +02:00
|
|
|
return m_text;
|
2012-07-12 23:19:10 +02:00
|
|
|
}
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(false);
|
2014-05-16 20:29:39 +02:00
|
|
|
return {};
|
2012-07-12 23:19:10 +02:00
|
|
|
}
|
|
|
|
|
2012-09-30 16:21:20 +02:00
|
|
|
CharCount length() const
|
|
|
|
{
|
|
|
|
switch (m_type)
|
|
|
|
{
|
|
|
|
case BufferRange:
|
2013-05-22 19:53:17 +02:00
|
|
|
return utf8::distance(m_buffer->iterator_at(m_begin),
|
|
|
|
m_buffer->iterator_at(m_end));
|
2012-09-30 16:21:20 +02:00
|
|
|
case Text:
|
|
|
|
case ReplacedBufferRange:
|
2012-10-11 00:41:48 +02:00
|
|
|
return m_text.char_length();
|
2012-09-30 16:21:20 +02:00
|
|
|
}
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(false);
|
2012-10-11 01:17:29 +02:00
|
|
|
return 0;
|
2012-09-30 16:21:20 +02:00
|
|
|
}
|
|
|
|
|
2014-05-07 20:51:01 +02:00
|
|
|
const ByteCoord& begin() const
|
2012-07-03 23:23:07 +02:00
|
|
|
{
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(has_buffer_range());
|
2012-07-12 23:19:10 +02:00
|
|
|
return m_begin;
|
|
|
|
}
|
2011-10-15 06:45:49 +02:00
|
|
|
|
2014-05-07 20:51:01 +02:00
|
|
|
const ByteCoord& end() const
|
2012-07-12 23:19:10 +02:00
|
|
|
{
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(has_buffer_range());
|
2012-07-12 23:19:10 +02:00
|
|
|
return m_end;
|
|
|
|
}
|
2011-10-15 06:45:49 +02:00
|
|
|
|
2012-07-12 23:19:10 +02:00
|
|
|
void replace(String text)
|
|
|
|
{
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(m_type == BufferRange);
|
2012-07-12 23:19:10 +02:00
|
|
|
m_type = ReplacedBufferRange;
|
|
|
|
m_text = std::move(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool has_buffer_range() const
|
|
|
|
{
|
|
|
|
return m_type == BufferRange or m_type == ReplacedBufferRange;
|
|
|
|
}
|
2011-10-15 06:45:49 +02:00
|
|
|
|
2014-01-21 19:52:51 +01:00
|
|
|
const Buffer& buffer() const { kak_assert(m_buffer); return *m_buffer; }
|
|
|
|
|
2012-07-12 23:19:10 +02:00
|
|
|
Type type() const { return m_type; }
|
2011-10-17 21:01:04 +02:00
|
|
|
|
2013-06-28 00:03:11 +02:00
|
|
|
void trim_begin(CharCount count);
|
|
|
|
void trim_end(CharCount count);
|
2013-07-24 14:55:57 +02:00
|
|
|
|
2013-12-12 14:45:08 +01:00
|
|
|
void check_invariant() const;
|
2014-07-07 21:13:08 +02:00
|
|
|
|
|
|
|
bool operator==(const DisplayAtom& other) const
|
|
|
|
{
|
2014-07-11 01:27:04 +02:00
|
|
|
return face == other.face and content() == other.content();
|
2014-07-07 21:13:08 +02:00
|
|
|
}
|
|
|
|
|
2013-07-24 14:55:57 +02:00
|
|
|
public:
|
2014-07-11 01:27:04 +02:00
|
|
|
Face face;
|
2013-07-24 14:55:57 +02:00
|
|
|
|
2011-10-15 06:45:49 +02:00
|
|
|
private:
|
2012-10-08 14:28:38 +02:00
|
|
|
friend class DisplayLine;
|
|
|
|
|
2012-07-12 23:19:10 +02:00
|
|
|
Type m_type;
|
|
|
|
|
2013-05-22 19:53:17 +02:00
|
|
|
const Buffer* m_buffer = nullptr;
|
2014-05-07 20:51:01 +02:00
|
|
|
ByteCoord m_begin;
|
|
|
|
ByteCoord m_end;
|
2012-07-12 23:19:10 +02:00
|
|
|
String m_text;
|
2011-09-02 18:51:20 +02:00
|
|
|
};
|
|
|
|
|
2014-05-07 20:51:01 +02:00
|
|
|
using BufferRange = std::pair<ByteCoord, ByteCoord>;
|
2013-07-24 01:34:00 +02:00
|
|
|
using AtomList = std::vector<DisplayAtom>;
|
2013-07-23 20:11:26 +02:00
|
|
|
|
2012-07-12 23:19:10 +02:00
|
|
|
class DisplayLine
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using iterator = AtomList::iterator;
|
|
|
|
using const_iterator = AtomList::const_iterator;
|
2013-12-11 22:38:43 +01:00
|
|
|
using value_type = AtomList::value_type;
|
2012-07-12 23:19:10 +02:00
|
|
|
|
2013-07-23 20:11:26 +02:00
|
|
|
DisplayLine() = default;
|
|
|
|
DisplayLine(AtomList atoms);
|
2014-07-11 01:27:04 +02:00
|
|
|
DisplayLine(String str, Face face)
|
|
|
|
{ push_back({ std::move(str), face }); }
|
2011-10-15 06:45:49 +02:00
|
|
|
|
2011-09-02 18:51:20 +02:00
|
|
|
iterator begin() { return m_atoms.begin(); }
|
2012-07-12 23:19:10 +02:00
|
|
|
iterator end() { return m_atoms.end(); }
|
2011-09-02 18:51:20 +02:00
|
|
|
|
|
|
|
const_iterator begin() const { return m_atoms.begin(); }
|
2012-07-12 23:19:10 +02:00
|
|
|
const_iterator end() const { return m_atoms.end(); }
|
2011-09-29 11:10:27 +02:00
|
|
|
|
2012-10-23 22:55:44 +02:00
|
|
|
const AtomList& atoms() const { return m_atoms; }
|
|
|
|
|
2013-04-04 18:50:00 +02:00
|
|
|
CharCount length() const;
|
2013-07-23 20:11:26 +02:00
|
|
|
const BufferRange& range() const { return m_range; }
|
2013-04-04 18:50:00 +02:00
|
|
|
|
2012-07-12 23:19:10 +02:00
|
|
|
// Split atom pointed by it at pos, returns an iterator to the first atom
|
2014-05-07 20:51:01 +02:00
|
|
|
iterator split(iterator it, ByteCoord pos);
|
2011-10-17 21:00:38 +02:00
|
|
|
|
2013-07-23 20:11:26 +02:00
|
|
|
iterator insert(iterator it, DisplayAtom atom);
|
2013-12-11 22:38:43 +01:00
|
|
|
iterator erase(iterator beg, iterator end);
|
2013-07-23 20:11:26 +02:00
|
|
|
void push_back(DisplayAtom atom);
|
2011-10-15 06:45:49 +02:00
|
|
|
|
2013-06-28 00:03:11 +02:00
|
|
|
// remove first_char from the begining of the line, and make sure
|
|
|
|
// the line is less that char_count character
|
|
|
|
void trim(CharCount first_char, CharCount char_count);
|
|
|
|
|
2012-10-22 13:20:02 +02:00
|
|
|
void optimize();
|
2011-09-02 18:51:20 +02:00
|
|
|
private:
|
2013-07-23 20:11:26 +02:00
|
|
|
void compute_range();
|
|
|
|
BufferRange m_range = { { INT_MAX, INT_MAX }, { INT_MIN, INT_MIN } };
|
2012-08-22 23:33:52 +02:00
|
|
|
AtomList m_atoms;
|
2011-09-02 18:51:20 +02:00
|
|
|
};
|
|
|
|
|
2012-07-12 23:19:10 +02:00
|
|
|
class DisplayBuffer
|
|
|
|
{
|
|
|
|
public:
|
2012-10-23 22:55:04 +02:00
|
|
|
using LineList = std::vector<DisplayLine>;
|
2012-07-12 23:19:10 +02:00
|
|
|
DisplayBuffer() {}
|
|
|
|
|
|
|
|
LineList& lines() { return m_lines; }
|
|
|
|
const LineList& lines() const { return m_lines; }
|
2012-07-12 23:51:13 +02:00
|
|
|
|
2013-05-23 13:59:33 +02:00
|
|
|
// returns the smallest BufferRange which contains every DisplayAtoms
|
2012-07-12 23:51:13 +02:00
|
|
|
const BufferRange& range() const { return m_range; }
|
2012-10-22 13:20:02 +02:00
|
|
|
void optimize();
|
2012-07-12 23:51:13 +02:00
|
|
|
void compute_range();
|
|
|
|
|
2012-07-12 23:19:10 +02:00
|
|
|
private:
|
|
|
|
LineList m_lines;
|
2012-07-12 23:51:13 +02:00
|
|
|
BufferRange m_range;
|
2012-07-12 23:19:10 +02:00
|
|
|
};
|
|
|
|
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // display_buffer_hh_INCLUDED
|