From bef53804e0c9bdd37d158d80dd31e91596443840 Mon Sep 17 00:00:00 2001 From: Laszlo Juhasz Date: Wed, 10 Jan 2024 21:44:31 +0100 Subject: [PATCH] new format config for skipping white spaces before and after block parentheses --- .../sqlformatter/core/AbstractFormatter.java | 10 ++++++-- .../sqlformatter/core/FormatConfig.java | 24 +++++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/github/vertical_blank/sqlformatter/core/AbstractFormatter.java b/src/main/java/com/github/vertical_blank/sqlformatter/core/AbstractFormatter.java index 8e319c2..1286b5d 100644 --- a/src/main/java/com/github/vertical_blank/sqlformatter/core/AbstractFormatter.java +++ b/src/main/java/com/github/vertical_blank/sqlformatter/core/AbstractFormatter.java @@ -167,7 +167,9 @@ private String formatOpeningParentheses(Token token, String query) { if (!this.inlineBlock.isActive()) { this.indentation.increaseBlockLevel(); - query = this.addNewline(query); + if (!cfg.skipWhitespaceNearBlockParentheses) { + query = this.addNewline(query); + } } return query; } @@ -179,7 +181,11 @@ private String formatClosingParentheses(Token token, String query) { return this.formatWithSpaceAfter(token, query); } else { this.indentation.decreaseBlockLevel(); - return this.formatWithSpaces(token, this.addNewline(query)); + if (!cfg.skipWhitespaceNearBlockParentheses) { + return this.formatWithSpaces(token, this.addNewline(query)); + } else { + return this.formatWithoutSpaces(token, query); + } } } diff --git a/src/main/java/com/github/vertical_blank/sqlformatter/core/FormatConfig.java b/src/main/java/com/github/vertical_blank/sqlformatter/core/FormatConfig.java index 3761994..a462fd7 100644 --- a/src/main/java/com/github/vertical_blank/sqlformatter/core/FormatConfig.java +++ b/src/main/java/com/github/vertical_blank/sqlformatter/core/FormatConfig.java @@ -14,18 +14,21 @@ public class FormatConfig { public final Params params; public final boolean uppercase; public final Integer linesBetweenQueries; + public final boolean skipWhitespaceNearBlockParentheses; FormatConfig( String indent, int maxColumnLength, Params params, boolean uppercase, - Integer linesBetweenQueries) { + Integer linesBetweenQueries, + boolean skipWhitespaceNearBlockParentheses) { this.indent = indent; this.maxColumnLength = maxColumnLength; this.params = params == null ? Params.EMPTY : params; this.uppercase = uppercase; this.linesBetweenQueries = linesBetweenQueries; + this.skipWhitespaceNearBlockParentheses = skipWhitespaceNearBlockParentheses; } /** @@ -44,6 +47,7 @@ public static class FormatConfigBuilder { private Params params; private boolean uppercase; private Integer linesBetweenQueries; + private boolean skipWhitespaceNearBlockParentheses; FormatConfigBuilder() {} @@ -108,6 +112,17 @@ public FormatConfigBuilder linesBetweenQueries(int linesBetweenQueries) { return this; } + /** + * @param skipWhitespaceNearBlockParentheses skip adding whitespace before and after block + * Parentheses + * @return This + */ + public FormatConfigBuilder skipWhitespaceNearBlockParentheses( + boolean skipWhitespaceNearBlockParentheses) { + this.skipWhitespaceNearBlockParentheses = skipWhitespaceNearBlockParentheses; + return this; + } + /** * Returns an instance of FormatConfig created from the fields set on this builder. * @@ -115,7 +130,12 @@ public FormatConfigBuilder linesBetweenQueries(int linesBetweenQueries) { */ public FormatConfig build() { return new FormatConfig( - this.indent, this.maxColumnLength, this.params, this.uppercase, this.linesBetweenQueries); + this.indent, + this.maxColumnLength, + this.params, + this.uppercase, + this.linesBetweenQueries, + this.skipWhitespaceNearBlockParentheses); } } }