From 13ed87410f87e4acfeb9d7340cc801eafbfbdbd9 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 26 Jul 2016 23:31:23 +0100 Subject: [PATCH] Rework compute_modified_ranges Fixes #743 --- src/selection.cc | 22 ++++++++++--------- test/compose/undo-multi-replace-at-end/cmd | 1 + test/compose/undo-multi-replace-at-end/in | 3 +++ .../undo-multi-replace-at-end/selections | 2 ++ test/regression/743-crash-on-replace-undo/cmd | 1 + test/regression/743-crash-on-replace-undo/in | 5 +++++ test/regression/743-crash-on-replace-undo/out | 5 +++++ 7 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 test/compose/undo-multi-replace-at-end/cmd create mode 100644 test/compose/undo-multi-replace-at-end/in create mode 100644 test/compose/undo-multi-replace-at-end/selections create mode 100644 test/regression/743-crash-on-replace-undo/cmd create mode 100644 test/regression/743-crash-on-replace-undo/in create mode 100644 test/regression/743-crash-on-replace-undo/out diff --git a/src/selection.cc b/src/selection.cc index 47597a50..a06fdc89 100644 --- a/src/selection.cc +++ b/src/selection.cc @@ -65,7 +65,8 @@ ByteCoord update_erase(ByteCoord coord, ByteCoord begin, ByteCoord end) bool compare_selections(const Selection& lhs, const Selection& rhs) { - return lhs.min() < rhs.min(); + const auto lmin = lhs.min(), rmin = rhs.min(); + return lmin == rmin ? lhs.max() < rhs.max() : lmin < rmin; } template @@ -305,17 +306,15 @@ Vector compute_modified_ranges(Buffer& buffer, size_t timestamp) change_it = backward_end; } - const auto end_coord = buffer.end_coord(); - for (auto it = ranges.begin() + prev_size; it != ranges.end(); ++it) - { - it->anchor() = std::min(it->anchor(), end_coord); - it->cursor() = std::min(it->cursor(), end_coord); - } - - kak_assert(std::is_sorted(ranges.begin() + prev_size, ranges.end(), compare_selections)); std::inplace_merge(ranges.begin(), ranges.begin() + prev_size, ranges.end(), compare_selections); - ranges.erase(merge_overlapping(ranges.begin(), ranges.end(), dummy, overlaps), ranges.end()); + } + + const auto end_coord = buffer.end_coord(); + for (auto& range : ranges) + { + range.anchor() = std::min(range.anchor(), end_coord); + range.cursor() = std::min(range.cursor(), end_coord); } auto touches = [&](const Selection& lhs, const Selection& rhs) { @@ -326,6 +325,9 @@ Vector compute_modified_ranges(Buffer& buffer, size_t timestamp) for (auto& sel : ranges) { + kak_assert(buffer.is_valid(sel.anchor())); + kak_assert(buffer.is_valid(sel.cursor())); + if (buffer.is_end(sel.anchor())) sel.anchor() = buffer.back_coord(); if (buffer.is_end(sel.cursor())) diff --git a/test/compose/undo-multi-replace-at-end/cmd b/test/compose/undo-multi-replace-at-end/cmd new file mode 100644 index 00000000..0c359aa9 --- /dev/null +++ b/test/compose/undo-multi-replace-at-end/cmd @@ -0,0 +1 @@ +%y1Ru diff --git a/test/compose/undo-multi-replace-at-end/in b/test/compose/undo-multi-replace-at-end/in new file mode 100644 index 00000000..a92d664b --- /dev/null +++ b/test/compose/undo-multi-replace-at-end/in @@ -0,0 +1,3 @@ +line 1 +line 2 +line 3 diff --git a/test/compose/undo-multi-replace-at-end/selections b/test/compose/undo-multi-replace-at-end/selections new file mode 100644 index 00000000..48fcc423 --- /dev/null +++ b/test/compose/undo-multi-replace-at-end/selections @@ -0,0 +1,2 @@ +line 2 +line 3 diff --git a/test/regression/743-crash-on-replace-undo/cmd b/test/regression/743-crash-on-replace-undo/cmd new file mode 100644 index 00000000..0d7c3cbf --- /dev/null +++ b/test/regression/743-crash-on-replace-undo/cmd @@ -0,0 +1 @@ +%s1xyjRu diff --git a/test/regression/743-crash-on-replace-undo/in b/test/regression/743-crash-on-replace-undo/in new file mode 100644 index 00000000..3a4e87d1 --- /dev/null +++ b/test/regression/743-crash-on-replace-undo/in @@ -0,0 +1,5 @@ +line 1 +line 2 + +line 1 +line 2 diff --git a/test/regression/743-crash-on-replace-undo/out b/test/regression/743-crash-on-replace-undo/out new file mode 100644 index 00000000..3a4e87d1 --- /dev/null +++ b/test/regression/743-crash-on-replace-undo/out @@ -0,0 +1,5 @@ +line 1 +line 2 + +line 1 +line 2