Skip to content

Commit 69d4393

Browse files
committed
Formatter keeps now blank lines per default, closes #110
1 parent a248e05 commit 69d4393

File tree

9 files changed

+475
-116
lines changed

9 files changed

+475
-116
lines changed

yamleditor-plugin/src/main/java-eclipse/de/jcup/yamleditor/YamlEditor.java

+1
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,7 @@ public void formatSourceCode() {
876876
config.setScalarStyle(scalarStyle);
877877
config.setPreventTypeConversion(getPreferences().isPreventingTypeConversionOnFormat());
878878
config.setRestoreCommentsEnabled(getPreferences().isSourceFormatterRescuingComments());
879+
config.setKeepBlankLines(!getPreferences().isSourceFormatterClearingAllBlankLines());
879880

880881
String output = new YamlSourceFormatter().format(code, config);
881882

yamleditor-plugin/src/main/java-eclipse/de/jcup/yamleditor/preferences/YamlEditorFormatterPreferencePage.java

+6
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ protected void createFieldEditors() {
7777
+ "(Full line comments will be at same line number as before, comments\n"
7878
+ "at end of a yaml line are tried to be added at end of those lines again)");
7979

80+
BooleanFieldEditor clearBlankLinesEditor = new BooleanFieldEditor(P_SOURCE_FORMAT_CLEAR_ALL_BLANK_LINES_ENABLED.getId(),
81+
"Clear all blank lines", parent);
82+
addField(clearBlankLinesEditor);
83+
clearBlankLinesEditor.getDescriptionControl(parent).setToolTipText(
84+
"When enabled blank lines will be removed.");
85+
8086
BooleanFieldEditor tcpOnFormatEditor = new BooleanFieldEditor(P_PREVENT_TYPE_CONVERSION_ON_FORMAT_ENABLED.getId(), "Prevent type conversion", parent);
8187
addField(tcpOnFormatEditor);
8288
tcpOnFormatEditor.getDescriptionControl(parent)

yamleditor-plugin/src/main/java-eclipse/de/jcup/yamleditor/preferences/YamlEditorPreferenceConstants.java

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public enum YamlEditorPreferenceConstants implements PreferenceIdentifiable{
4444
P_SOURCE_SCALAR_STYLE_ID("sourceFormatScalarStyleId"),
4545

4646
P_SOURCE_FORMAT_RESCUE_COMMENTS_ENABLED("sourceFormatRescueCommentsEnabled"),
47+
P_SOURCE_FORMAT_CLEAR_ALL_BLANK_LINES_ENABLED("sourceFormatClearAllBlankLinesEnabled"),
4748

4849
P_GO_TEMPLATE_SUPPORT_ENABLED("goTemplateSupportEnabled"),
4950

yamleditor-plugin/src/main/java-eclipse/de/jcup/yamleditor/preferences/YamlEditorPreferenceInitializer.java

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public void initializeDefaultPreferences() {
6565
store.setDefault(P_SOURCE_FORMAT_LINE_LENGTH.getId(),120);
6666
store.setDefault(P_SOURCE_SCALAR_STYLE_ID.getId(), YamlEdtiorFormatterScalarStyle.PLAIN.getId());
6767
store.setDefault(P_SOURCE_FORMAT_RESCUE_COMMENTS_ENABLED.getId(), true);
68+
store.setDefault(P_SOURCE_FORMAT_CLEAR_ALL_BLANK_LINES_ENABLED.getId(), false);
6869

6970
store.setDefault(P_GO_TEMPLATE_SUPPORT_ENABLED.getId(), true);
7071
store.setDefault(P_PREVENT_TYPE_CONVERSION_ON_FORMAT_ENABLED.getId(), true);

yamleditor-plugin/src/main/java-eclipse/de/jcup/yamleditor/preferences/YamlEditorPreferences.java

+4
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ public String getSourceFormatterScalarStyleId() {
185185
public boolean isSourceFormatterRescuingComments() {
186186
return getPreferenceStore().getBoolean(P_SOURCE_FORMAT_RESCUE_COMMENTS_ENABLED.getId());
187187
}
188+
189+
public boolean isSourceFormatterClearingAllBlankLines() {
190+
return getPreferenceStore().getBoolean(P_SOURCE_FORMAT_CLEAR_ALL_BLANK_LINES_ENABLED.getId());
191+
}
188192

189193
public boolean isGoTemplateSupportEnabled() {
190194
return getPreferenceStore().getBoolean(P_GO_TEMPLATE_SUPPORT_ENABLED.getId());

yamleditor-plugin/src/main/java/de/jcup/yamleditor/script/formatter/DefaultYamlSourceFormatterConfig.java

+10
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ public class DefaultYamlSourceFormatterConfig implements YamlSourceFormatterConf
2121
private static final int DEFAULT_MAX_LINE_LENGTH = 80;
2222
private static final int DEFAULT_INDENT = 2;
2323
private static final boolean DEFAULT_RESTORE_COMMENTS_ENABLED =true;
24+
private static final boolean DEFAULT_RESTORE_BLANK_LINES_ENABLED =true;
2425

2526
private int indent=DEFAULT_INDENT;
2627
private int maxLineLength=DEFAULT_MAX_LINE_LENGTH;
2728
private YamlEdtiorFormatterScalarStyle scalarStyle = DEFAULT_SCALAR_STYLE;
2829
private boolean restoreCommentsEnabled=DEFAULT_RESTORE_COMMENTS_ENABLED;
2930
private boolean preventTypeConversion;
31+
private boolean keepBlankLines=DEFAULT_RESTORE_BLANK_LINES_ENABLED;
3032

3133
public void setIndent(int indent) {
3234
this.indent = indent;
@@ -72,4 +74,12 @@ public boolean isPreventingTypeConversion() {
7274
return preventTypeConversion;
7375
}
7476

77+
public void setKeepBlankLines(boolean keep) {
78+
this.keepBlankLines=keep;
79+
}
80+
81+
public boolean isKeepingBlankLines() {
82+
return keepBlankLines;
83+
}
84+
7585
}

yamleditor-plugin/src/main/java/de/jcup/yamleditor/script/formatter/YamlSourceFormatter.java

+59-15
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@
2929
public class YamlSourceFormatter {
3030
private static final String START_BLOCK = "---";
3131

32-
public String format(String source) {
33-
return format(source, null);
34-
}
35-
3632
public String format(String source, YamlSourceFormatterConfig config) {
3733
if (config == null) {
3834
config = new DefaultYamlSourceFormatterConfig();
@@ -41,13 +37,15 @@ public String format(String source, YamlSourceFormatterConfig config) {
4137

4238
/* backup meta info */
4339
boolean restoreCommentsEnabled = config.isRestoreCommentsEnabled();
40+
boolean restoreBlankLines = config.isKeepingBlankLines();
41+
4442
CommentsRescueContext rescueContext = null;
45-
if (restoreCommentsEnabled) {
43+
if (restoreCommentsEnabled || restoreBlankLines) {
4644
rescueContext = backupCommentsAndMetaData(source, snakeConfig);
4745
}
4846

4947
/* parse + pretty print */
50-
Yaml yamlParser = createYaml(new DumperOptions(),snakeConfig);
48+
Yaml yamlParser = createYaml(new DumperOptions(), snakeConfig);
5149
Iterable<Object> yamlDocuments = yamlParser.loadAll(source);
5250
String formatted = formatDocuments(yamlDocuments, snakeConfig);
5351

@@ -68,8 +66,54 @@ public String format(String source, YamlSourceFormatterConfig config) {
6866

6967
result = appendOrphanedComments(result, rescueContext, snakeConfig);
7068
}
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");
72104

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();
73117
}
74118

75119
private String appendOrphanedComments(String result, CommentsRescueContext context, SnakeYamlConfig internalConfig) {
@@ -79,9 +123,9 @@ private String appendOrphanedComments(String result, CommentsRescueContext conte
79123
StringBuilder sb = new StringBuilder();
80124
sb.append(result.trim());
81125
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");
85129
for (CommentMarker marker : context.commentMarkers) {
86130
if (marker.fullLine) {
87131
sb.append("# Was at begin of line:" + marker.lineNr + " :");
@@ -351,7 +395,7 @@ private String formatDocuments(Iterable<Object> documents, SnakeYamlConfig confi
351395
options.setPrettyFlow(config.isPrettyFlow());
352396
options.setDefaultScalarStyle(config.getScalarStyle());
353397

354-
Yaml yaml = createYaml(options,config);
398+
Yaml yaml = createYaml(options, config);
355399

356400
StringBuilder sb = new StringBuilder();
357401

@@ -368,13 +412,13 @@ private String formatDocuments(Iterable<Object> documents, SnakeYamlConfig confi
368412

369413
private Yaml createYaml(DumperOptions options, SnakeYamlConfig config) {
370414
Resolver resolver = null;
371-
372-
if (config!=null && config.isPreventingTypeConversionOnFormat()) {
415+
416+
if (config != null && config.isPreventingTypeConversionOnFormat()) {
373417
resolver = new TypeConversionPreventionSnakeYamlResolver();
374-
}else {
418+
} else {
375419
resolver = new Resolver();
376420
}
377-
421+
378422
return new Yaml(new Constructor(), new Representer(), options, new LoaderOptions(), resolver);
379423
}
380424

yamleditor-plugin/src/main/java/de/jcup/yamleditor/script/formatter/YamlSourceFormatterConfig.java

+2
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ public interface YamlSourceFormatterConfig {
2727

2828
boolean isPreventingTypeConversion();
2929

30+
boolean isKeepingBlankLines();
31+
3032
}

0 commit comments

Comments
 (0)