29
29
public class YamlSourceFormatter {
30
30
private static final String START_BLOCK = "---" ;
31
31
32
- public String format (String source ) {
33
- return format (source , null );
34
- }
35
-
36
32
public String format (String source , YamlSourceFormatterConfig config ) {
37
33
if (config == null ) {
38
34
config = new DefaultYamlSourceFormatterConfig ();
@@ -41,13 +37,15 @@ public String format(String source, YamlSourceFormatterConfig config) {
41
37
42
38
/* backup meta info */
43
39
boolean restoreCommentsEnabled = config .isRestoreCommentsEnabled ();
40
+ boolean restoreBlankLines = config .isKeepingBlankLines ();
41
+
44
42
CommentsRescueContext rescueContext = null ;
45
- if (restoreCommentsEnabled ) {
43
+ if (restoreCommentsEnabled || restoreBlankLines ) {
46
44
rescueContext = backupCommentsAndMetaData (source , snakeConfig );
47
45
}
48
46
49
47
/* parse + pretty print */
50
- Yaml yamlParser = createYaml (new DumperOptions (),snakeConfig );
48
+ Yaml yamlParser = createYaml (new DumperOptions (), snakeConfig );
51
49
Iterable <Object > yamlDocuments = yamlParser .loadAll (source );
52
50
String formatted = formatDocuments (yamlDocuments , snakeConfig );
53
51
@@ -68,8 +66,54 @@ public String format(String source, YamlSourceFormatterConfig config) {
68
66
69
67
result = appendOrphanedComments (result , rescueContext , snakeConfig );
70
68
}
71
- return result .trim ();
69
+ if (restoreBlankLines ) {
70
+ result = restoreBlankLines (result , rescueContext , snakeConfig );
71
+ }
72
+ result = removeLastTrailingNewLine (result );
73
+ return result ;
74
+
75
+ }
76
+
77
+ private String removeLastTrailingNewLine (String formatted ) {
78
+ if (!formatted .endsWith ("\n " )) {
79
+ return formatted ;
80
+ }
81
+ return formatted .substring (0 , formatted .length () - 1 );
82
+ }
83
+
84
+ private String restoreBlankLines (String formatted , CommentsRescueContext rescueContext , SnakeYamlConfig snakeConfig ) {
85
+ String [] originLines = rescueContext .originSourceLines ;
86
+ if (originLines == null || originLines .length == 0 ) {
87
+ return formatted ;
88
+ }
89
+ List <Integer > numberListForBlankLines = new ArrayList <Integer >();
90
+ int i = -1 ; // -1 so we start inside with 0 when doing i++
91
+ for (String originLine : originLines ) {
92
+ i ++;
93
+ if (originLine .trim ().length () > 0 ) {
94
+ continue ;
95
+ }
96
+ numberListForBlankLines .add (Integer .valueOf (i ));
97
+ }
98
+ if (numberListForBlankLines .isEmpty ()) {
99
+ /* no blank lines detected... */
100
+ return formatted ;
101
+ }
102
+
103
+ String [] linesFormatted = formatted .split ("\n " );
72
104
105
+ StringBuilder sb = new StringBuilder ();
106
+ int lineNr = 0 ;
107
+ for (String line : linesFormatted ) {
108
+ while (numberListForBlankLines .contains (Integer .valueOf (lineNr ))) {
109
+ sb .append ("\n " );
110
+ lineNr ++;
111
+ }
112
+ sb .append (line );
113
+ sb .append ("\n " );
114
+ lineNr ++;
115
+ }
116
+ return sb .toString ();
73
117
}
74
118
75
119
private String appendOrphanedComments (String result , CommentsRescueContext context , SnakeYamlConfig internalConfig ) {
@@ -79,9 +123,9 @@ private String appendOrphanedComments(String result, CommentsRescueContext conte
79
123
StringBuilder sb = new StringBuilder ();
80
124
sb .append (result .trim ());
81
125
sb .append ("\n " );
82
- sb .append ("# ----------------\n " );
83
- sb .append ("# Orphan comments:\n " );
84
- sb .append ("# ----------------\n " );
126
+ sb .append ("# ------------------ \n " );
127
+ sb .append ("# Orphaned comments:\n " );
128
+ sb .append ("# ------------------ \n " );
85
129
for (CommentMarker marker : context .commentMarkers ) {
86
130
if (marker .fullLine ) {
87
131
sb .append ("# Was at begin of line:" + marker .lineNr + " :" );
@@ -351,7 +395,7 @@ private String formatDocuments(Iterable<Object> documents, SnakeYamlConfig confi
351
395
options .setPrettyFlow (config .isPrettyFlow ());
352
396
options .setDefaultScalarStyle (config .getScalarStyle ());
353
397
354
- Yaml yaml = createYaml (options ,config );
398
+ Yaml yaml = createYaml (options , config );
355
399
356
400
StringBuilder sb = new StringBuilder ();
357
401
@@ -368,13 +412,13 @@ private String formatDocuments(Iterable<Object> documents, SnakeYamlConfig confi
368
412
369
413
private Yaml createYaml (DumperOptions options , SnakeYamlConfig config ) {
370
414
Resolver resolver = null ;
371
-
372
- if (config != null && config .isPreventingTypeConversionOnFormat ()) {
415
+
416
+ if (config != null && config .isPreventingTypeConversionOnFormat ()) {
373
417
resolver = new TypeConversionPreventionSnakeYamlResolver ();
374
- }else {
418
+ } else {
375
419
resolver = new Resolver ();
376
420
}
377
-
421
+
378
422
return new Yaml (new Constructor (), new Representer (), options , new LoaderOptions (), resolver );
379
423
}
380
424
0 commit comments