reveal method

  1. @useResult
TreeRows<T> reveal(
  1. NodeId id
)

Returns rows in which the node id is visible, by expanding its collapsed ancestors.

Use it to show a search result, a selected file, or a node opened from elsewhere, then find its row with indexOfNode. Works for files and directories; a directory is revealed, not expanded. Only the ancestors that decide visibility are expanded, in both expansion and requestedExpansion: not the intermediate directories of compacted rows (their own state is never consulted), and not top-level nodes while showRoots is false (they always count as expanded). So unlike expanding the ancestors one by one with expandNode, it never opens a compacted row that already shows the node.

A directory that the filter hides but that a compacted row would show (compaction follows the tree, so with the filter name == 'x' the row p/x/b shows b) is revealed through its chain: the chain's ancestors are expanded, exactly as for revealing x.

Returns these rows when id is unknown, when the node is already visible (also as an intermediate directory of a visible compacted row), when the filter hides the node and no compacted row would show it (no expansion would show it), and for top-level nodes while showRoots is false. Otherwise the topmost collapsed ancestor's row expands and the newly visible rows are inserted after it, as with expandNode; every other row is the identical object as before.

The result equals tree.flatten(expansion: result.expansion, filter: filter, expandMatches: false, compactFolders: compactFolders, showRoots: showRoots). Runs in O(depth + log rows + inserted rows) node visits without calling the filter, plus copying the row list and the expansion states.

Implementation

@useResult
TreeRows<T> reveal(NodeId id) {
  final tree = _source.tree;
  final position = tree.indexOf(id);
  if (position < 0 || _indexAt(position) >= 0) return this;
  final node = tree.nodes[position];
  // A top-level node that is not visible is a hidden root (showRoots is
  // false), which never has a row. A compacted chain is visible exactly
  // when its first node passes the filter, so a chain member the filter
  // hides is still revealed when its chain can show.
  if (node.isRoot || !_source.passesFilter(_source.chainHead(position))) {
    return this;
  }
  // The ancestors whose own state decides visibility, and the topmost of
  // them that is collapsed now. Every ancestor of the chain's first node
  // passes the filter, because that node does, so that ancestor's row is
  // visible; the chain members above the node are skipped, as their
  // state is never consulted.
  final deciding = <NodeId>[];
  TreeNode<T>? topmostCollapsed;
  for (final ancestor in tree.ancestorsOf(id)) {
    if (ancestor.isRoot && !_source.showRoots) continue;
    if (_source.continuesChain(ancestor)) continue;
    deciding.add(ancestor.id);
    if (!expansion.isExpanded(ancestor.id)) topmostCollapsed = ancestor;
  }
  if (topmostCollapsed == null) return this;
  final nextExpansion = expansion.expandAll(deciding);
  final nextRequested = identical(requestedExpansion, expansion)
      ? nextExpansion
      : requestedExpansion.expandAll(deciding);
  final index = _indexAt(tree.indexOf(topmostCollapsed.id));
  final rows = index < 0
      ? _source.buildRows(nextExpansion)
      : _splice(index, _rows[index]!, nextExpansion, expanded: true);
  return TreeRows<T>._(_source, nextExpansion, nextRequested, rows);
}