diff options
| author | Maxime Coste <mawww@kakoune.org> | 2018-06-21 08:07:17 +1000 |
|---|---|---|
| committer | Maxime Coste <mawww@kakoune.org> | 2018-06-21 08:07:17 +1000 |
| commit | 667777521b654156722cfe0a307fbd381fa1f629 (patch) | |
| tree | 7ad8015045c98299a87eb1668792c7d0267750a8 /src/normal.cc | |
| parent | 52a3e9a84d852595f51f117abd1a715da4d4f9e2 (diff) | |
Refactor the way main selection is determined after rotating contents
Fixes #2133
Diffstat (limited to 'src/normal.cc')
| -rw-r--r-- | src/normal.cc | 19 |
1 files changed, 8 insertions, 11 deletions
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 |
