1use anyhow::{anyhow, bail, Result};
2use fuzzy::{StringMatch, StringMatchCandidate};
3use git::repository::Branch;
4use gpui::{
5 actions, rems, AnyElement, AppContext, DismissEvent, Element, EventEmitter, FocusHandle,
6 FocusableView, InteractiveElement, IntoElement, ParentElement, Render, SharedString, Styled,
7 Subscription, Task, View, ViewContext, VisualContext, WindowContext,
8};
9use picker::{Picker, PickerDelegate};
10use std::{ops::Not, sync::Arc};
11use ui::{
12 h_flex, v_flex, Button, ButtonCommon, Clickable, Color, HighlightedLabel, Label, LabelCommon,
13 LabelSize, ListItem, ListItemSpacing, Selectable,
14};
15use util::ResultExt;
16use workspace::notifications::NotificationId;
17use workspace::{ModalView, Toast, Workspace};
18
19actions!(branches, [OpenRecent]);
20
21pub fn init(cx: &mut AppContext) {
22 cx.observe_new_views(|workspace: &mut Workspace, _| {
23 workspace.register_action(|workspace, action, cx| {
24 BranchList::toggle_modal(workspace, action, cx).log_err();
25 });
26 })
27 .detach();
28}
29
30pub struct BranchList {
31 pub picker: View<Picker<BranchListDelegate>>,
32 rem_width: f32,
33 _subscription: Subscription,
34}
35
36impl BranchList {
37 fn new(delegate: BranchListDelegate, rem_width: f32, cx: &mut ViewContext<Self>) -> Self {
38 let picker = cx.new_view(|cx| Picker::uniform_list(delegate, cx));
39 let _subscription = cx.subscribe(&picker, |_, _, _, cx| cx.emit(DismissEvent));
40 Self {
41 picker,
42 rem_width,
43 _subscription,
44 }
45 }
46 fn toggle_modal(
47 workspace: &mut Workspace,
48 _: &OpenRecent,
49 cx: &mut ViewContext<Workspace>,
50 ) -> Result<()> {
51 // Modal branch picker has a longer trailoff than a popover one.
52 let delegate = BranchListDelegate::new(workspace, cx.view().clone(), 70, cx)?;
53 workspace.toggle_modal(cx, |cx| BranchList::new(delegate, 34., cx));
54
55 Ok(())
56 }
57}
58impl ModalView for BranchList {}
59impl EventEmitter<DismissEvent> for BranchList {}
60
61impl FocusableView for BranchList {
62 fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
63 self.picker.focus_handle(cx)
64 }
65}
66
67impl Render for BranchList {
68 fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
69 v_flex()
70 .w(rems(self.rem_width))
71 .child(self.picker.clone())
72 .on_mouse_down_out(cx.listener(|this, _, cx| {
73 this.picker.update(cx, |this, cx| {
74 this.cancel(&Default::default(), cx);
75 })
76 }))
77 }
78}
79
80pub fn build_branch_list(
81 workspace: View<Workspace>,
82 cx: &mut WindowContext<'_>,
83) -> Result<View<BranchList>> {
84 let delegate = workspace.update(cx, |workspace, cx| {
85 BranchListDelegate::new(workspace, cx.view().clone(), 29, cx)
86 })?;
87 Ok(cx.new_view(move |cx| BranchList::new(delegate, 20., cx)))
88}
89
90pub struct BranchListDelegate {
91 matches: Vec<StringMatch>,
92 all_branches: Vec<Branch>,
93 workspace: View<Workspace>,
94 selected_index: usize,
95 last_query: String,
96 /// Max length of branch name before we truncate it and add a trailing `...`.
97 branch_name_trailoff_after: usize,
98}
99
100impl BranchListDelegate {
101 fn new(
102 workspace: &Workspace,
103 handle: View<Workspace>,
104 branch_name_trailoff_after: usize,
105 cx: &AppContext,
106 ) -> Result<Self> {
107 let project = workspace.project().read(&cx);
108 let Some(worktree) = project.visible_worktrees(cx).next() else {
109 bail!("Cannot update branch list as there are no visible worktrees")
110 };
111
112 let mut cwd = worktree.read(cx).abs_path().to_path_buf();
113 cwd.push(".git");
114 let Some(repo) = project.fs().open_repo(&cwd) else {
115 bail!("Project does not have associated git repository.")
116 };
117 let all_branches = repo.lock().branches()?;
118 Ok(Self {
119 matches: vec![],
120 workspace: handle,
121 all_branches,
122 selected_index: 0,
123 last_query: Default::default(),
124 branch_name_trailoff_after,
125 })
126 }
127
128 fn display_error_toast(&self, message: String, cx: &mut WindowContext<'_>) {
129 self.workspace.update(cx, |model, ctx| {
130 struct GitCheckoutFailure;
131 let id = NotificationId::unique::<GitCheckoutFailure>();
132
133 model.show_toast(Toast::new(id, message), ctx)
134 });
135 }
136}
137
138impl PickerDelegate for BranchListDelegate {
139 type ListItem = ListItem;
140
141 fn placeholder_text(&self, _cx: &mut WindowContext) -> Arc<str> {
142 "Select branch...".into()
143 }
144
145 fn match_count(&self) -> usize {
146 self.matches.len()
147 }
148
149 fn selected_index(&self) -> usize {
150 self.selected_index
151 }
152
153 fn set_selected_index(&mut self, ix: usize, _: &mut ViewContext<Picker<Self>>) {
154 self.selected_index = ix;
155 }
156
157 fn update_matches(&mut self, query: String, cx: &mut ViewContext<Picker<Self>>) -> Task<()> {
158 cx.spawn(move |picker, mut cx| async move {
159 let candidates = picker.update(&mut cx, |view, _| {
160 const RECENT_BRANCHES_COUNT: usize = 10;
161 let mut branches = view.delegate.all_branches.clone();
162 if query.is_empty() && branches.len() > RECENT_BRANCHES_COUNT {
163 // Truncate list of recent branches
164 // Do a partial sort to show recent-ish branches first.
165 branches.select_nth_unstable_by(RECENT_BRANCHES_COUNT - 1, |lhs, rhs| {
166 rhs.unix_timestamp.cmp(&lhs.unix_timestamp)
167 });
168 branches.truncate(RECENT_BRANCHES_COUNT);
169 branches.sort_unstable_by(|lhs, rhs| lhs.name.cmp(&rhs.name));
170 }
171 branches
172 .into_iter()
173 .enumerate()
174 .map(|(ix, command)| StringMatchCandidate {
175 id: ix,
176 char_bag: command.name.chars().collect(),
177 string: command.name.into(),
178 })
179 .collect::<Vec<StringMatchCandidate>>()
180 });
181 let Some(candidates) = candidates.log_err() else {
182 return;
183 };
184 let matches = if query.is_empty() {
185 candidates
186 .into_iter()
187 .enumerate()
188 .map(|(index, candidate)| StringMatch {
189 candidate_id: index,
190 string: candidate.string,
191 positions: Vec::new(),
192 score: 0.0,
193 })
194 .collect()
195 } else {
196 fuzzy::match_strings(
197 &candidates,
198 &query,
199 true,
200 10000,
201 &Default::default(),
202 cx.background_executor().clone(),
203 )
204 .await
205 };
206 picker
207 .update(&mut cx, |picker, _| {
208 let delegate = &mut picker.delegate;
209 delegate.matches = matches;
210 if delegate.matches.is_empty() {
211 delegate.selected_index = 0;
212 } else {
213 delegate.selected_index =
214 core::cmp::min(delegate.selected_index, delegate.matches.len() - 1);
215 }
216 delegate.last_query = query;
217 })
218 .log_err();
219 })
220 }
221
222 fn confirm(&mut self, _: bool, cx: &mut ViewContext<Picker<Self>>) {
223 let current_pick = self.selected_index();
224 let Some(current_pick) = self
225 .matches
226 .get(current_pick)
227 .map(|pick| pick.string.clone())
228 else {
229 return;
230 };
231 cx.spawn(|picker, mut cx| async move {
232 picker
233 .update(&mut cx, |this, cx| {
234 let project = this.delegate.workspace.read(cx).project().read(cx);
235 let mut cwd = project
236 .visible_worktrees(cx)
237 .next()
238 .ok_or_else(|| anyhow!("There are no visisible worktrees."))?
239 .read(cx)
240 .abs_path()
241 .to_path_buf();
242 cwd.push(".git");
243 let status = project
244 .fs()
245 .open_repo(&cwd)
246 .ok_or_else(|| {
247 anyhow!(
248 "Could not open repository at path `{}`",
249 cwd.as_os_str().to_string_lossy()
250 )
251 })?
252 .lock()
253 .change_branch(¤t_pick);
254 if status.is_err() {
255 this.delegate.display_error_toast(format!("Failed to checkout branch '{current_pick}', check for conflicts or unstashed files"), cx);
256 status?;
257 }
258 cx.emit(DismissEvent);
259
260 Ok::<(), anyhow::Error>(())
261 })
262 .log_err();
263 })
264 .detach();
265 }
266
267 fn dismissed(&mut self, cx: &mut ViewContext<Picker<Self>>) {
268 cx.emit(DismissEvent);
269 }
270
271 fn render_match(
272 &self,
273 ix: usize,
274 selected: bool,
275 _cx: &mut ViewContext<Picker<Self>>,
276 ) -> Option<Self::ListItem> {
277 let hit = &self.matches[ix];
278 let shortened_branch_name =
279 util::truncate_and_trailoff(&hit.string, self.branch_name_trailoff_after);
280 let highlights: Vec<_> = hit
281 .positions
282 .iter()
283 .filter(|index| index < &&self.branch_name_trailoff_after)
284 .copied()
285 .collect();
286 Some(
287 ListItem::new(SharedString::from(format!("vcs-menu-{ix}")))
288 .inset(true)
289 .spacing(ListItemSpacing::Sparse)
290 .selected(selected)
291 .start_slot(HighlightedLabel::new(shortened_branch_name, highlights)),
292 )
293 }
294 fn render_header(&self, _: &mut ViewContext<Picker<Self>>) -> Option<AnyElement> {
295 let label = if self.last_query.is_empty() {
296 h_flex()
297 .ml_3()
298 .child(Label::new("Recent Branches").size(LabelSize::Small))
299 } else {
300 let match_label = self.matches.is_empty().not().then(|| {
301 let suffix = if self.matches.len() == 1 { "" } else { "es" };
302 Label::new(format!("{} match{}", self.matches.len(), suffix))
303 .color(Color::Muted)
304 .size(LabelSize::Small)
305 });
306 h_flex()
307 .px_3()
308 .h_full()
309 .justify_between()
310 .child(Label::new("Branches").size(LabelSize::Small))
311 .children(match_label)
312 };
313 Some(label.mt_1().into_any())
314 }
315 fn render_footer(&self, cx: &mut ViewContext<Picker<Self>>) -> Option<AnyElement> {
316 if self.last_query.is_empty() {
317 return None;
318 }
319
320 Some(
321 h_flex().mr_3().pb_2().child(h_flex().w_full()).child(
322 Button::new("branch-picker-create-branch-button", "Create branch").on_click(
323 cx.listener(|_, _, cx| {
324 cx.spawn(|picker, mut cx| async move {
325 picker.update(&mut cx, |this, cx| {
326 let project = this.delegate.workspace.read(cx).project().read(cx);
327 let current_pick = &this.delegate.last_query;
328 let mut cwd = project
329 .visible_worktrees(cx)
330 .next()
331 .ok_or_else(|| anyhow!("There are no visisible worktrees."))?
332 .read(cx)
333 .abs_path()
334 .to_path_buf();
335 cwd.push(".git");
336 let repo = project
337 .fs()
338 .open_repo(&cwd)
339 .ok_or_else(|| anyhow!("Could not open repository at path `{}`", cwd.as_os_str().to_string_lossy()))?;
340 let repo = repo
341 .lock();
342 let status = repo
343 .create_branch(¤t_pick);
344 if status.is_err() {
345 this.delegate.display_error_toast(format!("Failed to create branch '{current_pick}', check for conflicts or unstashed files"), cx);
346 status?;
347 }
348 let status = repo.change_branch(¤t_pick);
349 if status.is_err() {
350 this.delegate.display_error_toast(format!("Failed to check branch '{current_pick}', check for conflicts or unstashed files"), cx);
351 status?;
352 }
353 this.cancel(&Default::default(), cx);
354 Ok::<(), anyhow::Error>(())
355 })
356
357 }).detach_and_log_err(cx);
358 }),
359 ).style(ui::ButtonStyle::Filled)).into_any_element(),
360 )
361 }
362}