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: Add function to save new file in directory nearest tab #22563

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
18 changes: 16 additions & 2 deletions crates/workspace/src/pane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,17 @@ impl Pane {
self.items.get(self.active_item_index).cloned()
}

pub fn active_nearest_item(&self) -> Option<Box<dyn ItemHandle>> {
if self.items_len() == 1 {
return None;
}
let index = match self.active_item_index {
0 => self.active_item_index + 1,
_ => self.active_item_index - 1,
};
self.items.get(index).cloned()
}

pub fn pixel_position_of_cursor(&self, cx: &AppContext) -> Option<Point<Pixels>> {
self.items
.get(self.active_item_index)?
Expand Down Expand Up @@ -1493,6 +1504,7 @@ impl Pane {
item_ix,
&*item_to_close,
save_intent,
None,
&mut cx,
)
.await?
Expand Down Expand Up @@ -1657,6 +1669,7 @@ impl Pane {
item_ix: usize,
item: &dyn ItemHandle,
save_intent: SaveIntent,
relative_project_path: Option<ProjectPath>,
cx: &mut AsyncWindowContext,
) -> Result<bool> {
const CONFLICT_MESSAGE: &str =
Expand Down Expand Up @@ -1806,8 +1819,9 @@ impl Pane {
.await?;
} else if can_save_as {
let abs_path = pane.update(cx, |pane, cx| {
pane.workspace
.update(cx, |workspace, cx| workspace.prompt_for_new_path(cx))
pane.workspace.update(cx, |workspace, cx| {
workspace.prompt_for_new_path(relative_project_path, cx)
})
})??;
if let Some(abs_path) = abs_path.await.ok().flatten() {
pane.update(cx, |pane, cx| {
Expand Down
48 changes: 43 additions & 5 deletions crates/workspace/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1582,6 +1582,7 @@ impl Workspace {

pub fn prompt_for_new_path(
&mut self,
relative_project_path: Option<ProjectPath>,
cx: &mut ViewContext<Self>,
) -> oneshot::Receiver<Option<ProjectPath>> {
if (self.project.read(cx).is_via_collab() || self.project.read(cx).is_via_ssh())
Expand All @@ -1595,8 +1596,17 @@ impl Workspace {
let start_abs_path = self
.project
.update(cx, |project, cx| {
let worktree = project.visible_worktrees(cx).next()?;
Some(worktree.read(cx).as_local()?.abs_path().to_path_buf())
let relative_path = relative_project_path.and_then(|relative_path| {
project
.absolute_path(&relative_path, cx)
.and_then(|p| p.parent().map(PathBuf::from))
});
if relative_path.is_none() {
let worktree = project.visible_worktrees(cx).next()?;
Some(worktree.read(cx).as_local()?.abs_path().to_path_buf())
} else {
relative_path
}
})
.unwrap_or_else(|| Path::new("").into());

Expand Down Expand Up @@ -1936,6 +1946,7 @@ impl Workspace {
ix,
&*item,
save_intent,
None,
&mut cx,
)
.await?
Expand Down Expand Up @@ -2184,6 +2195,24 @@ impl Workspace {
self.active_item(cx).and_then(|item| item.project_path(cx))
}

fn active_nearest_project_path(&self, cx: &AppContext) -> Option<ProjectPath> {
let p = self
.active_pane()
.read(cx)
.active_nearest_item()
.and_then(|item| item.project_path(cx));
if p.is_some() {
return p;
}
self.last_active_center_pane.clone().and_then(|p| {
p.upgrade().and_then(|pane| {
pane.read(cx)
.active_item()
.and_then(|item| item.project_path(cx))
})
})
}

pub fn save_active_item(
&mut self,
save_intent: SaveIntent,
Expand All @@ -2194,12 +2223,21 @@ impl Workspace {
let item_ix = pane.read(cx).active_item_index();
let item = pane.read(cx).active_item();
let pane = pane.downgrade();
let relative_project_path = self.active_nearest_project_path(cx);

cx.spawn(|mut cx| async move {
if let Some(item) = item {
Pane::save_item(project, &pane, item_ix, item.as_ref(), save_intent, &mut cx)
.await
.map(|_| ())
Pane::save_item(
project,
&pane,
item_ix,
item.as_ref(),
save_intent,
relative_project_path,
&mut cx,
)
.await
.map(|_| ())
} else {
Ok(())
}
Expand Down
Loading