Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maintain the current cursor's position and view in the vsplit/hsplit commands too #7220

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4636,33 +4636,35 @@ fn transpose_view(cx: &mut Context) {
cx.editor.transpose_view()
}

// split helper, clear it later
fn split(cx: &mut Context, action: Action) {
let (view, doc) = current!(cx.editor);
/// Open a new split in the given direction specified by the action.
///
/// Maintain the current view (both the cursor's position and view in document).
fn split_helper(editor: &mut Editor, action: Action) {
let (view, doc) = current!(editor);
let id = doc.id();
let selection = doc.selection(view.id).clone();
let offset = view.offset;

cx.editor.switch(id, action);
editor.switch(id, action);

// match the selection in the previous view
let (view, doc) = current!(cx.editor);
let (view, doc) = current!(editor);
doc.set_selection(view.id, selection);
// match the view scroll offset (switch doesn't handle this fully
// since the selection is only matched after the split)
view.offset = offset;
}

fn hsplit(cx: &mut Context) {
split(cx, Action::HorizontalSplit);
split_helper(&mut cx.editor, Action::HorizontalSplit);
}

fn hsplit_new(cx: &mut Context) {
cx.editor.new_file(Action::HorizontalSplit);
}

fn vsplit(cx: &mut Context) {
split(cx, Action::VerticalSplit);
split_helper(&mut cx.editor, Action::VerticalSplit);
}

fn vsplit_new(cx: &mut Context) {
Expand Down
8 changes: 2 additions & 6 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1506,10 +1506,8 @@ fn vsplit(
return Ok(());
}

let id = view!(cx.editor).doc;

if args.is_empty() {
cx.editor.switch(id, Action::VerticalSplit);
split_helper(&mut cx.editor, Action::VerticalSplit);
} else {
for arg in args {
cx.editor
Expand All @@ -1529,10 +1527,8 @@ fn hsplit(
return Ok(());
}

let id = view!(cx.editor).doc;

if args.is_empty() {
cx.editor.switch(id, Action::HorizontalSplit);
split_helper(&mut cx.editor, Action::HorizontalSplit);
} else {
for arg in args {
cx.editor
Expand Down