1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! `@| tree` (spec: The tree): the context drawn as Unix `tree`
//! draws a directory. Every context node is a starting point whose
//! subtree prints in full, solid box lines in document order; the
//! mount root is the top line always, and a starting point below it
//! hangs by a dotted stub labelled with the skipped path. A drawn
//! node's outgoing crosslinks to context nodes are listed under its
//! line, one hop deep; a node is drawn once, where first met. The
//! operands pick a depth, the traits, the property names, the
//! properties as a record, and column expressions. One text value.
use crate::graph::adapter::AstAdapter;
use crate::graph::identity::name_path;
use crate::graph::memo::links_of;
use crate::syntax::ast::{Arg, FnCall, Operand};
use crate::{NodeId, Value};
use std::collections::HashSet;
/// What a node's line carries after its label, in the operands'
/// order.
pub(crate) enum Part {
/// `traits`: the node's traits, `<file; md>`.
Traits,
/// `props`: the node's property names, `::name ::size`.
Props,
/// `%`: the node's properties as a record.
Record,
/// Any other expression, evaluated at the node.
Column(Operand),
}
/// The operands of a `tree` call.
pub(crate) struct TreeSpec {
/// Levels drawn below each starting point; None draws all.
pub(crate) depth: Option<usize>,
pub(crate) parts: Vec<Part>,
}
impl TreeSpec {
/// The call's operands, in any order (the parser admitted them).
pub(crate) fn from_call(call: &FnCall) -> TreeSpec {
let mut spec = TreeSpec {
depth: None,
parts: Vec::new(),
};
for arg in &call.args {
match arg {
Arg::Lit(Value::Int(n)) => spec.depth = Some((*n).max(0) as usize),
Arg::Lit(Value::Str(s)) if s == "traits" => spec.parts.push(Part::Traits),
Arg::Lit(Value::Str(s)) if s == "props" => spec.parts.push(Part::Props),
Arg::Lit(Value::Str(s)) if s == "%" => spec.parts.push(Part::Record),
Arg::Expr(e) => spec.parts.push(Part::Column(e.clone())),
_ => {}
}
}
spec
}
}
/// The label of a symlink-style crosslink: the one outgoing link an
/// adapter gives a node that stands for another (the filesystem
/// adapter's symlink), drawn inline on the node's line as Unix
/// `tree` draws `CLAUDE.md -> SYNOPSIS/CLAUDE.md`.
const INLINE_LINK: &str = "target";
struct Drawer<'a, A: AstAdapter> {
adapter: &'a A,
spec: &'a TreeSpec,
column: &'a dyn Fn(NodeId, &Operand) -> Value,
/// The starting points.
context: HashSet<NodeId>,
/// Drawn already: a node is drawn once, where first met.
drawn: HashSet<NodeId>,
out: Vec<String>,
}
/// Draw the tree of `starts` (the context, in its order).
pub(crate) fn render(
adapter: &impl AstAdapter,
starts: &[NodeId],
spec: &TreeSpec,
column: &dyn Fn(NodeId, &Operand) -> Value,
) -> String {
let mut d = Drawer {
adapter,
spec,
column,
context: starts.iter().copied().collect(),
drawn: HashSet::new(),
out: Vec::new(),
};
let root = adapter.root();
// Several mounts under an unnamed, sourceless root: a forest,
// one top line per mount (each mount root carries its source).
let tops: Vec<NodeId> = {
let children = adapter.children(root);
let forest = adapter.name(root).is_none()
&& adapter.provenance(root).source.is_none()
&& !children.is_empty()
&& children
.iter()
.all(|c| adapter.provenance(*c).source.is_some());
if forest { children } else { vec![root] }
};
// A starting point under no top at all (a non-tree node brought
// in by `*//` or an arrow) hangs from the last top, without a
// stub.
let mut placed: HashSet<NodeId> = HashSet::new();
for (i, top) in tops.iter().enumerate() {
let last = i + 1 == tops.len();
let under: Vec<NodeId> = starts
.iter()
.copied()
.filter(|s| !placed.contains(s) && d.under(*s, *top))
.collect();
placed.extend(under.iter().copied());
let strays: Vec<NodeId> = if last {
starts
.iter()
.copied()
.filter(|s| !placed.contains(s))
.collect()
} else {
Vec::new()
};
d.draw_top(*top, &under, &strays);
}
d.out.join("\n")
}
impl<A: AstAdapter> Drawer<'_, A> {
/// Whether `node` is `top` or one of its descendants.
fn under(&self, node: NodeId, top: NodeId) -> bool {
let mut cur = Some(node);
while let Some(n) = cur {
if n == top {
return true;
}
cur = self.adapter.parent(n);
}
false
}
/// Whether a strict ancestor of `node` is a starting point: the
/// node is then drawn inside that ancestor's subtree.
fn covered(&self, node: NodeId) -> bool {
let mut cur = self.adapter.parent(node);
while let Some(n) = cur {
if self.context.contains(&n) {
return true;
}
cur = self.adapter.parent(n);
}
false
}
/// The names from `top`'s child down to `node`, `/`-joined: the
/// path a stub stands for.
fn skipped(&self, node: NodeId, top: NodeId) -> String {
let mut parts = Vec::new();
let mut cur = Some(node);
while let Some(n) = cur
&& n != top
{
parts.push(self.label(n));
cur = self.adapter.parent(n);
}
parts.reverse();
parts.join("/")
}
/// A node's label: its name; a nameless node its locator
/// segment (`[0]`).
fn label(&self, node: NodeId) -> String {
if let Some(name) = self.adapter.name(node) {
return name;
}
match self.adapter.parent(node) {
Some(p) => {
let pos = self
.adapter
.children(p)
.iter()
.position(|c| *c == node)
.unwrap_or(0);
format!("[{pos}]")
}
None => "/".to_string(),
}
}
/// The top line: the mount root as its locator — `.` for a
/// directory, a mount's name, a document's file name.
fn top_label(&self, top: NodeId) -> String {
if self.adapter.traits(top).iter().any(|t| t == "dir") {
return ".".to_string();
}
if let Some(name) = self.adapter.name(top) {
return name;
}
match self.adapter.provenance(top).source {
Some(src) => std::path::Path::new(&src)
.file_name()
.map(|f| f.to_string_lossy().into_owned())
.unwrap_or(src),
None => "/".to_string(),
}
}
fn draw_top(&mut self, top: NodeId, starts: &[NodeId], strays: &[NodeId]) {
let line = format!("{}{}", self.top_label(top), self.suffix(top));
self.out.push(line);
self.drawn.insert(top);
// The items under the top line, in the context's order: a
// starting point that is the top itself draws the top's
// subtree in place; one directly below hangs without a
// stub; one deeper hangs by the stub of its skipped path,
// shared with its siblings. A starting point inside another
// starting point's subtree is drawn there.
enum Item {
Whole,
Direct(NodeId),
Stub(String, Vec<NodeId>),
Stray(NodeId),
}
let mut items: Vec<Item> = Vec::new();
for &s in starts {
if s == top {
items.push(Item::Whole);
} else if self.covered(s) {
continue;
} else if self.adapter.parent(s) == Some(top) {
items.push(Item::Direct(s));
} else {
let parent = self.adapter.parent(s).expect("under the top");
let path = self.skipped(parent, top);
match items
.iter_mut()
.find(|i| matches!(i, Item::Stub(p, _) if *p == path))
{
Some(Item::Stub(_, members)) => members.push(s),
_ => items.push(Item::Stub(path, vec![s])),
}
}
}
items.extend(strays.iter().map(|s| Item::Stray(*s)));
if items.iter().any(|i| matches!(i, Item::Whole)) {
// the whole subtree, every other item inside it
self.links_and_children(top, "", self.spec.depth);
return;
}
let n = items.len();
for (i, item) in items.into_iter().enumerate() {
let last = i + 1 == n;
match item {
Item::Whole => unreachable!(),
Item::Direct(s) | Item::Stray(s) => self.draw_node(s, "", last, self.spec.depth),
Item::Stub(path, members) => {
let (tee, below) = if last {
("└┄┄ ", " ")
} else {
("├┄┄ ", "│ ")
};
self.out.push(format!("{tee}{path}"));
let m = members.len();
for (j, s) in members.into_iter().enumerate() {
self.draw_node(s, below, j + 1 == m, self.spec.depth);
}
}
}
}
}
/// One node's line under `prefix`, then its crosslinks and its
/// children. `remaining` is the depth still allowed below it; a
/// starting point resets it to the call's.
fn draw_node(&mut self, node: NodeId, prefix: &str, last: bool, remaining: Option<usize>) {
let (tee, below) = if last {
("└── ", " ")
} else {
("├── ", "│ ")
};
self.out.push(format!(
"{prefix}{tee}{}{}",
self.label(node),
self.suffix(node)
));
self.drawn.insert(node);
let remaining = if self.context.contains(&node) {
self.spec.depth
} else {
remaining
};
let child_prefix = format!("{prefix}{below}");
self.links_and_children(node, &child_prefix, remaining);
}
/// The crosslink lines of a drawn node (hop one: the target in
/// the context, drawn under the line when not drawn yet), then
/// its children not drawn yet, in document order.
fn links_and_children(&mut self, node: NodeId, prefix: &str, remaining: Option<usize>) {
for (label, target) in self.listed_links(node) {
if self.drawn.contains(&target) {
self.out.push(format!(
"{prefix}->{label} {}",
name_path(self.adapter, target)
));
continue;
}
self.out.push(format!(
"{prefix}->{label} {}",
name_path(self.adapter, target)
));
self.drawn.insert(target);
// hop two: the target's own crosslinks by path only, and
// its children
let deeper = format!("{prefix} ");
for (l2, t2) in self.listed_links(target) {
let onward = if links_of(self.adapter, t2)
.iter()
.any(|(_, t)| self.context.contains(t))
{
" -> ..."
} else {
""
};
self.out.push(format!(
"{deeper}->{l2} {}{onward}",
name_path(self.adapter, t2)
));
}
self.children(target, &deeper, self.spec.depth);
}
self.children(node, prefix, remaining);
}
fn children(&mut self, node: NodeId, prefix: &str, remaining: Option<usize>) {
if remaining == Some(0) {
return;
}
let kids: Vec<NodeId> = self
.adapter
.children(node)
.into_iter()
.filter(|c| !self.drawn.contains(c))
.collect();
let n = kids.len();
for (i, kid) in kids.into_iter().enumerate() {
self.draw_node(kid, prefix, i + 1 == n, remaining.map(|r| r - 1));
}
}
/// The crosslinks listed under a node's line: the outgoing links
/// whose target is a context node, the inline one excepted.
fn listed_links(&self, node: NodeId) -> Vec<(String, NodeId)> {
if self.inline_link(node).is_some() {
return Vec::new();
}
links_of(self.adapter, node)
.iter()
.filter(|(_, t)| self.context.contains(t))
.cloned()
.collect()
}
/// The one link drawn inline (`name -> path`): a node whose only
/// outgoing link is its symlink target.
fn inline_link(&self, node: NodeId) -> Option<NodeId> {
match links_of(self.adapter, node).as_slice() {
[(label, target)] if label == INLINE_LINK => Some(*target),
_ => None,
}
}
/// What follows a node's label: the inline link, then the parts
/// in the operands' order.
fn suffix(&self, node: NodeId) -> String {
let mut s = String::new();
if let Some(target) = self.inline_link(node) {
let path = name_path(self.adapter, target);
s.push_str(" -> ");
s.push_str(path.strip_prefix('/').unwrap_or(&path));
}
for part in &self.spec.parts {
match part {
Part::Traits => {
let traits = self.adapter.traits(node);
if !traits.is_empty() {
s.push_str(" <");
s.push_str(&traits.join("; "));
s.push('>');
}
}
Part::Props => {
for name in self.adapter.property_names(node) {
s.push_str(" ::");
s.push_str(&name);
}
}
// the properties by name, as `props` lists them
Part::Record => {
let fields: Vec<(String, Value)> = self
.adapter
.property_names(node)
.into_iter()
.map(|name| {
let v = self.adapter.property(node, &name).unwrap_or(Value::Null);
(name, v)
})
.collect();
s.push(' ');
s.push_str(&Value::Record(fields).display_form());
}
// a null column value prints nothing, not a blank
Part::Column(op) => match (self.column)(node, op) {
Value::Null => {}
v => {
s.push_str(" ");
s.push_str(&v.display_form());
}
},
}
}
s
}
}