Skip to content

Commit a10c2b9

Browse files
committed
Check first non-space match for compact-inline notation char
1 parent 3d394f6 commit a10c2b9

File tree

1 file changed

+33
-37
lines changed

1 file changed

+33
-37
lines changed

pkgs/yaml_edit/lib/src/utils.dart

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -351,45 +351,41 @@ int indexOfLastLineEnding(String yaml, int offset) {
351351
String yaml,
352352
int start,
353353
) {
354-
var startOffset = max(0, start - 1);
355-
356-
scanner:
357-
while (true) {
358-
switch (yaml[startOffset]) {
359-
// This is either indent or separation space
360-
case ' ' || '\t':
361-
{
362-
startOffset = yaml.lastIndexOf(_nonSpaceMatch, startOffset);
363-
if (startOffset == -1) break scanner;
364-
}
365-
366-
case '\r' || '\n':
367-
return (compactCharOffset: -1, lineEndingIndex: startOffset);
368-
369-
/// Block sequences and explicit keys/values can be used to declare block
370-
/// maps in a compact-inline notation.
371-
///
372-
/// - a: b
373-
/// c: d
374-
///
375-
/// OR as an explicit key with its explicit value
376-
///
377-
/// ? a: b
378-
/// c: d
379-
/// : e: f
380-
/// g: h
381-
///
382-
/// See "Example 8.19 Compact Block Mappings" at
383-
/// https://yaml.org/spec/1.2.2/#822-block-mappings
384-
case '-' || '?' || ':':
385-
return (compactCharOffset: startOffset, lineEndingIndex: -1);
354+
/// Look back past the indent/separation space.
355+
final startOffset = max(
356+
0,
357+
yaml.lastIndexOf(_nonSpaceMatch, max(0, start - 1)),
358+
);
386359

387-
default:
388-
break scanner;
389-
}
390-
}
360+
return switch (yaml[startOffset]) {
361+
'\r' || '\n' => (compactCharOffset: -1, lineEndingIndex: startOffset),
391362

392-
return (compactCharOffset: -1, lineEndingIndex: -1);
363+
/// Block sequences and explicit keys/values can be used to declare block
364+
/// maps/sequences in a compact-inline notation.
365+
///
366+
/// - a: b
367+
/// c: d
368+
///
369+
/// - - a
370+
/// - b
371+
///
372+
/// OR as an explicit key with its explicit value
373+
///
374+
/// ? a: b
375+
/// c: d
376+
/// : e: f
377+
/// g: h
378+
///
379+
/// ? - sequence
380+
/// - as key
381+
/// : - sequence
382+
/// - as value
383+
///
384+
/// See "Example 8.19 Compact Block Mappings" at
385+
/// https://yaml.org/spec/1.2.2/#822-block-mappings
386+
'-' || '?' || ':' => (compactCharOffset: startOffset, lineEndingIndex: -1),
387+
_ => (compactCharOffset: -1, lineEndingIndex: -1)
388+
};
393389
}
394390

395391
typedef NextBlockNodeInfo = ({int nearestLineEnding, int nextNodeColStart});

0 commit comments

Comments
 (0)