|
| 1 | +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 |
| 2 | +From: zml <zml@stellardrift.ca> |
| 3 | +Date: Sat, 14 Oct 2023 20:01:14 -0700 |
| 4 | +Subject: [PATCH] fix: Avoid emitting unnecessary whitespace with comments |
| 5 | + |
| 6 | + |
| 7 | +diff --git a/src/main/java/org/yaml/snakeyaml/emitter/Emitter.java b/src/main/java/org/yaml/snakeyaml/emitter/Emitter.java |
| 8 | +index b2f9e9486fd43570b831cf2409e1321aeedcb38b..b6f9ca0f81d93128b492d56e7dc249cb6a798bfa 100644 |
| 9 | +--- a/src/main/java/org/yaml/snakeyaml/emitter/Emitter.java |
| 10 | ++++ b/src/main/java/org/yaml/snakeyaml/emitter/Emitter.java |
| 11 | +@@ -1334,6 +1334,10 @@ public final class Emitter implements Emitable { |
| 12 | + } |
| 13 | + |
| 14 | + void writeIndent() throws IOException { |
| 15 | ++ this.writeIndent(true); |
| 16 | ++ } |
| 17 | ++ |
| 18 | ++ void writeIndent(boolean actualIndent) throws IOException { |
| 19 | + int indent; |
| 20 | + if (this.indent != null) { |
| 21 | + indent = this.indent; |
| 22 | +@@ -1345,7 +1349,8 @@ public final class Emitter implements Emitable { |
| 23 | + writeLineBreak(null); |
| 24 | + } |
| 25 | + |
| 26 | +- writeWhitespace(indent - this.column); |
| 27 | ++ if (actualIndent) |
| 28 | ++ writeWhitespace(indent - this.column); |
| 29 | + } |
| 30 | + |
| 31 | + private void writeWhitespace(int length) throws IOException { |
| 32 | +@@ -1539,7 +1544,8 @@ public final class Emitter implements Emitable { |
| 33 | + writeIndicator("\"", false, false, false); |
| 34 | + } |
| 35 | + |
| 36 | +- private boolean writeCommentLines(List<CommentLine> commentLines) throws IOException { |
| 37 | ++ private boolean writeCommentLines(List<CommentLine> commentLines, boolean extraIndent) |
| 38 | ++ throws IOException { |
| 39 | + boolean wroteComment = false; |
| 40 | + if (emitComments) { |
| 41 | + int indentColumns = 0; |
| 42 | +@@ -1548,6 +1554,8 @@ public final class Emitter implements Emitable { |
| 43 | + if (commentLine.getCommentType() != CommentType.BLANK_LINE) { |
| 44 | + if (firstComment) { |
| 45 | + firstComment = false; |
| 46 | ++ if (extraIndent) |
| 47 | ++ writeIndent(); |
| 48 | + writeIndicator("#", commentLine.getCommentType() == CommentType.IN_LINE, false, false); |
| 49 | + indentColumns = this.column > 0 ? this.column - 1 : 0; |
| 50 | + } else { |
| 51 | +@@ -1558,7 +1566,7 @@ public final class Emitter implements Emitable { |
| 52 | + writeLineBreak(null); |
| 53 | + } else { |
| 54 | + writeLineBreak(null); |
| 55 | +- writeIndent(); |
| 56 | ++ writeIndent(false); |
| 57 | + } |
| 58 | + wroteComment = true; |
| 59 | + } |
| 60 | +@@ -1568,8 +1576,8 @@ public final class Emitter implements Emitable { |
| 61 | + |
| 62 | + private void writeBlockComment() throws IOException { |
| 63 | + if (!blockCommentsCollector.isEmpty()) { |
| 64 | +- writeIndent(); |
| 65 | +- writeCommentLines(blockCommentsCollector.consume()); |
| 66 | ++ writeIndent(false); |
| 67 | ++ writeCommentLines(blockCommentsCollector.consume(), true); |
| 68 | + if (!prettyFlow && flowLevel > 0) { |
| 69 | + prettyFlow = true; |
| 70 | + requiredPrettyFlowLevel++; |
| 71 | +@@ -1578,7 +1586,7 @@ public final class Emitter implements Emitable { |
| 72 | + } |
| 73 | + |
| 74 | + private boolean writeInlineComments() throws IOException { |
| 75 | +- return writeCommentLines(inlineCommentsCollector.consume()); |
| 76 | ++ return writeCommentLines(inlineCommentsCollector.consume(), false); |
| 77 | + } |
| 78 | + |
| 79 | + private String determineBlockHints(String text) { |
0 commit comments