collectInlineCodeRuns function
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 length = span.text?.length ?? 0;
if (span is CodeTextSpan && length > 0) {
runs.add(
InlineCodeRun(
start: offset,
end: offset + length,
style: span.codeStyle,
),
);
}
offset += length;
final children = span.children;
if (children != null) {
for (final child in children) {
visit(child);
}
}
} else if (span is PlaceholderSpan) {
offset += 1;
} else {
offset += span.toPlainText(includePlaceholders: true).length;
}
}
visit(root);
return runs;
}