Merge pull request #2361 from zed-industries/add-close-items-to-left-and-right

Add close items to left and right
This commit is contained in:
Joseph T. Lyons 2023-04-06 13:06:25 -04:00 committed by GitHub
commit dd00966cc6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -46,6 +46,8 @@ actions!(
CloseActiveItem, CloseActiveItem,
CloseInactiveItems, CloseInactiveItems,
CloseCleanItems, CloseCleanItems,
CloseItemsToTheLeft,
CloseItemsToTheRight,
CloseAllItems, CloseAllItems,
ReopenClosedItem, ReopenClosedItem,
SplitLeft, SplitLeft,
@ -122,6 +124,8 @@ pub fn init(cx: &mut MutableAppContext) {
cx.add_async_action(Pane::close_active_item); cx.add_async_action(Pane::close_active_item);
cx.add_async_action(Pane::close_inactive_items); cx.add_async_action(Pane::close_inactive_items);
cx.add_async_action(Pane::close_clean_items); cx.add_async_action(Pane::close_clean_items);
cx.add_async_action(Pane::close_items_to_the_left);
cx.add_async_action(Pane::close_items_to_the_right);
cx.add_async_action(Pane::close_all_items); cx.add_async_action(Pane::close_all_items);
cx.add_async_action(|workspace: &mut Workspace, action: &CloseItem, cx| { cx.add_async_action(|workspace: &mut Workspace, action: &CloseItem, cx| {
let pane = action.pane.upgrade(cx)?; let pane = action.pane.upgrade(cx)?;
@ -261,13 +265,6 @@ pub enum ReorderBehavior {
MoveToIndex(usize), MoveToIndex(usize),
} }
enum ItemType {
Active,
Inactive,
Clean,
All,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TabBarContextMenuKind { enum TabBarContextMenuKind {
New, New,
@ -642,7 +639,7 @@ impl Pane {
self.items.len() self.items.len()
} }
pub fn items(&self) -> impl Iterator<Item = &Box<dyn ItemHandle>> { pub fn items(&self) -> impl Iterator<Item = &Box<dyn ItemHandle>> + DoubleEndedIterator {
self.items.iter() self.items.iter()
} }
@ -741,7 +738,18 @@ impl Pane {
_: &CloseActiveItem, _: &CloseActiveItem,
cx: &mut ViewContext<Workspace>, cx: &mut ViewContext<Workspace>,
) -> Option<Task<Result<()>>> { ) -> Option<Task<Result<()>>> {
Self::close_main(workspace, ItemType::Active, cx) let pane_handle = workspace.active_pane().clone();
let pane = pane_handle.read(cx);
let active_item_id = pane.items[pane.active_item_index].id();
let task = Self::close_items(workspace, pane_handle, cx, move |item_id| {
item_id == active_item_id
});
Some(cx.foreground().spawn(async move {
task.await?;
Ok(())
}))
} }
pub fn close_inactive_items( pub fn close_inactive_items(
@ -749,54 +757,103 @@ impl Pane {
_: &CloseInactiveItems, _: &CloseInactiveItems,
cx: &mut ViewContext<Workspace>, cx: &mut ViewContext<Workspace>,
) -> Option<Task<Result<()>>> { ) -> Option<Task<Result<()>>> {
Self::close_main(workspace, ItemType::Inactive, cx) let pane_handle = workspace.active_pane().clone();
} let pane = pane_handle.read(cx);
let active_item_id = pane.items[pane.active_item_index].id();
pub fn close_all_items( let task = Self::close_items(workspace, pane_handle, cx, move |item_id| {
workspace: &mut Workspace, item_id != active_item_id
_: &CloseAllItems, });
cx: &mut ViewContext<Workspace>,
) -> Option<Task<Result<()>>> { Some(cx.foreground().spawn(async move {
Self::close_main(workspace, ItemType::All, cx) task.await?;
Ok(())
}))
} }
pub fn close_clean_items( pub fn close_clean_items(
workspace: &mut Workspace, workspace: &mut Workspace,
_: &CloseCleanItems, _: &CloseCleanItems,
cx: &mut ViewContext<Workspace>, cx: &mut ViewContext<Workspace>,
) -> Option<Task<Result<()>>> {
Self::close_main(workspace, ItemType::Clean, cx)
}
fn close_main(
workspace: &mut Workspace,
close_item_type: ItemType,
cx: &mut ViewContext<Workspace>,
) -> Option<Task<Result<()>>> { ) -> Option<Task<Result<()>>> {
let pane_handle = workspace.active_pane().clone(); let pane_handle = workspace.active_pane().clone();
let pane = pane_handle.read(cx); let pane = pane_handle.read(cx);
if pane.items.is_empty() {
return None;
}
let active_item_id = pane.items[pane.active_item_index].id(); let item_ids: Vec<_> = pane
let clean_item_ids: Vec<_> = pane
.items() .items()
.filter(|item| !item.is_dirty(cx)) .filter(|item| !item.is_dirty(cx))
.map(|item| item.id()) .map(|item| item.id())
.collect(); .collect();
let task =
Self::close_items( let task = Self::close_items(workspace, pane_handle, cx, move |item_id| {
workspace, item_ids.contains(&item_id)
pane_handle, });
cx,
move |item_id| match close_item_type { Some(cx.foreground().spawn(async move {
ItemType::Active => item_id == active_item_id, task.await?;
ItemType::Inactive => item_id != active_item_id, Ok(())
ItemType::Clean => clean_item_ids.contains(&item_id), }))
ItemType::All => true, }
},
); pub fn close_items_to_the_left(
workspace: &mut Workspace,
_: &CloseItemsToTheLeft,
cx: &mut ViewContext<Workspace>,
) -> Option<Task<Result<()>>> {
let pane_handle = workspace.active_pane().clone();
let pane = pane_handle.read(cx);
let active_item_id = pane.items[pane.active_item_index].id();
let item_ids: Vec<_> = pane
.items()
.take_while(|item| item.id() != active_item_id)
.map(|item| item.id())
.collect();
let task = Self::close_items(workspace, pane_handle, cx, move |item_id| {
item_ids.contains(&item_id)
});
Some(cx.foreground().spawn(async move {
task.await?;
Ok(())
}))
}
pub fn close_items_to_the_right(
workspace: &mut Workspace,
_: &CloseItemsToTheRight,
cx: &mut ViewContext<Workspace>,
) -> Option<Task<Result<()>>> {
let pane_handle = workspace.active_pane().clone();
let pane = pane_handle.read(cx);
let active_item_id = pane.items[pane.active_item_index].id();
let item_ids: Vec<_> = pane
.items()
.rev()
.take_while(|item| item.id() != active_item_id)
.map(|item| item.id())
.collect();
let task = Self::close_items(workspace, pane_handle, cx, move |item_id| {
item_ids.contains(&item_id)
});
Some(cx.foreground().spawn(async move {
task.await?;
Ok(())
}))
}
pub fn close_all_items(
workspace: &mut Workspace,
_: &CloseAllItems,
cx: &mut ViewContext<Workspace>,
) -> Option<Task<Result<()>>> {
let pane_handle = workspace.active_pane().clone();
let task = Self::close_items(workspace, pane_handle, cx, move |_| true);
Some(cx.foreground().spawn(async move { Some(cx.foreground().spawn(async move {
task.await?; task.await?;