Skip to content

find files via fuzzy finder #890

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Sep 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ bugreport = "0.4"
lazy_static = "1.4"
syntect = { version = "4.5", default-features = false, features = ["metadata", "default-fancy"]}
gh-emoji = "1.0.6"
fuzzy-matcher = "0.3"

[target.'cfg(all(target_family="unix",not(target_os="macos")))'.dependencies]
which = "4.1"
Expand Down
19 changes: 19 additions & 0 deletions filetreelist/src/filetree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,25 @@ impl FileTree {
})
}

pub fn select_file(&mut self, path: &Path) -> bool {
let new_selection = self
.items
.tree_items
.iter()
.position(|item| item.info().full_path() == path);

if new_selection == self.selection {
return false;
}

self.selection = new_selection;
if let Some(selection) = self.selection {
self.items.show_element(selection);
}
self.visual_selection = self.calc_visual_selection();
true
}

fn visual_index_to_absolute(
&self,
visual_index: usize,
Expand Down
232 changes: 231 additions & 1 deletion filetreelist/src/filetreeitems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl FileTreeItems {
if item_path.starts_with(&path) {
item.hide();
} else {
return;
break;
}
}
}
Expand Down Expand Up @@ -188,6 +188,90 @@ impl FileTreeItems {
}
}

/// makes sure `index` is visible.
/// this expands all parents and shows all siblings
pub fn show_element(&mut self, index: usize) -> Option<usize> {
Some(
self.show_element_upward(index)?
+ self.show_element_downward(index)?,
)
}

fn show_element_upward(&mut self, index: usize) -> Option<usize> {
let mut shown = 0_usize;

let item = self.tree_items.get(index)?;
let mut current_folder: (PathBuf, u8) = (
item.info().full_path().parent()?.to_path_buf(),
item.info().indent(),
);

let item_count = self.tree_items.len();
for item in self
.tree_items
.iter_mut()
.rev()
.skip(item_count - index - 1)
{
if item.info().indent() == current_folder.1 {
item.show();
shown += 1;
} else if item.info().indent() == current_folder.1 - 1 {
// this must be our parent

item.expand_path();

if item.info().is_visible() {
// early out if parent already visible
break;
}

item.show();
shown += 1;

current_folder = (
item.info().full_path().parent()?.to_path_buf(),
item.info().indent(),
);
}
}

Some(shown)
}

fn show_element_downward(
&mut self,
index: usize,
) -> Option<usize> {
let mut shown = 0_usize;

let item = self.tree_items.get(index)?;
let mut current_folder: (PathBuf, u8) = (
item.info().full_path().parent()?.to_path_buf(),
item.info().indent(),
);

for item in self.tree_items.iter_mut().skip(index + 1) {
if item.info().indent() == current_folder.1 {
item.show();
shown += 1;
}
if item.info().indent() == current_folder.1 - 1 {
// this must be our parent

item.show();
shown += 1;

current_folder = (
item.info().full_path().parent()?.to_path_buf(),
item.info().indent(),
);
}
}

Some(shown)
}

fn update_visibility(
&mut self,
prefix: &Option<PathBuf>,
Expand Down Expand Up @@ -687,6 +771,152 @@ mod tests {
]
);
}

#[test]
fn test_show_element() {
let items = vec![
Path::new("a/b/c"), //
Path::new("a/b2/d"), //
Path::new("a/b2/e"), //
];

//0 a/
//1 b/
//2 c
//3 b2/
//4 d
//5 e

let mut tree =
FileTreeItems::new(&items, &BTreeSet::new()).unwrap();

tree.collapse(0, true);

let res = tree.show_element(5).unwrap();
assert_eq!(res, 4);
assert!(tree.tree_items[3].kind().is_path());
assert!(!tree.tree_items[3].kind().is_path_collapsed());

assert_eq!(
get_visibles(&tree),
vec![
true, //
true, //
false, //
true, //
true, //
true,
]
);
}

#[test]
fn test_show_element_later_elements() {
let items = vec![
Path::new("a/b"), //
Path::new("a/c"), //
];

//0 a/
//1 b
//2 c

let mut tree =
FileTreeItems::new(&items, &BTreeSet::new()).unwrap();

tree.collapse(0, true);

assert_eq!(
get_visibles(&tree),
vec![
true, //
false, //
false, //
]
);

let res = tree.show_element(1).unwrap();
assert_eq!(res, 2);

assert_eq!(
get_visibles(&tree),
vec![
true, //
true, //
true, //
]
);
}

#[test]
fn test_show_element_downward_parent() {
let items = vec![
Path::new("a/b/c"), //
Path::new("a/d"), //
Path::new("a/e"), //
];

//0 a/
//1 b/
//2 c
//3 d
//4 e

let mut tree =
FileTreeItems::new(&items, &BTreeSet::new()).unwrap();

tree.collapse(0, true);

let res = tree.show_element(2).unwrap();
assert_eq!(res, 4);

assert_eq!(
get_visibles(&tree),
vec![
true, //
true, //
true, //
true, //
true, //
]
);
}

#[test]
fn test_show_element_expand_visible_parent() {
let items = vec![
Path::new("a/b"), //
];

//0 a/
//1 b

let mut tree =
FileTreeItems::new(&items, &BTreeSet::new()).unwrap();

tree.collapse(0, true);

assert_eq!(
get_visibles(&tree),
vec![
true, //
false, //
]
);

let res = tree.show_element(1).unwrap();
assert_eq!(res, 1);
assert!(tree.tree_items[0].kind().is_path());
assert!(!tree.tree_items[0].kind().is_path_collapsed());

assert_eq!(
get_visibles(&tree),
vec![
true, //
true, //
]
);
}
}

#[cfg(test)]
Expand Down
13 changes: 11 additions & 2 deletions filetreelist/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,29 @@ impl FileTreeItem {
&self.kind
}

///
/// # Panics
/// panics if self is not a path
pub fn collapse_path(&mut self) {
assert!(self.kind.is_path());
self.kind = FileTreeItemKind::Path(PathCollapsed(true));
}

///
/// # Panics
/// panics if self is not a path
pub fn expand_path(&mut self) {
assert!(self.kind.is_path());
self.kind = FileTreeItemKind::Path(PathCollapsed(false));
}

///
pub fn hide(&mut self) {
self.info.visible = false;
}

///
pub fn show(&mut self) {
self.info.visible = true;
}
}

impl Eq for FileTreeItem {}
Expand Down
Loading