1717include "std.arbori"
1818
1919/**
20- * Lightweight Formatter for SQL Developer and SQLcl, version 21.4.2
20+ * Lightweight Formatter for SQL Developer and SQLcl, version 21.4.3
2121 * The idea is to keep the code formatted "as is" and apply chosen formatting rules only.
2222 *
2323 * The Arbori program is processed from top to bottom.
@@ -550,7 +550,8 @@ i5_define_global_functions:
550550 "merge",
551551 "block_stmt",
552552 "pkg_body",
553- "create_plsql"
553+ "create_plsql",
554+ "stmt"
554555 ]);
555556 }
556557
@@ -862,6 +863,7 @@ i9_remove_duplicate_spaces_in_scope:
862863-- - A2: Remove trailing spaces.
863864-- - A3: Do not format code between @formatter:off and @formatter:on comments.
864865-- - O2: White Space: Around operators. Options: true; false. (spaceAroundOperators).
866+ -- - A22: No space between sign and digits.
865867-- - A13: Keep short nodes on the same line.
866868-- - A5: No space before node.
867869-- - A6: No space after node.
@@ -884,6 +886,7 @@ i9_remove_duplicate_spaces_in_scope:
884886-- - O11: Line Breaks: On subqueries. Options: true; false. (breakOnSubqueries).
885887-- - A16: Line break if the parent element uses multiple lines.
886888-- - R5: Commas in front of separated elements.
889+ -- - A23: Enforce line break after single line comments.
887890-- - R2: 3 space indention.
888891-- - R7: SQL keywords are right aligned within a SQL command.
889892-- - A11: Align parameter names.
@@ -1013,6 +1016,21 @@ o2_whitespace_around_operators:
10131016 }
10141017}
10151018
1019+ -- --------------------------------------------------------------------------------------------------------------------
1020+ -- A22: No space between sign and digits.
1021+ -- --------------------------------------------------------------------------------------------------------------------
1022+
1023+ a22_no_space_between_sign_and_digits:
1024+ ([node) '-' | [node) '+')
1025+ & ![node) binary_add_op
1026+ & [node+1) digits
1027+ & ![node-1) digits
1028+ & ![node-1) expr
1029+ -> {
1030+ struct.putNewline(node.to, "");
1031+ logger.fine(struct.getClass(), "a22_no_space_between_sign_and_digits: at " + node.to + ".");
1032+ }
1033+
10161034-- --------------------------------------------------------------------------------------------------------------------
10171035-- A13: Keep short nodes on the same line.
10181036-- --------------------------------------------------------------------------------------------------------------------
@@ -1107,10 +1125,19 @@ a13_short_nodes:
11071125 return false;
11081126 }
11091127
1128+ var containsCase = function(node) {
1129+ for (var i=node.from+1; i < node.to; i++) {
1130+ if (target.src.get(i).content.toLowerCase() == 'case') {
1131+ return true;
1132+ }
1133+ }
1134+ return false;
1135+ }
1136+
11101137 var maxLen = maxCharLineSize / 2;
11111138 var node = tuple.get("node");
11121139 if (getNodeLength(node) <= maxLen) {
1113- if (breaksConcat == Format$Breaks.None || !containsConcat(node)) {
1140+ if (( breaksConcat == Format$Breaks.None || !containsConcat(node)) && !containsCase (node)) {
11141141 reduceNodeIndents(node);
11151142 }
11161143 }
@@ -2577,6 +2604,28 @@ r5_commas:
25772604 addSpacesAroundComma(node);
25782605}
25792606
2607+ -- --------------------------------------------------------------------------------------------------------------------
2608+ -- A23: Enforce line break after single line comments.
2609+ -- --------------------------------------------------------------------------------------------------------------------
2610+
2611+ a23_enforce_line_break_after_single_line_comment:
2612+ runOnce
2613+ -> {
2614+ var keys = newlinePositions.keySet().stream().collect(Collectors.toList()).toArray();
2615+ for (var i in keys) {
2616+ var key = keys[i];
2617+ if (key > 0) {
2618+ var comment = getLastCommentBetweenPos(key-1, key);
2619+ if (comment != null && comment.indexOf("--") == 0) {
2620+ if (value.indexOf("\n") == -1) {
2621+ struct.putNewline(key, theLineSeparator);
2622+ logger.fine(struct.getClass(), "a23_enforce_line_break_after_single_line_comment" + ": at " + key + ".");
2623+ }
2624+ }
2625+ }
2626+ }
2627+ }
2628+
25802629-- --------------------------------------------------------------------------------------------------------------------
25812630-- R2: 3 space indention.
25822631-- --------------------------------------------------------------------------------------------------------------------
@@ -2744,6 +2793,7 @@ r2_common:
27442793 | [node^) bulk_loop_stmt & ![node) 'FORALL'
27452794 | [node) analytic_clause & ![node^) over_clause
27462795 | [node) 'RETURN' & [node^) subprg_spec
2796+ | [node-1) 'RETURN' & [node^) stmt
27472797 | [node) plsql_declarations & ([node^) with_clause | [node^) with_clause[12,20))
27482798;
27492799
@@ -3121,6 +3171,22 @@ r2_fix_indent_for_subquery_in_paren:
31213171 }
31223172}
31233173
3174+ r2_fix_indent_for_case_expr:
3175+ ([parent) case_expr | [parent) case_expression)
3176+ & [endcase) 'END'
3177+ & endcase^ = parent
3178+ -> {
3179+ var parent = tuple.get("parent");
3180+ if (getIndent(parent.from).indexOf("\n") == -1) {
3181+ var endcase = tuple.get("endcase");
3182+ var marginDiff = getColumn(parent.from) - getColumn(endcase.from);
3183+ if (marginDiff > 0) {
3184+ addIndent(parent, marginDiff);
3185+ logger.fine(struct.getClass(), "r2_fix_indent_for_case_expr: at " + parent.from + ".");
3186+ }
3187+ }
3188+ }
3189+
31243190-- --------------------------------------------------------------------------------------------------------------------
31253191-- R7: SQL keywords are right aligned within a SQL command.
31263192-- --------------------------------------------------------------------------------------------------------------------
0 commit comments