Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Port from snakeyaml-engine] Remove extra indent after blank line comments. #265

Merged
merged 2 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1130,12 +1130,14 @@ class Emitter(
stream.write(indicator)
}

private fun writeIndent() {
private fun writeIndent(): Int {
val indentToWrite = indent ?: 0
if (!indention || column > indentToWrite || column == indentToWrite && !whitespace) {
writeLineBreak()
}
writeWhitespace(indentToWrite - column)
val whitespaces = indentToWrite - this.column
writeWhitespace(whitespaces)
return whitespaces
}

private fun writeWhitespace(length: Int) {
Expand Down Expand Up @@ -1308,6 +1310,7 @@ class Emitter(
var wroteComment = false
if (emitComments) {
var indentColumns = 0
var prevColumns = 0
var firstComment = true
for (commentLine in commentLines) {
if (commentLine.commentType != CommentType.BLANK_LINE) {
Expand All @@ -1319,14 +1322,15 @@ class Emitter(
)
indentColumns = if (column > 0) column - 1 else 0
} else {
writeWhitespace(indentColumns)
writeWhitespace(indentColumns - prevColumns)
writeIndicator(indicator = "#")
}
stream.write(commentLine.value)
writeLineBreak()
prevColumns = 0
} else {
writeLineBreak()
writeIndent()
prevColumns = writeIndent()
}
wroteComment = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,16 @@ public void testCommentsIndentFirstLineBlank() throws Exception {
assertEquals(data, result);
}

@Test
public void testCommentsLineBlank() throws Exception {
String data = "# Comment 1\n" + "key1:\n" + " \n" + " # Comment 2\n" + "\n" + " # Comment 3\n" + "\n"
+ " key2: value1\n" + "# \"Fun\" options\n" + "key3:\n" + " # Comment 4\n"
+ " # Comment 5\n" + " key4: value2\n" + "key5:\n" + " key6: value3\n";

String result = runEmitterWithCommentsEnabled(data);
assertEquals(data, result);
}

@Test
public void testMultiLineString() throws Exception {
String data = "# YAML load and save bug with keep block chomping indicator\n" + "example:\n"
Expand Down
2 changes: 1 addition & 1 deletion upstream-commit.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ef7ebe9c06e963e13f4ab465f300a0dd6f0940c9
cf808a16bff0eb531a48debcd04be0aab562c21e