selection.hh (6630B)
1 #ifndef selection_hh_INCLUDED 2 #define selection_hh_INCLUDED 3 4 #include "coord.hh" 5 #include "range.hh" 6 #include "safe_ptr.hh" 7 #include "utils.hh" 8 #include "string.hh" 9 #include "vector.hh" 10 11 #include <limits> 12 13 namespace Kakoune 14 { 15 16 class Buffer; 17 using BufferRange = Range<BufferCoord>; 18 19 using CaptureList = Vector<String, MemoryDomain::Selections>; 20 21 constexpr ColumnCount max_column{std::numeric_limits<int>::max()}; 22 constexpr ColumnCount max_non_eol_column{max_column-1}; 23 24 struct BasicSelection 25 { 26 static constexpr MemoryDomain Domain = MemoryDomain::Selections; 27 28 BasicSelection() = default; 29 BasicSelection(BufferCoord pos) : BasicSelection(pos,pos) {} 30 BasicSelection(BufferCoordAndTarget pos) : BasicSelection(pos,pos) {} 31 BasicSelection(BufferCoord anchor, BufferCoordAndTarget cursor) 32 : m_anchor{anchor}, m_cursor{cursor} {} 33 34 BufferCoord& anchor() { return m_anchor; } 35 BufferCoordAndTarget& cursor() { return m_cursor; } 36 37 const BufferCoord& anchor() const { return m_anchor; } 38 const BufferCoordAndTarget& cursor() const { return m_cursor; } 39 40 void set(BufferCoord anchor, BufferCoord cursor) 41 { 42 m_anchor = anchor; 43 m_cursor = cursor; 44 } 45 46 void set(BufferCoord coord) { set(coord, coord); } 47 48 friend bool operator==(const BasicSelection&, const BasicSelection&) = default; 49 50 // When selections are single char, we want the anchor to be considered min, and cursor max 51 const BufferCoord& min() const { return m_anchor <= m_cursor ? m_anchor : m_cursor; } 52 const BufferCoord& max() const { return m_anchor <= m_cursor ? m_cursor : m_anchor; } 53 54 BufferCoord& min() { return m_anchor <= m_cursor ? m_anchor : m_cursor; } 55 BufferCoord& max() { return m_anchor <= m_cursor ? m_cursor : m_anchor; } 56 57 private: 58 BufferCoord m_anchor; 59 BufferCoordAndTarget m_cursor; 60 }; 61 62 struct Selection : BasicSelection 63 { 64 Selection() = default; 65 Selection(BufferCoord pos) : BasicSelection(pos,pos) {} 66 Selection(BufferCoordAndTarget pos) : BasicSelection(pos,pos) {} 67 Selection(BufferCoord anchor, BufferCoordAndTarget cursor, CaptureList captures = {}) 68 : BasicSelection{anchor, cursor}, m_captures(std::move(captures)) {} 69 CaptureList& captures() { return m_captures; } 70 const CaptureList& captures() const { return m_captures; } 71 72 friend bool operator==(const Selection&, const Selection&) = default; 73 74 private: 75 CaptureList m_captures; 76 }; 77 78 inline bool overlaps(const BasicSelection& lhs, const BasicSelection& rhs) 79 { 80 return lhs.min() <= rhs.min() ? lhs.max() >= rhs.min() 81 : lhs.min() <= rhs.max(); 82 } 83 84 void update_selections(Vector<Selection>& selections, size_t& main, 85 const Buffer& buffer, size_t timestamp, bool merge = true); 86 87 bool compare_selections(const Selection& lhs, const Selection& rhs); 88 void sort_selections(Vector<Selection>& selections, size_t& main); 89 void merge_overlapping_selections(Vector<Selection>& selections, size_t& main); 90 void clamp_selections(Vector<Selection>& sel, const Buffer& buffer); 91 92 void replace(Buffer& buffer, Selection& sel, StringView content); 93 BufferRange insert(Buffer& buffer, Selection& sel, BufferCoord pos, StringView content); 94 95 struct SelectionList 96 { 97 static constexpr MemoryDomain Domain = MemoryDomain::Selections; 98 99 ~SelectionList(); 100 SelectionList(Buffer& buffer, Selection s); 101 SelectionList(Buffer& buffer, Selection s, size_t timestamp); 102 SelectionList(Buffer& buffer, Vector<Selection> s); 103 SelectionList(Buffer& buffer, Vector<Selection> s, size_t timestamp); 104 105 SelectionList(const SelectionList&); 106 SelectionList(SelectionList&&); 107 108 SelectionList& operator=(const SelectionList&); 109 SelectionList& operator=(SelectionList&&); 110 111 void update(bool merge = true); 112 void check_invariant() const; 113 114 const Selection& main() const { return (*this)[m_main]; } 115 Selection& main() { return (*this)[m_main]; } 116 size_t main_index() const { return m_main; } 117 void set_main_index(size_t main) { kak_assert(main < size()); m_main = main; } 118 119 void push_back(const Selection& sel) { m_selections.push_back(sel); } 120 void push_back(Selection&& sel) { m_selections.push_back(std::move(sel)); } 121 122 Selection& operator[](size_t i) { return m_selections[i]; } 123 const Selection& operator[](size_t i) const { return m_selections[i]; } 124 125 void set(Vector<Selection> list, size_t main); 126 SelectionList& operator=(Vector<Selection> list) 127 { 128 const size_t main_index = list.size()-1; 129 set(std::move(list), main_index); 130 return *this; 131 } 132 133 using iterator = Vector<Selection>::iterator; 134 iterator begin() { return m_selections.begin(); } 135 iterator end() { return m_selections.end(); } 136 137 using const_iterator = Vector<Selection>::const_iterator; 138 const_iterator begin() const { return m_selections.begin(); } 139 const_iterator end() const { return m_selections.end(); } 140 141 void remove(size_t index); 142 void remove_from(size_t index); 143 144 const Selection* data() const { return m_selections.data(); } 145 size_t size() const { return m_selections.size(); } 146 147 bool operator==(const SelectionList& other) const { return m_buffer == other.m_buffer and m_selections == other.m_selections; } 148 149 void sort(); 150 void merge_overlapping(); 151 void merge_consecutive(); 152 void sort_and_merge_overlapping(); 153 154 Buffer& buffer() const { return *m_buffer; } 155 156 size_t timestamp() const { return m_timestamp; } 157 void force_timestamp(size_t timestamp) { m_timestamp = timestamp; } 158 159 using ApplyFunc = FunctionRef<void (size_t index, Selection& sel)>; 160 void for_each(ApplyFunc apply, bool may_append); 161 162 void replace(ConstArrayView<String> strings); 163 164 void erase(); 165 166 private: 167 size_t m_main = 0; 168 Vector<Selection> m_selections; 169 170 SafePtr<Buffer> m_buffer; 171 size_t m_timestamp; 172 }; 173 174 Vector<Selection> compute_modified_ranges(const Buffer& buffer, size_t timestamp); 175 176 enum class ColumnType 177 { 178 Byte, 179 Codepoint, 180 DisplayColumn 181 }; 182 183 Selection selection_from_string(ColumnType column_type, const Buffer& buffer, StringView desc, ColumnCount tabstop = -1); 184 String selection_to_string(ColumnType column_type, const Buffer& buffer, const Selection& selection, ColumnCount tabstop = -1); 185 186 String selection_list_to_string(ColumnType column_type, const SelectionList& selections, ColumnCount tabstop = -1); 187 188 SelectionList selection_list_from_strings(Buffer& buffer, ColumnType column_type, ConstArrayView<String> descs, size_t timestamp, size_t main, ColumnCount tabstop = -1); 189 190 } 191 192 #endif // selection_hh_INCLUDED