From 8958bf0a926a6e6afc63f0c59f3fa6761f5da709 Mon Sep 17 00:00:00 2001 From: Roland Kovacs Date: Fri, 20 May 2022 03:25:04 +0200 Subject: [PATCH] Implement view transpose (#2461) Change the layout of existing split view from horizontal to vertical and vica-versa. It only effects the focused view and its siblings, i.e. not recursive. Command is mapped to 't' or 'C-t' under the Window menus. --- helix-term/src/commands.rs | 5 +++++ helix-term/src/keymap/default.rs | 2 ++ helix-view/src/editor.rs | 4 ++++ helix-view/src/tree.rs | 12 ++++++++++++ 4 files changed, 23 insertions(+) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index f0b54e0b7..855f2d7dc 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -360,6 +360,7 @@ pub fn doc(&self) -> &str { 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", + transpose_view, "Transpose splits", rotate_view, "Goto next window", hsplit, "Horizontal bottom split", hsplit_new, "Horizontal bottom split scratch buffer", @@ -3863,6 +3864,10 @@ fn jump_view_down(cx: &mut Context) { cx.editor.focus_down() } +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); diff --git a/helix-term/src/keymap/default.rs b/helix-term/src/keymap/default.rs index a8ff8be91..124517d4a 100644 --- a/helix-term/src/keymap/default.rs +++ b/helix-term/src/keymap/default.rs @@ -171,6 +171,7 @@ pub fn default() -> HashMap { "C-w" | "w" => rotate_view, "C-s" | "s" => hsplit, "C-v" | "v" => vsplit, + "C-t" | "t" => transpose_view, "f" => goto_file_hsplit, "F" => goto_file_vsplit, "C-q" | "q" => wclose, @@ -226,6 +227,7 @@ pub fn default() -> HashMap { "C-w" | "w" => rotate_view, "C-s" | "s" => hsplit, "C-v" | "v" => vsplit, + "C-t" | "t" => transpose_view, "f" => goto_file_hsplit, "F" => goto_file_vsplit, "C-q" | "q" => wclose, diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index f4a48ba65..1ad210592 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -885,6 +885,10 @@ pub fn focus_down(&mut self) { self.tree.focus_direction(tree::Direction::Down); } + pub fn transpose_view(&mut self) { + self.tree.transpose(); + } + pub fn should_close(&self) -> bool { self.tree.is_empty() } diff --git a/helix-view/src/tree.rs b/helix-view/src/tree.rs index b068f4c7e..522a79d78 100644 --- a/helix-view/src/tree.rs +++ b/helix-view/src/tree.rs @@ -526,6 +526,18 @@ pub fn focus_next(&mut self) { } } + pub fn transpose(&mut self) { + let focus = self.focus; + let parent = self.nodes[focus].parent; + if let Content::Container(container) = &mut self.nodes[parent].content { + container.layout = match container.layout { + Layout::Vertical => Layout::Horizontal, + Layout::Horizontal => Layout::Vertical, + }; + self.recalculate(); + } + } + pub fn area(&self) -> Rect { self.area }