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

Workspace move editor actions #21760

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
24 changes: 23 additions & 1 deletion assets/keymaps/linux/sublime_text.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,41 @@
"ctrl-shift-[": "pane::ActivatePrevItem",
"ctrl-shift-]": "pane::ActivateNextItem",
"ctrl-pageup": "pane::ActivatePrevItem",
"ctrl-pagedown": "pane::ActivateNextItem"
"ctrl-pagedown": "pane::ActivateNextItem",
"ctrl-1": ["workspace::ActivatePane", 0],
"ctrl-2": ["workspace::ActivatePane", 1],
"ctrl-3": ["workspace::ActivatePane", 2],
"ctrl-4": ["workspace::ActivatePane", 3],
"ctrl-5": ["workspace::ActivatePane", 4],
"ctrl-6": ["workspace::ActivatePane", 5],
"ctrl-7": ["workspace::ActivatePane", 6],
"ctrl-8": ["workspace::ActivatePane", 7],
"ctrl-9": ["workspace::ActivatePane", 8],
"ctrl-shift-1": ["workspace::MoveItemToPane", 0],
"ctrl-shift-2": ["workspace::MoveItemToPane", 1],
"ctrl-shift-3": ["workspace::MoveItemToPane", 2],
"ctrl-shift-4": ["workspace::MoveItemToPane", 3],
"ctrl-shift-5": ["workspace::MoveItemToPane", 4],
"ctrl-shift-6": ["workspace::MoveItemToPane", 5],
"ctrl-shift-7": ["workspace::MoveItemToPane", 6],
"ctrl-shift-8": ["workspace::MoveItemToPane", 7],
"ctrl-shift-9": ["workspace::MoveItemToPane", 8]
}
},
{
"context": "Editor",
"bindings": {
"ctrl-alt-up": "editor::AddSelectionAbove",
"ctrl-alt-down": "editor::AddSelectionBelow",
"ctrl-shift-up": "editor::MoveLineUp",
"ctrl-shift-down": "editor::MoveLineDown",
"ctrl-shift-m": "editor::SelectLargerSyntaxNode",
"ctrl-shift-l": "editor::SplitSelectionIntoLines",
"ctrl-shift-a": "editor::SelectLargerSyntaxNode",
"ctrl-shift-d": "editor::DuplicateLineDown",
"alt-f3": "editor::SelectAllMatches", // find_all_under
"f9": "editor::SortLinesCaseSensitive",
Copy link
Contributor

Choose a reason for hiding this comment

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

These two lines look unrelated?
I see two more below, and same question to AddSelectionAbove and AddSelectionBelow.

"ctrl-f9": "editor::SortLinesCaseInsensitive",
"f12": "editor::GoToDefinition",
"ctrl-f12": "editor::GoToDefinitionSplit",
"shift-f12": "editor::FindAllReferences",
Expand Down
22 changes: 21 additions & 1 deletion assets/keymaps/macos/sublime_text.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,25 @@
"cmd-shift-[": "pane::ActivatePrevItem",
"cmd-shift-]": "pane::ActivateNextItem",
"ctrl-pageup": "pane::ActivatePrevItem",
"ctrl-pagedown": "pane::ActivateNextItem"
"ctrl-pagedown": "pane::ActivateNextItem",
"ctrl-1": ["workspace::ActivatePane", 0],
"ctrl-2": ["workspace::ActivatePane", 1],
"ctrl-3": ["workspace::ActivatePane", 2],
"ctrl-4": ["workspace::ActivatePane", 3],
"ctrl-5": ["workspace::ActivatePane", 4],
"ctrl-6": ["workspace::ActivatePane", 5],
"ctrl-7": ["workspace::ActivatePane", 6],
"ctrl-8": ["workspace::ActivatePane", 7],
"ctrl-9": ["workspace::ActivatePane", 8],
"ctrl-shift-1": ["workspace::MoveItemToPane", 0],
"ctrl-shift-2": ["workspace::MoveItemToPane", 1],
"ctrl-shift-3": ["workspace::MoveItemToPane", 2],
"ctrl-shift-4": ["workspace::MoveItemToPane", 3],
"ctrl-shift-5": ["workspace::MoveItemToPane", 4],
"ctrl-shift-6": ["workspace::MoveItemToPane", 5],
"ctrl-shift-7": ["workspace::MoveItemToPane", 6],
"ctrl-shift-8": ["workspace::MoveItemToPane", 7],
"ctrl-shift-9": ["workspace::MoveItemToPane", 8]
}
},
{
Expand All @@ -20,6 +38,8 @@
"cmd-shift-a": "editor::SelectLargerSyntaxNode",
"cmd-shift-d": "editor::DuplicateLineDown",
"ctrl-cmd-g": "editor::SelectAllMatches", // find_all_under
"f5": "editor::SortLinesCaseSensitive",
"ctrl-f5": "editor::SortLinesCaseInsensitive",
"shift-f12": "editor::FindAllReferences",
"alt-cmd-down": "editor::GoToDefinition",
"ctrl-alt-cmd-down": "editor::GoToDefinitionSplit",
Expand Down
42 changes: 38 additions & 4 deletions crates/terminal_view/src/terminal_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ use workspace::{
move_item, pane,
ui::IconName,
ActivateNextPane, ActivatePane, ActivatePaneInDirection, ActivatePreviousPane, DraggedTab,
ItemId, NewTerminal, Pane, PaneGroup, SplitDirection, SplitDown, SplitLeft, SplitRight,
SplitUp, SwapPaneInDirection, ToggleZoom, Workspace,
ItemId, MoveItemToPane, MoveItemToPaneInDirection, NewTerminal, Pane, PaneGroup,
SplitDirection, SplitDown, SplitLeft, SplitRight, SplitUp, SwapPaneInDirection, ToggleZoom,
Workspace,
};

use anyhow::Result;
Expand Down Expand Up @@ -1124,8 +1125,8 @@ impl Render for TerminalPanel {
.detach();
}
}))
.on_action(cx.listener(
|terminal_panel, action: &SwapPaneInDirection, cx| {
.on_action(
cx.listener(|terminal_panel, action: &SwapPaneInDirection, cx| {
SomeoneToIgnore marked this conversation as resolved.
Show resolved Hide resolved
if let Some(to) = terminal_panel
.center
.find_pane_in_direction(&terminal_panel.active_pane, action.0, cx)
Expand All @@ -1136,6 +1137,39 @@ impl Render for TerminalPanel {
.swap(&terminal_panel.active_pane.clone(), &to);
cx.notify();
}
}),
)
.on_action(cx.listener(|terminal_panel, action: &MoveItemToPane, cx| {
let panes = terminal_panel.center.panes();
let Some(target_pane) = panes.get(action.0).map(|p| (*p).clone()) else {
return;
};
let source_pane = terminal_panel.active_pane.clone();
terminal_panel.center.move_active_item(
&source_pane,
&target_pane,
true,
true,
cx,
);
}))
.on_action(cx.listener(
|terminal_panel, action: &MoveItemToPaneInDirection, cx| {
let source_pane = terminal_panel.active_pane.clone();
let Some(target_pane) = terminal_panel
.center
.find_pane_in_direction(&source_pane, action.0, cx)
.cloned()
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
.cloned()
.cloned()

NIT: The other part does not need this, so we can also do it in a similar way?

else {
return;
};
terminal_panel.center.move_active_item(
&source_pane,
&target_pane,
true,
true,
cx,
);
},
))
})
Expand Down
29 changes: 29 additions & 0 deletions crates/workspace/src/pane_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,35 @@ impl PaneGroup {
};
}

