editor: Add rotation commands for selections and lines (#41236)
Introduces RotateSelectionsForward and RotateSelectionsBackward actions that rotate content in a circular fashion across multiple cursors. Behavior based on context: - With selections: rotates the selected text at each cursor position (e.g., x=1, y=2, z=3 becomes x=3, y=1, z=2) - With just cursors: rotates entire lines at cursor positions (e.g., three lines cycle to line3, line1, line2) Selections are preserved after rotation, allowing repeated cycling. Useful for quickly rearranging values, lines, or arguments. For more examples and use cases, please refer to #5315. I'm eager to read your thoughts and make any adjustments or improvements to any aspect of this change. Closes #5315 Release Notes: - Added `RotateSelectionsForward` and `RotateSelectionsBackward` actions that rotate content in a circular fashion across multiple cursors
This commit is contained in:
@@ -5777,6 +5777,116 @@ fn test_duplicate_line(cx: &mut TestAppContext) {
|
||||
});
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_rotate_selections(cx: &mut TestAppContext) {
|
||||
init_test(cx, |_| {});
|
||||
|
||||
let mut cx = EditorTestContext::new(cx).await;
|
||||
|
||||
// Rotate text selections (horizontal)
|
||||
cx.set_state("x=«1ˇ», y=«2ˇ», z=«3ˇ»");
|
||||
cx.update_editor(|e, window, cx| {
|
||||
e.rotate_selections_forward(&RotateSelectionsForward, window, cx)
|
||||
});
|
||||
cx.assert_editor_state("x=«3ˇ», y=«1ˇ», z=«2ˇ»");
|
||||
cx.update_editor(|e, window, cx| {
|
||||
e.rotate_selections_backward(&RotateSelectionsBackward, window, cx)
|
||||
});
|
||||
cx.assert_editor_state("x=«1ˇ», y=«2ˇ», z=«3ˇ»");
|
||||
|
||||
// Rotate text selections (vertical)
|
||||
cx.set_state(indoc! {"
|
||||
x=«1ˇ»
|
||||
y=«2ˇ»
|
||||
z=«3ˇ»
|
||||
"});
|
||||
cx.update_editor(|e, window, cx| {
|
||||
e.rotate_selections_forward(&RotateSelectionsForward, window, cx)
|
||||
});
|
||||
cx.assert_editor_state(indoc! {"
|
||||
x=«3ˇ»
|
||||
y=«1ˇ»
|
||||
z=«2ˇ»
|
||||
"});
|
||||
cx.update_editor(|e, window, cx| {
|
||||
e.rotate_selections_backward(&RotateSelectionsBackward, window, cx)
|
||||
});
|
||||
cx.assert_editor_state(indoc! {"
|
||||
x=«1ˇ»
|
||||
y=«2ˇ»
|
||||
z=«3ˇ»
|
||||
"});
|
||||
|
||||
// Rotate text selections (vertical, different lengths)
|
||||
cx.set_state(indoc! {"
|
||||
x=\"«ˇ»\"
|
||||
y=\"«aˇ»\"
|
||||
z=\"«aaˇ»\"
|
||||
"});
|
||||
cx.update_editor(|e, window, cx| {
|
||||
e.rotate_selections_forward(&RotateSelectionsForward, window, cx)
|
||||
});
|
||||
cx.assert_editor_state(indoc! {"
|
||||
x=\"«aaˇ»\"
|
||||
y=\"«ˇ»\"
|
||||
z=\"«aˇ»\"
|
||||
"});
|
||||
cx.update_editor(|e, window, cx| {
|
||||
e.rotate_selections_backward(&RotateSelectionsBackward, window, cx)
|
||||
});
|
||||
cx.assert_editor_state(indoc! {"
|
||||
x=\"«ˇ»\"
|
||||
y=\"«aˇ»\"
|
||||
z=\"«aaˇ»\"
|
||||
"});
|
||||
|
||||
// Rotate whole lines (cursor positions preserved)
|
||||
cx.set_state(indoc! {"
|
||||
ˇline123
|
||||
liˇne23
|
||||
line3ˇ
|
||||
"});
|
||||
cx.update_editor(|e, window, cx| {
|
||||
e.rotate_selections_forward(&RotateSelectionsForward, window, cx)
|
||||
});
|
||||
cx.assert_editor_state(indoc! {"
|
||||
line3ˇ
|
||||
ˇline123
|
||||
liˇne23
|
||||
"});
|
||||
cx.update_editor(|e, window, cx| {
|
||||
e.rotate_selections_backward(&RotateSelectionsBackward, window, cx)
|
||||
});
|
||||
cx.assert_editor_state(indoc! {"
|
||||
ˇline123
|
||||
liˇne23
|
||||
line3ˇ
|
||||
"});
|
||||
|
||||
// Rotate whole lines, multiple cursors per line (positions preserved)
|
||||
cx.set_state(indoc! {"
|
||||
ˇliˇne123
|
||||
ˇline23
|
||||
ˇline3
|
||||
"});
|
||||
cx.update_editor(|e, window, cx| {
|
||||
e.rotate_selections_forward(&RotateSelectionsForward, window, cx)
|
||||
});
|
||||
cx.assert_editor_state(indoc! {"
|
||||
ˇline3
|
||||
ˇliˇne123
|
||||
ˇline23
|
||||
"});
|
||||
cx.update_editor(|e, window, cx| {
|
||||
e.rotate_selections_backward(&RotateSelectionsBackward, window, cx)
|
||||
});
|
||||
cx.assert_editor_state(indoc! {"
|
||||
ˇliˇne123
|
||||
ˇline23
|
||||
ˇline3
|
||||
"});
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn test_move_line_up_down(cx: &mut TestAppContext) {
|
||||
init_test(cx, |_| {});
|
||||
|
||||
Reference in New Issue
Block a user