use std::collections::HashMap;
use super::{Entry, LIMIT, NodeKind, Row, Traversal};
use crate::{
RepoPath,
graph::{Graph, NodeKey},
};
impl Traversal<'_> {
pub(super) fn line(key: NodeKey) -> Entry {
Entry::Layout(Row {
node: key,
elsewhere: false,
entries: Vec::new(),
})
}
pub(super) fn layout_line(&mut self, key: NodeKey) -> Entry {
if self.query.layout_edges {
Entry::Layout(self.row(key))
} else {
Self::line(key)
}
}
pub(super) fn cap(&self, mut out: Vec<Entry>) -> Vec<Entry> {
if !self.query.all && out.len() > LIMIT {
let count = out.len() - LIMIT;
out.truncate(LIMIT);
out.push(Entry::More { rel: None, count });
}
out
}
pub(super) fn layout(&mut self, dir: &RepoPath) -> Vec<Entry> {
let graph = &self.graph;
let index = self.children.get_or_insert_with(|| {
let mut index: HashMap<NodeKey, Vec<(NodeKey, NodeKind)>> = HashMap::new();
for n in graph.nodes.values() {
if let Some(parent) = Graph::parent(&n.key) {
index
.entry(parent)
.or_default()
.push((n.key.clone(), n.kind));
}
}
index
});
let children = index
.get(&NodeKey::Path(dir.clone()))
.cloned()
.unwrap_or_default();
let selecting = !self.query.selector.is_empty();
let mut out = Vec::new();
for (key, kind) in children {
match kind {
NodeKind::Root | NodeKind::Directory => {
let Some(path) = key.path() else { continue };
let before = self.hidden;
let entries = self.layout(&path.clone());
if entries.is_empty() && (selecting || self.hidden > before) {
continue;
}
out.push(Entry::Layout(Row {
node: key,
elsewhere: false,
entries,
}));
}
NodeKind::Document => {
let query = self.query;
if !self.hides(&key) && self.document_matches(&key, &query.selector) {
out.push(self.layout_line(key));
}
}
NodeKind::Additional | NodeKind::Asset => {
let sidecar = kind == NodeKind::Asset
&& self
.graph
.node(&key)
.is_some_and(|n| n.fields.contains_key("sidecar"));
if !selecting && !sidecar {
out.push(self.layout_line(key));
}
}
NodeKind::File | NodeKind::Key | NodeKind::Missing => {}
}
}
self.cap(out)
}
pub(super) fn outside(&mut self, dir: &RepoPath) -> Vec<Entry> {
let roots: Vec<RepoPath> = self
.graph
.nodes
.values()
.filter(|n| n.kind == NodeKind::Root)
.filter_map(|n| n.key.path().cloned())
.filter(|p| p.starts_with(dir) && p != dir)
.collect();
let candidates: Vec<(NodeKey, NodeKind)> = self
.graph
.nodes
.values()
.filter(|n| n.key.path().is_some_and(|p| p.starts_with(dir) && p != dir))
.filter(|n| match n.kind {
NodeKind::Root => true,
NodeKind::Additional => n
.key
.path()
.is_some_and(|p| !roots.iter().any(|r| p.starts_with(r))),
NodeKind::File => !self.graph.edge_indices_from(&n.key).is_empty(),
NodeKind::Directory
| NodeKind::Document
| NodeKind::Asset
| NodeKind::Key
| NodeKind::Missing => false,
})
.map(|n| (n.key.clone(), n.kind))
.collect();
let selecting = !self.query.selector.is_empty();
let mut out = Vec::new();
for (key, kind) in candidates {
match kind {
NodeKind::Root => {
let Some(path) = key.path() else { continue };
let entries = self.layout(&path.clone());
if selecting && entries.is_empty() {
continue;
}
out.push(Entry::Layout(Row {
node: key,
elsewhere: false,
entries,
}));
}
_ => {
if !selecting {
out.push(Self::line(key));
}
}
}
}
self.cap(out)
}
}