display_buffer.hh (6103B)
1 #ifndef display_buffer_hh_INCLUDED 2 #define display_buffer_hh_INCLUDED 3 4 #include "face.hh" 5 #include "coord.hh" 6 #include "range.hh" 7 #include "string.hh" 8 #include "vector.hh" 9 #include "hash_map.hh" 10 11 #include <algorithm> 12 13 namespace Kakoune 14 { 15 16 class Buffer; 17 using BufferRange = Range<BufferCoord>; 18 19 class BufferIterator; 20 // Return a buffer iterator to the coord, tolerating one past end of line coords 21 BufferIterator get_iterator(const Buffer& buffer, BufferCoord coord); 22 23 struct DisplayAtom : public UseMemoryDomain<MemoryDomain::Display> 24 { 25 public: 26 enum Type { Range, ReplacedRange, Text }; 27 28 DisplayAtom(const Buffer& buffer, BufferRange range, Face face = {}) 29 : face(face), m_type(Range), m_buffer(&buffer), m_range{range} {} 30 31 DisplayAtom(const Buffer& buffer, BufferRange range, String str, Face face = {}) 32 : face(face), m_type(ReplacedRange), m_buffer(&buffer), m_range{range}, m_text{std::move(str)} {} 33 34 DisplayAtom(String str, Face face) 35 : face(face), m_type(Text), m_text(std::move(str)) {} 36 37 explicit DisplayAtom(String str) 38 : DisplayAtom(std::move(str), Face{}) {} 39 40 StringView content() const; 41 ColumnCount length() const; 42 bool empty() const; 43 44 const BufferCoord& begin() const 45 { 46 kak_assert(has_buffer_range()); 47 return m_range.begin; 48 } 49 50 const BufferCoord& end() const 51 { 52 kak_assert(has_buffer_range()); 53 return m_range.end; 54 } 55 56 void replace(String text) 57 { 58 if (m_type == Range) 59 m_type = ReplacedRange; 60 m_text = std::move(text); 61 } 62 63 void replace(const BufferRange& range) 64 { 65 kak_assert(m_type == Text); 66 m_type = ReplacedRange; 67 m_range = range; 68 } 69 70 bool has_buffer_range() const 71 { 72 return m_type == Range or m_type == ReplacedRange; 73 } 74 75 const Buffer& buffer() const { kak_assert(m_buffer); return *m_buffer; } 76 77 Type type() const { return m_type; } 78 79 ColumnCount trim_begin(ColumnCount count); 80 ColumnCount trim_end_to_length(ColumnCount count); 81 82 bool operator==(const DisplayAtom& other) const 83 { 84 return face == other.face and type() == other.type() and 85 content() == other.content(); 86 } 87 88 Face face; 89 90 private: 91 friend class DisplayLine; 92 93 Type m_type; 94 95 const Buffer* m_buffer = nullptr; 96 BufferRange m_range; 97 String m_text; 98 }; 99 100 using AtomList = Vector<DisplayAtom, MemoryDomain::Display>; 101 102 class DisplayLine : public UseMemoryDomain<MemoryDomain::Display> 103 { 104 public: 105 using iterator = AtomList::iterator; 106 using const_iterator = AtomList::const_iterator; 107 using value_type = AtomList::value_type; 108 109 DisplayLine() = default; 110 DisplayLine(AtomList atoms); 111 DisplayLine(String str, Face face) 112 { push_back({ std::move(str), face }); } 113 114 iterator begin() { return m_atoms.begin(); } 115 iterator end() { return m_atoms.end(); } 116 117 const_iterator begin() const { return m_atoms.begin(); } 118 const_iterator end() const { return m_atoms.end(); } 119 120 const AtomList& atoms() const { return m_atoms; } 121 122 ColumnCount length() const; 123 const BufferRange& range() const { return m_range; } 124 125 // Split atom pointed by it at buffer coord pos, 126 // returns an iterator to the first atom 127 iterator split(iterator it, BufferCoord pos); 128 129 // Split atom pointed by it at its pos column, 130 // returns an iterator to the first atom 131 iterator split(iterator it, ColumnCount pos); 132 133 iterator split(BufferCoord pos); 134 135 iterator insert(iterator it, DisplayAtom atom); 136 137 template<typename It> 138 iterator insert(iterator pos, It beg, It end) 139 { 140 auto has_buffer_range = [](const DisplayAtom& atom) { return atom.has_buffer_range(); }; 141 auto had_range = any_of(*this, has_buffer_range); 142 if (auto first = std::find_if(beg, end, has_buffer_range); first != end) 143 { 144 auto& last = *std::find_if(std::reverse_iterator(end), std::reverse_iterator(first), has_buffer_range); 145 m_range.begin = had_range ? std::min(m_range.begin, (*first).begin()) : (*first).begin(); 146 m_range.end = had_range ? std::max(m_range.end, last.end()) : last.end(); 147 } 148 return m_atoms.insert(pos, beg, end); 149 } 150 151 DisplayLine extract(iterator beg, iterator end); 152 iterator erase(iterator beg, iterator end); 153 DisplayAtom& push_back(DisplayAtom atom); 154 155 // remove front from the begining of the line, and make sure 156 // the line is less that col_count character 157 bool trim(ColumnCount front, ColumnCount col_count); 158 159 // remove front from the begining of the line + first_col, and make sure 160 // the line is less that col_count character 161 bool trim_from(ColumnCount first_col, ColumnCount front, ColumnCount col_count); 162 163 // Merge together consecutive atoms sharing the same display attributes 164 void optimize(); 165 private: 166 void compute_range(bool preserve_if_no_atoms = false); 167 BufferRange m_range = { { INT_MAX, INT_MAX }, { INT_MIN, INT_MIN } }; 168 AtomList m_atoms; 169 }; 170 171 using DisplayLineList = Vector<DisplayLine>; 172 class FaceRegistry; 173 174 DisplayLine parse_display_line(StringView line, const FaceRegistry& faces, const HashMap<String, DisplayLine>& builtins = {}); 175 DisplayLineList parse_display_line_list(StringView content, const FaceRegistry& faces, const HashMap<String, DisplayLine>& builtins = {}); 176 177 class DisplayBuffer : public UseMemoryDomain<MemoryDomain::Display> 178 { 179 public: 180 DisplayBuffer() {} 181 182 DisplayLineList& lines() { return m_lines; } 183 const DisplayLineList& lines() const { return m_lines; } 184 185 // returns the smallest BufferRange which contains every DisplayAtoms 186 const BufferRange& range() const { return m_range; } 187 void compute_range(); 188 189 // Optimize all lines, set DisplayLine::optimize 190 void optimize(); 191 192 void set_timestamp(size_t timestamp) { m_timestamp = timestamp; } 193 size_t timestamp() const { return m_timestamp; } 194 195 private: 196 DisplayLineList m_lines; 197 BufferRange m_range; 198 size_t m_timestamp = -1; 199 }; 200 201 } 202 203 #endif // display_buffer_hh_INCLUDED