file_context_picker.rs

  1use std::fmt::Write as _;
  2use std::ops::RangeInclusive;
  3use std::path::Path;
  4use std::sync::atomic::AtomicBool;
  5use std::sync::Arc;
  6
  7use fuzzy::PathMatch;
  8use gpui::{AppContext, DismissEvent, FocusHandle, FocusableView, Task, View, WeakModel, WeakView};
  9use picker::{Picker, PickerDelegate};
 10use project::{PathMatchCandidateSet, WorktreeId};
 11use ui::{prelude::*, ListItem};
 12use util::ResultExt as _;
 13use workspace::Workspace;
 14
 15use crate::context::ContextKind;
 16use crate::context_picker::{ConfirmBehavior, ContextPicker};
 17use crate::context_store::ContextStore;
 18
 19pub struct FileContextPicker {
 20    picker: View<Picker<FileContextPickerDelegate>>,
 21}
 22
 23impl FileContextPicker {
 24    pub fn new(
 25        context_picker: WeakView<ContextPicker>,
 26        workspace: WeakView<Workspace>,
 27        context_store: WeakModel<ContextStore>,
 28        confirm_behavior: ConfirmBehavior,
 29        cx: &mut ViewContext<Self>,
 30    ) -> Self {
 31        let delegate = FileContextPickerDelegate::new(
 32            context_picker,
 33            workspace,
 34            context_store,
 35            confirm_behavior,
 36        );
 37        let picker = cx.new_view(|cx| Picker::uniform_list(delegate, cx));
 38
 39        Self { picker }
 40    }
 41}
 42
 43impl FocusableView for FileContextPicker {
 44    fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
 45        self.picker.focus_handle(cx)
 46    }
 47}
 48
 49impl Render for FileContextPicker {
 50    fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
 51        self.picker.clone()
 52    }
 53}
 54
 55pub struct FileContextPickerDelegate {
 56    context_picker: WeakView<ContextPicker>,
 57    workspace: WeakView<Workspace>,
 58    context_store: WeakModel<ContextStore>,
 59    confirm_behavior: ConfirmBehavior,
 60    matches: Vec<PathMatch>,
 61    selected_index: usize,
 62}
 63
 64impl FileContextPickerDelegate {
 65    pub fn new(
 66        context_picker: WeakView<ContextPicker>,
 67        workspace: WeakView<Workspace>,
 68        context_store: WeakModel<ContextStore>,
 69        confirm_behavior: ConfirmBehavior,
 70    ) -> Self {
 71        Self {
 72            context_picker,
 73            workspace,
 74            context_store,
 75            confirm_behavior,
 76            matches: Vec::new(),
 77            selected_index: 0,
 78        }
 79    }
 80
 81    fn search(
 82        &mut self,
 83        query: String,
 84        cancellation_flag: Arc<AtomicBool>,
 85        workspace: &View<Workspace>,
 86        cx: &mut ViewContext<Picker<Self>>,
 87    ) -> Task<Vec<PathMatch>> {
 88        if query.is_empty() {
 89            let workspace = workspace.read(cx);
 90            let project = workspace.project().read(cx);
 91            let recent_matches = workspace
 92                .recent_navigation_history(Some(10), cx)
 93                .into_iter()
 94                .filter_map(|(project_path, _)| {
 95                    let worktree = project.worktree_for_id(project_path.worktree_id, cx)?;
 96                    Some(PathMatch {
 97                        score: 0.,
 98                        positions: Vec::new(),
 99                        worktree_id: project_path.worktree_id.to_usize(),
100                        path: project_path.path,
101                        path_prefix: worktree.read(cx).root_name().into(),
102                        distance_to_relative_ancestor: 0,
103                        is_dir: false,
104                    })
105                });
106
107            let file_matches = project.worktrees(cx).flat_map(|worktree| {
108                let worktree = worktree.read(cx);
109                let path_prefix: Arc<str> = worktree.root_name().into();
110                worktree.files(true, 0).map(move |entry| PathMatch {
111                    score: 0.,
112                    positions: Vec::new(),
113                    worktree_id: worktree.id().to_usize(),
114                    path: entry.path.clone(),
115                    path_prefix: path_prefix.clone(),
116                    distance_to_relative_ancestor: 0,
117                    is_dir: false,
118                })
119            });
120
121            Task::ready(recent_matches.chain(file_matches).collect())
122        } else {
123            let worktrees = workspace.read(cx).visible_worktrees(cx).collect::<Vec<_>>();
124            let candidate_sets = worktrees
125                .into_iter()
126                .map(|worktree| {
127                    let worktree = worktree.read(cx);
128
129                    PathMatchCandidateSet {
130                        snapshot: worktree.snapshot(),
131                        include_ignored: worktree
132                            .root_entry()
133                            .map_or(false, |entry| entry.is_ignored),
134                        include_root_name: true,
135                        candidates: project::Candidates::Files,
136                    }
137                })
138                .collect::<Vec<_>>();
139
140            let executor = cx.background_executor().clone();
141            cx.foreground_executor().spawn(async move {
142                fuzzy::match_path_sets(
143                    candidate_sets.as_slice(),
144                    query.as_str(),
145                    None,
146                    false,
147                    100,
148                    &cancellation_flag,
149                    executor,
150                )
151                .await
152            })
153        }
154    }
155}
156
157impl PickerDelegate for FileContextPickerDelegate {
158    type ListItem = ListItem;
159
160    fn match_count(&self) -> usize {
161        self.matches.len()
162    }
163
164    fn selected_index(&self) -> usize {
165        self.selected_index
166    }
167
168    fn set_selected_index(&mut self, ix: usize, _cx: &mut ViewContext<Picker<Self>>) {
169        self.selected_index = ix;
170    }
171
172    fn placeholder_text(&self, _cx: &mut WindowContext) -> Arc<str> {
173        "Search files…".into()
174    }
175
176    fn update_matches(&mut self, query: String, cx: &mut ViewContext<Picker<Self>>) -> Task<()> {
177        let Some(workspace) = self.workspace.upgrade() else {
178            return Task::ready(());
179        };
180
181        let search_task = self.search(query, Arc::<AtomicBool>::default(), &workspace, cx);
182
183        cx.spawn(|this, mut cx| async move {
184            // TODO: This should be probably be run in the background.
185            let paths = search_task.await;
186
187            this.update(&mut cx, |this, _cx| {
188                this.delegate.matches = paths;
189            })
190            .log_err();
191        })
192    }
193
194    fn confirm(&mut self, _secondary: bool, cx: &mut ViewContext<Picker<Self>>) {
195        let Some(mat) = self.matches.get(self.selected_index) else {
196            return;
197        };
198
199        let workspace = self.workspace.clone();
200        let Some(project) = workspace
201            .upgrade()
202            .map(|workspace| workspace.read(cx).project().clone())
203        else {
204            return;
205        };
206        let path = mat.path.clone();
207        let worktree_id = WorktreeId::from_usize(mat.worktree_id);
208        let confirm_behavior = self.confirm_behavior;
209        cx.spawn(|this, mut cx| async move {
210            let Some(open_buffer_task) = project
211                .update(&mut cx, |project, cx| {
212                    project.open_buffer((worktree_id, path.clone()), cx)
213                })
214                .ok()
215            else {
216                return anyhow::Ok(());
217            };
218
219            let buffer = open_buffer_task.await?;
220
221            this.update(&mut cx, |this, cx| {
222                this.delegate
223                    .context_store
224                    .update(cx, |context_store, cx| {
225                        let mut text = String::new();
226                        text.push_str(&codeblock_fence_for_path(Some(&path), None));
227                        text.push_str(&buffer.read(cx).text());
228                        if !text.ends_with('\n') {
229                            text.push('\n');
230                        }
231
232                        text.push_str("```\n");
233
234                        context_store.insert_context(
235                            ContextKind::File,
236                            path.to_string_lossy().to_string(),
237                            text,
238                        );
239                    })?;
240
241                match confirm_behavior {
242                    ConfirmBehavior::KeepOpen => {}
243                    ConfirmBehavior::Close => this.delegate.dismissed(cx),
244                }
245
246                anyhow::Ok(())
247            })??;
248
249            anyhow::Ok(())
250        })
251        .detach_and_log_err(cx);
252    }
253
254    fn dismissed(&mut self, cx: &mut ViewContext<Picker<Self>>) {
255        self.context_picker
256            .update(cx, |this, cx| {
257                this.reset_mode();
258                cx.emit(DismissEvent);
259            })
260            .ok();
261    }
262
263    fn render_match(
264        &self,
265        ix: usize,
266        selected: bool,
267        _cx: &mut ViewContext<Picker<Self>>,
268    ) -> Option<Self::ListItem> {
269        let path_match = &self.matches[ix];
270
271        let (file_name, directory) = if path_match.path.as_ref() == Path::new("") {
272            (SharedString::from(path_match.path_prefix.clone()), None)
273        } else {
274            let file_name = path_match
275                .path
276                .file_name()
277                .unwrap_or_default()
278                .to_string_lossy()
279                .to_string()
280                .into();
281
282            let mut directory = format!("{}/", path_match.path_prefix);
283            if let Some(parent) = path_match
284                .path
285                .parent()
286                .filter(|parent| parent != &Path::new(""))
287            {
288                directory.push_str(&parent.to_string_lossy());
289                directory.push('/');
290            }
291
292            (file_name, Some(directory))
293        };
294
295        Some(
296            ListItem::new(ix).inset(true).toggle_state(selected).child(
297                h_flex()
298                    .gap_2()
299                    .child(Label::new(file_name))
300                    .children(directory.map(|directory| {
301                        Label::new(directory)
302                            .size(LabelSize::Small)
303                            .color(Color::Muted)
304                    })),
305            ),
306        )
307    }
308}
309
310fn codeblock_fence_for_path(path: Option<&Path>, row_range: Option<RangeInclusive<u32>>) -> String {
311    let mut text = String::new();
312    write!(text, "```").unwrap();
313
314    if let Some(path) = path {
315        if let Some(extension) = path.extension().and_then(|ext| ext.to_str()) {
316            write!(text, "{} ", extension).unwrap();
317        }
318
319        write!(text, "{}", path.display()).unwrap();
320    } else {
321        write!(text, "untitled").unwrap();
322    }
323
324    if let Some(row_range) = row_range {
325        write!(text, ":{}-{}", row_range.start() + 1, row_range.end() + 1).unwrap();
326    }
327
328    text.push('\n');
329    text
330}