30 _start_first.resize(size);
31 _start_last.resize(size);
32 _finish_first.resize(size);
33 _finish_last.resize(size);
49 auto first =
std::min(_start_first, _finish_first);
50 auto last =
std::max(_start_last, _finish_last);
52 first = first.before_neighbor(size);
53 last = last.after_neighbor(size);
62 auto first =
std::min(_start_first, _finish_first);
63 auto last =
std::max(_start_last, _finish_last);
65 auto first_index = first.after() ? first.index() + 1 : first.index();
66 auto last_index = last.after() ? last.index() + 1 : last.index();
67 return {first_index, last_index};
70 [[nodiscard]]
constexpr bool empty() const noexcept
72 auto const[first_index, last_index] = selection_indices();
73 return first_index >= last_index;
76 constexpr explicit operator bool() const noexcept
81 constexpr text_selection& clear_selection(
size_t size)
noexcept
83 return set_cursor(_cursor.resize(size));
86 constexpr text_selection& set_cursor(text_cursor new_cursor)
noexcept
88 _cursor = _start_first = _start_last = _finish_first = _finish_last = new_cursor;
89 hi_axiom(holds_invariant());
93 constexpr text_selection& operator=(text_cursor
const& rhs)
noexcept
95 return set_cursor(rhs);
98 constexpr void start_selection(text_cursor new_cursor, text_cursor first, text_cursor last)
noexcept
100 _start_first = _finish_first = first;
101 _start_last = _finish_last = last;
102 _cursor = new_cursor == first ? first : last;
103 hi_axiom(holds_invariant());
108 return start_selection(new_cursor, selection.first, selection.second);
111 constexpr void drag_selection(text_cursor drag_cursor)
noexcept
113 _finish_first = _finish_last = drag_cursor;
114 _cursor = drag_cursor;
117 constexpr void drag_selection(text_cursor drag_cursor, text_cursor first, text_cursor last)
noexcept
119 _finish_first = first;
121 _cursor = first < _start_first ? first : last > _start_last ? last : drag_cursor == first ? first : last;
122 hi_axiom(holds_invariant());
127 return drag_selection(drag_cursor, selection.first, selection.second);
130 [[nodiscard]]
constexpr friend bool operator==(text_selection
const&, text_selection
const&)
noexcept =
default;
133 [[nodiscard]]
constexpr bool holds_invariant() const noexcept
135 return _start_first <= _start_last and _finish_first <= _finish_last and
136 (_cursor == _start_first or _cursor == _start_last or _cursor == _finish_first or _cursor == _finish_last);
148 text_cursor _start_first;
152 text_cursor _start_last;
156 text_cursor _finish_first;
160 text_cursor _finish_last;
constexpr std::pair< size_t, size_t > selection_indices() const noexcept
Get the text indices for the selection.
Definition text_selection.hpp:60
constexpr std::pair< text_cursor, text_cursor > selection(size_t size) const noexcept
Return the selection of characters.
Definition text_selection.hpp:47