Skip to content

Commit

Permalink
YAML - fixing handling of space before colon (#4967)
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-at-moderne authored Jan 29, 2025
1 parent f37372e commit 35f0b0b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
41 changes: 33 additions & 8 deletions rewrite-yaml/src/main/java/org/openrewrite/yaml/YamlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ private Yaml.Documents parseFromInput(Path sourceFile, EncodingDetectingInputStr
}
lastEnd = event.getEndMark().getIndex() + commaIndex + 1;
sequenceBuilder.push(finalScalar, commaPrefix);

} else if (builder == null) {
if (!"".equals(finalScalar.getValue())) {
// If the "scalar" is just a comment, allow it to accrue to the Document.End rather than create a phantom scalar
Expand All @@ -286,23 +285,24 @@ private Yaml.Documents parseFromInput(Path sourceFile, EncodingDetectingInputStr
case SequenceEnd:
case MappingEnd: {
Yaml.Block mappingOrSequence = blockStack.pop().build();
if (mappingOrSequence instanceof Yaml.Sequence) {
Yaml.Sequence seq = (Yaml.Sequence) mappingOrSequence;
if (mappingOrSequence instanceof SequenceWithPrefix) {
SequenceWithPrefix seq = (SequenceWithPrefix) mappingOrSequence;
if (seq.getOpeningBracketPrefix() != null) {
String s = reader.readStringFromBuffer(lastEnd, event.getStartMark().getIndex());
int closingBracketIndex = commentAwareIndexOf(']', s);
lastEnd = lastEnd + closingBracketIndex + 1;
mappingOrSequence = seq.withClosingBracketPrefix(s.substring(0, closingBracketIndex));
}
}
if (mappingOrSequence instanceof Yaml.Mapping) {
} else if (mappingOrSequence instanceof Yaml.Mapping) {
Yaml.Mapping map = (Yaml.Mapping) mappingOrSequence;
if (map.getOpeningBracePrefix() != null) {
String s = reader.readStringFromBuffer(lastEnd, event.getStartMark().getIndex());
int closingBraceIndex = commentAwareIndexOf('}', s);
lastEnd = lastEnd + closingBraceIndex + 1;
mappingOrSequence = map.withClosingBracePrefix(s.substring(0, closingBraceIndex));
}
} else {
throw new IllegalStateException("Unsupported element type: " + mappingOrSequence.getClass());
}
if (blockStack.isEmpty()) {
assert document != null;
Expand Down Expand Up @@ -334,7 +334,7 @@ private Yaml.Documents parseFromInput(Path sourceFile, EncodingDetectingInputStr
String startBracketPrefix = null;
int openingBracketIndex = commentAwareIndexOf('[', fullPrefix);
if (openingBracketIndex != -1) {
int startIndex = commentAwareIndexOf(':', fullPrefix) + 1;
int startIndex = commentAwareIndexOf(Arrays.asList(':', '-'), fullPrefix) + 1;
startBracketPrefix = fullPrefix.substring(startIndex, openingBracketIndex);
}
lastEnd = event.getEndMark().getIndex();
Expand Down Expand Up @@ -415,6 +415,10 @@ private Yaml.Anchor buildYamlAnchor(FormatPreservingReader reader, int lastEnd,
}

private static int commentAwareIndexOf(char target, String s) {
return commentAwareIndexOf(Collections.singleton(target), s);
}

private static int commentAwareIndexOf(Collection<Character> anyOf, String s) {
boolean inComment = false;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
Expand All @@ -423,7 +427,7 @@ private static int commentAwareIndexOf(char target, String s) {
inComment = false;
}
} else {
if (c == target) {
if (anyOf.contains(c)) {
return i;
} else if (c == '#') {
inComment = true;
Expand Down Expand Up @@ -589,11 +593,26 @@ public SequenceWithPrefix(String prefix, @Nullable String startBracketPrefix, Li
this.prefix = prefix;
}

public SequenceWithPrefix(UUID id, Markers markers, @Nullable String openingBracketPrefix, List<Entry> entries, @Nullable String closingBracketPrefix, @Nullable Anchor anchor, String prefix) {
super(id, markers, openingBracketPrefix, entries, closingBracketPrefix, anchor);
this.prefix = prefix;
}

@Override
public Sequence withPrefix(String prefix) {
this.prefix = prefix;
return this;
}

@Override
public SequenceWithPrefix withClosingBracketPrefix(@Nullable String closingBracketPrefix) {
// Cannot use super as this returns Yaml.Sequence
return new SequenceWithPrefix(getId(), getMarkers(), getOpeningBracketPrefix(), getEntries(), closingBracketPrefix, getAnchor(), prefix);
}

public Sequence toSequence() {
return new Yaml.Sequence(getId(), getMarkers(), getOpeningBracketPrefix(), getEntries(), getClosingBracketPrefix(), getAnchor());
}
}

private Yaml.Documents unwrapPrefixedMappings(Yaml.Documents y) {
Expand All @@ -603,7 +622,12 @@ private Yaml.Documents unwrapPrefixedMappings(Yaml.Documents y) {
public Yaml.Sequence visitSequence(Yaml.Sequence sequence, Integer p) {
if (sequence instanceof SequenceWithPrefix) {
SequenceWithPrefix sequenceWithPrefix = (SequenceWithPrefix) sequence;
return super.visitSequence(
if (sequenceWithPrefix.getOpeningBracketPrefix() != null) {
// For inline sequence, the prefix got already transferred to the left-hand neighbor
return sequenceWithPrefix.toSequence();
} else {
// For normal sequence with dashes, the prefix of the sequence gets transferred to the first entry
return super.visitSequence(
new Yaml.Sequence(
sequenceWithPrefix.getId(),
sequenceWithPrefix.getMarkers(),
Expand All @@ -612,6 +636,7 @@ public Yaml.Sequence visitSequence(Yaml.Sequence sequence, Integer p) {
sequenceWithPrefix.getClosingBracketPrefix(),
sequenceWithPrefix.getAnchor()
), p);
}
}
return super.visitSequence(sequence, p);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,15 @@ void pipeLiteralInASequenceWithDoubleQuotes() {
)
);
}

@Test
void spaceBeforeColon() {
rewriteRun(
yaml(
"""
index_patterns : []
"""
)
);
}
}

0 comments on commit 35f0b0b

Please sign in to comment.