pub fn move_active_item(
Copy link
Contributor

Choose a reason for hiding this comment

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

Looking at the code better, it looks like we have

pub fn move_item(
source: &View<Pane>,
destination: &View<Pane>,
item_id_to_move: EntityId,
destination_index: usize,
cx: &mut WindowContext<'_>,
) {
let Some((item_ix, item_handle)) = source
.read(cx)
.items()
.enumerate()
.find(|(_, item_handle)| item_handle.item_id() == item_id_to_move)
.map(|(ix, item)| (ix, item.clone()))
else {
// Tab was closed during drag
return;
};
if source != destination {
// Close item from previous pane
source.update(cx, |source, cx| {
source.remove_item_and_focus_on_pane(item_ix, false, destination.clone(), cx);
});
}
// This automatically removes duplicate items in the pane
destination.update(cx, |destination, cx| {
destination.add_item(item_handle, true, true, Some(destination_index), cx);
destination.focus(cx)
});
}

which does almost the same thing but also focuses the right pane?

Let's unity them (by removing this method and moving the existing one here; optionally we can also replace remove_item_and_focus_on_pane noop wrapper with remove_item).

I think, that focusing the last pane is important, and might solve the observed lag issues.

&self,
from: &View<Pane>,
to: &View<Pane>,
focus_target: bool,
close_if_empty: bool,
cx: &mut WindowContext,
) {
if from == to {
return;
}
let Some(active_item) = from.read(cx).active_item() else {
return;
};
from.update(cx, |source_pane, cx| {
let item_id = active_item.item_id();
source_pane.remove_item(item_id, false, close_if_empty, cx);
to.update(cx, |target_pane, cx| {
target_pane.add_item(
active_item,
focus_target,
focus_target,
Some(target_pane.items_len()),
cx,
);
});
});
}

#[allow(clippy::too_many_arguments)]
pub fn render(
&self,
Expand Down
36 changes: 36 additions & 0 deletions crates/workspace/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ pub struct ActivatePaneInDirection(pub SplitDirection);
#[derive(Clone, Deserialize, PartialEq)]
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 @@ -226,6 +232,8 @@ impl_actions!(
ActivatePaneInDirection,
CloseAllItemsAndPanes,
CloseInactiveTabsAndPanes,
MoveItemToPane,
MoveItemToPaneInDirection,
OpenTerminal,
Reload,
Save,
Expand Down Expand Up @@ -2832,6 +2840,16 @@ impl Workspace {
}
}

fn move_item_to_pane_at_index(&mut self, action: &MoveItemToPane, cx: &mut ViewContext<Self>) {
let panes = self.center.panes();
let source_pane = self.active_pane.clone();
let Some(target_pane) = panes.get(action.0).map(|p| (*p).clone()) else {
return;
};
self.center
.move_active_item(&source_pane, &target_pane, true, true, cx);
}

pub fn activate_next_pane(&mut self, cx: &mut WindowContext) {
let panes = self.center.panes();
if let Some(ix) = panes.iter().position(|pane| **pane == self.active_pane) {
Expand Down Expand Up @@ -2950,6 +2968,18 @@ 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();
self.center
.move_active_item(&source_pane, &target_pane, true, true, 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 @@ -4411,6 +4441,7 @@ impl Workspace {
.on_action(cx.listener(Self::follow_next_collaborator))
.on_action(cx.listener(Self::close_window))
.on_action(cx.listener(Self::activate_pane_at_index))
.on_action(cx.listener(Self::move_item_to_pane_at_index))
.on_action(cx.listener(|workspace, _: &Unfollow, cx| {
let pane = workspace.active_pane().clone();
workspace.unfollow_in_pane(&pane, cx);
Expand Down Expand Up @@ -4441,6 +4472,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