directory_context_picker.rs

  1use std::path::Path;
  2use std::sync::atomic::AtomicBool;
  3use std::sync::Arc;
  4
  5use fuzzy::PathMatch;
  6use gpui::{AppContext, DismissEvent, FocusHandle, FocusableView, Task, View, WeakModel, WeakView};
  7use picker::{Picker, PickerDelegate};
  8use project::{PathMatchCandidateSet, ProjectPath, WorktreeId};
  9use ui::{prelude::*, ListItem};
 10use util::ResultExt as _;
 11use workspace::Workspace;
 12
 13use crate::context_picker::{ConfirmBehavior, ContextPicker};
 14use crate::context_store::ContextStore;
 15
 16pub struct DirectoryContextPicker {
 17    picker: View<Picker<DirectoryContextPickerDelegate>>,
 18}
 19
 20impl DirectoryContextPicker {
 21    pub fn new(
 22        context_picker: WeakView<ContextPicker>,
 23        workspace: WeakView<Workspace>,
 24        context_store: WeakModel<ContextStore>,
 25        confirm_behavior: ConfirmBehavior,
 26        cx: &mut ViewContext<Self>,
 27    ) -> Self {
 28        let delegate = DirectoryContextPickerDelegate::new(
 29            context_picker,
 30            workspace,
 31            context_store,
 32            confirm_behavior,
 33        );
 34        let picker = cx.new_view(|cx| Picker::uniform_list(delegate, cx));
 35
 36        Self { picker }
 37    }
 38}
 39
 40impl FocusableView for DirectoryContextPicker {
 41    fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
 42        self.picker.focus_handle(cx)
 43    }
 44}
 45
 46impl Render for DirectoryContextPicker {
 47    fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
 48        self.picker.clone()
 49    }
 50}
 51
 52pub struct DirectoryContextPickerDelegate {
 53    context_picker: WeakView<ContextPicker>,
 54    workspace: WeakView<Workspace>,
 55    context_store: WeakModel<ContextStore>,
 56    confirm_behavior: ConfirmBehavior,
 57    matches: Vec<PathMatch>,
 58    selected_index: usize,
 59}
 60
 61impl DirectoryContextPickerDelegate {
 62    pub fn new(
 63        context_picker: WeakView<ContextPicker>,
 64        workspace: WeakView<Workspace>,
 65        context_store: WeakModel<ContextStore>,
 66        confirm_behavior: ConfirmBehavior,
 67    ) -> Self {
 68        Self {
 69            context_picker,
 70            workspace,
 71            context_store,
 72            confirm_behavior,
 73            matches: Vec::new(),
 74            selected_index: 0,
 75        }
 76    }
 77
 78    fn search(
 79        &mut self,
 80        query: String,
 81        cancellation_flag: Arc<AtomicBool>,
 82        workspace: &View<Workspace>,
 83        cx: &mut ViewContext<Picker<Self>>,
 84    ) -> Task<Vec<PathMatch>> {
 85        if query.is_empty() {
 86            let workspace = workspace.read(cx);
 87            let project = workspace.project().read(cx);
 88            let directory_matches = project.worktrees(cx).flat_map(|worktree| {
 89                let worktree = worktree.read(cx);
 90                let path_prefix: Arc<str> = worktree.root_name().into();
 91                worktree.directories(false, 0).map(move |entry| PathMatch {
 92                    score: 0.,
 93                    positions: Vec::new(),
 94                    worktree_id: worktree.id().to_usize(),
 95                    path: entry.path.clone(),
 96                    path_prefix: path_prefix.clone(),
 97                    distance_to_relative_ancestor: 0,
 98                    is_dir: true,
 99                })
100            });
101
102            Task::ready(directory_matches.collect())
103        } else {
104            let worktrees = workspace.read(cx).visible_worktrees(cx).collect::<Vec<_>>();
105            let candidate_sets = worktrees
106                .into_iter()
107                .map(|worktree| {
108                    let worktree = worktree.read(cx);
109
110                    PathMatchCandidateSet {
111                        snapshot: worktree.snapshot(),
112                        include_ignored: worktree
113                            .root_entry()
114                            .map_or(false, |entry| entry.is_ignored),
115                        include_root_name: true,
116                        candidates: project::Candidates::Directories,
117                    }
118                })
119                .collect::<Vec<_>>();
120
121            let executor = cx.background_executor().clone();
122            cx.foreground_executor().spawn(async move {
123                fuzzy::match_path_sets(
124                    candidate_sets.as_slice(),
125                    query.as_str(),
126                    None,
127                    false,
128                    100,
129                    &cancellation_flag,
130                    executor,
131                )
132                .await
133            })
134        }
135    }
136}
137
138impl PickerDelegate for DirectoryContextPickerDelegate {
139    type ListItem = ListItem;
140
141    fn match_count(&self) -> usize {
142        self.matches.len()
143    }
144
145    fn selected_index(&self) -> usize {
146        self.selected_index
147    }
148
149    fn set_selected_index(&mut self, ix: usize, _cx: &mut ViewContext<Picker<Self>>) {
150        self.selected_index = ix;
151    }
152
153    fn placeholder_text(&self, _cx: &mut WindowContext) -> Arc<str> {
154        "Search folders…".into()
155    }
156
157    fn update_matches(&mut self, query: String, cx: &mut ViewContext<Picker<Self>>) -> Task<()> {
158        let Some(workspace) = self.workspace.upgrade() else {
159            return Task::ready(());
160        };
161
162        let search_task = self.search(query, Arc::<AtomicBool>::default(), &workspace, cx);
163
164        cx.spawn(|this, mut cx| async move {
165            let mut paths = search_task.await;
166            let empty_path = Path::new("");
167            paths.retain(|path_match| path_match.path.as_ref() != empty_path);
168
169            this.update(&mut cx, |this, _cx| {
170                this.delegate.matches = paths;
171            })
172            .log_err();
173        })
174    }
175
176    fn confirm(&mut self, _secondary: bool, cx: &mut ViewContext<Picker<Self>>) {
177        let Some(mat) = self.matches.get(self.selected_index) else {
178            return;
179        };
180
181        let project_path = ProjectPath {
182            worktree_id: WorktreeId::from_usize(mat.worktree_id),
183            path: mat.path.clone(),
184        };
185
186        let Some(task) = self
187            .context_store
188            .update(cx, |context_store, cx| {
189                context_store.add_directory(project_path, cx)
190            })
191            .ok()
192        else {
193            return;
194        };
195
196        let workspace = self.workspace.clone();
197        let confirm_behavior = self.confirm_behavior;
198        cx.spawn(|this, mut cx| async move {
199            match task.await {
200                Ok(()) => {
201                    this.update(&mut cx, |this, cx| match confirm_behavior {
202                        ConfirmBehavior::KeepOpen => {}
203                        ConfirmBehavior::Close => this.delegate.dismissed(cx),
204                    })?;
205                }
206                Err(err) => {
207                    let Some(workspace) = workspace.upgrade() else {
208                        return anyhow::Ok(());
209                    };
210
211                    workspace.update(&mut cx, |workspace, cx| {
212                        workspace.show_error(&err, cx);
213                    })?;
214                }
215            }
216
217            anyhow::Ok(())
218        })
219        .detach_and_log_err(cx);
220    }
221
222    fn dismissed(&mut self, cx: &mut ViewContext<Picker<Self>>) {
223        self.context_picker
224            .update(cx, |_, cx| {
225                cx.emit(DismissEvent);
226            })
227            .ok();
228    }
229
230    fn render_match(
231        &self,
232        ix: usize,
233        selected: bool,
234        cx: &mut ViewContext<Picker<Self>>,
235    ) -> Option<Self::ListItem> {
236        let path_match = &self.matches[ix];
237        let directory_name = path_match.path.to_string_lossy().to_string();
238
239        let added = self.context_store.upgrade().map_or(false, |context_store| {
240            context_store
241                .read(cx)
242                .includes_directory(&path_match.path)
243                .is_some()
244        });
245
246        Some(
247            ListItem::new(ix)
248                .inset(true)
249                .toggle_state(selected)
250                .child(h_flex().gap_2().child(Label::new(directory_name)))
251                .when(added, |el| {
252                    el.end_slot(
253                        h_flex()
254                            .gap_1()
255                            .child(
256                                Icon::new(IconName::Check)
257                                    .size(IconSize::Small)
258                                    .color(Color::Success),
259                            )
260                            .child(Label::new("Added").size(LabelSize::Small)),
261                    )
262                }),
263        )
264    }
265}