collectInlineCodeRuns function

List<InlineCodeRun> collectInlineCodeRuns(
  1. InlineSpan root
)

Every CodeTextSpan in root, as plain-text ranges.

Offsets count a placeholder as the single U+FFFC code unit the engine lays out, so they line up with RenderParagraph.getBoxesForSelection.

Implementation

List<InlineCodeRun> collectInlineCodeRuns(InlineSpan root) {
  final runs = <InlineCodeRun>[];
  var offset = 0;

  void visit(InlineSpan span) {
    if (span is TextSpan) {
      final start = offset;
      offset += span.text?.length ?? 0;
      final children = span.children;
      if (children != null) {
        for (final child in children) {
          visit(child);
        }
      }
      // Measured after the subtree, so a tagged span built over children —
      // [CodeTextSpan.revealing], the reveal's partially arrived form —
      // covers everything inside it, not just its own (absent) text.
      if (span is CodeTextSpan && offset > start) {
        runs.add(
          InlineCodeRun(start: start, end: offset, style: span.codeStyle),
        );
      }
    } else if (span is PlaceholderSpan) {
      offset += 1;
    } else {
      offset += span.toPlainText(includePlaceholders: true).length;
    }
  }

  visit(root);
  return runs;
}