Initial public release
Luxe 0.1.0: a coding agent harness for the terminal. See README.md to get started and CHANGELOG.md for releases.
This commit is contained in:
236
crates/luxe-tui/src/files/filter.rs
Normal file
236
crates/luxe-tui/src/files/filter.rs
Normal file
@@ -0,0 +1,236 @@
|
||||
//! Tree filter — what `/` in FILES narrows the tree to.
|
||||
//!
|
||||
//! The tree reads one folder at a time, so filtering what it has read would miss most of a large
|
||||
//! project. The filter matches against the whole project's file list instead, the one the `@`
|
||||
//! picker reads, with the same matcher, and draws the matches as a tree of their own: each file
|
||||
//! under the folders that hold it, every folder open. Only the best matches are drawn, in tree
|
||||
//! order, since a tree sorted by score would not read as a tree.
|
||||
//!
|
||||
//! The list leaves out what `.gitignore` ignores, as the `@` picker does, so a filter never finds
|
||||
//! an ignored file even while the tree is showing them.
|
||||
|
||||
use std::collections::{BTreeMap, HashMap, HashSet};
|
||||
|
||||
use super::TreeRow;
|
||||
use crate::workspace_dock::DockChangeItem;
|
||||
|
||||
/// The most matches the tree draws; past this, typing more is the way to the rest.
|
||||
pub(crate) const SHOWN: usize = 100;
|
||||
|
||||
/// A filter being typed, or kept after typing stopped.
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub struct TreeFilter {
|
||||
/// What was typed
|
||||
pub query: String,
|
||||
/// Whether keys type into the query rather than move through the matches
|
||||
pub typing: bool,
|
||||
}
|
||||
|
||||
/// What a filter matched, beside the rows it drew.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct FilterHits {
|
||||
/// Character positions in each drawn path that the query matched, by path
|
||||
pub positions: HashMap<String, Vec<usize>>,
|
||||
/// Every file that matched, drawn or not
|
||||
pub total: usize,
|
||||
/// The best match, where the cursor goes as the query changes
|
||||
pub best: Option<String>,
|
||||
}
|
||||
|
||||
/// A folder of the matches' tree: what it holds, each keyed by its case-blind name and then its
|
||||
/// name, so `A` and `a` stay apart and the order is the tree's own.
|
||||
#[derive(Default)]
|
||||
struct Node {
|
||||
folders: BTreeMap<(String, String), Node>,
|
||||
files: BTreeMap<(String, String), String>,
|
||||
}
|
||||
|
||||
impl Node {
|
||||
fn insert(&mut self, path: &str) {
|
||||
let mut node = self;
|
||||
let mut parts = path.split('/').peekable();
|
||||
while let Some(part) = parts.next() {
|
||||
let key = (part.to_lowercase(), part.to_string());
|
||||
if parts.peek().is_none() {
|
||||
node.files.insert(key, path.to_string());
|
||||
} else {
|
||||
node = node.folders.entry(key).or_default();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The rows and hits for `query` over `files` and the changes.
|
||||
///
|
||||
/// Args: `(query, files, changes, ancestors)` — `ancestors` is every folder on the way to a
|
||||
/// change, so a folder still says it holds one.
|
||||
pub(crate) fn build(
|
||||
query: &str,
|
||||
files: &[String],
|
||||
changes: &[DockChangeItem],
|
||||
ancestors: &HashSet<String>,
|
||||
) -> (Vec<TreeRow>, FilterHits) {
|
||||
// A deleted file or one created since the list was read is still a change worth finding
|
||||
let listed: HashSet<&str> = files.iter().map(String::as_str).collect();
|
||||
let candidates: Vec<&str> = files
|
||||
.iter()
|
||||
.map(String::as_str)
|
||||
.chain(changes.iter().map(|item| item.path.as_str()).filter(|path| !listed.contains(path)))
|
||||
.collect();
|
||||
if candidates.is_empty() {
|
||||
let note = TreeRow::Note { depth: 0, text: "reading the project…".into() };
|
||||
return (vec![note], FilterHits::default());
|
||||
}
|
||||
let matches = crate::fuzzy::rank_paths(query, &candidates);
|
||||
let mut hits = FilterHits {
|
||||
total: matches.len(),
|
||||
best: matches.first().map(|hit| candidates[hit.idx].to_string()),
|
||||
..FilterHits::default()
|
||||
};
|
||||
let mut root = Node::default();
|
||||
for hit in matches.iter().take(SHOWN) {
|
||||
let path = candidates[hit.idx];
|
||||
root.insert(path);
|
||||
hits.positions.insert(path.to_string(), hit.positions.clone());
|
||||
// Each folder on the way lights the matched characters that fall inside it
|
||||
for (end, _) in path.match_indices('/') {
|
||||
let length = path[..end].chars().count();
|
||||
let inside = hit.positions.iter().copied().filter(|at| *at < length);
|
||||
hits.positions.entry(path[..end].to_string()).or_default().extend(inside);
|
||||
}
|
||||
}
|
||||
let changed: HashMap<&str, &DockChangeItem> =
|
||||
changes.iter().map(|item| (item.path.as_str(), item)).collect();
|
||||
let mut rows = Vec::new();
|
||||
emit(&root, "", 0, &changed, ancestors, &mut rows);
|
||||
match hits.total {
|
||||
0 => rows.push(TreeRow::Note { depth: 0, text: "no file matches".into() }),
|
||||
total if total > SHOWN => rows.push(TreeRow::Note {
|
||||
depth: 0,
|
||||
text: format!("… {} more, keep typing", total - SHOWN),
|
||||
}),
|
||||
_ => {}
|
||||
}
|
||||
(rows, hits)
|
||||
}
|
||||
|
||||
/// One folder's rows, folders first, a chain of lone folders drawn as one row the way the tree
|
||||
/// compacts them.
|
||||
fn emit(
|
||||
node: &Node,
|
||||
prefix: &str,
|
||||
depth: usize,
|
||||
changed: &HashMap<&str, &DockChangeItem>,
|
||||
ancestors: &HashSet<String>,
|
||||
rows: &mut Vec<TreeRow>,
|
||||
) {
|
||||
for ((_, name), child) in &node.folders {
|
||||
let (mut path, mut name, mut child) = (format!("{prefix}{name}"), name.clone(), child);
|
||||
while child.files.is_empty() && child.folders.len() == 1 {
|
||||
let Some(((_, only), next)) = child.folders.iter().next() else { break };
|
||||
path = format!("{path}/{only}");
|
||||
name = format!("{name}/{only}");
|
||||
child = next;
|
||||
}
|
||||
rows.push(TreeRow::Folder {
|
||||
holds_change: ancestors.contains(&path),
|
||||
path: path.clone(),
|
||||
name,
|
||||
depth,
|
||||
open: true,
|
||||
ignored: false,
|
||||
});
|
||||
emit(child, &format!("{path}/"), depth + 1, changed, ancestors, rows);
|
||||
}
|
||||
for ((_, name), path) in &node.files {
|
||||
rows.push(TreeRow::File {
|
||||
path: path.clone(),
|
||||
name: name.clone(),
|
||||
depth,
|
||||
change: changed.get(path.as_str()).map(|item| (*item).clone()),
|
||||
ignored: false,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use luxe_core::FileChangeStatus;
|
||||
|
||||
fn files(paths: &[&str]) -> Vec<String> {
|
||||
paths.iter().map(|path| (*path).to_string()).collect()
|
||||
}
|
||||
|
||||
fn drawn(rows: &[TreeRow]) -> Vec<String> {
|
||||
rows.iter()
|
||||
.map(|row| match row {
|
||||
TreeRow::Folder { name, depth, .. } => format!("{}{name}/", " ".repeat(*depth)),
|
||||
TreeRow::File { name, depth, change, .. } => {
|
||||
let mark = if change.is_some() { " M" } else { "" };
|
||||
format!("{}{name}{mark}", " ".repeat(*depth))
|
||||
}
|
||||
TreeRow::Note { text, .. } => format!("({text})"),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The matches are a tree in the tree's own order, whatever order they scored in, each under
|
||||
/// the folders that hold it, and a lone chain of folders is one row.
|
||||
#[test]
|
||||
fn matches_are_drawn_as_a_tree() {
|
||||
let project = files(&[
|
||||
"Cargo.toml",
|
||||
"crates/luxe-tui/src/view/dock.rs",
|
||||
"crates/luxe-tui/src/workspace_dock.rs",
|
||||
"docs/guide/interface.md",
|
||||
]);
|
||||
let (rows, hits) = build("dock", &project, &[], &HashSet::new());
|
||||
assert_eq!(
|
||||
drawn(&rows),
|
||||
["crates/luxe-tui/src/", " view/", " dock.rs", " workspace_dock.rs"]
|
||||
);
|
||||
assert_eq!(hits.total, 2);
|
||||
assert!(hits.best.as_deref().is_some_and(|best| best.ends_with("dock.rs")));
|
||||
}
|
||||
|
||||
/// The whole path is matched, so a folder's name finds what is in it, and the folder lights
|
||||
/// the characters that fell inside it.
|
||||
#[test]
|
||||
fn a_folder_name_finds_its_files_and_lights_up() {
|
||||
let project = files(&["src/view/dock.rs", "src/app.rs"]);
|
||||
let (rows, hits) = build("view", &project, &[], &HashSet::new());
|
||||
assert_eq!(drawn(&rows), ["src/view/", " dock.rs"]);
|
||||
assert_eq!(hits.positions["src/view"], [4, 5, 6, 7]);
|
||||
}
|
||||
|
||||
/// A change the list does not hold yet, a file created since it was read, is still found, and
|
||||
/// a changed file keeps its mark.
|
||||
#[test]
|
||||
fn a_change_is_found_whether_listed_or_not() {
|
||||
let change = DockChangeItem {
|
||||
path: "src/new.rs".into(),
|
||||
status: FileChangeStatus::Added,
|
||||
additions: Some(3),
|
||||
deletions: Some(0),
|
||||
};
|
||||
let (rows, _) = build("new", &files(&["src/old.rs"]), &[change], &HashSet::new());
|
||||
assert_eq!(drawn(&rows), ["src/", " new.rs M"]);
|
||||
}
|
||||
|
||||
/// The tree draws the best few, and says how many more there are and how to reach them; with
|
||||
/// nothing matched, or nothing read yet, it says so rather than going blank.
|
||||
#[test]
|
||||
fn the_rest_and_the_nothing_are_said() {
|
||||
let many: Vec<String> = (0..SHOWN + 5).map(|n| format!("file{n:03}.rs")).collect();
|
||||
let (rows, hits) = build("file", &many, &[], &HashSet::new());
|
||||
assert_eq!(rows.len(), SHOWN + 1);
|
||||
assert_eq!(drawn(&rows).last().map(String::as_str), Some("(… 5 more, keep typing)"));
|
||||
assert_eq!(hits.total, SHOWN + 5);
|
||||
|
||||
let (rows, _) = build("zzz", &many, &[], &HashSet::new());
|
||||
assert_eq!(drawn(&rows), ["(no file matches)"]);
|
||||
let (rows, _) = build("a", &[], &[], &HashSet::new());
|
||||
assert_eq!(drawn(&rows), ["(reading the project…)"]);
|
||||
}
|
||||
}
|
||||
102
crates/luxe-tui/src/files/listings.rs
Normal file
102
crates/luxe-tui/src/files/listings.rs
Normal file
@@ -0,0 +1,102 @@
|
||||
//! File listings — which folders the tree wants read, and the reads it accepts, fails and refreshes.
|
||||
|
||||
use super::*;
|
||||
|
||||
impl FilesTree {
|
||||
/// Folders drawn open that want a listing asked for, the project root first.
|
||||
pub fn wanted(&self, changes: &[DockChangeItem]) -> Vec<String> {
|
||||
if self.changed_only {
|
||||
return Vec::new();
|
||||
}
|
||||
let needs = |folder: &str| {
|
||||
self.listings.get(folder).is_none_or(|listing| {
|
||||
listing.request.is_none()
|
||||
&& (listing.stale || (listing.entries.is_none() && listing.failed.is_none()))
|
||||
})
|
||||
};
|
||||
let mut wanted = Vec::new();
|
||||
if needs("") {
|
||||
wanted.push(String::new());
|
||||
}
|
||||
if self.listed("").is_none() {
|
||||
return wanted;
|
||||
}
|
||||
for row in self.rows(changes).iter() {
|
||||
if let TreeRow::Folder { path, open: true, .. } = row {
|
||||
if needs(path) {
|
||||
wanted.push(path.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
wanted
|
||||
}
|
||||
|
||||
/// Records that `folder`'s listing was asked for under `request`.
|
||||
pub fn requested(&mut self, folder: &str, request: ListingRequestId) {
|
||||
self.generation += 1;
|
||||
let listing = self.listings.entry(folder.to_string()).or_default();
|
||||
listing.request = Some(request);
|
||||
}
|
||||
|
||||
/// Takes a listing, unless a newer request for the same folder has superseded it.
|
||||
pub fn accept(&mut self, request: &ListingRequestId, listing: DirectoryListing) -> bool {
|
||||
let Some(entry) = self.listings.get_mut(&listing.path) else { return false };
|
||||
if entry.request.as_ref() != Some(request) {
|
||||
return false;
|
||||
}
|
||||
self.generation += 1;
|
||||
*entry = Listing { entries: Some(listing), ..Listing::default() };
|
||||
true
|
||||
}
|
||||
|
||||
/// Takes a failure under the same guard.
|
||||
pub fn fail(&mut self, request: &ListingRequestId, folder: &str, error: String) -> bool {
|
||||
let Some(entry) = self.listings.get_mut(folder) else { return false };
|
||||
if entry.request.as_ref() != Some(request) {
|
||||
return false;
|
||||
}
|
||||
entry.request = None;
|
||||
entry.failed = Some(error);
|
||||
self.generation += 1;
|
||||
true
|
||||
}
|
||||
|
||||
/// Marks every listing as overtaken when the changes moved, so open folders are asked again —
|
||||
/// the agent creating a file is exactly when the tree must not keep last turn's view of it.
|
||||
///
|
||||
/// Answers whether anything was marked.
|
||||
pub fn refresh_if_changed(&mut self, changes: &[DockChangeItem]) -> bool {
|
||||
let mut hasher = std::collections::hash_map::DefaultHasher::new();
|
||||
for item in changes {
|
||||
item.path.hash(&mut hasher);
|
||||
item.additions.hash(&mut hasher);
|
||||
item.deletions.hash(&mut hasher);
|
||||
}
|
||||
let seen = hasher.finish();
|
||||
if seen == self.changes_seen {
|
||||
return false;
|
||||
}
|
||||
self.changes_seen = seen;
|
||||
self.generation += 1;
|
||||
for listing in self.listings.values_mut() {
|
||||
listing.stale = listing.entries.is_some() || listing.failed.is_some();
|
||||
listing.failed = None;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// Marks every listing as overtaken, whatever the changes did: the reader asked to re-read.
|
||||
pub fn refresh_all(&mut self) {
|
||||
self.generation += 1;
|
||||
for listing in self.listings.values_mut() {
|
||||
listing.stale = listing.entries.is_some() || listing.failed.is_some();
|
||||
listing.failed = None;
|
||||
}
|
||||
}
|
||||
|
||||
/// Lists what `.gitignore` ignores too, or stops; every listing is asked again to match.
|
||||
pub fn toggle_ignored(&mut self) {
|
||||
self.show_ignored = !self.show_ignored;
|
||||
self.refresh_all();
|
||||
}
|
||||
}
|
||||
144
crates/luxe-tui/src/files/projection.rs
Normal file
144
crates/luxe-tui/src/files/projection.rs
Normal file
@@ -0,0 +1,144 @@
|
||||
//! File projection — the rows the tree shows, walked from its listings, with single-child folders
|
||||
//! compacted.
|
||||
|
||||
use super::*;
|
||||
|
||||
impl FilesTree {
|
||||
pub(super) fn build(&self, changes: &[DockChangeItem]) -> Vec<TreeRow> {
|
||||
let changed: HashMap<&str, &DockChangeItem> =
|
||||
changes.iter().map(|item| (item.path.as_str(), item)).collect();
|
||||
let ancestors = ancestors(changes);
|
||||
let from_changes = self.changed_only || self.listed("").is_none();
|
||||
let mut rows = Vec::new();
|
||||
if from_changes && changes.is_empty() {
|
||||
if !self.changed_only {
|
||||
rows.push(self.note_for("", 0));
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
self.walk("", 0, from_changes, &changed, &ancestors, &mut rows);
|
||||
rows
|
||||
}
|
||||
|
||||
fn walk(
|
||||
&self,
|
||||
folder: &str,
|
||||
depth: usize,
|
||||
from_changes: bool,
|
||||
changed: &HashMap<&str, &DockChangeItem>,
|
||||
ancestors: &HashSet<String>,
|
||||
rows: &mut Vec<TreeRow>,
|
||||
) {
|
||||
let (children, listing) = self.children(folder, from_changes, changed);
|
||||
// A folder with changes under it already has rows to show while it is read; a note above
|
||||
// them would only push them down for a moment and pull them back up
|
||||
if !from_changes && listing.is_none() && children.is_empty() {
|
||||
rows.push(self.note_for(folder, depth));
|
||||
}
|
||||
let prefix = if folder.is_empty() { String::new() } else { format!("{folder}/") };
|
||||
for (name, kind, ignored) in children {
|
||||
let path = format!("{prefix}{name}");
|
||||
match kind {
|
||||
Child::Folder => {
|
||||
let (path, name, open) =
|
||||
self.compacted(path, name, from_changes, changed, ancestors);
|
||||
rows.push(TreeRow::Folder {
|
||||
path: path.clone(),
|
||||
name,
|
||||
depth,
|
||||
open,
|
||||
holds_change: ancestors.contains(&path),
|
||||
ignored,
|
||||
});
|
||||
if open {
|
||||
self.walk(&path, depth + 1, from_changes, changed, ancestors, rows);
|
||||
}
|
||||
}
|
||||
Child::File => {
|
||||
let change = changed.get(path.as_str()).map(|item| (*item).clone());
|
||||
rows.push(TreeRow::File { path, name, depth, change, ignored });
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(omitted) = listing.map(|listing| listing.omitted).filter(|count| *count > 0) {
|
||||
rows.push(TreeRow::Note { depth, text: format!("… {omitted} more not listed") });
|
||||
}
|
||||
}
|
||||
|
||||
/// One folder's children, folders first, then files, each in case-blind name order: what its
|
||||
/// listing holds, and what changed under it whether listed or not (a deleted file, or a folder
|
||||
/// not read yet).
|
||||
fn children(
|
||||
&self,
|
||||
folder: &str,
|
||||
from_changes: bool,
|
||||
changed: &HashMap<&str, &DockChangeItem>,
|
||||
) -> (Vec<(String, Child, bool)>, Option<&DirectoryListing>) {
|
||||
let mut children: BTreeMap<(bool, String, String), (Child, bool)> = BTreeMap::new();
|
||||
let mut insert = |name: &str, kind: Child, ignored: bool| {
|
||||
// A case-blind name, then the name itself to keep `A` and `a` apart
|
||||
let key = (kind == Child::File, name.to_lowercase(), name.to_string());
|
||||
children.entry(key).or_insert((kind, ignored));
|
||||
};
|
||||
let listing = (!from_changes).then(|| self.listed(folder)).flatten();
|
||||
if let Some(listing) = listing {
|
||||
for entry in &listing.entries {
|
||||
let kind = match entry.kind {
|
||||
EntryKind::Directory => Child::Folder,
|
||||
EntryKind::File => Child::File,
|
||||
};
|
||||
insert(&entry.name, kind, entry.ignored);
|
||||
}
|
||||
}
|
||||
let prefix = if folder.is_empty() { String::new() } else { format!("{folder}/") };
|
||||
for path in changed.keys() {
|
||||
let Some(rest) = path.strip_prefix(prefix.as_str()) else { continue };
|
||||
match rest.split_once('/') {
|
||||
Some((name, _)) => insert(name, Child::Folder, false),
|
||||
None => insert(rest, Child::File, false),
|
||||
}
|
||||
}
|
||||
let children = children
|
||||
.into_iter()
|
||||
.map(|((_, _, name), (kind, ignored))| (name, kind, ignored))
|
||||
.collect();
|
||||
(children, listing)
|
||||
}
|
||||
|
||||
/// A folder whose only entry is another folder, drawn as one row: `crates/luxe-core/src`.
|
||||
///
|
||||
/// What VS Code calls compact folders. A rail forty cells wide cannot spend a row and two cells
|
||||
/// of indent on every `src` in a chain, and the chain is one decision for the reader anyway.
|
||||
/// Only while the folder is drawn open and its listing is known, so a chain never hides a file
|
||||
/// that has not been read yet; the row toggles the deepest folder in it, and says whether it is
|
||||
/// drawn open.
|
||||
///
|
||||
/// A folder taken into the chain is open unless the reader closed it. Opening `a` and finding
|
||||
/// it holds only `b` is opening the row the reader is now looking at, `a/b`: drawing that row
|
||||
/// closed made the folder they had just opened snap shut the moment its listing landed.
|
||||
fn compacted(
|
||||
&self,
|
||||
mut path: String,
|
||||
mut name: String,
|
||||
from_changes: bool,
|
||||
changed: &HashMap<&str, &DockChangeItem>,
|
||||
ancestors: &HashSet<String>,
|
||||
) -> (String, String, bool) {
|
||||
let mut open = self.is_open(&path, ancestors, from_changes);
|
||||
loop {
|
||||
if !open {
|
||||
return (path, name, false);
|
||||
}
|
||||
let (children, listing) = self.children(&path, from_changes, changed);
|
||||
let known = from_changes || listing.is_some_and(|listing| listing.omitted == 0);
|
||||
match children.as_slice() {
|
||||
[(only, Child::Folder, _)] if known => {
|
||||
path = format!("{path}/{only}");
|
||||
name = format!("{name}/{only}");
|
||||
open = !self.closed.contains(&path);
|
||||
}
|
||||
_ => return (path, name, true),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
218
crates/luxe-tui/src/files/tests.rs
Normal file
218
crates/luxe-tui/src/files/tests.rs
Normal file
@@ -0,0 +1,218 @@
|
||||
use super::*;
|
||||
use luxe_core::project_files::DirectoryEntry;
|
||||
use luxe_core::FileChangeStatus;
|
||||
|
||||
fn change(path: &str) -> DockChangeItem {
|
||||
DockChangeItem {
|
||||
path: path.into(),
|
||||
status: FileChangeStatus::Modified,
|
||||
additions: Some(1),
|
||||
deletions: Some(0),
|
||||
}
|
||||
}
|
||||
|
||||
fn listing(path: &str, folders: &[&str], files: &[&str]) -> DirectoryListing {
|
||||
let entry = |name: &&str, kind| DirectoryEntry { name: (*name).into(), kind, ignored: false };
|
||||
DirectoryListing {
|
||||
path: path.into(),
|
||||
entries: folders
|
||||
.iter()
|
||||
.map(|name| entry(name, EntryKind::Directory))
|
||||
.chain(files.iter().map(|name| entry(name, EntryKind::File)))
|
||||
.collect(),
|
||||
omitted: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Answer the outstanding request for `folder` with `listed`.
|
||||
fn answer(tree: &mut FilesTree, listed: DirectoryListing) {
|
||||
let request = ListingRequestId::new();
|
||||
tree.requested(&listed.path, request.clone());
|
||||
assert!(tree.accept(&request, listed));
|
||||
}
|
||||
|
||||
fn drawn(rows: &[TreeRow]) -> Vec<String> {
|
||||
rows.iter()
|
||||
.map(|row| match row {
|
||||
TreeRow::Folder { name, depth, open, .. } => {
|
||||
format!("{}{}{name}/", " ".repeat(*depth), if *open { "-" } else { "+" })
|
||||
}
|
||||
TreeRow::File { name, depth, change, .. } => {
|
||||
format!("{}{name}{}", " ".repeat(*depth), if change.is_some() { " M" } else { "" })
|
||||
}
|
||||
TreeRow::Note { depth, text } => format!("{}({text})", " ".repeat(*depth)),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Closing a filter opens the way to the file it found, so the cursor is still on a row once
|
||||
/// the whole tree is back.
|
||||
#[test]
|
||||
fn closing_a_filter_opens_the_way_to_the_cursor() {
|
||||
let mut tree = FilesTree::default();
|
||||
answer(&mut tree, listing("", &["src"], &["README.md"]));
|
||||
answer(&mut tree, listing("src", &["view"], &["app.rs"]));
|
||||
answer(&mut tree, listing("src/view", &[], &["dock.rs"]));
|
||||
tree.set_project_files(&["src/view/dock.rs".to_string(), "README.md".to_string()]);
|
||||
tree.open_filter();
|
||||
"dock".chars().for_each(|character| tree.edit_filter(Some(character)));
|
||||
assert_eq!(drawn(&tree.rows(&[])), ["-src/view/", " dock.rs"]);
|
||||
tree.cursor = Some("src/view/dock.rs".into());
|
||||
tree.close_filter();
|
||||
assert_eq!(
|
||||
drawn(&tree.rows(&[])),
|
||||
["-src/", " -view/", " dock.rs", " app.rs", "README.md"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn before_anything_is_listed_the_changes_are_the_tree() {
|
||||
let tree = FilesTree::default();
|
||||
let changes = [change("src/view/dock.rs"), change("PLAN.md")];
|
||||
assert_eq!(drawn(&tree.rows(&changes)), ["-src/view/", " dock.rs M", "PLAN.md M"]);
|
||||
assert_eq!(drawn(&tree.rows(&[])), ["(reading…)"], "a clean tree says it is on its way");
|
||||
assert_eq!(tree.wanted(&changes), [""], "and the root is what it asks for first");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn folders_on_the_way_to_a_change_open_and_the_rest_wait() {
|
||||
let mut tree = FilesTree::default();
|
||||
let changes = [change("src/view/dock.rs")];
|
||||
answer(&mut tree, listing("", &["docs", "src"], &["Cargo.toml", "PLAN.md"]));
|
||||
assert_eq!(
|
||||
drawn(&tree.rows(&changes)),
|
||||
["+docs/", "-src/", " -view/", " dock.rs M", "Cargo.toml", "PLAN.md"]
|
||||
);
|
||||
assert_eq!(tree.wanted(&changes), ["src", "src/view"]);
|
||||
|
||||
answer(&mut tree, listing("src", &["view"], &["lib.rs"]));
|
||||
answer(&mut tree, listing("src/view", &[], &["dock.rs", "input.rs"]));
|
||||
assert_eq!(
|
||||
drawn(&tree.rows(&changes)),
|
||||
[
|
||||
"+docs/",
|
||||
"-src/",
|
||||
" -view/",
|
||||
" dock.rs M",
|
||||
" input.rs",
|
||||
" lib.rs",
|
||||
"Cargo.toml",
|
||||
"PLAN.md"
|
||||
]
|
||||
);
|
||||
assert!(tree.wanted(&changes).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_readers_own_choice_outlives_the_changes() {
|
||||
let mut tree = FilesTree::default();
|
||||
let changes = [change("src/lib.rs")];
|
||||
answer(&mut tree, listing("", &["docs", "src"], &[]));
|
||||
answer(&mut tree, listing("src", &[], &["lib.rs"]));
|
||||
tree.toggle("src", &changes);
|
||||
assert_eq!(drawn(&tree.rows(&changes)), ["+docs/", "+src/"], "closed by hand stays closed");
|
||||
|
||||
tree.toggle("docs", &changes);
|
||||
assert_eq!(tree.wanted(&changes), ["docs"]);
|
||||
answer(&mut tree, listing("docs", &[], &["guide.md"]));
|
||||
assert_eq!(drawn(&tree.rows(&[])), ["-docs/", " guide.md", "+src/"], "and opened stays open");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_deleted_file_is_drawn_from_the_changes_alone() {
|
||||
let mut tree = FilesTree::default();
|
||||
answer(&mut tree, listing("", &[], &["kept.rs"]));
|
||||
let gone = DockChangeItem { status: FileChangeStatus::Deleted, ..change("gone.rs") };
|
||||
assert_eq!(drawn(&tree.rows(&[gone])), ["gone.rs M", "kept.rs"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn changed_only_is_the_changes_list_in_tree_form() {
|
||||
let mut tree = FilesTree { changed_only: true, ..FilesTree::default() };
|
||||
answer(&mut tree, listing("", &["docs", "src"], &["Cargo.toml"]));
|
||||
let changes = [change("src/lib.rs")];
|
||||
assert_eq!(drawn(&tree.rows(&changes)), ["-src/", " lib.rs M"]);
|
||||
assert!(tree.wanted(&changes).is_empty(), "nothing to list for a list of changes");
|
||||
assert!(drawn(&tree.rows(&[])).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_superseded_listing_is_discarded_and_a_new_change_re_asks() {
|
||||
let mut tree = FilesTree::default();
|
||||
let old = ListingRequestId::new();
|
||||
tree.requested("", old.clone());
|
||||
let newer = ListingRequestId::new();
|
||||
tree.requested("", newer.clone());
|
||||
assert!(!tree.accept(&old, listing("", &[], &["a.rs"])));
|
||||
assert!(tree.accept(&newer, listing("", &[], &["a.rs"])));
|
||||
assert!(tree.wanted(&[]).is_empty());
|
||||
|
||||
assert!(tree.refresh_if_changed(&[change("b.rs")]));
|
||||
assert!(!tree.refresh_if_changed(&[change("b.rs")]), "the same changes are not news");
|
||||
assert_eq!(tree.wanted(&[change("b.rs")]), [""], "the root is asked again");
|
||||
assert_eq!(
|
||||
drawn(&tree.rows(&[change("b.rs")])),
|
||||
["a.rs", "b.rs M"],
|
||||
"and stays drawn meanwhile"
|
||||
);
|
||||
}
|
||||
|
||||
/// A chain of lone folders is one row, as long as each link is known to be alone.
|
||||
#[test]
|
||||
fn a_chain_of_lone_folders_is_drawn_as_one_row() {
|
||||
let mut tree = FilesTree::default();
|
||||
let changes = [change("crates/core/src/lib.rs")];
|
||||
answer(&mut tree, listing("", &["crates"], &[]));
|
||||
assert_eq!(
|
||||
drawn(&tree.rows(&changes)),
|
||||
["-crates/", " -core/", " -src/", " lib.rs M"],
|
||||
"nothing is merged before it is read"
|
||||
);
|
||||
answer(&mut tree, listing("crates", &["core"], &[]));
|
||||
answer(&mut tree, listing("crates/core", &["src"], &[]));
|
||||
answer(&mut tree, listing("crates/core/src", &[], &["lib.rs", "main.rs"]));
|
||||
assert_eq!(drawn(&tree.rows(&changes)), ["-crates/core/src/", " lib.rs M", " main.rs"]);
|
||||
|
||||
// Closing the row closes the folder it ends on, and the chain stays one row
|
||||
tree.toggle("crates/core/src", &changes);
|
||||
assert_eq!(drawn(&tree.rows(&changes)), ["+crates/core/src/"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn revealing_a_file_opens_the_way_to_it() {
|
||||
let mut tree = FilesTree::default();
|
||||
answer(&mut tree, listing("", &["crates"], &[]));
|
||||
tree.reveal("crates/core/src/lib.rs");
|
||||
assert_eq!(tree.wanted(&[]), ["crates"]);
|
||||
answer(&mut tree, listing("crates", &["core"], &[]));
|
||||
assert_eq!(tree.wanted(&[]), ["crates/core"]);
|
||||
}
|
||||
|
||||
/// An empty folder opens on the first click and stays open once its empty listing lands.
|
||||
#[test]
|
||||
fn an_empty_folder_opens_on_the_first_click() {
|
||||
let mut tree = FilesTree::default();
|
||||
answer(&mut tree, listing("", &["empty", "src"], &["a.txt"]));
|
||||
tree.toggle("empty", &[]);
|
||||
assert_eq!(tree.wanted(&[]), ["empty"]);
|
||||
answer(&mut tree, listing("empty", &[], &[]));
|
||||
assert_eq!(drawn(&tree.rows(&[]))[0], "-empty/", "{:?}", drawn(&tree.rows(&[])));
|
||||
}
|
||||
|
||||
/// Opening a folder that turns out to hold only another folder opens the chain, rather than
|
||||
/// folding it into a closed `a/b` row the moment the listing lands; closing that row closes it.
|
||||
#[test]
|
||||
fn a_folder_that_holds_one_folder_stays_open_when_its_listing_lands() {
|
||||
let mut tree = FilesTree::default();
|
||||
answer(&mut tree, listing("", &["a"], &[]));
|
||||
tree.toggle("a", &[]);
|
||||
answer(&mut tree, listing("a", &["b"], &[]));
|
||||
assert_eq!(drawn(&tree.rows(&[])), ["-a/b/", " (reading…)"]);
|
||||
answer(&mut tree, listing("a/b", &[], &[]));
|
||||
assert_eq!(drawn(&tree.rows(&[])), ["-a/b/"]);
|
||||
|
||||
tree.toggle("a/b", &[]);
|
||||
assert_eq!(drawn(&tree.rows(&[])), ["+a/b/"], "closed by hand stays closed");
|
||||
tree.toggle("a/b", &[]);
|
||||
assert_eq!(drawn(&tree.rows(&[])), ["-a/b/"]);
|
||||
}
|
||||
Reference in New Issue
Block a user