kakoune/src/display_buffer.hh

181 lines
4.0 KiB
C++
Raw Normal View History

2011-09-02 18:51:20 +02:00
#ifndef display_buffer_hh_INCLUDED
#define display_buffer_hh_INCLUDED
#include <vector>
#include "string.hh"
#include "color.hh"
#include "line_and_column.hh"
#include "buffer.hh"
#include "utf8.hh"
2011-09-02 18:51:20 +02:00
namespace Kakoune
{
struct DisplayCoord : LineAndColumn<DisplayCoord, LineCount, CharCount>
{
2012-09-04 23:54:10 +02:00
constexpr DisplayCoord(LineCount line = 0, CharCount column = 0)
: LineAndColumn(line, column) {}
};
2012-10-23 22:43:12 +02:00
typedef char Attribute;
2011-09-02 18:51:20 +02:00
enum Attributes
{
2011-09-26 01:51:12 +02:00
Normal = 0,
Underline = 1,
Reverse = 2,
Blink = 4,
Bold = 8
2011-09-26 01:51:12 +02:00
};
class DisplayLine;
struct AtomContent
2011-09-02 18:51:20 +02:00
{
public:
enum Type { BufferRange, ReplacedBufferRange, Text };
AtomContent(BufferIterator begin, BufferIterator end)
: m_type(BufferRange),
m_begin(std::move(begin)),
m_end(std::move(end)) {}
AtomContent(String str)
: m_type(Text), m_text(std::move(str)) {}
String content() const
{
switch (m_type)
{
case BufferRange:
return m_begin.buffer().string(m_begin, m_end);
case Text:
case ReplacedBufferRange:
return m_text;
}
2012-10-11 01:17:29 +02:00
assert(false);
return 0;
}
2012-09-30 16:21:20 +02:00
CharCount length() const
{
switch (m_type)
{
case BufferRange:
return utf8::distance(m_begin, m_end);
2012-09-30 16:21:20 +02:00
case Text:
case ReplacedBufferRange:
return m_text.char_length();
2012-09-30 16:21:20 +02:00
}
2012-10-11 01:17:29 +02:00
assert(false);
return 0;
2012-09-30 16:21:20 +02:00
}
const BufferIterator& begin() const
{
assert(has_buffer_range());
return m_begin;
}
const BufferIterator& end() const
{
assert(has_buffer_range());
return m_end;
}
void replace(String text)
{
assert(m_type == BufferRange);
m_type = ReplacedBufferRange;
m_text = std::move(text);
}
bool has_buffer_range() const
{
return m_type == BufferRange or m_type == ReplacedBufferRange;
}
Type type() const { return m_type; }
2011-10-17 21:01:04 +02:00
private:
friend class DisplayLine;
Type m_type;
BufferIterator m_begin;
BufferIterator m_end;
String m_text;
2011-09-02 18:51:20 +02:00
};
struct DisplayAtom
2011-09-02 18:51:20 +02:00
{
Color fg_color;
Color bg_color;
Attribute attribute;
2011-09-02 18:51:20 +02:00
AtomContent content;
2011-09-02 18:51:20 +02:00
DisplayAtom(AtomContent content)
2013-01-04 18:39:13 +01:00
: content(std::move(content)), attribute(Normal),
fg_color(Color::Default), bg_color(Color::Default) {}
};
2011-09-02 18:51:20 +02:00
class DisplayLine
{
public:
using AtomList = std::vector<DisplayAtom>;
using iterator = AtomList::iterator;
using const_iterator = AtomList::const_iterator;
explicit DisplayLine(LineCount buffer_line) : m_buffer_line(buffer_line) {}
DisplayLine(LineCount buffer_line, AtomList atoms)
: m_buffer_line(buffer_line), m_atoms(std::move(atoms)) {}
LineCount buffer_line() const { return m_buffer_line; }
2011-09-02 18:51:20 +02:00
iterator begin() { return m_atoms.begin(); }
iterator end() { return m_atoms.end(); }
2011-09-02 18:51:20 +02:00
const_iterator begin() const { return m_atoms.begin(); }
const_iterator end() const { return m_atoms.end(); }
const AtomList& atoms() const { return m_atoms; }
// Split atom pointed by it at pos, returns an iterator to the first atom
iterator split(iterator it, BufferIterator pos);
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();
2011-09-02 18:51:20 +02:00
private:
LineCount m_buffer_line;
AtomList m_atoms;
2011-09-02 18:51:20 +02:00
};
using BufferRange = std::pair<BufferIterator, BufferIterator>;
class DisplayBuffer
{
public:
using LineList = std::vector<DisplayLine>;
DisplayBuffer() {}
LineList& lines() { 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 optimize();
void compute_range();
private:
LineList m_lines;
BufferRange m_range;
};
2011-09-02 18:51:20 +02:00
}
#endif // display_buffer_hh_INCLUDED