From 8cc27354e827a58f6b8199ae0770121db6970996 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sat, 30 Jul 2016 15:32:47 +0100 Subject: [PATCH] Support sorting and merging overlapping separately, fix bug in move Fixes #754 --- src/normal.cc | 4 +++- src/selection.cc | 22 +++++++++++++++++----- src/selection.hh | 2 ++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/normal.cc b/src/normal.cc index c22b47fc..38de574b 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -1513,10 +1513,12 @@ void move(Context& context, NormalParams params) sel.anchor() = mode == SelectMode::Extend ? sel.anchor() : cursor; sel.cursor() = cursor; } + selections.sort(); + if (std::is_same::value) selections.avoid_eol(); - selections.sort_and_merge_overlapping(); + selections.merge_overlapping(); } void select_whole_buffer(Context& context, NormalParams) diff --git a/src/selection.cc b/src/selection.cc index b76f0d38..e9e30ca9 100644 --- a/src/selection.cc +++ b/src/selection.cc @@ -419,7 +419,7 @@ void SelectionList::check_invariant() const #endif } -void SelectionList::sort_and_merge_overlapping() +void SelectionList::sort() { if (size() == 1) return; @@ -434,7 +434,21 @@ void SelectionList::sort_and_merge_overlapping() return begin < main_begin; }); std::stable_sort(begin(), end(), compare_selections); - m_selections.erase(merge_overlapping(begin(), end(), m_main, overlaps), end()); +} + +void SelectionList::merge_overlapping() +{ + if (size() == 1) + return; + + m_selections.erase(Kakoune::merge_overlapping(begin(), end(), + m_main, overlaps), end()); +} + +void SelectionList::sort_and_merge_overlapping() +{ + sort(); + merge_overlapping(); } static inline void _avoid_eol(const Buffer& buffer, ByteCoord& coord) @@ -547,9 +561,7 @@ void SelectionList::insert(ConstArrayView strings, InsertMode mode, void SelectionList::erase() { update(); - - m_selections.erase(merge_overlapping(begin(), end(), m_main, overlaps), - end()); + merge_overlapping(); ForwardChangesTracker changes_tracker; for (auto& sel : m_selections) diff --git a/src/selection.hh b/src/selection.hh index 706907c2..31ed41da 100644 --- a/src/selection.hh +++ b/src/selection.hh @@ -124,6 +124,8 @@ struct SelectionList bool operator==(const SelectionList& other) const { return m_buffer == other.m_buffer and m_selections == other.m_selections; } bool operator!=(const SelectionList& other) const { return not ((*this) == other); } + void sort(); + void merge_overlapping(); void sort_and_merge_overlapping(); Buffer& buffer() const { return *m_buffer; }