Fix save related tests, and refactor saves again

This commit is contained in:
Conrad Irwin 2023-09-20 20:13:52 -06:00
parent a59da3634b
commit 7a7ff4bb96
5 changed files with 156 additions and 163 deletions

View File

@ -2,7 +2,7 @@ use command_palette::CommandInterceptResult;
use editor::{SortLinesCaseInsensitive, SortLinesCaseSensitive}; use editor::{SortLinesCaseInsensitive, SortLinesCaseSensitive};
use gpui::{impl_actions, Action, AppContext}; use gpui::{impl_actions, Action, AppContext};
use serde_derive::Deserialize; use serde_derive::Deserialize;
use workspace::{SaveBehavior, Workspace}; use workspace::{SaveIntent, Workspace};
use crate::{ use crate::{
motion::{EndOfDocument, Motion}, motion::{EndOfDocument, Motion},
@ -50,112 +50,119 @@ pub fn command_interceptor(mut query: &str, _: &AppContext) -> Option<CommandInt
"w" | "wr" | "wri" | "writ" | "write" => ( "w" | "wr" | "wri" | "writ" | "write" => (
"write", "write",
workspace::Save { workspace::Save {
save_behavior: Some(SaveBehavior::PromptOnConflict), save_behavior: Some(SaveIntent::Save),
} }
.boxed_clone(), .boxed_clone(),
), ),
"w!" | "wr!" | "wri!" | "writ!" | "write!" => ( "w!" | "wr!" | "wri!" | "writ!" | "write!" => (
"write!", "write!",
workspace::Save { workspace::Save {
save_behavior: Some(SaveBehavior::SilentlyOverwrite), save_behavior: Some(SaveIntent::Overwrite),
} }
.boxed_clone(), .boxed_clone(),
), ),
"q" | "qu" | "qui" | "quit" => ( "q" | "qu" | "qui" | "quit" => (
"quit", "quit",
workspace::CloseActiveItem { workspace::CloseActiveItem {
save_behavior: Some(SaveBehavior::PromptOnWrite), save_behavior: Some(SaveIntent::Close),
} }
.boxed_clone(), .boxed_clone(),
), ),
"q!" | "qu!" | "qui!" | "quit!" => ( "q!" | "qu!" | "qui!" | "quit!" => (
"quit!", "quit!",
workspace::CloseActiveItem { workspace::CloseActiveItem {
save_behavior: Some(SaveBehavior::DontSave), save_behavior: Some(SaveIntent::Skip),
} }
.boxed_clone(), .boxed_clone(),
), ),
"wq" => ( "wq" => (
"wq", "wq",
workspace::CloseActiveItem { workspace::CloseActiveItem {
save_behavior: Some(SaveBehavior::PromptOnConflict), save_behavior: Some(SaveIntent::Save),
} }
.boxed_clone(), .boxed_clone(),
), ),
"wq!" => ( "wq!" => (
"wq!", "wq!",
workspace::CloseActiveItem { workspace::CloseActiveItem {
save_behavior: Some(SaveBehavior::SilentlyOverwrite), save_behavior: Some(SaveIntent::Overwrite),
} }
.boxed_clone(), .boxed_clone(),
), ),
"x" | "xi" | "xit" | "exi" | "exit" => ( "x" | "xi" | "xit" | "exi" | "exit" => (
"exit", "exit",
workspace::CloseActiveItem { workspace::CloseActiveItem {
save_behavior: Some(SaveBehavior::PromptOnConflict), save_behavior: Some(SaveIntent::Save),
} }
.boxed_clone(), .boxed_clone(),
), ),
"x!" | "xi!" | "xit!" | "exi!" | "exit!" => ( "x!" | "xi!" | "xit!" | "exi!" | "exit!" => (
"exit!", "exit!",
workspace::CloseActiveItem { workspace::CloseActiveItem {
save_behavior: Some(SaveBehavior::SilentlyOverwrite), save_behavior: Some(SaveIntent::Overwrite),
}
.boxed_clone(),
),
"up" | "upd" | "upda" | "updat" | "update" => (
"update",
workspace::Save {
save_behavior: Some(SaveIntent::SaveAll),
} }
.boxed_clone(), .boxed_clone(),
), ),
"wa" | "wal" | "wall" => ( "wa" | "wal" | "wall" => (
"wall", "wall",
workspace::SaveAll { workspace::SaveAll {
save_behavior: Some(SaveBehavior::PromptOnConflict), save_behavior: Some(SaveIntent::SaveAll),
} }
.boxed_clone(), .boxed_clone(),
), ),
"wa!" | "wal!" | "wall!" => ( "wa!" | "wal!" | "wall!" => (
"wall!", "wall!",
workspace::SaveAll { workspace::SaveAll {
save_behavior: Some(SaveBehavior::SilentlyOverwrite), save_behavior: Some(SaveIntent::Overwrite),
} }
.boxed_clone(), .boxed_clone(),
), ),
"qa" | "qal" | "qall" | "quita" | "quital" | "quitall" => ( "qa" | "qal" | "qall" | "quita" | "quital" | "quitall" => (
"quitall", "quitall",
workspace::CloseAllItemsAndPanes { workspace::CloseAllItemsAndPanes {
save_behavior: Some(SaveBehavior::PromptOnWrite), save_behavior: Some(SaveIntent::Close),
} }
.boxed_clone(), .boxed_clone(),
), ),
"qa!" | "qal!" | "qall!" | "quita!" | "quital!" | "quitall!" => ( "qa!" | "qal!" | "qall!" | "quita!" | "quital!" | "quitall!" => (
"quitall!", "quitall!",
workspace::CloseAllItemsAndPanes { workspace::CloseAllItemsAndPanes {
save_behavior: Some(SaveBehavior::DontSave), save_behavior: Some(SaveIntent::Skip),
} }
.boxed_clone(), .boxed_clone(),
), ),
"xa" | "xal" | "xall" => ( "xa" | "xal" | "xall" => (
"xall", "xall",
workspace::CloseAllItemsAndPanes { workspace::CloseAllItemsAndPanes {
save_behavior: Some(SaveBehavior::PromptOnConflict), save_behavior: Some(SaveIntent::SaveAll),
} }
.boxed_clone(), .boxed_clone(),
), ),
"xa!" | "xal!" | "xall!" => ( "xa!" | "xal!" | "xall!" => (
"xall!", "xall!",
workspace::CloseAllItemsAndPanes { workspace::CloseAllItemsAndPanes {
save_behavior: Some(SaveBehavior::SilentlyOverwrite), save_behavior: Some(SaveIntent::Overwrite),
} }
.boxed_clone(), .boxed_clone(),
), ),
"wqa" | "wqal" | "wqall" => ( "wqa" | "wqal" | "wqall" => (
"wqall", "wqall",
workspace::CloseAllItemsAndPanes { workspace::CloseAllItemsAndPanes {
save_behavior: Some(SaveBehavior::PromptOnConflict), save_behavior: Some(SaveIntent::SaveAll),
} }
.boxed_clone(), .boxed_clone(),
), ),
"wqa!" | "wqal!" | "wqall!" => ( "wqa!" | "wqal!" | "wqall!" => (
"wqall!", "wqall!",
workspace::CloseAllItemsAndPanes { workspace::CloseAllItemsAndPanes {
save_behavior: Some(SaveBehavior::SilentlyOverwrite), save_behavior: Some(SaveIntent::Overwrite),
} }
.boxed_clone(), .boxed_clone(),
), ),
@ -190,7 +197,7 @@ pub fn command_interceptor(mut query: &str, _: &AppContext) -> Option<CommandInt
"tabc" | "tabcl" | "tabclo" | "tabclos" | "tabclose" => ( "tabc" | "tabcl" | "tabclo" | "tabclos" | "tabclose" => (
"tabclose", "tabclose",
workspace::CloseActiveItem { workspace::CloseActiveItem {
save_behavior: Some(SaveBehavior::PromptOnWrite), save_behavior: Some(SaveIntent::Close),
} }
.boxed_clone(), .boxed_clone(),
), ),

View File

@ -475,11 +475,7 @@ impl<T: Item> ItemHandle for ViewHandle<T> {
match item_event { match item_event {
ItemEvent::CloseItem => { ItemEvent::CloseItem => {
pane.update(cx, |pane, cx| { pane.update(cx, |pane, cx| {
pane.close_item_by_id( pane.close_item_by_id(item.id(), crate::SaveIntent::Close, cx)
item.id(),
crate::SaveBehavior::PromptOnWrite,
cx,
)
}) })
.detach_and_log_err(cx); .detach_and_log_err(cx);
return; return;

View File

@ -45,17 +45,21 @@ use theme::{Theme, ThemeSettings};
#[derive(PartialEq, Clone, Copy, Deserialize, Debug)] #[derive(PartialEq, Clone, Copy, Deserialize, Debug)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub enum SaveBehavior { pub enum SaveIntent {
/// ask before overwriting conflicting files (used by default with cmd-s) /// write all files (even if unchanged)
PromptOnConflict, /// prompt before overwriting on-disk changes
/// ask for a new path before writing (used with cmd-shift-s) Save,
PromptForNewPath, /// write any files that have local changes
/// ask before writing any file that wouldn't be auto-saved (used by default with cmd-w) /// prompt before overwriting on-disk changes
PromptOnWrite, SaveAll,
/// never prompt, write on conflict (used with vim's :w!) /// always prompt for a new path
SilentlyOverwrite, SaveAs,
/// skip all save-related behaviour (used with vim's :q!) /// prompt "you have unsaved changes" before writing
DontSave, Close,
/// write all dirty files, don't prompt on conflict
Overwrite,
/// skip all save-related behavior
Skip,
} }
#[derive(Clone, Deserialize, PartialEq)] #[derive(Clone, Deserialize, PartialEq)]
@ -82,13 +86,13 @@ pub struct CloseItemsToTheRightById {
#[derive(Clone, PartialEq, Debug, Deserialize, Default)] #[derive(Clone, PartialEq, Debug, Deserialize, Default)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct CloseActiveItem { pub struct CloseActiveItem {
pub save_behavior: Option<SaveBehavior>, pub save_behavior: Option<SaveIntent>,
} }
#[derive(Clone, PartialEq, Debug, Deserialize)] #[derive(Clone, PartialEq, Debug, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct CloseAllItems { pub struct CloseAllItems {
pub save_behavior: Option<SaveBehavior>, pub save_behavior: Option<SaveIntent>,
} }
actions!( actions!(
@ -730,7 +734,7 @@ impl Pane {
let active_item_id = self.items[self.active_item_index].id(); let active_item_id = self.items[self.active_item_index].id();
Some(self.close_item_by_id( Some(self.close_item_by_id(
active_item_id, active_item_id,
action.save_behavior.unwrap_or(SaveBehavior::PromptOnWrite), action.save_behavior.unwrap_or(SaveIntent::Close),
cx, cx,
)) ))
} }
@ -738,7 +742,7 @@ impl Pane {
pub fn close_item_by_id( pub fn close_item_by_id(
&mut self, &mut self,
item_id_to_close: usize, item_id_to_close: usize,
save_behavior: SaveBehavior, save_behavior: SaveIntent,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Task<Result<()>> { ) -> Task<Result<()>> {
self.close_items(cx, save_behavior, move |view_id| { self.close_items(cx, save_behavior, move |view_id| {
@ -756,11 +760,9 @@ impl Pane {
} }
let active_item_id = self.items[self.active_item_index].id(); let active_item_id = self.items[self.active_item_index].id();
Some( Some(self.close_items(cx, SaveIntent::Close, move |item_id| {
self.close_items(cx, SaveBehavior::PromptOnWrite, move |item_id| { item_id != active_item_id
item_id != active_item_id }))
}),
)
} }
pub fn close_clean_items( pub fn close_clean_items(
@ -773,11 +775,9 @@ impl Pane {
.filter(|item| !item.is_dirty(cx)) .filter(|item| !item.is_dirty(cx))
.map(|item| item.id()) .map(|item| item.id())
.collect(); .collect();
Some( Some(self.close_items(cx, SaveIntent::Close, move |item_id| {
self.close_items(cx, SaveBehavior::PromptOnWrite, move |item_id| { item_ids.contains(&item_id)
item_ids.contains(&item_id) }))
}),
)
} }
pub fn close_items_to_the_left( pub fn close_items_to_the_left(
@ -802,7 +802,7 @@ impl Pane {
.take_while(|item| item.id() != item_id) .take_while(|item| item.id() != item_id)
.map(|item| item.id()) .map(|item| item.id())
.collect(); .collect();
self.close_items(cx, SaveBehavior::PromptOnWrite, move |item_id| { self.close_items(cx, SaveIntent::Close, move |item_id| {
item_ids.contains(&item_id) item_ids.contains(&item_id)
}) })
} }
@ -830,7 +830,7 @@ impl Pane {
.take_while(|item| item.id() != item_id) .take_while(|item| item.id() != item_id)
.map(|item| item.id()) .map(|item| item.id())
.collect(); .collect();
self.close_items(cx, SaveBehavior::PromptOnWrite, move |item_id| { self.close_items(cx, SaveIntent::Close, move |item_id| {
item_ids.contains(&item_id) item_ids.contains(&item_id)
}) })
} }
@ -846,7 +846,7 @@ impl Pane {
Some(self.close_items( Some(self.close_items(
cx, cx,
action.save_behavior.unwrap_or(SaveBehavior::PromptOnWrite), action.save_behavior.unwrap_or(SaveIntent::Close),
|_| true, |_| true,
)) ))
} }
@ -854,7 +854,7 @@ impl Pane {
pub fn close_items( pub fn close_items(
&mut self, &mut self,
cx: &mut ViewContext<Pane>, cx: &mut ViewContext<Pane>,
save_behavior: SaveBehavior, save_behavior: SaveIntent,
should_close: impl 'static + Fn(usize) -> bool, should_close: impl 'static + Fn(usize) -> bool,
) -> Task<Result<()>> { ) -> Task<Result<()>> {
// Find the items to close. // Find the items to close.
@ -1010,18 +1010,18 @@ impl Pane {
pane: &WeakViewHandle<Pane>, pane: &WeakViewHandle<Pane>,
item_ix: usize, item_ix: usize,
item: &dyn ItemHandle, item: &dyn ItemHandle,
save_behavior: SaveBehavior, save_behavior: SaveIntent,
cx: &mut AsyncAppContext, cx: &mut AsyncAppContext,
) -> Result<bool> { ) -> Result<bool> {
const CONFLICT_MESSAGE: &str = const CONFLICT_MESSAGE: &str =
"This file has changed on disk since you started editing it. Do you want to overwrite it?"; "This file has changed on disk since you started editing it. Do you want to overwrite it?";
const DIRTY_MESSAGE: &str = "This file contains unsaved edits. Do you want to save it?"; const DIRTY_MESSAGE: &str = "This file contains unsaved edits. Do you want to save it?";
if save_behavior == SaveBehavior::DontSave { if save_behavior == SaveIntent::Skip {
return Ok(true); return Ok(true);
} }
let (mut has_conflict, mut is_dirty, mut can_save, is_singleton) = cx.read(|cx| { let (mut has_conflict, mut is_dirty, mut can_save, can_save_as) = cx.read(|cx| {
( (
item.has_conflict(cx), item.has_conflict(cx),
item.is_dirty(cx), item.is_dirty(cx),
@ -1030,73 +1030,76 @@ impl Pane {
) )
}); });
if save_behavior == SaveBehavior::PromptForNewPath { // when saving a single buffer, we ignore whether or not it's dirty.
has_conflict = false; if save_behavior == SaveIntent::Save {
is_dirty = true; is_dirty = true;
}
if save_behavior == SaveIntent::SaveAs {
is_dirty = true;
has_conflict = false;
can_save = false; can_save = false;
} }
if save_behavior == SaveIntent::Overwrite {
has_conflict = false;
}
if has_conflict && can_save { if has_conflict && can_save {
if save_behavior == SaveBehavior::SilentlyOverwrite { let mut answer = pane.update(cx, |pane, cx| {
pane.update(cx, |_, cx| item.save(project, cx))?.await?; pane.activate_item(item_ix, true, true, cx);
} else { cx.prompt(
let mut answer = pane.update(cx, |pane, cx| { PromptLevel::Warning,
pane.activate_item(item_ix, true, true, cx); CONFLICT_MESSAGE,
cx.prompt( &["Overwrite", "Discard", "Cancel"],
PromptLevel::Warning, )
CONFLICT_MESSAGE, })?;
&["Overwrite", "Discard", "Cancel"], match answer.next().await {
) Some(0) => pane.update(cx, |_, cx| item.save(project, cx))?.await?,
})?; Some(1) => pane.update(cx, |_, cx| item.reload(project, cx))?.await?,
match answer.next().await { _ => return Ok(false),
Some(0) => pane.update(cx, |_, cx| item.save(project, cx))?.await?, }
Some(1) => pane.update(cx, |_, cx| item.reload(project, cx))?.await?, } else if is_dirty && (can_save || can_save_as) {
_ => return Ok(false), if save_behavior == SaveIntent::Close {
let will_autosave = cx.read(|cx| {
matches!(
settings::get::<WorkspaceSettings>(cx).autosave,
AutosaveSetting::OnFocusChange | AutosaveSetting::OnWindowChange
) && Self::can_autosave_item(&*item, cx)
});
if !will_autosave {
let mut answer = pane.update(cx, |pane, cx| {
pane.activate_item(item_ix, true, true, cx);
cx.prompt(
PromptLevel::Warning,
DIRTY_MESSAGE,
&["Save", "Don't Save", "Cancel"],
)
})?;
match answer.next().await {
Some(0) => {}
Some(1) => return Ok(true), // Don't save this file
_ => return Ok(false), // Cancel
}
} }
} }
} else if is_dirty && (can_save || is_singleton) {
let will_autosave = cx.read(|cx| {
matches!(
settings::get::<WorkspaceSettings>(cx).autosave,
AutosaveSetting::OnFocusChange | AutosaveSetting::OnWindowChange
) && Self::can_autosave_item(&*item, cx)
});
let should_save = if save_behavior == SaveBehavior::PromptOnWrite && !will_autosave {
let mut answer = pane.update(cx, |pane, cx| {
pane.activate_item(item_ix, true, true, cx);
cx.prompt(
PromptLevel::Warning,
DIRTY_MESSAGE,
&["Save", "Don't Save", "Cancel"],
)
})?;
match answer.next().await {
Some(0) => true,
Some(1) => false,
_ => return Ok(false),
}
} else {
true
};
if should_save { if can_save {
if can_save { pane.update(cx, |_, cx| item.save(project, cx))?.await?;
pane.update(cx, |_, cx| item.save(project, cx))?.await?; } else if can_save_as {
} else if is_singleton { let start_abs_path = project
let start_abs_path = project .read_with(cx, |project, cx| {
.read_with(cx, |project, cx| { let worktree = project.visible_worktrees(cx).next()?;
let worktree = project.visible_worktrees(cx).next()?; Some(worktree.read(cx).as_local()?.abs_path().to_path_buf())
Some(worktree.read(cx).as_local()?.abs_path().to_path_buf()) })
}) .unwrap_or_else(|| Path::new("").into());
.unwrap_or_else(|| Path::new("").into());
let mut abs_path = cx.update(|cx| cx.prompt_for_new_path(&start_abs_path)); let mut abs_path = cx.update(|cx| cx.prompt_for_new_path(&start_abs_path));
if let Some(abs_path) = abs_path.next().await.flatten() { if let Some(abs_path) = abs_path.next().await.flatten() {
pane.update(cx, |_, cx| item.save_as(project, abs_path, cx))? pane.update(cx, |_, cx| item.save_as(project, abs_path, cx))?
.await?; .await?;
} else { } else {
return Ok(false); return Ok(false);
}
} }
} }
} }
@ -1210,7 +1213,7 @@ impl Pane {
pane.update(cx, |pane, cx| { pane.update(cx, |pane, cx| {
pane.close_item_by_id( pane.close_item_by_id(
target_item_id, target_item_id,
SaveBehavior::PromptOnWrite, SaveIntent::Close,
cx, cx,
) )
.detach_and_log_err(cx); .detach_and_log_err(cx);
@ -1367,12 +1370,8 @@ impl Pane {
.on_click(MouseButton::Middle, { .on_click(MouseButton::Middle, {
let item_id = item.id(); let item_id = item.id();
move |_, pane, cx| { move |_, pane, cx| {
pane.close_item_by_id( pane.close_item_by_id(item_id, SaveIntent::Close, cx)
item_id, .detach_and_log_err(cx);
SaveBehavior::PromptOnWrite,
cx,
)
.detach_and_log_err(cx);
} }
}) })
.on_down( .on_down(
@ -1580,7 +1579,7 @@ impl Pane {
cx.window_context().defer(move |cx| { cx.window_context().defer(move |cx| {
if let Some(pane) = pane.upgrade(cx) { if let Some(pane) = pane.upgrade(cx) {
pane.update(cx, |pane, cx| { pane.update(cx, |pane, cx| {
pane.close_item_by_id(item_id, SaveBehavior::PromptOnWrite, cx) pane.close_item_by_id(item_id, SaveIntent::Close, cx)
.detach_and_log_err(cx); .detach_and_log_err(cx);
}); });
} }

View File

@ -163,19 +163,19 @@ pub struct NewFileInDirection(pub SplitDirection);
#[derive(Clone, PartialEq, Debug, Deserialize)] #[derive(Clone, PartialEq, Debug, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct SaveAll { pub struct SaveAll {
pub save_behavior: Option<SaveBehavior>, pub save_behavior: Option<SaveIntent>,
} }
#[derive(Clone, PartialEq, Debug, Deserialize)] #[derive(Clone, PartialEq, Debug, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct Save { pub struct Save {
pub save_behavior: Option<SaveBehavior>, pub save_behavior: Option<SaveIntent>,
} }
#[derive(Clone, PartialEq, Debug, Deserialize, Default)] #[derive(Clone, PartialEq, Debug, Deserialize, Default)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct CloseAllItemsAndPanes { pub struct CloseAllItemsAndPanes {
pub save_behavior: Option<SaveBehavior>, pub save_behavior: Option<SaveIntent>,
} }
#[derive(Deserialize)] #[derive(Deserialize)]
@ -294,19 +294,14 @@ pub fn init(app_state: Arc<AppState>, cx: &mut AppContext) {
cx.add_action( cx.add_action(
|workspace: &mut Workspace, action: &Save, cx: &mut ViewContext<Workspace>| { |workspace: &mut Workspace, action: &Save, cx: &mut ViewContext<Workspace>| {
workspace workspace
.save_active_item( .save_active_item(action.save_behavior.unwrap_or(SaveIntent::Save), cx)
action
.save_behavior
.unwrap_or(SaveBehavior::PromptOnConflict),
cx,
)
.detach_and_log_err(cx); .detach_and_log_err(cx);
}, },
); );
cx.add_action( cx.add_action(
|workspace: &mut Workspace, _: &SaveAs, cx: &mut ViewContext<Workspace>| { |workspace: &mut Workspace, _: &SaveAs, cx: &mut ViewContext<Workspace>| {
workspace workspace
.save_active_item(SaveBehavior::PromptForNewPath, cx) .save_active_item(SaveIntent::SaveAs, cx)
.detach_and_log_err(cx); .detach_and_log_err(cx);
}, },
); );
@ -1356,7 +1351,7 @@ impl Workspace {
Ok(this Ok(this
.update(&mut cx, |this, cx| { .update(&mut cx, |this, cx| {
this.save_all_internal(SaveBehavior::PromptOnWrite, cx) this.save_all_internal(SaveIntent::Close, cx)
})? })?
.await?) .await?)
}) })
@ -1367,12 +1362,8 @@ impl Workspace {
action: &SaveAll, action: &SaveAll,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Option<Task<Result<()>>> { ) -> Option<Task<Result<()>>> {
let save_all = self.save_all_internal( let save_all =
action self.save_all_internal(action.save_behavior.unwrap_or(SaveIntent::SaveAll), cx);
.save_behavior
.unwrap_or(SaveBehavior::PromptOnConflict),
cx,
);
Some(cx.foreground().spawn(async move { Some(cx.foreground().spawn(async move {
save_all.await?; save_all.await?;
Ok(()) Ok(())
@ -1381,7 +1372,7 @@ impl Workspace {
fn save_all_internal( fn save_all_internal(
&mut self, &mut self,
save_behaviour: SaveBehavior, save_behaviour: SaveIntent,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Task<Result<bool>> { ) -> Task<Result<bool>> {
if self.project.read(cx).is_read_only() { if self.project.read(cx).is_read_only() {
@ -1688,7 +1679,7 @@ impl Workspace {
pub fn save_active_item( pub fn save_active_item(
&mut self, &mut self,
save_behavior: SaveBehavior, save_behavior: SaveIntent,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Task<Result<()>> { ) -> Task<Result<()>> {
let project = self.project.clone(); let project = self.project.clone();
@ -1720,7 +1711,7 @@ impl Workspace {
_: &CloseInactiveTabsAndPanes, _: &CloseInactiveTabsAndPanes,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Option<Task<Result<()>>> { ) -> Option<Task<Result<()>>> {
self.close_all_internal(true, SaveBehavior::PromptOnWrite, cx) self.close_all_internal(true, SaveIntent::Close, cx)
} }
pub fn close_all_items_and_panes( pub fn close_all_items_and_panes(
@ -1728,17 +1719,13 @@ impl Workspace {
action: &CloseAllItemsAndPanes, action: &CloseAllItemsAndPanes,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Option<Task<Result<()>>> { ) -> Option<Task<Result<()>>> {
self.close_all_internal( self.close_all_internal(false, action.save_behavior.unwrap_or(SaveIntent::Close), cx)
false,
action.save_behavior.unwrap_or(SaveBehavior::PromptOnWrite),
cx,
)
} }
fn close_all_internal( fn close_all_internal(
&mut self, &mut self,
retain_active_pane: bool, retain_active_pane: bool,
save_behavior: SaveBehavior, save_behavior: SaveIntent,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Option<Task<Result<()>>> { ) -> Option<Task<Result<()>>> {
let current_pane = self.active_pane(); let current_pane = self.active_pane();
@ -4433,7 +4420,7 @@ mod tests {
let item1_id = item1.id(); let item1_id = item1.id();
let item3_id = item3.id(); let item3_id = item3.id();
let item4_id = item4.id(); let item4_id = item4.id();
pane.close_items(cx, SaveBehavior::PromptOnWrite, move |id| { pane.close_items(cx, SaveIntent::Close, move |id| {
[item1_id, item3_id, item4_id].contains(&id) [item1_id, item3_id, item4_id].contains(&id)
}) })
}); });
@ -4571,7 +4558,7 @@ mod tests {
// prompts, the task should complete. // prompts, the task should complete.
let close = left_pane.update(cx, |pane, cx| { let close = left_pane.update(cx, |pane, cx| {
pane.close_items(cx, SaveBehavior::PromptOnWrite, move |_| true) pane.close_items(cx, SaveIntent::Close, move |_| true)
}); });
cx.foreground().run_until_parked(); cx.foreground().run_until_parked();
left_pane.read_with(cx, |pane, cx| { left_pane.read_with(cx, |pane, cx| {
@ -4689,7 +4676,7 @@ mod tests {
}); });
pane.update(cx, |pane, cx| { pane.update(cx, |pane, cx| {
pane.close_items(cx, SaveBehavior::PromptOnWrite, move |id| id == item_id) pane.close_items(cx, SaveIntent::Close, move |id| id == item_id)
}) })
.await .await
.unwrap(); .unwrap();
@ -4712,7 +4699,7 @@ mod tests {
// Ensure autosave is prevented for deleted files also when closing the buffer. // Ensure autosave is prevented for deleted files also when closing the buffer.
let _close_items = pane.update(cx, |pane, cx| { let _close_items = pane.update(cx, |pane, cx| {
pane.close_items(cx, SaveBehavior::PromptOnWrite, move |id| id == item_id) pane.close_items(cx, SaveIntent::Close, move |id| id == item_id)
}); });
deterministic.run_until_parked(); deterministic.run_until_parked();
assert!(window.has_pending_prompt(cx)); assert!(window.has_pending_prompt(cx));

View File

@ -744,7 +744,7 @@ mod tests {
use theme::{ThemeRegistry, ThemeSettings}; use theme::{ThemeRegistry, ThemeSettings};
use workspace::{ use workspace::{
item::{Item, ItemHandle}, item::{Item, ItemHandle},
open_new, open_paths, pane, NewFile, SaveBehavior, SplitDirection, WorkspaceHandle, open_new, open_paths, pane, NewFile, SaveIntent, SplitDirection, WorkspaceHandle,
}; };
#[gpui::test] #[gpui::test]
@ -945,12 +945,14 @@ mod tests {
editor.update(cx, |editor, cx| { editor.update(cx, |editor, cx| {
assert!(editor.text(cx).is_empty()); assert!(editor.text(cx).is_empty());
assert!(!editor.is_dirty(cx));
}); });
let save_task = workspace.update(cx, |workspace, cx| { let save_task = workspace.update(cx, |workspace, cx| {
workspace.save_active_item(SaveBehavior::PromptOnConflict, cx) workspace.save_active_item(SaveIntent::Save, cx)
}); });
app_state.fs.create_dir(Path::new("/root")).await.unwrap(); app_state.fs.create_dir(Path::new("/root")).await.unwrap();
cx.foreground().run_until_parked();
cx.simulate_new_path_selection(|_| Some(PathBuf::from("/root/the-new-name"))); cx.simulate_new_path_selection(|_| Some(PathBuf::from("/root/the-new-name")));
save_task.await.unwrap(); save_task.await.unwrap();
editor.read_with(cx, |editor, cx| { editor.read_with(cx, |editor, cx| {
@ -1314,7 +1316,7 @@ mod tests {
cx.read(|cx| assert!(editor.is_dirty(cx))); cx.read(|cx| assert!(editor.is_dirty(cx)));
let save_task = workspace.update(cx, |workspace, cx| { let save_task = workspace.update(cx, |workspace, cx| {
workspace.save_active_item(SaveBehavior::PromptOnConflict, cx) workspace.save_active_item(SaveIntent::Save, cx)
}); });
window.simulate_prompt_answer(0, cx); window.simulate_prompt_answer(0, cx);
save_task.await.unwrap(); save_task.await.unwrap();
@ -1358,8 +1360,9 @@ mod tests {
// Save the buffer. This prompts for a filename. // Save the buffer. This prompts for a filename.
let save_task = workspace.update(cx, |workspace, cx| { let save_task = workspace.update(cx, |workspace, cx| {
workspace.save_active_item(SaveBehavior::PromptOnConflict, cx) workspace.save_active_item(SaveIntent::Save, cx)
}); });
cx.foreground().run_until_parked();
cx.simulate_new_path_selection(|parent_dir| { cx.simulate_new_path_selection(|parent_dir| {
assert_eq!(parent_dir, Path::new("/root")); assert_eq!(parent_dir, Path::new("/root"));
Some(parent_dir.join("the-new-name.rs")) Some(parent_dir.join("the-new-name.rs"))
@ -1384,7 +1387,7 @@ mod tests {
assert!(editor.is_dirty(cx)); assert!(editor.is_dirty(cx));
}); });
let save_task = workspace.update(cx, |workspace, cx| { let save_task = workspace.update(cx, |workspace, cx| {
workspace.save_active_item(SaveBehavior::PromptOnConflict, cx) workspace.save_active_item(SaveIntent::Save, cx)
}); });
save_task.await.unwrap(); save_task.await.unwrap();
assert!(!cx.did_prompt_for_new_path()); assert!(!cx.did_prompt_for_new_path());
@ -1453,8 +1456,9 @@ mod tests {
// Save the buffer. This prompts for a filename. // Save the buffer. This prompts for a filename.
let save_task = workspace.update(cx, |workspace, cx| { let save_task = workspace.update(cx, |workspace, cx| {
workspace.save_active_item(SaveBehavior::PromptOnConflict, cx) workspace.save_active_item(SaveIntent::Save, cx)
}); });
cx.foreground().run_until_parked();
cx.simulate_new_path_selection(|_| Some(PathBuf::from("/root/the-new-name.rs"))); cx.simulate_new_path_selection(|_| Some(PathBuf::from("/root/the-new-name.rs")));
save_task.await.unwrap(); save_task.await.unwrap();
// The buffer is not dirty anymore and the language is assigned based on the path. // The buffer is not dirty anymore and the language is assigned based on the path.
@ -1692,7 +1696,7 @@ mod tests {
pane.update(cx, |pane, cx| { pane.update(cx, |pane, cx| {
let editor3_id = editor3.id(); let editor3_id = editor3.id();
drop(editor3); drop(editor3);
pane.close_item_by_id(editor3_id, SaveBehavior::PromptOnWrite, cx) pane.close_item_by_id(editor3_id, SaveIntent::Close, cx)
}) })
.await .await
.unwrap(); .unwrap();
@ -1727,7 +1731,7 @@ mod tests {
pane.update(cx, |pane, cx| { pane.update(cx, |pane, cx| {
let editor2_id = editor2.id(); let editor2_id = editor2.id();
drop(editor2); drop(editor2);
pane.close_item_by_id(editor2_id, SaveBehavior::PromptOnWrite, cx) pane.close_item_by_id(editor2_id, SaveIntent::Close, cx)
}) })
.await .await
.unwrap(); .unwrap();
@ -1884,28 +1888,28 @@ mod tests {
// Close all the pane items in some arbitrary order. // Close all the pane items in some arbitrary order.
pane.update(cx, |pane, cx| { pane.update(cx, |pane, cx| {
pane.close_item_by_id(file1_item_id, SaveBehavior::PromptOnWrite, cx) pane.close_item_by_id(file1_item_id, SaveIntent::Close, cx)
}) })
.await .await
.unwrap(); .unwrap();
assert_eq!(active_path(&workspace, cx), Some(file4.clone())); assert_eq!(active_path(&workspace, cx), Some(file4.clone()));
pane.update(cx, |pane, cx| { pane.update(cx, |pane, cx| {
pane.close_item_by_id(file4_item_id, SaveBehavior::PromptOnWrite, cx) pane.close_item_by_id(file4_item_id, SaveIntent::Close, cx)
}) })
.await .await
.unwrap(); .unwrap();
assert_eq!(active_path(&workspace, cx), Some(file3.clone())); assert_eq!(active_path(&workspace, cx), Some(file3.clone()));
pane.update(cx, |pane, cx| { pane.update(cx, |pane, cx| {
pane.close_item_by_id(file2_item_id, SaveBehavior::PromptOnWrite, cx) pane.close_item_by_id(file2_item_id, SaveIntent::Close, cx)
}) })
.await .await
.unwrap(); .unwrap();
assert_eq!(active_path(&workspace, cx), Some(file3.clone())); assert_eq!(active_path(&workspace, cx), Some(file3.clone()));
pane.update(cx, |pane, cx| { pane.update(cx, |pane, cx| {
pane.close_item_by_id(file3_item_id, SaveBehavior::PromptOnWrite, cx) pane.close_item_by_id(file3_item_id, SaveIntent::Close, cx)
}) })
.await .await
.unwrap(); .unwrap();