From 667777521b654156722cfe0a307fbd381fa1f629 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 21 Jun 2018 08:07:17 +1000 Subject: [PATCH] Refactor the way main selection is determined after rotating contents Fixes #2133 --- src/normal.cc | 19 ++++++++----------- .../2133-assert-on-rotate-contents/cmd | 1 + .../2133-assert-on-rotate-contents/in | 1 + .../2133-assert-on-rotate-contents/out | 1 + 4 files changed, 11 insertions(+), 11 deletions(-) create mode 100644 test/regression/2133-assert-on-rotate-contents/cmd create mode 100644 test/regression/2133-assert-on-rotate-contents/in create mode 100644 test/regression/2133-assert-on-rotate-contents/out diff --git a/src/normal.cc b/src/normal.cc index 37e25931..0fe60ef8 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -1435,23 +1435,20 @@ void rotate_selections_content(Context& context, NormalParams params) if (group == 0 or group > (int)strings.size()) group = (int)strings.size(); count = count % group; + auto& selections = context.selections(); + auto main = strings.begin() + selections.main_index(); for (auto it = strings.begin(); it != strings.end(); ) { auto end = std::min(strings.end(), it + group); - if (direction == Direction::Forward) - std::rotate(it, end-count, end); - else - std::rotate(it, it+count, end); + auto new_beg = (direction == Direction::Forward) ? end - count : it + count; + std::rotate(it, new_beg, end); + + if (it <= main and main < end) + main = main < new_beg ? end - (new_beg - main) : it + (main - new_beg); it = end; } - auto& selections = context.selections(); selections.insert(strings, InsertMode::Replace); - const size_t index = selections.main_index(); - const size_t index_in_group = index % group; - selections.set_main_index(index - index_in_group + - ((direction == Forward) ? - (index_in_group + count) % group - : (index_in_group + group - count % group) % group)); + selections.set_main_index(main - strings.begin()); } enum class SelectFlags diff --git a/test/regression/2133-assert-on-rotate-contents/cmd b/test/regression/2133-assert-on-rotate-contents/cmd new file mode 100644 index 00000000..1e43e08f --- /dev/null +++ b/test/regression/2133-assert-on-rotate-contents/cmd @@ -0,0 +1 @@ +2 diff --git a/test/regression/2133-assert-on-rotate-contents/in b/test/regression/2133-assert-on-rotate-contents/in new file mode 100644 index 00000000..478a6fee --- /dev/null +++ b/test/regression/2133-assert-on-rotate-contents/in @@ -0,0 +1 @@ +%(foo) %(bar) %(baz) diff --git a/test/regression/2133-assert-on-rotate-contents/out b/test/regression/2133-assert-on-rotate-contents/out new file mode 100644 index 00000000..1a5d2c33 --- /dev/null +++ b/test/regression/2133-assert-on-rotate-contents/out @@ -0,0 +1 @@ +bar foo baz