Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 8a96070

Browse files
Merge pull request #200 from Trivadis/bugfix/issue-197-case-when-indent
Bugfix/issue 197 case when indent
2 parents 35f50bb + 26d4c87 commit 8a96070

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

settings/sql_developer/trivadis_custom_format.arbori

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,7 @@ i9_remove_duplicate_spaces_in_scope:
885885
-- - O11: Line Breaks: On subqueries. Options: true; false. (breakOnSubqueries).
886886
-- - A16: Line break if the parent element uses multiple lines.
887887
-- - R5: Commas in front of separated elements.
888+
-- - A23: Enforce line break after single line comments.
888889
-- - R2: 3 space indention.
889890
-- - R7: SQL keywords are right aligned within a SQL command.
890891
-- - A11: Align parameter names.
@@ -2593,6 +2594,28 @@ r5_commas:
25932594
addSpacesAroundComma(node);
25942595
}
25952596

2597+
-- --------------------------------------------------------------------------------------------------------------------
2598+
-- A23: Enforce line break after single line comments.
2599+
-- --------------------------------------------------------------------------------------------------------------------
2600+
2601+
a23_enforce_line_break_after_single_line_comment:
2602+
runOnce
2603+
-> {
2604+
var keys = newlinePositions.keySet().stream().collect(Collectors.toList()).toArray();
2605+
for (var i in keys) {
2606+
var key = keys[i];
2607+
if (key > 0) {
2608+
var comment = getLastCommentBetweenPos(key-1, key);
2609+
if (comment != null && comment.indexOf("--") == 0) {
2610+
if (value.indexOf("\n") == -1) {
2611+
struct.putNewline(key, theLineSeparator);
2612+
logger.fine(struct.getClass(), "a23_enforce_line_break_after_single_line_comment" + ": at " + key + ".");
2613+
}
2614+
}
2615+
}
2616+
}
2617+
}
2618+
25962619
-- --------------------------------------------------------------------------------------------------------------------
25972620
-- R2: 3 space indention.
25982621
-- --------------------------------------------------------------------------------------------------------------------
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.trivadis.plsql.formatter.settings.tests.rules;
2+
3+
import com.trivadis.plsql.formatter.settings.ConfiguredTestFormatter;
4+
import org.junit.jupiter.api.Nested;
5+
import org.junit.jupiter.api.Test;
6+
7+
public class A23_enforce_line_break_after_single_line_comment extends ConfiguredTestFormatter {
8+
9+
@Nested
10+
class case_expression {
11+
12+
@Test
13+
public void comment_before_then() {
14+
var sql = """
15+
select empno,
16+
ename,
17+
case
18+
when 'JOB' = 'SALESMAN' -- make gender-neutral
19+
then
20+
'SALESPERSON'
21+
else
22+
job
23+
end as job,
24+
sal
25+
from emp;
26+
""";
27+
formatAndAssert(sql);
28+
}
29+
30+
@Test
31+
public void comment_after_every_token() {
32+
var sql = """
33+
select empno,
34+
ename,
35+
case
36+
when -- comment 1
37+
'JOB' -- comment 2
38+
= -- comment 3
39+
'SALESMAN' -- comment 4
40+
then -- comment 5
41+
'SALESPERSON' -- comment 6
42+
else -- comment 7
43+
job -- comment 8
44+
end -- comment 9
45+
as -- comment 10
46+
job,
47+
sal
48+
from emp;
49+
""";
50+
formatAndAssert(sql);
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)