DirectoryTree<T> class final

An immutable, indexed directory tree built from a set of TreeEntrys.

The tree stores its nodes in pre-order (each node, then its children in TreeOptions.order), with O(1) lookup by NodeId and O(1) subtree ranges. That layout makes the common UI questions cheap: indexOf and subtreeSizeOf give a node's slice of nodes, isAncestor is two lookups, and descendantsOf is a view, not a copy.

Build a tree with DirectoryTree.build, DirectoryTree.fromPaths, or a DirectoryTreeBuilder. Building is deterministic: the same entries in any order produce an equal tree with an identical nodes order, and node ids are stable across rebuilds. Order matters only when several entries name one node: the later one wins, so it decides that node's entry and spelling. Bad input never throws; it is skipped and reported in issues.

final tree = DirectoryTree.fromPaths([
  '/repo/lib/main.dart',
  '/repo/lib/src/tree.dart',
  '/repo/README.md',
]);
print(tree.rootNodes.single.name); // repo
final lib = tree.nodeAt('/repo/lib')!;
print(tree.childrenOf(lib.id).map((node) => node.name)); // (src, main.dart)

To change a tree, derive a new one with rebuild or toBuilder; the original never changes.

Lookups never throw for unknown ids: they return null, -1, 0, false, or an empty result.

Annotations
  • @immutable

Constructors

DirectoryTree.build(Iterable<TreeEntry<T>> entries, {TreeOptions options = const TreeOptions()})
Builds a tree from entries using options.
factory
DirectoryTree.empty({TreeOptions options = const TreeOptions()})
Creates a tree without entries.
factory

Properties

directories → Iterable<DirectoryNode<T>>
The directory nodes in pre-order. Lazy; each iteration walks nodes.
no setter
directoryCount → int
The number of DirectoryNodes, explicit or inferred.
no setter
entries → List<TreeEntry<T>>
The entries that produced nodes, in first-insertion order.
no setter
fileCount → int
The number of FileNodes.
final
files → Iterable<FileNode<T>>
The file nodes in pre-order. Lazy; each iteration walks nodes.
no setter
hashCode → int
A hash of options and nodes, computed on first use and cached.
latefinal
isEmpty → bool
Whether the tree has no nodes.
no setter
isNotEmpty → bool
Whether the tree has at least one node.
no setter
issues → List<TreeIssue<T>>
The problems found while building, in input order.
no setter
length → int
The number of nodes.
no setter
nodes → List<TreeNode<T>>
Every node in pre-order: each top-level node in roots order, followed by its descendants, each directory's children in TreeOptions.order.
no setter
options → TreeOptions
The options this tree was built with.
final
rootNodes → List<TreeNode<T>>
The top-level nodes, in the same order as roots.
final
roots → List<NodeId>
The ids of the top-level nodes, in TreeOptions.order.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

ancestorsOf(NodeId id) → Iterable<TreeNode<T>>
The ancestors of id, nearest first, excluding the node itself.
childrenOf(NodeId id) → List<TreeNode<T>>
The children of id in TreeOptions.order; empty for files and unknown ids.
contains(NodeId id) → bool
Whether the tree has a node with id. O(1).
descendantsOf(NodeId id) → Iterable<TreeNode<T>>
The descendants of id in pre-order, excluding the node itself.
flatten({ExpansionState expansion = const ExpansionState.expanded(), TreeFilter? filter, bool expandMatches = true, bool compactFolders = false, bool showRoots = true}) → TreeRows<T>
Returns the visible rows of a tree view of this tree, in display order.
idOf(String path) → NodeId?
The id that the raw path has under TreeOptions.paths, whether or not the tree contains it.
indexOf(NodeId id) → int
The index of the node with id in nodes, or -1 when absent. O(1).
isAncestor(NodeId ancestor, NodeId descendant) → bool
Whether ancestor is a strict ancestor of descendant. O(1).
nodeAt(String path) → TreeNode<T>?
The node at the raw path, or null when the path is invalid or not in the tree.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
parentOf(NodeId id) → TreeNode<T>?
The parent of id, or null for top-level nodes and unknown ids. O(1).
rebuild(void update(DirectoryTreeBuilder<T> builder)) → DirectoryTree<T>
Returns a new tree with the changes that update makes to a builder seeded from this tree.
render({TreeStyle style = TreeStyle.unicode, ExpansionState expansion = const ExpansionState.expanded(), TreeFilter? filter, bool compactFolders = false, bool showRoots = true, int? maxDepth, String label(TreeRow<T> row)?}) → String
Renders this tree as text, one line per visible row, like the tree command.
rootOf(NodeId id) → TreeNode<T>?
The top-level node whose subtree contains id (the node itself when it is top level), or null for unknown ids. O(log number of roots).
subtreeSizeOf(NodeId id) → int
The number of nodes in the subtree of id, including the node itself; 0 when absent. O(1).
toBuilder() → DirectoryTreeBuilder<T>
Returns a builder seeded with this tree's options and entries.
toString() → String
A one-line summary such as DirectoryTree(roots: 1, nodes: 5, files: 3, issues: 0).
override

Operators

operator ==(Object other) → bool
Whether other is a DirectoryTree with equal options and equal nodes in the same order.
override
operator [](NodeId id) → TreeNode<T>?
The node with id, or null when the tree has no such node. O(1).

Static Methods

fromPaths(Iterable<String> paths, {TreeOptions options = const TreeOptions()}) → DirectoryTree<Object?>
Builds a tree from raw path strings, without entry data.