on master help sign in
Maintain the current cursor's position and view in the vsplit/hsplit commands too (#8109)

Co-authored-by: Benjamin Bouvier <public@benj.me>

commite8fc77fe98a79473faacba544acae9e52b36903f authorLorenzo Bellina <59364991+TheRealLorenz@users.noreply.github.com> dateMon 4 Sep 2023, 05:39 (3 years ago) parentbb3e699
2 files changed+11−13
helix-term/src/commands.rs+9−7 helix-term/src/commands/typed.rs+2−6
helix-term/src/commands.rs+9−7
@@ -4886,17 +4886,19 @@ fn transpose_view(cx: &mut Context) {
48864886 cx.editor.transpose_view()
48874887}
48884888
4889−// split helper, clear it later
4890−fn split(cx: &mut Context, action: Action) {
4891− let (view, doc) = current!(cx.editor);
4889+/// Open a new split in the given direction specified by the action.
4890+///
4891+/// Maintain the current view (both the cursor's position and view in document).
4892+fn split(editor: &mut Editor, action: Action) {
4893+ let (view, doc) = current!(editor);
48924894 let id = doc.id();
48934895 let selection = doc.selection(view.id).clone();
48944896 let offset = view.offset;
48954897
4896− cx.editor.switch(id, action);
4898+ editor.switch(id, action);
48974899
48984900 // match the selection in the previous view
4899− let (view, doc) = current!(cx.editor);
4901+ let (view, doc) = current!(editor);
49004902 doc.set_selection(view.id, selection);
49014903 // match the view scroll offset (switch doesn't handle this fully
49024904 // since the selection is only matched after the split)
@@ -4904,7 +4906,7 @@ fn split(cx: &mut Context, action: Action) {
49044906}
49054907
49064908fn hsplit(cx: &mut Context) {
4907− split(cx, Action::HorizontalSplit);
4909+ split(cx.editor, Action::HorizontalSplit);
49084910}
49094911
49104912fn hsplit_new(cx: &mut Context) {
@@ -4912,7 +4914,7 @@ fn hsplit_new(cx: &mut Context) {
49124914}
49134915
49144916fn vsplit(cx: &mut Context) {
4915− split(cx, Action::VerticalSplit);
4917+ split(cx.editor, Action::VerticalSplit);
49164918}
49174919
49184920fn vsplit_new(cx: &mut Context) {
helix-term/src/commands/typed.rs+2−6
@@ -1536,10 +1536,8 @@ fn vsplit(
15361536 return Ok(());
15371537 }
15381538
1539− let id = view!(cx.editor).doc;
1540−
15411539 if args.is_empty() {
1542− cx.editor.switch(id, Action::VerticalSplit);
1540+ split(cx.editor, Action::VerticalSplit);
15431541 } else {
15441542 for arg in args {
15451543 cx.editor
@@ -1559,10 +1557,8 @@ fn hsplit(
15591557 return Ok(());
15601558 }
15611559
1562− let id = view!(cx.editor).doc;
1563−
15641560 if args.is_empty() {
1565− cx.editor.switch(id, Action::HorizontalSplit);
1561+ split(cx.editor, Action::HorizontalSplit);
15661562 } else {
15671563 for arg in args {
15681564 cx.editor