removeYamlPath function
Edits text so the dotted segments path is REMOVED, keeping every
unrelated line byte-identical:
- the leaf key line and its block body are deleted;
- an ancestor whose block becomes empty is deleted too (a removed last
leaf under
models.overrides.zai.glmdrops the emptiedglm:/zai:blocks up the chain); - a missing path is a no-op.
Same line-scan rules as upsertYamlPath (the CRAP ratchet split those helpers out; this one shares them).
Implementation
String removeYamlPath(String text, List<String> segments) {
if (segments.isEmpty) return text;
final hadTrailingNewline = text.endsWith('\n');
final lines = text.isEmpty
? <String>[]
: (hadTrailingNewline ? text.substring(0, text.length - 1) : text).split(
'\n',
);
final foundAt = <int>[];
var cursor = 0;
for (var depth = 0; depth < segments.length; depth++) {
final found = _findKeyLine(lines, segments, depth, cursor);
if (found < 0) return text; // absent path — nothing to remove
foundAt.add(found);
cursor = found + 1;
}
// Delete deepest-first: the leaf's block body, then emptied ancestors.
for (var depth = segments.length - 1; depth >= 0; depth--) {
final keyLine = foundAt[depth];
final end = _blockEnd(lines, keyLine, depth);
lines.removeRange(keyLine, end);
// An emptied ancestor block: the parent key line now carries no body.
if (depth > 0) {
final parentLine = foundAt[depth - 1];
if (_blockEnd(lines, parentLine, depth - 1) == parentLine + 1) {
continue; // loop's next iteration removes the emptied parent
}
break;
}
}
return _join(lines, hadTrailingNewline);
}