rewrite method
The lines text at indent stands for. from is where it is written
when it is not the page's own line.
Implementation
List<DefinedLine> rewrite(int indent, String text, {String? from, int? line}) {
_line = line ?? _line + 1;
// A `define` in the page: its lines are the definition's, not the page's.
if (_defining case final d?) {
if (indent > d.indent) {
_definingBody.add((indent: indent, text: text, line: _line));
return const [];
}
_endDefine();
}
if (indent == 0 && isDeclaration(text) && context == null) {
_defining = (line: _line, signature: text, indent: indent);
return const [];
}
if (context == null) {
final w = RegExp(r'^[A-Z][A-Za-z0-9]*').stringMatch(text);
if (w != null && !has(w)) _metBefore.putIfAbsent(w, () => _line);
}
if (definitions.isEmpty && page.isEmpty && _uses.isEmpty) return [(indent: indent, text: text, from: from)];
final out = <DefinedLine>[];
while (_uses.isNotEmpty && _uses.last.indent >= indent) {
out.addAll(_uses.removeLast().close());
}
var at = indent;
if (_uses.isNotEmpty) {
final placed = _uses.last.place(indent, text, out);
if (placed == null) return out;
at = placed.at;
text = placed.text;
}
// `Track#{tr.id} name=…`, `Track#track-{tr.id}`: an id from data is its
// fixed part, as on any line (none when it has none).
if (RegExp(r'^([A-Z][A-Za-z0-9]*)((?:\.[A-Za-z][A-Za-z0-9]*)*)#([A-Za-z0-9_-]*)\{[^{}]*\}[A-Za-z0-9_-]*(?=\s|$)').firstMatch(text) case final m? when has(m[1]!)) {
final fixed = m[3]!.replaceAll(RegExp(r'[-_]+$'), '');
text = '${m[1]}${m[2]}${RegExp(r'^[A-Za-z_]').hasMatch(fixed) ? '#$fixed' : ''}${text.substring(m.end)}';
}
final parts = LineParts.of(text);
var defs = parts == null || !has(parts.word) || (_statements.contains(parts.word) && !(_of(parts.word)?.any((d) => d.statement) ?? false)) ? null : _of(parts.word);
// A component's word with named meanings defined (`Stepper#steps`): one
// of them when the use fits it better than the component's own names.
if (parts != null && defs == null && !_statements.contains(parts.word)) {
final named = meanings(parts.word);
if (named.isNotEmpty) {
final best = _choose(named, parts);
final takes = component!(parts.word)!;
if (_score(best, parts) > _scoreOf(takes, const [], const [], parts)) defs = [best];
}
}
if (defs != null && defs.isNotEmpty) {
out.addAll(_openUse(at, indent, text, parts!, defs));
return out;
}
out.add((indent: at, text: text, from: from));
return out;
}