Skip to content

Commit

Permalink
Move active editor in direction
Browse files Browse the repository at this point in the history
Closes zed-industries#20205

An action to move the active editor to a pane in a specified direction
  • Loading branch information
Igonato committed Dec 10, 2024
1 parent a3674ec commit e403cc8
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions crates/workspace/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ pub struct SwapPaneInDirection(pub SplitDirection);
#[derive(Clone, Deserialize, PartialEq)]
pub struct MoveItemToPane(pub usize);

#[derive(Clone, Deserialize, PartialEq)]
pub struct MoveItemToPaneInDirection(pub SplitDirection);

#[derive(Clone, PartialEq, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SaveAll {
Expand Down Expand Up @@ -230,6 +233,7 @@ impl_actions!(
CloseAllItemsAndPanes,
CloseInactiveTabsAndPanes,
MoveItemToPane,
MoveItemToPaneInDirection,
OpenTerminal,
Reload,
Save,
Expand Down Expand Up @@ -2850,13 +2854,10 @@ impl Workspace {
};
source_pane.update(cx, |pane, cx| {
let item_id = active_item.item_id();
pane.remove_item(item_id, false, false, cx);
pane.remove_item(item_id, false, true, cx);
target_pane.update(cx, |target_pane, cx| {
target_pane.add_item(active_item, true, true, Some(target_pane.items_len()), cx);
});
if pane.items_len() == 0 {
let _ = self.center.remove(&source_pane);
}
});
}

Expand Down Expand Up @@ -2978,6 +2979,35 @@ impl Workspace {
}
}

pub fn move_item_to_pane_in_direction(
&mut self,
direction: SplitDirection,
cx: &mut WindowContext,
) {
if let Some(target_pane) = self.find_pane_in_direction(direction, cx) {
let source_pane = self.active_pane.clone();
if target_pane == source_pane {
return;
}
let Some(active_item) = source_pane.read(cx).active_item() else {
return;
};
source_pane.update(cx, |pane, cx| {
let item_id = active_item.item_id();
pane.remove_item(item_id, false, true, cx);
target_pane.update(cx, |target_pane, cx| {
target_pane.add_item(
active_item,
true,
true,
Some(target_pane.items_len()),
cx,
);
});
});
}
}

pub fn bounding_box_for_pane(&self, pane: &View<Pane>) -> Option<Bounds<Pixels>> {
self.center.bounding_box_for_pane(pane)
}
Expand Down Expand Up @@ -4470,6 +4500,11 @@ impl Workspace {
workspace.activate_pane_in_direction(action.0, cx)
}),
)
.on_action(
cx.listener(|workspace, action: &MoveItemToPaneInDirection, cx| {
workspace.move_item_to_pane_in_direction(action.0, cx)
}),
)
.on_action(cx.listener(|workspace, action: &SwapPaneInDirection, cx| {
workspace.swap_pane_in_direction(action.0, cx)
}))
Expand Down

0 comments on commit e403cc8

Please sign in to comment.