2023-06-30 12:00:37 +00:00
|
|
|
use anyhow::{anyhow, bail};
|
2023-06-27 10:02:30 +00:00
|
|
|
use fuzzy::{StringMatch, StringMatchCandidate};
|
2023-07-05 12:08:21 +00:00
|
|
|
use gpui::{elements::*, AppContext, MouseState, Task, ViewContext, ViewHandle};
|
2023-06-27 10:02:30 +00:00
|
|
|
use picker::{Picker, PickerDelegate, PickerEvent};
|
2023-06-30 14:23:27 +00:00
|
|
|
use std::{ops::Not, sync::Arc};
|
2023-06-29 09:42:20 +00:00
|
|
|
use util::ResultExt;
|
2023-06-27 18:12:20 +00:00
|
|
|
use workspace::{Toast, Workspace};
|
2023-06-27 10:02:30 +00:00
|
|
|
|
|
|
|
pub fn init(cx: &mut AppContext) {
|
|
|
|
Picker::<BranchListDelegate>::init(cx);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type BranchList = Picker<BranchListDelegate>;
|
|
|
|
|
|
|
|
pub fn build_branch_list(
|
2023-06-27 18:12:20 +00:00
|
|
|
workspace: ViewHandle<Workspace>,
|
2023-06-27 10:02:30 +00:00
|
|
|
cx: &mut ViewContext<BranchList>,
|
|
|
|
) -> BranchList {
|
|
|
|
Picker::new(
|
|
|
|
BranchListDelegate {
|
|
|
|
matches: vec![],
|
2023-06-27 18:12:20 +00:00
|
|
|
workspace,
|
2023-06-27 10:02:30 +00:00
|
|
|
selected_index: 0,
|
2023-06-27 10:22:44 +00:00
|
|
|
last_query: String::default(),
|
2023-06-27 10:02:30 +00:00
|
|
|
},
|
|
|
|
cx,
|
|
|
|
)
|
|
|
|
.with_theme(|theme| theme.picker.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct BranchListDelegate {
|
|
|
|
matches: Vec<StringMatch>,
|
2023-06-27 18:12:20 +00:00
|
|
|
workspace: ViewHandle<Workspace>,
|
2023-06-27 10:02:30 +00:00
|
|
|
selected_index: usize,
|
2023-06-27 10:22:44 +00:00
|
|
|
last_query: String,
|
2023-06-27 10:02:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PickerDelegate for BranchListDelegate {
|
|
|
|
fn placeholder_text(&self) -> Arc<str> {
|
|
|
|
"Select branch...".into()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn match_count(&self) -> usize {
|
|
|
|
self.matches.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn selected_index(&self) -> usize {
|
|
|
|
self.selected_index
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_selected_index(&mut self, ix: usize, _: &mut ViewContext<Picker<Self>>) {
|
|
|
|
self.selected_index = ix;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_matches(&mut self, query: String, cx: &mut ViewContext<Picker<Self>>) -> Task<()> {
|
|
|
|
cx.spawn(move |picker, mut cx| async move {
|
2023-06-30 12:00:37 +00:00
|
|
|
let Some(candidates) = picker
|
2023-06-27 10:02:30 +00:00
|
|
|
.read_with(&mut cx, |view, cx| {
|
|
|
|
let delegate = view.delegate();
|
2023-06-27 18:12:20 +00:00
|
|
|
let project = delegate.workspace.read(cx).project().read(&cx);
|
2023-07-03 17:22:43 +00:00
|
|
|
let mut cwd =
|
|
|
|
project
|
2023-06-27 10:02:30 +00:00
|
|
|
.visible_worktrees(cx)
|
|
|
|
.next()
|
|
|
|
.unwrap()
|
|
|
|
.read(cx)
|
2023-07-03 17:22:43 +00:00
|
|
|
.abs_path()
|
2023-06-27 10:02:30 +00:00
|
|
|
.to_path_buf();
|
|
|
|
cwd.push(".git");
|
2023-06-30 12:00:37 +00:00
|
|
|
let Some(repo) = project.fs().open_repo(&cwd) else {bail!("Project does not have associated git repository.")};
|
|
|
|
let mut branches = repo
|
2023-06-27 12:20:36 +00:00
|
|
|
.lock()
|
2023-06-30 12:00:37 +00:00
|
|
|
.branches()?;
|
2023-07-03 17:22:43 +00:00
|
|
|
const RECENT_BRANCHES_COUNT: usize = 10;
|
|
|
|
if query.is_empty() && branches.len() > RECENT_BRANCHES_COUNT {
|
|
|
|
// Truncate list of recent branches
|
2023-06-27 12:20:36 +00:00
|
|
|
// Do a partial sort to show recent-ish branches first.
|
2023-07-03 17:22:43 +00:00
|
|
|
branches.select_nth_unstable_by(RECENT_BRANCHES_COUNT - 1, |lhs, rhs| {
|
2023-06-27 12:20:36 +00:00
|
|
|
rhs.unix_timestamp.cmp(&lhs.unix_timestamp)
|
|
|
|
});
|
|
|
|
branches.truncate(RECENT_BRANCHES_COUNT);
|
|
|
|
branches.sort_unstable_by(|lhs, rhs| lhs.name.cmp(&rhs.name));
|
|
|
|
}
|
2023-06-30 12:00:37 +00:00
|
|
|
Ok(branches
|
2023-06-27 10:02:30 +00:00
|
|
|
.iter()
|
|
|
|
.cloned()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(ix, command)| StringMatchCandidate {
|
|
|
|
id: ix,
|
2023-06-27 11:28:50 +00:00
|
|
|
char_bag: command.name.chars().collect(),
|
|
|
|
string: command.name.into(),
|
2023-06-27 10:02:30 +00:00
|
|
|
})
|
2023-06-30 12:00:37 +00:00
|
|
|
.collect::<Vec<_>>())
|
2023-06-27 10:02:30 +00:00
|
|
|
})
|
2023-06-30 12:00:37 +00:00
|
|
|
.log_err() else { return; };
|
|
|
|
let Some(candidates) = candidates.log_err() else {return;};
|
2023-06-27 10:02:30 +00:00
|
|
|
let matches = if query.is_empty() {
|
|
|
|
candidates
|
|
|
|
.into_iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(index, candidate)| StringMatch {
|
|
|
|
candidate_id: index,
|
|
|
|
string: candidate.string,
|
|
|
|
positions: Vec::new(),
|
|
|
|
score: 0.0,
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
} else {
|
|
|
|
fuzzy::match_strings(
|
|
|
|
&candidates,
|
|
|
|
&query,
|
|
|
|
true,
|
|
|
|
10000,
|
|
|
|
&Default::default(),
|
|
|
|
cx.background(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
};
|
|
|
|
picker
|
|
|
|
.update(&mut cx, |picker, _| {
|
|
|
|
let delegate = picker.delegate_mut();
|
|
|
|
delegate.matches = matches;
|
|
|
|
if delegate.matches.is_empty() {
|
|
|
|
delegate.selected_index = 0;
|
|
|
|
} else {
|
|
|
|
delegate.selected_index =
|
|
|
|
core::cmp::min(delegate.selected_index, delegate.matches.len() - 1);
|
|
|
|
}
|
2023-06-27 12:20:36 +00:00
|
|
|
delegate.last_query = query;
|
2023-06-27 10:02:30 +00:00
|
|
|
})
|
|
|
|
.log_err();
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn confirm(&mut self, cx: &mut ViewContext<Picker<Self>>) {
|
|
|
|
let current_pick = self.selected_index();
|
|
|
|
let current_pick = self.matches[current_pick].string.clone();
|
2023-06-30 12:00:37 +00:00
|
|
|
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
|
|
|
|
.visible_worktrees(cx)
|
|
|
|
.next()
|
|
|
|
.ok_or_else(|| anyhow!("There are no visisible worktrees."))?
|
|
|
|
.read(cx)
|
2023-07-03 17:22:43 +00:00
|
|
|
.abs_path()
|
2023-06-30 12:00:37 +00:00
|
|
|
.to_path_buf();
|
|
|
|
cwd.push(".git");
|
|
|
|
let status = project
|
|
|
|
.fs()
|
|
|
|
.open_repo(&cwd)
|
|
|
|
.ok_or_else(|| anyhow!("Could not open repository at path `{}`", cwd.as_os_str().to_string_lossy()))?
|
|
|
|
.lock()
|
|
|
|
.change_branch(¤t_pick);
|
|
|
|
if status.is_err() {
|
|
|
|
const GIT_CHECKOUT_FAILURE_ID: usize = 2048;
|
|
|
|
this.delegate().workspace.update(cx, |model, ctx| {
|
|
|
|
model.show_toast(
|
|
|
|
Toast::new(
|
|
|
|
GIT_CHECKOUT_FAILURE_ID,
|
|
|
|
format!("Failed to checkout branch '{current_pick}', check for conflicts or unstashed files"),
|
|
|
|
),
|
|
|
|
ctx,
|
|
|
|
)
|
|
|
|
});
|
|
|
|
status?;
|
|
|
|
}
|
|
|
|
cx.emit(PickerEvent::Dismiss);
|
|
|
|
|
|
|
|
Ok::<(), anyhow::Error>(())
|
|
|
|
}).log_err();
|
|
|
|
}).detach();
|
2023-06-27 10:02:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn dismissed(&mut self, cx: &mut ViewContext<Picker<Self>>) {
|
|
|
|
cx.emit(PickerEvent::Dismiss);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render_match(
|
|
|
|
&self,
|
|
|
|
ix: usize,
|
|
|
|
mouse_state: &mut MouseState,
|
|
|
|
selected: bool,
|
|
|
|
cx: &gpui::AppContext,
|
|
|
|
) -> AnyElement<Picker<Self>> {
|
2023-06-27 10:40:53 +00:00
|
|
|
const DISPLAYED_MATCH_LEN: usize = 29;
|
2023-06-27 10:02:30 +00:00
|
|
|
let theme = &theme::current(cx);
|
2023-06-27 10:40:53 +00:00
|
|
|
let hit = &self.matches[ix];
|
|
|
|
let shortened_branch_name = util::truncate_and_trailoff(&hit.string, DISPLAYED_MATCH_LEN);
|
|
|
|
let highlights = hit
|
|
|
|
.positions
|
|
|
|
.iter()
|
|
|
|
.copied()
|
|
|
|
.filter(|index| index < &DISPLAYED_MATCH_LEN)
|
|
|
|
.collect();
|
2023-06-27 10:02:30 +00:00
|
|
|
let style = theme.picker.item.in_state(selected).style_for(mouse_state);
|
|
|
|
Flex::row()
|
|
|
|
.with_child(
|
2023-06-27 10:40:53 +00:00
|
|
|
Label::new(shortened_branch_name.clone(), style.label.clone())
|
|
|
|
.with_highlights(highlights)
|
2023-06-27 10:02:30 +00:00
|
|
|
.contained()
|
|
|
|
.aligned()
|
|
|
|
.left(),
|
|
|
|
)
|
|
|
|
.contained()
|
|
|
|
.with_style(style.container)
|
|
|
|
.constrained()
|
|
|
|
.with_height(theme.contact_finder.row_height)
|
|
|
|
.into_any()
|
|
|
|
}
|
2023-06-30 23:38:36 +00:00
|
|
|
fn render_header(
|
|
|
|
&self,
|
|
|
|
cx: &mut ViewContext<Picker<Self>>,
|
|
|
|
) -> Option<AnyElement<Picker<Self>>> {
|
2023-06-27 10:22:44 +00:00
|
|
|
let theme = &theme::current(cx);
|
2023-06-30 14:23:27 +00:00
|
|
|
let style = theme.picker.header.clone();
|
2023-06-30 23:38:36 +00:00
|
|
|
let label = if self.last_query.is_empty() {
|
2023-07-05 12:04:16 +00:00
|
|
|
Flex::row()
|
|
|
|
.with_child(Label::new("Recent branches", style.label.clone()))
|
2023-06-30 23:38:36 +00:00
|
|
|
.contained()
|
|
|
|
.with_style(style.container)
|
2023-06-27 10:22:44 +00:00
|
|
|
} else {
|
2023-07-05 12:04:16 +00:00
|
|
|
Flex::row()
|
2023-07-05 14:56:08 +00:00
|
|
|
.with_child(Label::new("Branches", style.label.clone()))
|
2023-06-30 23:38:36 +00:00
|
|
|
.with_children(self.matches.is_empty().not().then(|| {
|
|
|
|
let suffix = if self.matches.len() == 1 { "" } else { "es" };
|
2023-07-05 14:56:08 +00:00
|
|
|
Label::new(
|
|
|
|
format!("{} match{}", self.matches.len(), suffix),
|
|
|
|
style.label,
|
|
|
|
)
|
|
|
|
.flex_float()
|
2023-06-30 23:38:36 +00:00
|
|
|
}))
|
|
|
|
.contained()
|
|
|
|
.with_style(style.container)
|
|
|
|
};
|
2023-07-05 12:04:16 +00:00
|
|
|
Some(label.into_any())
|
2023-06-27 10:22:44 +00:00
|
|
|
}
|
2023-06-27 10:02:30 +00:00
|
|
|
}
|