canvasFor function

PreviewCanvas? canvasFor(
  1. List<PreviewCanvas> canvases,
  2. String path
)

The canvas that applies to path, or null when none of them does.

Longest prefix wins, which is what makes the list ordered data rather than a rule set with precedence to learn: declare nothing and get the plain rectangle, declare one line and the package has a shape, declare a third when one subtree really is 1920 wide. Declaration order breaks a tie between two prefixes of equal length, which Previews refuses to let you write anyway.

The tool and a project's own tests are expected to call this function rather than each match the prefixes themselves — a rule applied twice is a rule that eventually differs, and the whole value of the list is that the panel, the screenshot and the test agree about what a directory is for.

Implementation

PreviewCanvas? canvasFor(List<PreviewCanvas> canvases, String path) {
  PreviewCanvas? best;
  for (var canvas in canvases) {
    if (!canvas.covers(path)) continue;
    if (best == null || canvas.root.length > best.root.length) best = canvas;
  }
  return best;
}