Cycle file finder entries on cmd-p

This commit is contained in:
Kirill Bulatov 2023-09-27 23:02:31 +02:00
parent 81a107f503
commit 541dd994a9

View file

@ -81,66 +81,82 @@ impl FoundPath {
actions!(file_finder, [Toggle]); actions!(file_finder, [Toggle]);
pub fn init(cx: &mut AppContext) { pub fn init(cx: &mut AppContext) {
cx.add_action(toggle_file_finder); cx.add_action(toggle_or_cycle_file_finder);
FileFinder::init(cx); FileFinder::init(cx);
} }
const MAX_RECENT_SELECTIONS: usize = 20; const MAX_RECENT_SELECTIONS: usize = 20;
fn toggle_file_finder(workspace: &mut Workspace, _: &Toggle, cx: &mut ViewContext<Workspace>) { fn toggle_or_cycle_file_finder(
workspace.toggle_modal(cx, |workspace, cx| { workspace: &mut Workspace,
let project = workspace.project().read(cx); _: &Toggle,
cx: &mut ViewContext<Workspace>,
) {
match workspace.modal::<FileFinder>() {
Some(file_finder) => file_finder.update(cx, |file_finder, cx| {
let current_index = file_finder.delegate().selected_index();
file_finder.select_next(&menu::SelectNext, cx);
let new_index = file_finder.delegate().selected_index();
if current_index == new_index {
file_finder.select_first(&menu::SelectFirst, cx);
}
}),
None => {
workspace.toggle_modal(cx, |workspace, cx| {
let project = workspace.project().read(cx);
let currently_opened_path = workspace let currently_opened_path = workspace
.active_item(cx) .active_item(cx)
.and_then(|item| item.project_path(cx)) .and_then(|item| item.project_path(cx))
.map(|project_path| { .map(|project_path| {
let abs_path = project let abs_path = project
.worktree_for_id(project_path.worktree_id, cx) .worktree_for_id(project_path.worktree_id, cx)
.map(|worktree| worktree.read(cx).abs_path().join(&project_path.path)); .map(|worktree| worktree.read(cx).abs_path().join(&project_path.path));
FoundPath::new(project_path, abs_path) FoundPath::new(project_path, abs_path)
}); });
// if exists, bubble the currently opened path to the top // if exists, bubble the currently opened path to the top
let history_items = currently_opened_path let history_items = currently_opened_path
.clone() .clone()
.into_iter()
.chain(
workspace
.recent_navigation_history(Some(MAX_RECENT_SELECTIONS), cx)
.into_iter() .into_iter()
.filter(|(history_path, _)| { .chain(
Some(history_path) workspace
!= currently_opened_path .recent_navigation_history(Some(MAX_RECENT_SELECTIONS), cx)
.as_ref() .into_iter()
.map(|found_path| &found_path.project) .filter(|(history_path, _)| {
}) Some(history_path)
.filter(|(_, history_abs_path)| { != currently_opened_path
history_abs_path.as_ref() .as_ref()
!= currently_opened_path .map(|found_path| &found_path.project)
.as_ref() })
.and_then(|found_path| found_path.absolute.as_ref()) .filter(|(_, history_abs_path)| {
}) history_abs_path.as_ref()
.map(|(history_path, abs_path)| FoundPath::new(history_path, abs_path)), != currently_opened_path
) .as_ref()
.collect(); .and_then(|found_path| found_path.absolute.as_ref())
})
.map(|(history_path, abs_path)| FoundPath::new(history_path, abs_path)),
)
.collect();
let project = workspace.project().clone(); let project = workspace.project().clone();
let workspace = cx.handle().downgrade(); let workspace = cx.handle().downgrade();
let finder = cx.add_view(|cx| { let finder = cx.add_view(|cx| {
Picker::new( Picker::new(
FileFinderDelegate::new( FileFinderDelegate::new(
workspace, workspace,
project, project,
currently_opened_path, currently_opened_path,
history_items, history_items,
cx, cx,
), ),
cx, cx,
) )
}); });
finder finder
}); });
}
}
} }
pub enum Event { pub enum Event {