diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2013-05-02 19:04:59 +0200 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2013-05-02 19:04:59 +0200 |
| commit | 9b3e0c8055fd78a12e03e6552202bf852ac745a2 (patch) | |
| tree | a3ca0253d78c0de59fc3d9898a45793e66dfbada /src | |
| parent | 4ab5c7a203e3d89773e316756a188617babc96c1 (diff) | |
Move selection update code to SelectionList
Diffstat (limited to 'src')
| -rw-r--r-- | src/dynamic_selection_list.cc | 84 | ||||
| -rw-r--r-- | src/selection.cc | 90 | ||||
| -rw-r--r-- | src/selection.hh | 9 |
3 files changed, 100 insertions, 83 deletions
diff --git a/src/dynamic_selection_list.cc b/src/dynamic_selection_list.cc index 1490dd9c..e6e32f6d 100644 --- a/src/dynamic_selection_list.cc +++ b/src/dynamic_selection_list.cc @@ -30,94 +30,14 @@ void DynamicSelectionList::check_invariant() const #endif } -namespace -{ - -template<template <bool, bool> class UpdateFunc> -void on_buffer_change(SelectionList& sels, const BufferCoord& begin, const BufferCoord& end, LineCount end_line) -{ - auto update_beg = std::lower_bound(sels.begin(), sels.end(), begin, - [](const Selection& s, const BufferCoord& c) { return std::max(s.first(), s.last()).coord() < c; }); - auto update_only_line_beg = std::upper_bound(sels.begin(), sels.end(), end_line, - [](LineCount l, const Selection& s) { return l < std::min(s.first(), s.last()).line(); }); - - if (update_beg != update_only_line_beg) - { - // for the first one, we are not sure if min < begin - UpdateFunc<false, false>{}(update_beg->first(), begin, end); - UpdateFunc<false, false>{}(update_beg->last(), begin, end); - } - for (auto it = update_beg+1; it < update_only_line_beg; ++it) - { - UpdateFunc<false, true>{}(it->first(), begin, end); - UpdateFunc<false, true>{}(it->last(), begin, end); - } - if (end.line > begin.line) - { - for (auto it = update_only_line_beg; it != sels.end(); ++it) - { - UpdateFunc<true, true>{}(it->first(), begin, end); - UpdateFunc<true, true>{}(it->last(), begin, end); - } - } -} - -template<bool assume_different_line, bool assume_greater_than_begin> -struct UpdateInsert -{ - void operator()(BufferIterator& it, - const BufferCoord& begin, const BufferCoord& end) const - { - BufferCoord coord = it.coord(); - if (assume_different_line) - kak_assert(begin.line < coord.line); - if (not assume_greater_than_begin and coord < begin) - return; - if (not assume_different_line and begin.line == coord.line) - coord.column = end.column + coord.column - begin.column; - - coord.line += end.line - begin.line; - it = coord; - } -}; - -template<bool assume_different_line, bool assume_greater_than_begin> -struct UpdateErase -{ - void operator()(BufferIterator& it, - const BufferCoord& begin, const BufferCoord& end) const - { - BufferCoord coord = it.coord(); - if (not assume_greater_than_begin and coord < begin) - return; - if (assume_different_line) - kak_assert(end.line < coord.line); - if (not assume_different_line and coord <= end) - coord = it.buffer().clamp(begin); - else - { - if (not assume_different_line and end.line == coord.line) - { - coord.line = begin.line; - coord.column = begin.column + coord.column - end.column; - } - else - coord.line -= end.line - begin.line; - } - it = coord; - } -}; - -} - void DynamicSelectionList::on_insert(const BufferIterator& begin, const BufferIterator& end) { - on_buffer_change<UpdateInsert>(*this, begin.coord(), end.coord(), begin.line()); + update_insert(begin.coord(), end.coord()); } void DynamicSelectionList::on_erase(const BufferIterator& begin, const BufferIterator& end) { - on_buffer_change<UpdateErase>(*this, begin.coord(), end.coord(), end.line()); + update_erase(begin.coord(), end.coord()); } } diff --git a/src/selection.cc b/src/selection.cc index 065aaefc..0e6bd710 100644 --- a/src/selection.cc +++ b/src/selection.cc @@ -52,4 +52,94 @@ void Selection::avoid_eol() Kakoune::avoid_eol(last()); } +namespace +{ + +template<template <bool, bool> class UpdateFunc> +void on_buffer_change(SelectionList& sels, const BufferCoord& begin, const BufferCoord& end, LineCount end_line) +{ + auto update_beg = std::lower_bound(sels.begin(), sels.end(), begin, + [](const Selection& s, const BufferCoord& c) { return std::max(s.first(), s.last()).coord() < c; }); + auto update_only_line_beg = std::upper_bound(sels.begin(), sels.end(), end_line, + [](LineCount l, const Selection& s) { return l < std::min(s.first(), s.last()).line(); }); + + if (update_beg != update_only_line_beg) + { + // for the first one, we are not sure if min < begin + UpdateFunc<false, false>{}(update_beg->first(), begin, end); + UpdateFunc<false, false>{}(update_beg->last(), begin, end); + } + for (auto it = update_beg+1; it < update_only_line_beg; ++it) + { + UpdateFunc<false, true>{}(it->first(), begin, end); + UpdateFunc<false, true>{}(it->last(), begin, end); + } + if (end.line > begin.line) + { + for (auto it = update_only_line_beg; it != sels.end(); ++it) + { + UpdateFunc<true, true>{}(it->first(), begin, end); + UpdateFunc<true, true>{}(it->last(), begin, end); + } + } +} + +template<bool assume_different_line, bool assume_greater_than_begin> +struct UpdateInsert +{ + void operator()(BufferIterator& it, + const BufferCoord& begin, const BufferCoord& end) const + { + BufferCoord coord = it.coord(); + if (assume_different_line) + kak_assert(begin.line < coord.line); + if (not assume_greater_than_begin and coord < begin) + return; + if (not assume_different_line and begin.line == coord.line) + coord.column = end.column + coord.column - begin.column; + + coord.line += end.line - begin.line; + it = coord; + } +}; + +template<bool assume_different_line, bool assume_greater_than_begin> +struct UpdateErase +{ + void operator()(BufferIterator& it, + const BufferCoord& begin, const BufferCoord& end) const + { + BufferCoord coord = it.coord(); + if (not assume_greater_than_begin and coord < begin) + return; + if (assume_different_line) + kak_assert(end.line < coord.line); + if (not assume_different_line and coord <= end) + coord = it.buffer().clamp(begin); + else + { + if (not assume_different_line and end.line == coord.line) + { + coord.line = begin.line; + coord.column = begin.column + coord.column - end.column; + } + else + coord.line -= end.line - begin.line; + } + it = coord; + } +}; + +} + +void SelectionList::update_insert(const BufferCoord& begin, const BufferCoord& end) +{ + on_buffer_change<UpdateInsert>(*this, begin, end, begin.line); +} + +void SelectionList::update_erase(const BufferCoord& begin, const BufferCoord& end) +{ + on_buffer_change<UpdateErase>(*this, begin, end, end.line); +} + } diff --git a/src/selection.hh b/src/selection.hh index 2faf61e5..50b8ad7c 100644 --- a/src/selection.hh +++ b/src/selection.hh @@ -67,7 +67,14 @@ private: CaptureList m_captures; }; -using SelectionList = std::vector<Selection>; + +struct SelectionList : std::vector<Selection> +{ + using std::vector<Selection>::vector; + + void update_insert(const BufferCoord& begin, const BufferCoord& end); + void update_erase(const BufferCoord& begin, const BufferCoord& end); +}; } |
