lineNumberForOffset function
1-based line number for offset, or null when unknown.
Implementation
int? lineNumberForOffset(String text, int? offset) {
if (offset == null || offset < 0) return null;
final clamped = offset.clamp(0, text.length);
return '\n'.allMatches(text.substring(0, clamped)).length + 1;
}