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

Implement view swapping #2445

Merged
merged 2 commits into from
May 21, 2022
Merged
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
20 changes: 20 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,10 @@ impl MappableCommand {
jump_view_left, "Jump to the split to the left",
jump_view_up, "Jump to the split above",
jump_view_down, "Jump to the split below",
swap_view_right, "Swap with the split to the right",
swap_view_left, "Swap with the split to the left",
swap_view_up, "Swap with the split above",
swap_view_down, "Swap with the split below",
transpose_view, "Transpose splits",
rotate_view, "Goto next window",
hsplit, "Horizontal bottom split",
Expand Down Expand Up @@ -3864,6 +3868,22 @@ fn jump_view_down(cx: &mut Context) {
cx.editor.focus_down()
}

fn swap_view_right(cx: &mut Context) {
cx.editor.swap_right()
}

fn swap_view_left(cx: &mut Context) {
cx.editor.swap_left()
}

fn swap_view_up(cx: &mut Context) {
cx.editor.swap_up()
}

fn swap_view_down(cx: &mut Context) {
cx.editor.swap_down()
}

fn transpose_view(cx: &mut Context) {
cx.editor.transpose_view()
}
Expand Down
8 changes: 8 additions & 0 deletions helix-term/src/keymap/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ pub fn default() -> HashMap<Mode, Keymap> {
"C-j" | "j" | "down" => jump_view_down,
"C-k" | "k" | "up" => jump_view_up,
"C-l" | "l" | "right" => jump_view_right,
"L" => swap_view_right,
"K" => swap_view_up,
"H" => swap_view_left,
"J" => swap_view_down,
Comment on lines +183 to +186
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will contradict with kakoune keys as in uppercase have different meanings but I think it is fine.

"n" => { "New split scratch buffer"
"C-s" | "s" => hsplit_new,
"C-v" | "v" => vsplit_new,
Expand Down Expand Up @@ -236,6 +240,10 @@ pub fn default() -> HashMap<Mode, Keymap> {
"C-j" | "j" | "down" => jump_view_down,
"C-k" | "k" | "up" => jump_view_up,
"C-l" | "l" | "right" => jump_view_right,
"H" => swap_view_left,
"J" => swap_view_down,
"K" => swap_view_up,
"L" => swap_view_right,
"n" => { "New split scratch buffer"
"C-s" | "s" => hsplit_new,
"C-v" | "v" => vsplit_new,
Expand Down
16 changes: 16 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,22 @@ impl Editor {
self.tree.focus_direction(tree::Direction::Down);
}

pub fn swap_right(&mut self) {
self.tree.swap_split_in_direction(tree::Direction::Right);
}

pub fn swap_left(&mut self) {
self.tree.swap_split_in_direction(tree::Direction::Left);
}

pub fn swap_up(&mut self) {
self.tree.swap_split_in_direction(tree::Direction::Up);
}

pub fn swap_down(&mut self) {
self.tree.swap_split_in_direction(tree::Direction::Down);
}

pub fn transpose_view(&mut self) {
self.tree.transpose();
}
Expand Down
199 changes: 199 additions & 0 deletions helix-view/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,76 @@ impl Tree {
}
}

pub fn swap_split_in_direction(&mut self, direction: Direction) -> Option<()> {
let focus = self.focus;
let target = self.find_split_in_direction(focus, direction)?;
let focus_parent = self.nodes[focus].parent;
let target_parent = self.nodes[target].parent;

if focus_parent == target_parent {
let parent = focus_parent;
let [parent, focus, target] = self.nodes.get_disjoint_mut([parent, focus, target])?;
match (&mut parent.content, &mut focus.content, &mut target.content) {
(
Content::Container(parent),
Content::View(focus_view),
Content::View(target_view),
) => {
let focus_pos = parent.children.iter().position(|id| focus_view.id == *id)?;
let target_pos = parent
.children
.iter()
.position(|id| target_view.id == *id)?;
// swap node positions so that traversal order is kept
parent.children[focus_pos] = target_view.id;
parent.children[target_pos] = focus_view.id;
// swap area so that views rendered at the correct location
std::mem::swap(&mut focus_view.area, &mut target_view.area);

Some(())
}
_ => unreachable!(),
}
} else {
let [focus_parent, target_parent, focus, target] =
self.nodes
.get_disjoint_mut([focus_parent, target_parent, focus, target])?;
match (
&mut focus_parent.content,
&mut target_parent.content,
&mut focus.content,
&mut target.content,
) {
(
Content::Container(focus_parent),
Content::Container(target_parent),
Content::View(focus_view),
Content::View(target_view),
) => {
let focus_pos = focus_parent
.children
.iter()
.position(|id| focus_view.id == *id)?;
let target_pos = target_parent
.children
.iter()
.position(|id| target_view.id == *id)?;
// re-parent target and focus nodes
std::mem::swap(
&mut focus_parent.children[focus_pos],
&mut target_parent.children[target_pos],
);
std::mem::swap(&mut focus.parent, &mut target.parent);
// swap area so that views rendered at the correct location
std::mem::swap(&mut focus_view.area, &mut target_view.area);

Some(())
}
_ => unreachable!(),
}
}
}

pub fn area(&self) -> Rect {
self.area
}
Expand Down Expand Up @@ -649,4 +719,133 @@ mod test {
assert_eq!(None, tree.find_split_in_direction(r0, Direction::Right));
assert_eq!(None, tree.find_split_in_direction(r0, Direction::Up));
}

#[test]
fn swap_split_in_direction() {
let mut tree = Tree::new(Rect {
x: 0,
y: 0,
width: 180,
height: 80,
});

let doc_l0 = DocumentId::default();
let mut view = View::new(
doc_l0,
vec![GutterType::Diagnostics, GutterType::LineNumbers],
);
view.area = Rect::new(0, 0, 180, 80);
tree.insert(view);

let l0 = tree.focus;

let doc_r0 = DocumentId::default();
let view = View::new(
doc_r0,
vec![GutterType::Diagnostics, GutterType::LineNumbers],
);
tree.split(view, Layout::Vertical);
let r0 = tree.focus;

tree.focus = l0;

let doc_l1 = DocumentId::default();
let view = View::new(
doc_l1,
vec![GutterType::Diagnostics, GutterType::LineNumbers],
);
tree.split(view, Layout::Horizontal);
let l1 = tree.focus;

tree.focus = l0;

let doc_l2 = DocumentId::default();
let view = View::new(
doc_l2,
vec![GutterType::Diagnostics, GutterType::LineNumbers],
);
tree.split(view, Layout::Vertical);
let l2 = tree.focus;

// Views in test
// | L0 | L2 | |
// | L1 | R0 |

// Document IDs in test
// | l0 | l2 | |
// | l1 | r0 |

fn doc_id(tree: &Tree, view_id: ViewId) -> Option<DocumentId> {
if let Content::View(view) = &tree.nodes[view_id].content {
Some(view.doc)
} else {
None
}
}

tree.focus = l0;
// `*` marks the view in focus from view table (here L0)
// | l0* | l2 | |
// | l1 | r0 |
tree.swap_split_in_direction(Direction::Down);
// | l1 | l2 | |
// | l0* | r0 |
assert_eq!(tree.focus, l0);
assert_eq!(doc_id(&tree, l0), Some(doc_l1));
assert_eq!(doc_id(&tree, l1), Some(doc_l0));
assert_eq!(doc_id(&tree, l2), Some(doc_l2));
assert_eq!(doc_id(&tree, r0), Some(doc_r0));

tree.swap_split_in_direction(Direction::Right);

// | l1 | l2 | |
// | r0 | l0* |
assert_eq!(tree.focus, l0);
assert_eq!(doc_id(&tree, l0), Some(doc_l1));
assert_eq!(doc_id(&tree, l1), Some(doc_r0));
assert_eq!(doc_id(&tree, l2), Some(doc_l2));
assert_eq!(doc_id(&tree, r0), Some(doc_l0));

// cannot swap, nothing changes
tree.swap_split_in_direction(Direction::Up);
// | l1 | l2 | |
// | r0 | l0* |
assert_eq!(tree.focus, l0);
assert_eq!(doc_id(&tree, l0), Some(doc_l1));
assert_eq!(doc_id(&tree, l1), Some(doc_r0));
assert_eq!(doc_id(&tree, l2), Some(doc_l2));
assert_eq!(doc_id(&tree, r0), Some(doc_l0));

// cannot swap, nothing changes
tree.swap_split_in_direction(Direction::Down);
// | l1 | l2 | |
// | r0 | l0* |
assert_eq!(tree.focus, l0);
assert_eq!(doc_id(&tree, l0), Some(doc_l1));
assert_eq!(doc_id(&tree, l1), Some(doc_r0));
assert_eq!(doc_id(&tree, l2), Some(doc_l2));
assert_eq!(doc_id(&tree, r0), Some(doc_l0));

tree.focus = l2;
// | l1 | l2* | |
// | r0 | l0 |

tree.swap_split_in_direction(Direction::Down);
// | l1 | r0 | |
// | l2* | l0 |
assert_eq!(tree.focus, l2);
assert_eq!(doc_id(&tree, l0), Some(doc_l1));
assert_eq!(doc_id(&tree, l1), Some(doc_l2));
assert_eq!(doc_id(&tree, l2), Some(doc_r0));
assert_eq!(doc_id(&tree, r0), Some(doc_l0));

tree.swap_split_in_direction(Direction::Up);
// | l2* | r0 | |
// | l1 | l0 |
assert_eq!(tree.focus, l2);
assert_eq!(doc_id(&tree, l0), Some(doc_l2));
assert_eq!(doc_id(&tree, l1), Some(doc_l1));
assert_eq!(doc_id(&tree, l2), Some(doc_r0));
assert_eq!(doc_id(&tree, r0), Some(doc_l0));
}
}