2011-09-02 20:51:20 +04:00
|
|
|
#ifndef display_buffer_hh_INCLUDED
|
|
|
|
#define display_buffer_hh_INCLUDED
|
|
|
|
|
2014-07-11 03:27:04 +04:00
|
|
|
#include "face.hh"
|
2015-04-23 23:38:45 +03:00
|
|
|
#include "hash.hh"
|
2014-05-07 22:51:01 +04:00
|
|
|
#include "coord.hh"
|
2013-04-09 22:05:40 +04:00
|
|
|
#include "string.hh"
|
2015-01-12 16:58:41 +03:00
|
|
|
#include "vector.hh"
|
2013-04-09 22:05:40 +04:00
|
|
|
|
2011-09-02 20:51:20 +04:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2014-11-13 00:27:07 +03:00
|
|
|
class Buffer;
|
2015-04-23 23:38:45 +03:00
|
|
|
struct BufferRange{ ByteCoord begin, end; };
|
|
|
|
|
|
|
|
inline bool operator==(const BufferRange& lhs, const BufferRange& rhs)
|
|
|
|
{
|
|
|
|
return lhs.begin == rhs.begin and lhs.end == rhs.end;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
size_t hash_value(const BufferRange& range)
|
|
|
|
{
|
|
|
|
return hash_values(range.begin, range.end);
|
|
|
|
}
|
2014-11-13 00:27:07 +03:00
|
|
|
|
2015-01-21 16:35:46 +03:00
|
|
|
struct DisplayAtom : public UseMemoryDomain<MemoryDomain::Display>
|
2011-09-02 20:51:20 +04:00
|
|
|
{
|
2012-07-13 01:19:10 +04:00
|
|
|
public:
|
|
|
|
enum Type { BufferRange, ReplacedBufferRange, Text };
|
|
|
|
|
2014-05-07 22:51:01 +04:00
|
|
|
DisplayAtom(const Buffer& buffer, ByteCoord begin, ByteCoord end)
|
2015-04-23 23:38:45 +03:00
|
|
|
: m_type(BufferRange), m_buffer(&buffer), m_range{begin, end}
|
2013-12-12 17:45:08 +04:00
|
|
|
{ check_invariant(); }
|
2012-07-13 01:19:10 +04:00
|
|
|
|
2014-07-11 03:27:04 +04:00
|
|
|
DisplayAtom(String str, Face face = Face{})
|
|
|
|
: m_type(Text), m_text(std::move(str)), face(face)
|
2013-12-12 17:45:08 +04:00
|
|
|
{ check_invariant(); }
|
2012-07-13 01:19:10 +04:00
|
|
|
|
2014-11-13 00:27:07 +03:00
|
|
|
StringView content() const;
|
|
|
|
CharCount length() const;
|
2012-09-30 18:21:20 +04:00
|
|
|
|
2014-05-07 22:51:01 +04:00
|
|
|
const ByteCoord& begin() const
|
2012-07-04 01:23:07 +04:00
|
|
|
{
|
2013-04-09 22:04:11 +04:00
|
|
|
kak_assert(has_buffer_range());
|
2015-04-23 23:38:45 +03:00
|
|
|
return m_range.begin;
|
2012-07-13 01:19:10 +04:00
|
|
|
}
|
2011-10-15 08:45:49 +04:00
|
|
|
|
2014-05-07 22:51:01 +04:00
|
|
|
const ByteCoord& end() const
|
2012-07-13 01:19:10 +04:00
|
|
|
{
|
2013-04-09 22:04:11 +04:00
|
|
|
kak_assert(has_buffer_range());
|
2015-04-23 23:38:45 +03:00
|
|
|
return m_range.end;
|
2012-07-13 01:19:10 +04:00
|
|
|
}
|
2011-10-15 08:45:49 +04:00
|
|
|
|
2012-07-13 01:19:10 +04:00
|
|
|
void replace(String text)
|
|
|
|
{
|
2013-04-09 22:04:11 +04:00
|
|
|
kak_assert(m_type == BufferRange);
|
2012-07-13 01:19:10 +04: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 08:45:49 +04:00
|
|
|
|
2014-01-21 22:52:51 +04:00
|
|
|
const Buffer& buffer() const { kak_assert(m_buffer); return *m_buffer; }
|
|
|
|
|
2012-07-13 01:19:10 +04:00
|
|
|
Type type() const { return m_type; }
|
2011-10-17 23:01:04 +04:00
|
|
|
|
2013-06-28 02:03:11 +04:00
|
|
|
void trim_begin(CharCount count);
|
|
|
|
void trim_end(CharCount count);
|
2013-07-24 16:55:57 +04:00
|
|
|
|
2013-12-12 17:45:08 +04:00
|
|
|
void check_invariant() const;
|
2014-07-07 23:13:08 +04:00
|
|
|
|
|
|
|
bool operator==(const DisplayAtom& other) const
|
|
|
|
{
|
2015-04-23 23:38:45 +03:00
|
|
|
return face == other.face and type() == other.type() and
|
|
|
|
content() == other.content();
|
2014-07-07 23:13:08 +04:00
|
|
|
}
|
|
|
|
|
2013-07-24 16:55:57 +04:00
|
|
|
public:
|
2014-07-11 03:27:04 +04:00
|
|
|
Face face;
|
2013-07-24 16:55:57 +04:00
|
|
|
|
2011-10-15 08:45:49 +04:00
|
|
|
private:
|
2012-10-08 16:28:38 +04:00
|
|
|
friend class DisplayLine;
|
|
|
|
|
2012-07-13 01:19:10 +04:00
|
|
|
Type m_type;
|
|
|
|
|
2013-05-22 21:53:17 +04:00
|
|
|
const Buffer* m_buffer = nullptr;
|
2015-04-23 23:38:45 +03:00
|
|
|
Kakoune::BufferRange m_range;
|
2012-07-13 01:19:10 +04:00
|
|
|
String m_text;
|
2011-09-02 20:51:20 +04:00
|
|
|
};
|
|
|
|
|
2015-01-12 16:58:41 +03:00
|
|
|
using AtomList = Vector<DisplayAtom>;
|
2013-07-23 22:11:26 +04:00
|
|
|
|
2015-01-21 16:35:46 +03:00
|
|
|
class DisplayLine : public UseMemoryDomain<MemoryDomain::Display>
|
2012-07-13 01:19:10 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
using iterator = AtomList::iterator;
|
|
|
|
using const_iterator = AtomList::const_iterator;
|
2013-12-12 01:38:43 +04:00
|
|
|
using value_type = AtomList::value_type;
|
2012-07-13 01:19:10 +04:00
|
|
|
|
2013-07-23 22:11:26 +04:00
|
|
|
DisplayLine() = default;
|
|
|
|
DisplayLine(AtomList atoms);
|
2014-07-11 03:27:04 +04:00
|
|
|
DisplayLine(String str, Face face)
|
|
|
|
{ push_back({ std::move(str), face }); }
|
2011-10-15 08:45:49 +04:00
|
|
|
|
2011-09-02 20:51:20 +04:00
|
|
|
iterator begin() { return m_atoms.begin(); }
|
2012-07-13 01:19:10 +04:00
|
|
|
iterator end() { return m_atoms.end(); }
|
2011-09-02 20:51:20 +04:00
|
|
|
|
|
|
|
const_iterator begin() const { return m_atoms.begin(); }
|
2012-07-13 01:19:10 +04:00
|
|
|
const_iterator end() const { return m_atoms.end(); }
|
2011-09-29 13:10:27 +04:00
|
|
|
|
2012-10-24 00:55:44 +04:00
|
|
|
const AtomList& atoms() const { return m_atoms; }
|
|
|
|
|
2013-04-04 20:50:00 +04:00
|
|
|
CharCount length() const;
|
2013-07-23 22:11:26 +04:00
|
|
|
const BufferRange& range() const { return m_range; }
|
2013-04-04 20:50:00 +04:00
|
|
|
|
2015-07-23 15:58:23 +03:00
|
|
|
// Split atom pointed by it at buffer coord pos,
|
|
|
|
// returns an iterator to the first atom
|
2014-05-07 22:51:01 +04:00
|
|
|
iterator split(iterator it, ByteCoord pos);
|
2015-07-23 15:58:23 +03:00
|
|
|
|
|
|
|
// Split atom pointed by it at its pos character,
|
|
|
|
// returns an iterator to the first atom
|
|
|
|
iterator split(iterator it, CharCount pos);
|
2011-10-17 23:00:38 +04:00
|
|
|
|
2013-07-23 22:11:26 +04:00
|
|
|
iterator insert(iterator it, DisplayAtom atom);
|
2013-12-12 01:38:43 +04:00
|
|
|
iterator erase(iterator beg, iterator end);
|
2013-07-23 22:11:26 +04:00
|
|
|
void push_back(DisplayAtom atom);
|
2011-10-15 08:45:49 +04:00
|
|
|
|
2013-06-28 02:03:11 +04:00
|
|
|
// remove first_char from the begining of the line, and make sure
|
|
|
|
// the line is less that char_count character
|
2015-04-23 23:11:50 +03:00
|
|
|
void trim(CharCount first_char, CharCount char_count, bool only_buffer);
|
2013-06-28 02:03:11 +04:00
|
|
|
|
2012-10-22 15:20:02 +04:00
|
|
|
void optimize();
|
2011-09-02 20:51:20 +04:00
|
|
|
private:
|
2013-07-23 22:11:26 +04:00
|
|
|
void compute_range();
|
|
|
|
BufferRange m_range = { { INT_MAX, INT_MAX }, { INT_MIN, INT_MIN } };
|
2012-08-23 01:33:52 +04:00
|
|
|
AtomList m_atoms;
|
2011-09-02 20:51:20 +04:00
|
|
|
};
|
|
|
|
|
2015-01-21 16:35:46 +03:00
|
|
|
class DisplayBuffer : public UseMemoryDomain<MemoryDomain::Display>
|
2012-07-13 01:19:10 +04:00
|
|
|
{
|
|
|
|
public:
|
2015-01-12 16:58:41 +03:00
|
|
|
using LineList = Vector<DisplayLine>;
|
2012-07-13 01:19:10 +04:00
|
|
|
DisplayBuffer() {}
|
|
|
|
|
|
|
|
LineList& lines() { return m_lines; }
|
|
|
|
const LineList& lines() const { return m_lines; }
|
2012-07-13 01:51:13 +04:00
|
|
|
|
2013-05-23 15:59:33 +04:00
|
|
|
// returns the smallest BufferRange which contains every DisplayAtoms
|
2012-07-13 01:51:13 +04:00
|
|
|
const BufferRange& range() const { return m_range; }
|
2012-10-22 15:20:02 +04:00
|
|
|
void optimize();
|
2012-07-13 01:51:13 +04:00
|
|
|
void compute_range();
|
|
|
|
|
2012-07-13 01:19:10 +04:00
|
|
|
private:
|
|
|
|
LineList m_lines;
|
2012-07-13 01:51:13 +04:00
|
|
|
BufferRange m_range;
|
2012-07-13 01:19:10 +04:00
|
|
|
};
|
|
|
|
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // display_buffer_hh_INCLUDED
|