Simplify selected index handling

This commit is contained in:
Kirill Bulatov 2023-05-26 14:15:58 +03:00
parent 364631a155
commit b75c27da6f

View file

@ -6,7 +6,7 @@ use gpui::{
use picker::{Picker, PickerDelegate}; use picker::{Picker, PickerDelegate};
use project::{PathMatchCandidateSet, Project, ProjectPath, WorktreeId}; use project::{PathMatchCandidateSet, Project, ProjectPath, WorktreeId};
use std::{ use std::{
path::{Path, PathBuf}, path::PathBuf,
sync::{ sync::{
atomic::{self, AtomicBool}, atomic::{self, AtomicBool},
Arc, Arc,
@ -27,7 +27,7 @@ pub struct FileFinderDelegate {
latest_search_query: Option<PathLikeWithPosition<FileSearchQuery>>, latest_search_query: Option<PathLikeWithPosition<FileSearchQuery>>,
currently_opened_path: Option<FoundPath>, currently_opened_path: Option<FoundPath>,
matches: Vec<PathMatch>, matches: Vec<PathMatch>,
selected: Option<(usize, Arc<Path>)>, selected_index: Option<usize>,
cancel_flag: Arc<AtomicBool>, cancel_flag: Arc<AtomicBool>,
history_items: Vec<FoundPath>, history_items: Vec<FoundPath>,
} }
@ -165,7 +165,7 @@ impl FileFinderDelegate {
latest_search_query: None, latest_search_query: None,
currently_opened_path, currently_opened_path,
matches: Vec::new(), matches: Vec::new(),
selected: None, selected_index: None,
cancel_flag: Arc::new(AtomicBool::new(false)), cancel_flag: Arc::new(AtomicBool::new(false)),
history_items, history_items,
} }
@ -264,21 +264,11 @@ impl PickerDelegate for FileFinderDelegate {
} }
fn selected_index(&self) -> usize { fn selected_index(&self) -> usize {
if let Some(selected) = self.selected.as_ref() { self.selected_index.unwrap_or(0)
for (ix, path_match) in self.matches.iter().enumerate() {
if (path_match.worktree_id, path_match.path.as_ref())
== (selected.0, selected.1.as_ref())
{
return ix;
}
}
}
0
} }
fn set_selected_index(&mut self, ix: usize, cx: &mut ViewContext<FileFinder>) { fn set_selected_index(&mut self, ix: usize, cx: &mut ViewContext<FileFinder>) {
let mat = &self.matches[ix]; self.selected_index = Some(ix);
self.selected = Some((mat.worktree_id, mat.path.clone()));
cx.notify(); cx.notify();
} }
@ -418,7 +408,7 @@ impl PickerDelegate for FileFinderDelegate {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::{assert_eq, collections::HashMap, time::Duration}; use std::{assert_eq, collections::HashMap, path::Path, time::Duration};
use super::*; use super::*;
use editor::Editor; use editor::Editor;