Skip to content

Commit 52b8c90

Browse files
committed
Check first non-space match for compact-inline notation char
1 parent 0dc457a commit 52b8c90

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
@@ -347,45 +347,41 @@ int indexOfLastLineEnding(String yaml, int offset) {
347347
String yaml,
348348
int start,
349349
) {
350-
var startOffset = max(0, start - 1);
351-
352-
scanner:
353-
while (true) {
354-
switch (yaml[startOffset]) {
355-
// This is either indent or separation space
356-
case ' ' || '\t':
357-
{
358-
startOffset = yaml.lastIndexOf(_nonSpaceMatch, startOffset);
359-
if (startOffset == -1) break scanner;
360-
}
361-
362-
case '\r' || '\n':
363-
return (compactCharOffset: -1, lineEndingIndex: startOffset);
364-
365-
/// Block sequences and explicit keys/values can be used to declare block
366-
/// maps in a compact-inline notation.
367-
///
368-
/// - a: b
369-
/// c: d
370-
///
371-
/// OR as an explicit key with its explicit value
372-
///
373-
/// ? a: b
374-
/// c: d
375-
/// : e: f
376-
/// g: h
377-
///
378-
/// See "Example 8.19 Compact Block Mappings" at
379-
/// https://yaml.org/spec/1.2.2/#822-block-mappings
380-
case '-' || '?' || ':':
381-
return (compactCharOffset: startOffset, lineEndingIndex: -1);
350+
/// Look back past the indent/separation space.
351+
final startOffset = max(
352+
0,
353+
yaml.lastIndexOf(_nonSpaceMatch, max(0, start - 1)),
354+
);
382355

383-
default:
384-
break scanner;
385-
}
386-
}
356+
return switch (yaml[startOffset]) {
357+
'\r' || '\n' => (compactCharOffset: -1, lineEndingIndex: startOffset),
387358

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

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

0 commit comments

Comments
 (0)