use std::path::Path; use std::sync::atomic::AtomicBool; use std::sync::Arc; use fuzzy::PathMatch; use gpui::{AppContext, DismissEvent, FocusHandle, FocusableView, Task, View, WeakModel, WeakView}; use picker::{Picker, PickerDelegate}; use project::{PathMatchCandidateSet, ProjectPath, WorktreeId}; use ui::{prelude::*, ListItem, Tooltip}; use util::ResultExt as _; use workspace::Workspace; use crate::context_picker::{ConfirmBehavior, ContextPicker}; use crate::context_store::{ContextStore, IncludedFile}; pub struct FileContextPicker { picker: View>, } impl FileContextPicker { pub fn new( context_picker: WeakView, workspace: WeakView, context_store: WeakModel, confirm_behavior: ConfirmBehavior, cx: &mut ViewContext, ) -> Self { let delegate = FileContextPickerDelegate::new( context_picker, workspace, context_store, confirm_behavior, ); let picker = cx.new_view(|cx| Picker::uniform_list(delegate, cx)); Self { picker } } } impl FocusableView for FileContextPicker { fn focus_handle(&self, cx: &AppContext) -> FocusHandle { self.picker.focus_handle(cx) } } impl Render for FileContextPicker { fn render(&mut self, _cx: &mut ViewContext) -> impl IntoElement { self.picker.clone() } } pub struct FileContextPickerDelegate { context_picker: WeakView, workspace: WeakView, context_store: WeakModel, confirm_behavior: ConfirmBehavior, matches: Vec, selected_index: usize, } impl FileContextPickerDelegate { pub fn new( context_picker: WeakView, workspace: WeakView, context_store: WeakModel, confirm_behavior: ConfirmBehavior, ) -> Self { Self { context_picker, workspace, context_store, confirm_behavior, matches: Vec::new(), selected_index: 0, } } fn search( &mut self, query: String, cancellation_flag: Arc, workspace: &View, cx: &mut ViewContext>, ) -> Task> { if query.is_empty() { let workspace = workspace.read(cx); let project = workspace.project().read(cx); let recent_matches = workspace .recent_navigation_history(Some(10), cx) .into_iter() .filter_map(|(project_path, _)| { let worktree = project.worktree_for_id(project_path.worktree_id, cx)?; Some(PathMatch { score: 0., positions: Vec::new(), worktree_id: project_path.worktree_id.to_usize(), path: project_path.path, path_prefix: worktree.read(cx).root_name().into(), distance_to_relative_ancestor: 0, is_dir: false, }) }); let file_matches = project.worktrees(cx).flat_map(|worktree| { let worktree = worktree.read(cx); let path_prefix: Arc = worktree.root_name().into(); worktree.files(true, 0).map(move |entry| PathMatch { score: 0., positions: Vec::new(), worktree_id: worktree.id().to_usize(), path: entry.path.clone(), path_prefix: path_prefix.clone(), distance_to_relative_ancestor: 0, is_dir: false, }) }); Task::ready(recent_matches.chain(file_matches).collect()) } else { let worktrees = workspace.read(cx).visible_worktrees(cx).collect::>(); let candidate_sets = worktrees .into_iter() .map(|worktree| { let worktree = worktree.read(cx); PathMatchCandidateSet { snapshot: worktree.snapshot(), include_ignored: worktree .root_entry() .map_or(false, |entry| entry.is_ignored), include_root_name: true, candidates: project::Candidates::Files, } }) .collect::>(); let executor = cx.background_executor().clone(); cx.foreground_executor().spawn(async move { fuzzy::match_path_sets( candidate_sets.as_slice(), query.as_str(), None, false, 100, &cancellation_flag, executor, ) .await }) } } } impl PickerDelegate for FileContextPickerDelegate { type ListItem = ListItem; fn match_count(&self) -> usize { self.matches.len() } fn selected_index(&self) -> usize { self.selected_index } fn set_selected_index(&mut self, ix: usize, _cx: &mut ViewContext>) { self.selected_index = ix; } fn placeholder_text(&self, _cx: &mut WindowContext) -> Arc { "Search files…".into() } fn update_matches(&mut self, query: String, cx: &mut ViewContext>) -> Task<()> { let Some(workspace) = self.workspace.upgrade() else { return Task::ready(()); }; let search_task = self.search(query, Arc::::default(), &workspace, cx); cx.spawn(|this, mut cx| async move { // TODO: This should be probably be run in the background. let paths = search_task.await; this.update(&mut cx, |this, _cx| { this.delegate.matches = paths; }) .log_err(); }) } fn confirm(&mut self, _secondary: bool, cx: &mut ViewContext>) { let Some(mat) = self.matches.get(self.selected_index) else { return; }; let workspace = self.workspace.clone(); let Some(project) = workspace .upgrade() .map(|workspace| workspace.read(cx).project().clone()) else { return; }; let path = mat.path.clone(); if self .context_store .update(cx, |context_store, _cx| { match context_store.included_file(&path) { Some(IncludedFile::Direct(context_id)) => { context_store.remove_context(&context_id); true } Some(IncludedFile::InDirectory(_)) => true, None => false, } }) .unwrap_or(true) { return; } let worktree_id = WorktreeId::from_usize(mat.worktree_id); let confirm_behavior = self.confirm_behavior; cx.spawn(|this, mut cx| async move { let Some(open_buffer_task) = project .update(&mut cx, |project, cx| { let project_path = ProjectPath { worktree_id, path: path.clone(), }; let task = project.open_buffer(project_path, cx); Some(task) }) .ok() .flatten() else { return anyhow::Ok(()); }; let buffer = open_buffer_task.await?; this.update(&mut cx, |this, cx| { this.delegate .context_store .update(cx, |context_store, cx| { context_store.insert_file(buffer.read(cx)); })?; match confirm_behavior { ConfirmBehavior::KeepOpen => {} ConfirmBehavior::Close => this.delegate.dismissed(cx), } anyhow::Ok(()) })??; anyhow::Ok(()) }) .detach_and_log_err(cx); } fn dismissed(&mut self, cx: &mut ViewContext>) { self.context_picker .update(cx, |this, cx| { this.reset_mode(); cx.emit(DismissEvent); }) .ok(); } fn render_match( &self, ix: usize, selected: bool, cx: &mut ViewContext>, ) -> Option { let path_match = &self.matches[ix]; let (file_name, directory) = if path_match.path.as_ref() == Path::new("") { (SharedString::from(path_match.path_prefix.clone()), None) } else { let file_name = path_match .path .file_name() .unwrap_or_default() .to_string_lossy() .to_string() .into(); let mut directory = format!("{}/", path_match.path_prefix); if let Some(parent) = path_match .path .parent() .filter(|parent| parent != &Path::new("")) { directory.push_str(&parent.to_string_lossy()); directory.push('/'); } (file_name, Some(directory)) }; let added = self .context_store .upgrade() .and_then(|context_store| context_store.read(cx).included_file(&path_match.path)); Some( ListItem::new(ix) .inset(true) .toggle_state(selected) .child( h_flex() .gap_2() .child(Label::new(file_name)) .children(directory.map(|directory| { Label::new(directory) .size(LabelSize::Small) .color(Color::Muted) })), ) .when_some(added, |el, added| match added { IncludedFile::Direct(_) => { el.end_slot(Label::new("Added").size(LabelSize::XSmall)) } IncludedFile::InDirectory(dir_name) => { let dir_name = dir_name.to_string_lossy().into_owned(); el.end_slot(Label::new("Included").size(LabelSize::XSmall)) .tooltip(move |cx| Tooltip::text(format!("in {dir_name}"), cx)) } }), ) } }