WIP on rebuilding with extracted UI framework

This commit is contained in:
Nathan Sobo
2021-03-18 13:13:31 -06:00
parent 356bc41752
commit 23308e17a9
33 changed files with 2673 additions and 657 deletions
+44
View File
@@ -0,0 +1,44 @@
#[derive(Copy, Clone, Debug)]
pub struct CharBag(u64);
impl CharBag {
pub fn is_superset(self, other: CharBag) -> bool {
self.0 & other.0 == other.0
}
fn insert(&mut self, c: char) {
if c >= 'a' && c <= 'z' {
let mut count = self.0;
let idx = c as u8 - 'a' as u8;
count = count >> (idx * 2);
count = ((count << 1) | 1) & 3;
count = count << idx * 2;
self.0 |= count;
} else if c >= '0' && c <= '9' {
let idx = c as u8 - '0' as u8;
self.0 |= 1 << (idx + 52);
} else if c == '-' {
self.0 |= 1 << 62;
}
}
}
impl From<&str> for CharBag {
fn from(s: &str) -> Self {
let mut bag = Self(0);
for c in s.chars() {
bag.insert(c);
}
bag
}
}
impl From<&[char]> for CharBag {
fn from(chars: &[char]) -> Self {
let mut bag = Self(0);
for c in chars {
bag.insert(*c);
}
bag
}
}
+494
View File
@@ -0,0 +1,494 @@
use easy_parallel::Parallel;
use super::char_bag::CharBag;
use std::{
cmp::{max, min, Ordering, Reverse},
collections::BinaryHeap,
};
const BASE_DISTANCE_PENALTY: f64 = 0.6;
const ADDITIONAL_DISTANCE_PENALTY: f64 = 0.05;
const MIN_DISTANCE_PENALTY: f64 = 0.2;
pub struct PathEntry {
pub entry_id: usize,
pub path_chars: CharBag,
pub path: Vec<char>,
pub lowercase_path: Vec<char>,
pub is_ignored: bool,
}
#[derive(Clone, Debug)]
pub struct PathMatch {
pub score: f64,
pub positions: Vec<usize>,
pub tree_id: usize,
pub entry_id: usize,
pub skipped_prefix_len: usize,
}
impl PartialEq for PathMatch {
fn eq(&self, other: &Self) -> bool {
self.score.eq(&other.score)
}
}
impl Eq for PathMatch {}
impl PartialOrd for PathMatch {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.score.partial_cmp(&other.score)
}
}
impl Ord for PathMatch {
fn cmp(&self, other: &Self) -> Ordering {
self.partial_cmp(other).unwrap_or(Ordering::Equal)
}
}
pub fn match_paths(
paths_by_tree_id: &[(usize, usize, &[PathEntry])],
query: &str,
include_ignored: bool,
smart_case: bool,
max_results: usize,
) -> Vec<PathMatch> {
let lowercase_query = query.to_lowercase().chars().collect::<Vec<_>>();
let query = query.chars().collect::<Vec<_>>();
let lowercase_query = &lowercase_query;
let query = &query;
let query_chars = CharBag::from(&lowercase_query[..]);
let cpus = num_cpus::get();
let path_count = paths_by_tree_id
.iter()
.fold(0, |sum, (_, _, paths)| sum + paths.len());
let segment_size = (path_count + cpus - 1) / cpus;
let mut segment_results = (0..cpus).map(|_| BinaryHeap::new()).collect::<Vec<_>>();
Parallel::new().each(
segment_results.iter_mut().enumerate(),
|(segment_idx, results)| {
let segment_start = segment_idx * segment_size;
let segment_end = segment_start + segment_size;
let mut min_score = 0.0;
let mut last_positions = Vec::new();
last_positions.resize(query.len(), 0);
let mut match_positions = Vec::new();
match_positions.resize(query.len(), 0);
let mut score_matrix = Vec::new();
let mut best_position_matrix = Vec::new();
let mut tree_start = 0;
for (tree_id, skipped_prefix_len, paths) in paths_by_tree_id {
let tree_end = tree_start + paths.len();
if tree_start < segment_end && segment_start < tree_end {
let start = max(tree_start, segment_start) - tree_start;
let end = min(tree_end, segment_end) - tree_start;
match_single_tree_paths(
*tree_id,
*skipped_prefix_len,
paths,
start,
end,
query,
lowercase_query,
query_chars,
include_ignored,
smart_case,
results,
max_results,
&mut min_score,
&mut match_positions,
&mut last_positions,
&mut score_matrix,
&mut best_position_matrix,
);
}
if tree_end >= segment_end {
break;
}
tree_start = tree_end;
}
},
);
let mut results = segment_results
.into_iter()
.flatten()
.map(|r| r.0)
.collect::<Vec<_>>();
results.sort_unstable_by(|a, b| b.score.partial_cmp(&a.score).unwrap());
results.truncate(max_results);
results
}
fn match_single_tree_paths(
tree_id: usize,
skipped_prefix_len: usize,
path_entries: &[PathEntry],
start: usize,
end: usize,
query: &[char],
lowercase_query: &[char],
query_chars: CharBag,
include_ignored: bool,
smart_case: bool,
results: &mut BinaryHeap<Reverse<PathMatch>>,
max_results: usize,
min_score: &mut f64,
match_positions: &mut Vec<usize>,
last_positions: &mut Vec<usize>,
score_matrix: &mut Vec<Option<f64>>,
best_position_matrix: &mut Vec<usize>,
) {
for i in start..end {
let path_entry = unsafe { &path_entries.get_unchecked(i) };
if !include_ignored && path_entry.is_ignored {
continue;
}
if !path_entry.path_chars.is_superset(query_chars) {
continue;
}
if !find_last_positions(
last_positions,
skipped_prefix_len,
&path_entry.lowercase_path,
&lowercase_query[..],
) {
continue;
}
let matrix_len = query.len() * (path_entry.path.len() - skipped_prefix_len);
score_matrix.clear();
score_matrix.resize(matrix_len, None);
best_position_matrix.clear();
best_position_matrix.resize(matrix_len, skipped_prefix_len);
let score = score_match(
&query[..],
&lowercase_query[..],
&path_entry.path,
&path_entry.lowercase_path,
skipped_prefix_len,
smart_case,
&last_positions,
score_matrix,
best_position_matrix,
match_positions,
*min_score,
);
if score > 0.0 {
results.push(Reverse(PathMatch {
tree_id,
entry_id: path_entry.entry_id,
score,
positions: match_positions.clone(),
skipped_prefix_len,
}));
if results.len() == max_results {
*min_score = results.peek().unwrap().0.score;
}
}
}
}
fn find_last_positions(
last_positions: &mut Vec<usize>,
skipped_prefix_len: usize,
path: &[char],
query: &[char],
) -> bool {
let mut path = path.iter();
for (i, char) in query.iter().enumerate().rev() {
if let Some(j) = path.rposition(|c| c == char) {
if j >= skipped_prefix_len {
last_positions[i] = j;
} else {
return false;
}
} else {
return false;
}
}
true
}
fn score_match(
query: &[char],
query_cased: &[char],
path: &[char],
path_cased: &[char],
skipped_prefix_len: usize,
smart_case: bool,
last_positions: &[usize],
score_matrix: &mut [Option<f64>],
best_position_matrix: &mut [usize],
match_positions: &mut [usize],
min_score: f64,
) -> f64 {
let score = recursive_score_match(
query,
query_cased,
path,
path_cased,
skipped_prefix_len,
smart_case,
last_positions,
score_matrix,
best_position_matrix,
min_score,
0,
skipped_prefix_len,
query.len() as f64,
) * query.len() as f64;
if score <= 0.0 {
return 0.0;
}
let path_len = path.len() - skipped_prefix_len;
let mut cur_start = 0;
for i in 0..query.len() {
match_positions[i] = best_position_matrix[i * path_len + cur_start] - skipped_prefix_len;
cur_start = match_positions[i] + 1;
}
score
}
fn recursive_score_match(
query: &[char],
query_cased: &[char],
path: &[char],
path_cased: &[char],
skipped_prefix_len: usize,
smart_case: bool,
last_positions: &[usize],
score_matrix: &mut [Option<f64>],
best_position_matrix: &mut [usize],
min_score: f64,
query_idx: usize,
path_idx: usize,
cur_score: f64,
) -> f64 {
if query_idx == query.len() {
return 1.0;
}
let path_len = path.len() - skipped_prefix_len;
if let Some(memoized) = score_matrix[query_idx * path_len + path_idx - skipped_prefix_len] {
return memoized;
}
let mut score = 0.0;
let mut best_position = 0;
let query_char = query_cased[query_idx];
let limit = last_positions[query_idx];
let mut last_slash = 0;
for j in path_idx..=limit {
let path_char = path_cased[j];
let is_path_sep = path_char == '/' || path_char == '\\';
if query_idx == 0 && is_path_sep {
last_slash = j;
}
if query_char == path_char || (is_path_sep && query_char == '_' || query_char == '\\') {
let mut char_score = 1.0;
if j > path_idx {
let last = path[j - 1];
let curr = path[j];
if last == '/' {
char_score = 0.9;
} else if last == '-' || last == '_' || last == ' ' || last.is_numeric() {
char_score = 0.8;
} else if last.is_lowercase() && curr.is_uppercase() {
char_score = 0.8;
} else if last == '.' {
char_score = 0.7;
} else if query_idx == 0 {
char_score = BASE_DISTANCE_PENALTY;
} else {
char_score = MIN_DISTANCE_PENALTY.max(
BASE_DISTANCE_PENALTY
- (j - path_idx - 1) as f64 * ADDITIONAL_DISTANCE_PENALTY,
);
}
}
// Apply a severe penalty if the case doesn't match.
// This will make the exact matches have higher score than the case-insensitive and the
// path insensitive matches.
if (smart_case || path[j] == '/') && query[query_idx] != path[j] {
char_score *= 0.001;
}
let mut multiplier = char_score;
// Scale the score based on how deep within the patch we found the match.
if query_idx == 0 {
multiplier /= (path.len() - last_slash) as f64;
}
let mut next_score = 1.0;
if min_score > 0.0 {
next_score = cur_score * multiplier;
// Scores only decrease. If we can't pass the previous best, bail
if next_score < min_score {
// Ensure that score is non-zero so we use it in the memo table.
if score == 0.0 {
score = 1e-18;
}
continue;
}
}
let new_score = recursive_score_match(
query,
query_cased,
path,
path_cased,
skipped_prefix_len,
smart_case,
last_positions,
score_matrix,
best_position_matrix,
min_score,
query_idx + 1,
j + 1,
next_score,
) * multiplier;
if new_score > score {
score = new_score;
best_position = j;
// Optimization: can't score better than 1.
if new_score == 1.0 {
break;
}
}
}
}
if best_position != 0 {
best_position_matrix[query_idx * path_len + path_idx - skipped_prefix_len] = best_position;
}
score_matrix[query_idx * path_len + path_idx - skipped_prefix_len] = Some(score);
score
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_match_path_entries() {
let paths = vec![
"",
"a",
"ab",
"abC",
"abcd",
"alphabravocharlie",
"AlphaBravoCharlie",
"thisisatestdir",
"/////ThisIsATestDir",
"/this/is/a/test/dir",
"/test/tiatd",
];
assert_eq!(
match_query("abc", false, &paths),
vec![
("abC", vec![0, 1, 2]),
("abcd", vec![0, 1, 2]),
("AlphaBravoCharlie", vec![0, 5, 10]),
("alphabravocharlie", vec![4, 5, 10]),
]
);
assert_eq!(
match_query("t/i/a/t/d", false, &paths),
vec![("/this/is/a/test/dir", vec![1, 5, 6, 8, 9, 10, 11, 15, 16]),]
);
assert_eq!(
match_query("tiatd", false, &paths),
vec![
("/test/tiatd", vec![6, 7, 8, 9, 10]),
("/this/is/a/test/dir", vec![1, 6, 9, 11, 16]),
("/////ThisIsATestDir", vec![5, 9, 11, 12, 16]),
("thisisatestdir", vec![0, 2, 6, 7, 11]),
]
);
}
fn match_query<'a>(
query: &str,
smart_case: bool,
paths: &Vec<&'a str>,
) -> Vec<(&'a str, Vec<usize>)> {
let lowercase_query = query.to_lowercase().chars().collect::<Vec<_>>();
let query = query.chars().collect::<Vec<_>>();
let query_chars = CharBag::from(&lowercase_query[..]);
let mut path_entries = Vec::new();
for (i, path) in paths.iter().enumerate() {
let lowercase_path = path.to_lowercase().chars().collect::<Vec<_>>();
let path_chars = CharBag::from(&lowercase_path[..]);
let path = path.chars().collect();
path_entries.push(PathEntry {
entry_id: i,
path_chars,
path,
lowercase_path,
is_ignored: false,
});
}
let mut match_positions = Vec::new();
let mut last_positions = Vec::new();
match_positions.resize(query.len(), 0);
last_positions.resize(query.len(), 0);
let mut results = BinaryHeap::new();
match_single_tree_paths(
0,
0,
&path_entries,
0,
path_entries.len(),
&query[..],
&lowercase_query[..],
query_chars,
true,
smart_case,
&mut results,
100,
&mut 0.0,
&mut match_positions,
&mut last_positions,
&mut Vec::new(),
&mut Vec::new(),
);
results
.into_iter()
.rev()
.map(|result| (paths[result.0.entry_id].clone(), result.0.positions))
.collect()
}
}
+5
View File
@@ -0,0 +1,5 @@
mod char_bag;
mod fuzzy;
mod worktree;
pub use worktree::{match_paths, FileHandle, PathMatch, Worktree};
+651
View File
@@ -0,0 +1,651 @@
pub use super::fuzzy::PathMatch;
use super::{
char_bag::CharBag,
fuzzy::{self, PathEntry},
};
use crate::{editor::History, timer, util::post_inc};
use anyhow::{anyhow, Result};
use crossbeam_queue::ArrayQueue;
use easy_parallel::Parallel;
use gpui::{AppContext, Entity, ModelContext, ModelHandle};
use ignore::dir::{Ignore, IgnoreBuilder};
use parking_lot::RwLock;
use smol::prelude::*;
use std::{
collections::HashMap,
ffi::{OsStr, OsString},
fmt, fs, io,
os::unix::fs::MetadataExt,
path::Path,
path::PathBuf,
sync::Arc,
time::Duration,
};
#[derive(Clone)]
pub struct Worktree(Arc<RwLock<WorktreeState>>);
struct WorktreeState {
id: usize,
path: PathBuf,
entries: Vec<Entry>,
file_paths: Vec<PathEntry>,
histories: HashMap<usize, History>,
scanning: bool,
}
struct DirToScan {
id: usize,
path: PathBuf,
relative_path: PathBuf,
ignore: Option<Ignore>,
dirs_to_scan: Arc<ArrayQueue<io::Result<DirToScan>>>,
}
impl Worktree {
pub fn new<T>(id: usize, path: T, ctx: Option<&mut ModelContext<Self>>) -> Self
where
T: Into<PathBuf>,
{
let tree = Self(Arc::new(RwLock::new(WorktreeState {
id,
path: path.into(),
entries: Vec::new(),
file_paths: Vec::new(),
histories: HashMap::new(),
scanning: ctx.is_some(),
})));
if let Some(ctx) = ctx {
tree.0.write().scanning = true;
let tree = tree.clone();
let (tx, rx) = smol::channel::bounded(1);
std::thread::spawn(move || {
let _ = smol::block_on(tx.send(tree.scan_dirs()));
});
let _ = ctx.spawn(async move { rx.recv().await.unwrap() }, Self::done_scanning);
let _ = ctx.spawn_stream_local(
timer::repeat(Duration::from_millis(100)).map(|_| ()),
Self::scanning,
);
}
tree
}
fn scan_dirs(&self) -> io::Result<()> {
let path = self.0.read().path.clone();
let metadata = fs::metadata(&path)?;
let ino = metadata.ino();
let is_symlink = fs::symlink_metadata(&path)?.file_type().is_symlink();
let name = path
.file_name()
.map(|name| OsString::from(name))
.unwrap_or(OsString::from("/"));
let relative_path = PathBuf::from(&name);
let mut ignore = IgnoreBuilder::new().build().add_parents(&path).unwrap();
if metadata.is_dir() {
ignore = ignore.add_child(&path).unwrap();
}
let is_ignored = ignore.matched(&path, metadata.is_dir()).is_ignore();
if metadata.file_type().is_dir() {
let is_ignored = is_ignored || name == ".git";
let id = self.push_dir(None, name, ino, is_symlink, is_ignored);
let queue = Arc::new(ArrayQueue::new(1000));
queue.push(Ok(DirToScan {
id,
path,
relative_path,
ignore: Some(ignore),
dirs_to_scan: queue.clone(),
}));
Parallel::<io::Result<()>>::new()
.each(0..16, |_| {
while let Some(result) = queue.pop() {
self.scan_dir(result?)?;
}
Ok(())
})
.run()
.into_iter()
.collect::<io::Result<()>>()?;
} else {
self.push_file(None, name, ino, is_symlink, is_ignored, relative_path);
}
Ok(())
}
fn scan_dir(&self, to_scan: DirToScan) -> io::Result<()> {
let mut new_children = Vec::new();
for child_entry in fs::read_dir(&to_scan.path)? {
let child_entry = child_entry?;
let name = child_entry.file_name();
let relative_path = to_scan.relative_path.join(&name);
let metadata = child_entry.metadata()?;
let ino = metadata.ino();
let is_symlink = metadata.file_type().is_symlink();
if metadata.is_dir() {
let path = to_scan.path.join(&name);
let mut is_ignored = true;
let mut ignore = None;
if let Some(parent_ignore) = to_scan.ignore.as_ref() {
let child_ignore = parent_ignore.add_child(&path).unwrap();
is_ignored = child_ignore.matched(&path, true).is_ignore() || name == ".git";
if !is_ignored {
ignore = Some(child_ignore);
}
}
let id = self.push_dir(Some(to_scan.id), name, ino, is_symlink, is_ignored);
new_children.push(id);
let dirs_to_scan = to_scan.dirs_to_scan.clone();
let _ = to_scan.dirs_to_scan.push(Ok(DirToScan {
id,
path,
relative_path,
ignore,
dirs_to_scan,
}));
} else {
let is_ignored = to_scan.ignore.as_ref().map_or(true, |i| {
i.matched(to_scan.path.join(&name), false).is_ignore()
});
new_children.push(self.push_file(
Some(to_scan.id),
name,
ino,
is_symlink,
is_ignored,
relative_path,
));
};
}
if let Entry::Dir { children, .. } = &mut self.0.write().entries[to_scan.id] {
*children = new_children.clone();
}
Ok(())
}
fn push_dir(
&self,
parent: Option<usize>,
name: OsString,
ino: u64,
is_symlink: bool,
is_ignored: bool,
) -> usize {
let entries = &mut self.0.write().entries;
let dir_id = entries.len();
entries.push(Entry::Dir {
parent,
name,
ino,
is_symlink,
is_ignored,
children: Vec::new(),
});
dir_id
}
fn push_file(
&self,
parent: Option<usize>,
name: OsString,
ino: u64,
is_symlink: bool,
is_ignored: bool,
path: PathBuf,
) -> usize {
let path = path.to_string_lossy();
let lowercase_path = path.to_lowercase().chars().collect::<Vec<_>>();
let path = path.chars().collect::<Vec<_>>();
let path_chars = CharBag::from(&path[..]);
let mut state = self.0.write();
let entry_id = state.entries.len();
state.entries.push(Entry::File {
parent,
name,
ino,
is_symlink,
is_ignored,
});
state.file_paths.push(PathEntry {
entry_id,
path_chars,
path,
lowercase_path,
is_ignored,
});
entry_id
}
pub fn entry_path(&self, mut entry_id: usize) -> Result<PathBuf> {
let state = self.0.read();
if entry_id >= state.entries.len() {
return Err(anyhow!("Entry does not exist in tree"));
}
let mut entries = Vec::new();
loop {
let entry = &state.entries[entry_id];
entries.push(entry);
if let Some(parent_id) = entry.parent() {
entry_id = parent_id;
} else {
break;
}
}
let mut path = PathBuf::new();
for entry in entries.into_iter().rev() {
path.push(entry.name());
}
Ok(path)
}
pub fn abs_entry_path(&self, entry_id: usize) -> Result<PathBuf> {
let mut path = self.0.read().path.clone();
path.pop();
Ok(path.join(self.entry_path(entry_id)?))
}
fn fmt_entry(&self, f: &mut fmt::Formatter<'_>, entry_id: usize, indent: usize) -> fmt::Result {
match &self.0.read().entries[entry_id] {
Entry::Dir { name, children, .. } => {
write!(
f,
"{}{}/ ({})\n",
" ".repeat(indent),
name.to_string_lossy(),
entry_id
)?;
for child_id in children.iter() {
self.fmt_entry(f, *child_id, indent + 2)?;
}
Ok(())
}
Entry::File { name, .. } => write!(
f,
"{}{} ({})\n",
" ".repeat(indent),
name.to_string_lossy(),
entry_id
),
}
}
pub fn path(&self) -> PathBuf {
PathBuf::from(&self.0.read().path)
}
pub fn contains_path(&self, path: &Path) -> bool {
path.starts_with(self.path())
}
pub fn iter(&self) -> Iter {
Iter {
tree: self.clone(),
stack: Vec::new(),
started: false,
}
}
pub fn files(&self) -> FilesIter {
FilesIter {
iter: self.iter(),
path: PathBuf::new(),
}
}
pub fn entry_count(&self) -> usize {
self.0.read().entries.len()
}
pub fn file_count(&self) -> usize {
self.0.read().file_paths.len()
}
pub fn load_history(&self, entry_id: usize) -> impl Future<Output = Result<History>> {
let tree = self.clone();
async move {
if let Some(history) = tree.0.read().histories.get(&entry_id) {
return Ok(history.clone());
}
let path = tree.abs_entry_path(entry_id)?;
let mut file = smol::fs::File::open(&path).await?;
let mut base_text = String::new();
file.read_to_string(&mut base_text).await?;
let history = History { base_text };
tree.0.write().histories.insert(entry_id, history.clone());
Ok(history)
}
}
fn scanning(&mut self, _: Option<()>, ctx: &mut ModelContext<Self>) {
if self.0.read().scanning {
ctx.notify();
} else {
ctx.halt_stream();
}
}
fn done_scanning(&mut self, result: io::Result<()>, ctx: &mut ModelContext<Self>) {
self.0.write().scanning = false;
if let Err(error) = result {
log::error!("error populating worktree: {}", error);
} else {
ctx.notify();
}
}
}
impl fmt::Debug for Worktree {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.entry_count() == 0 {
write!(f, "Empty tree\n")
} else {
self.fmt_entry(f, 0, 0)
}
}
}
impl Entity for Worktree {
type Event = ();
}
pub trait WorktreeHandle {
fn file(&self, entry_id: usize, app: &AppContext) -> Result<FileHandle>;
}
impl WorktreeHandle for ModelHandle<Worktree> {
fn file(&self, entry_id: usize, app: &AppContext) -> Result<FileHandle> {
if entry_id >= self.as_ref(app).entry_count() {
return Err(anyhow!("Entry does not exist in tree"));
}
Ok(FileHandle {
worktree: self.clone(),
entry_id,
})
}
}
#[derive(Clone, Debug)]
pub enum Entry {
Dir {
parent: Option<usize>,
name: OsString,
ino: u64,
is_symlink: bool,
is_ignored: bool,
children: Vec<usize>,
},
File {
parent: Option<usize>,
name: OsString,
ino: u64,
is_symlink: bool,
is_ignored: bool,
},
}
impl Entry {
fn parent(&self) -> Option<usize> {
match self {
Entry::Dir { parent, .. } | Entry::File { parent, .. } => *parent,
}
}
fn name(&self) -> &OsStr {
match self {
Entry::Dir { name, .. } | Entry::File { name, .. } => name,
}
}
}
#[derive(Clone)]
pub struct FileHandle {
worktree: ModelHandle<Worktree>,
entry_id: usize,
}
impl FileHandle {
pub fn path(&self, app: &AppContext) -> PathBuf {
self.worktree.as_ref(app).entry_path(self.entry_id).unwrap()
}
pub fn load_history(&self, app: &AppContext) -> impl Future<Output = Result<History>> {
self.worktree.as_ref(app).load_history(self.entry_id)
}
pub fn entry_id(&self) -> (usize, usize) {
(self.worktree.id(), self.entry_id)
}
}
struct IterStackEntry {
entry_id: usize,
child_idx: usize,
}
pub struct Iter {
tree: Worktree,
stack: Vec<IterStackEntry>,
started: bool,
}
impl Iterator for Iter {
type Item = Traversal;
fn next(&mut self) -> Option<Self::Item> {
let state = self.tree.0.read();
if !self.started {
self.started = true;
return if let Some(entry) = state.entries.first().cloned() {
self.stack.push(IterStackEntry {
entry_id: 0,
child_idx: 0,
});
Some(Traversal::Push { entry_id: 0, entry })
} else {
None
};
}
while let Some(parent) = self.stack.last_mut() {
if let Entry::Dir { children, .. } = &state.entries[parent.entry_id] {
if parent.child_idx < children.len() {
let child_id = children[post_inc(&mut parent.child_idx)];
self.stack.push(IterStackEntry {
entry_id: child_id,
child_idx: 0,
});
return Some(Traversal::Push {
entry_id: child_id,
entry: state.entries[child_id].clone(),
});
} else {
self.stack.pop();
return Some(Traversal::Pop);
}
} else {
self.stack.pop();
return Some(Traversal::Pop);
}
}
None
}
}
#[derive(Debug)]
pub enum Traversal {
Push { entry_id: usize, entry: Entry },
Pop,
}
pub struct FilesIter {
iter: Iter,
path: PathBuf,
}
pub struct FilesIterItem {
pub entry_id: usize,
pub path: PathBuf,
}
impl Iterator for FilesIter {
type Item = FilesIterItem;
fn next(&mut self) -> Option<Self::Item> {
loop {
match self.iter.next() {
Some(Traversal::Push {
entry_id, entry, ..
}) => match entry {
Entry::Dir { name, .. } => {
self.path.push(name);
}
Entry::File { name, .. } => {
self.path.push(name);
return Some(FilesIterItem {
entry_id,
path: self.path.clone(),
});
}
},
Some(Traversal::Pop) => {
self.path.pop();
}
None => {
return None;
}
}
}
}
}
trait UnwrapIgnoreTuple {
fn unwrap(self) -> Ignore;
}
impl UnwrapIgnoreTuple for (Ignore, Option<ignore::Error>) {
fn unwrap(self) -> Ignore {
if let Some(error) = self.1 {
log::error!("error loading gitignore data: {}", error);
}
self.0
}
}
pub fn match_paths(
trees: &[Worktree],
query: &str,
include_ignored: bool,
smart_case: bool,
max_results: usize,
) -> Vec<PathMatch> {
let tree_states = trees.iter().map(|tree| tree.0.read()).collect::<Vec<_>>();
fuzzy::match_paths(
&tree_states
.iter()
.map(|tree| {
let skip_prefix = if trees.len() == 1 {
if let Some(Entry::Dir { name, .. }) = tree.entries.get(0) {
let name = name.to_string_lossy();
if name == "/" {
1
} else {
name.chars().count() + 1
}
} else {
0
}
} else {
0
};
(tree.id, skip_prefix, &tree.file_paths[..])
})
.collect::<Vec<_>>()[..],
query,
include_ignored,
smart_case,
max_results,
)
}
// #[cfg(test)]
// mod test {
// use super::*;
// use crate::test_utils::*;
// use anyhow::Result;
// use std::os::unix;
//
// // #[test]
// // fn test_populate_and_search() -> Result<()> {
// // let dir = build_tempdir(json!({
// // "root": {
// // "apple": "",
// // "banana": {
// // "carrot": {
// // "date": "",
// // "endive": "",
// // }
// // },
// // "fennel": {
// // "grape": "",
// // }
// // }
// // }));
// //
// // let root_link_path = dir.path().join("root_link");
// // unix::fs::symlink(&dir.path().join("root"), &root_link_path)?;
// //
// // let tree = Worktree::new(1, root_link_path, None);
// // let (tx, _) = channel::unbounded();
// // tree.populate(&tx)?;
// // assert_eq!(tree.file_count(), 4);
// //
// // let results = match_paths(&[tree.clone()], "bna", false, false, 10)
// // .iter()
// // .map(|result| tree.entry_path(result.entry_id))
// // .collect::<Result<Vec<PathBuf>, _>>()?;
// //
// // assert_eq!(
// // results,
// // vec![
// // PathBuf::from("root_link/banana/carrot/date"),
// // PathBuf::from("root_link/banana/carrot/endive"),
// // ]
// // );
// //
// // Ok(())
// // }
// }