Skip to content

Commit 39a1102

Browse files
committed
Fix SQL whitespace handing in comments
This fixes several issues to how whitespace was formatted related to comments in SQL formatter: - A line after the comment would get an additional leading space, this was especially visible for multi-line comments. For example: ``` -- comment CREATE TABLE ... ``` Would be formatted as: ``` -- comment CREATE TABLE ... ``` - An inline comment would be indented and placed on a separate line. For example: ``` CREATE TABLE products ( category_id INTEGER, -- lookup in categories table color VARCHAR(50), ) ``` Would be formatted as: ``` CREATE TABLE products ( category_id INTEGER, -- lookup in categories table color VARCHAR(50), ) ``` This fixes those issues in by patching these specific cases.
1 parent fc350d4 commit 39a1102

File tree

8 files changed

+48
-27
lines changed

8 files changed

+48
-27
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
### Fixed
14+
* Improved [SQL formatting](https://github.com/diffplug/spotless/pull/897) with respect to comments
1315

1416
## [2.15.1] - 2021-07-06
1517
### Changed

lib/src/main/java/com/diffplug/spotless/sql/dbeaver/SQLTokenizedFormatter.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 DiffPlug
2+
* Copyright 2016-2021 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -263,6 +263,8 @@ private List<FormatterToken> format(final List<FormatterToken> argList) {
263263
index += insertReturnAndIndent(argList, index, 0);
264264
}
265265
index += insertReturnAndIndent(argList, index + 1, 0);
266+
} else if (token.getType() == TokenType.NAME && index > 0 && argList.get(index - 1).getType() == TokenType.COMMENT) {
267+
index += insertReturnAndIndent(argList, index, indent);
266268
} else {
267269
if (statementDelimiters.contains(tokenString)) {
268270
indent = 0;
@@ -322,6 +324,10 @@ private List<FormatterToken> format(final List<FormatterToken> argList) {
322324
// Do not add space between symbols
323325
continue;
324326
}
327+
if (prev.getType() == TokenType.COMMENT) {
328+
// Do not add spaces to comments
329+
continue;
330+
}
325331
argList.add(index, new FormatterToken(TokenType.SPACE, " "));
326332
}
327333
}
@@ -383,17 +389,25 @@ private int insertReturnAndIndent(final List<FormatterToken> argList, final int
383389
if (functionBracket.contains(Boolean.TRUE))
384390
return 0;
385391
try {
386-
StringBuilder s = new StringBuilder(getDefaultLineSeparator());
392+
final String defaultLineSeparator = getDefaultLineSeparator();
393+
StringBuilder s = new StringBuilder(defaultLineSeparator);
394+
for (int index = 0; index < argIndent; index++) {
395+
s.append(formatterCfg.getIndentString());
396+
}
387397
if (argIndex > 0) {
398+
final FormatterToken token = argList.get(argIndex);
388399
final FormatterToken prevToken = argList.get(argIndex - 1);
389-
if (prevToken.getType() == TokenType.COMMENT &&
390-
isCommentLine(sqlDialect, prevToken.getString())) {
391-
s = new StringBuilder();
400+
if (token.getType() == TokenType.COMMENT &&
401+
isCommentLine(sqlDialect, token.getString()) &&
402+
prevToken.getType() != TokenType.END) {
403+
s.setCharAt(0, ' ');
404+
s.setLength(1);
405+
406+
final String comment = token.getString();
407+
final String withoutTrailingWhitespace = comment.replaceFirst("\\s*$", "");
408+
token.setString(withoutTrailingWhitespace);
392409
}
393410
}
394-
for (int index = 0; index < argIndent; index++) {
395-
s.append(formatterCfg.getIndentString());
396-
}
397411

398412
FormatterToken token = argList.get(argIndex);
399413
if (token.getType() == TokenType.SPACE) {

plugin-gradle/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).
44

55
## [Unreleased]
6+
### Fixed
7+
* Improved [SQL formatting](https://github.com/diffplug/spotless/pull/897) with respect to comments
68

79
## [5.14.1] - 2021-07-06
810
### Changed

plugin-maven/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
44

55
## [Unreleased]
6+
### Fixed
7+
* Improved [SQL formatting](https://github.com/diffplug/spotless/pull/897) with respect to comments
68

79
## [2.12.1] - 2021-06-17
810

testlib/src/main/resources/sql/dbeaver/V1_initial.sql.clean

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
--- Account management
2-
CREATE
2+
CREATE
33
TABLE
44
account(
55
id serial PRIMARY KEY,
66
username VARCHAR(60) NOT NULL UNIQUE,
77
email VARCHAR(513) NOT NULL UNIQUE,
8-
name VARCHAR(255),
9-
--NULLABLE
10-
created_at TIMESTAMP NOT NULL,
8+
name VARCHAR(255), --NULLABLE
9+
created_at TIMESTAMP NOT NULL,
1110
created_ip inet NOT NULL,
1211
updated_at TIMESTAMP NOT NULL,
1312
updated_ip inet NOT NULL,
@@ -38,13 +37,12 @@ CREATE
3837
);
3938

4039
--- Takes
41-
CREATE
40+
CREATE
4241
TABLE
4342
takerevision(
4443
id serial PRIMARY KEY,
45-
parent_id INT REFERENCES takerevision(id),
46-
--NULLABLE (null for root)
47-
created_at TIMESTAMP NOT NULL,
44+
parent_id INT REFERENCES takerevision(id), --NULLABLE (null for root)
45+
created_at TIMESTAMP NOT NULL,
4846
created_ip inet NOT NULL,
4947
title VARCHAR(255) NOT NULL,
5048
blocks jsonb NOT NULL
@@ -68,19 +66,17 @@ CREATE
6866
blocks jsonb NOT NULL,
6967
published_at TIMESTAMP NOT NULL,
7068
published_ip inet NOT NULL,
71-
deleted_at TIMESTAMP,
72-
--NULLABLE
73-
deleted_ip inet,
74-
--NULLABLE
75-
count_view INT NOT NULL DEFAULT 0,
69+
deleted_at TIMESTAMP, --NULLABLE
70+
deleted_ip inet, --NULLABLE
71+
count_view INT NOT NULL DEFAULT 0,
7672
count_like INT NOT NULL DEFAULT 0,
7773
count_bookmark INT NOT NULL DEFAULT 0,
7874
count_spam INT NOT NULL DEFAULT 0,
7975
count_illegal INT NOT NULL DEFAULT 0
8076
);
8177

8278
-- /user/title must be unique, and fast to lookup
83-
CREATE
79+
CREATE
8480
UNIQUE INDEX takepublished_title_user ON
8581
takepublished(
8682
title_slug,
@@ -105,8 +101,7 @@ CREATE
105101
take_id,
106102
user_id,
107103
kind
108-
),
109-
--user can only have one of each kind of reaction per take
110-
reacted_at TIMESTAMP NOT NULL,
104+
), --user can only have one of each kind of reaction per take
105+
reacted_at TIMESTAMP NOT NULL,
111106
reacted_ip inet NOT NULL
112107
);

testlib/src/main/resources/sql/dbeaver/create.clean

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
-- multiline
2+
-- comment
13
CREATE
24
TABLE
35
films(
@@ -19,7 +21,7 @@ CREATE
1921
);
2022

2123
-- Create a table with a 2-dimensional array:
22-
CREATE
24+
CREATE
2325
TABLE
2426
array_int(
2527
vector INT [][]

testlib/src/main/resources/sql/dbeaver/create.clean.alternative

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
-- multiline
2+
-- comment
13
create
24
table
35
films(
@@ -19,7 +21,7 @@ create
1921
);
2022

2123
-- Create a table with a 2-dimensional array:
22-
create
24+
create
2325
table
2426
array_int(
2527
vector int [][]

testlib/src/main/resources/sql/dbeaver/create.dirty

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
-- multiline
2+
-- comment
13
CREATE TABLE films (
24
code char(5) CONSTRAINT firstkey PRIMARY KEY,
35
title varchar(40) NOT NULL,

0 commit comments

Comments
 (0)