Switch branches within spawn()
This commit is contained in:
parent
c1a6292152
commit
3be8977ee8
1 changed files with 46 additions and 41 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
use anyhow::{anyhow, bail};
|
||||||
use fuzzy::{StringMatch, StringMatchCandidate};
|
use fuzzy::{StringMatch, StringMatchCandidate};
|
||||||
use gpui::{elements::*, AppContext, MouseState, Task, ViewContext, ViewHandle};
|
use gpui::{elements::*, AppContext, MouseState, Task, ViewContext, ViewHandle};
|
||||||
use picker::{Picker, PickerDelegate, PickerEvent};
|
use picker::{Picker, PickerDelegate, PickerEvent};
|
||||||
|
@ -53,7 +54,7 @@ impl PickerDelegate for BranchListDelegate {
|
||||||
|
|
||||||
fn update_matches(&mut self, query: String, cx: &mut ViewContext<Picker<Self>>) -> Task<()> {
|
fn update_matches(&mut self, query: String, cx: &mut ViewContext<Picker<Self>>) -> Task<()> {
|
||||||
cx.spawn(move |picker, mut cx| async move {
|
cx.spawn(move |picker, mut cx| async move {
|
||||||
let candidates = picker
|
let Some(candidates) = picker
|
||||||
.read_with(&mut cx, |view, cx| {
|
.read_with(&mut cx, |view, cx| {
|
||||||
let delegate = view.delegate();
|
let delegate = view.delegate();
|
||||||
let project = delegate.workspace.read(cx).project().read(&cx);
|
let project = delegate.workspace.read(cx).project().read(&cx);
|
||||||
|
@ -67,13 +68,10 @@ impl PickerDelegate for BranchListDelegate {
|
||||||
.path
|
.path
|
||||||
.to_path_buf();
|
.to_path_buf();
|
||||||
cwd.push(".git");
|
cwd.push(".git");
|
||||||
let mut branches = project
|
let Some(repo) = project.fs().open_repo(&cwd) else {bail!("Project does not have associated git repository.")};
|
||||||
.fs()
|
let mut branches = repo
|
||||||
.open_repo(&cwd)
|
|
||||||
.unwrap()
|
|
||||||
.lock()
|
.lock()
|
||||||
.branches()
|
.branches()?;
|
||||||
.unwrap();
|
|
||||||
if query.is_empty() {
|
if query.is_empty() {
|
||||||
const RECENT_BRANCHES_COUNT: usize = 10;
|
const RECENT_BRANCHES_COUNT: usize = 10;
|
||||||
// Do a partial sort to show recent-ish branches first.
|
// Do a partial sort to show recent-ish branches first.
|
||||||
|
@ -83,7 +81,7 @@ impl PickerDelegate for BranchListDelegate {
|
||||||
branches.truncate(RECENT_BRANCHES_COUNT);
|
branches.truncate(RECENT_BRANCHES_COUNT);
|
||||||
branches.sort_unstable_by(|lhs, rhs| lhs.name.cmp(&rhs.name));
|
branches.sort_unstable_by(|lhs, rhs| lhs.name.cmp(&rhs.name));
|
||||||
}
|
}
|
||||||
branches
|
Ok(branches
|
||||||
.iter()
|
.iter()
|
||||||
.cloned()
|
.cloned()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
|
@ -92,9 +90,10 @@ impl PickerDelegate for BranchListDelegate {
|
||||||
char_bag: command.name.chars().collect(),
|
char_bag: command.name.chars().collect(),
|
||||||
string: command.name.into(),
|
string: command.name.into(),
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>())
|
||||||
})
|
})
|
||||||
.unwrap();
|
.log_err() else { return; };
|
||||||
|
let Some(candidates) = candidates.log_err() else {return;};
|
||||||
let matches = if query.is_empty() {
|
let matches = if query.is_empty() {
|
||||||
candidates
|
candidates
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
@ -136,26 +135,28 @@ impl PickerDelegate for BranchListDelegate {
|
||||||
fn confirm(&mut self, cx: &mut ViewContext<Picker<Self>>) {
|
fn confirm(&mut self, cx: &mut ViewContext<Picker<Self>>) {
|
||||||
let current_pick = self.selected_index();
|
let current_pick = self.selected_index();
|
||||||
let current_pick = self.matches[current_pick].string.clone();
|
let current_pick = self.matches[current_pick].string.clone();
|
||||||
let project = self.workspace.read(cx).project().read(cx);
|
cx.spawn(|picker, mut cx| async move {
|
||||||
|
picker.update(&mut cx, |this, cx| {
|
||||||
|
let project = this.delegate().workspace.read(cx).project().read(cx);
|
||||||
let mut cwd = project
|
let mut cwd = project
|
||||||
.visible_worktrees(cx)
|
.visible_worktrees(cx)
|
||||||
.next()
|
.next()
|
||||||
.unwrap()
|
.ok_or_else(|| anyhow!("There are no visisible worktrees."))?
|
||||||
.read(cx)
|
.read(cx)
|
||||||
.root_entry()
|
.root_entry()
|
||||||
.unwrap()
|
.ok_or_else(|| anyhow!("Worktree has no root entry."))?
|
||||||
.path
|
.path
|
||||||
.to_path_buf();
|
.to_path_buf();
|
||||||
cwd.push(".git");
|
cwd.push(".git");
|
||||||
let status = project
|
let status = project
|
||||||
.fs()
|
.fs()
|
||||||
.open_repo(&cwd)
|
.open_repo(&cwd)
|
||||||
.unwrap()
|
.ok_or_else(|| anyhow!("Could not open repository at path `{}`", cwd.as_os_str().to_string_lossy()))?
|
||||||
.lock()
|
.lock()
|
||||||
.change_branch(¤t_pick);
|
.change_branch(¤t_pick);
|
||||||
if status.is_err() {
|
if status.is_err() {
|
||||||
const GIT_CHECKOUT_FAILURE_ID: usize = 2048;
|
const GIT_CHECKOUT_FAILURE_ID: usize = 2048;
|
||||||
self.workspace.update(cx, |model, ctx| {
|
this.delegate().workspace.update(cx, |model, ctx| {
|
||||||
model.show_toast(
|
model.show_toast(
|
||||||
Toast::new(
|
Toast::new(
|
||||||
GIT_CHECKOUT_FAILURE_ID,
|
GIT_CHECKOUT_FAILURE_ID,
|
||||||
|
@ -164,9 +165,13 @@ impl PickerDelegate for BranchListDelegate {
|
||||||
ctx,
|
ctx,
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
|
status?;
|
||||||
}
|
}
|
||||||
status.log_err();
|
|
||||||
cx.emit(PickerEvent::Dismiss);
|
cx.emit(PickerEvent::Dismiss);
|
||||||
|
|
||||||
|
Ok::<(), anyhow::Error>(())
|
||||||
|
}).log_err();
|
||||||
|
}).detach();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dismissed(&mut self, cx: &mut ViewContext<Picker<Self>>) {
|
fn dismissed(&mut self, cx: &mut ViewContext<Picker<Self>>) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue