Skip to content

Commit 3b9f887

Browse files
author
Vincent Potucek
committed
fix SQLTokenizedFormatter
1 parent db413ab commit 3b9f887

File tree

2 files changed

+333
-76
lines changed

2 files changed

+333
-76
lines changed

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

Lines changed: 100 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,24 @@ private List<FormatterToken> format(final List<FormatterToken> argList) {
8787
}
8888
}
8989

90-
// Remove extra tokens (spaces, etc)
91-
for (int index = argList.size() - 1; index >= 1; index--) {
92-
token = argList.get(index);
93-
FormatterToken prevToken = argList.get(index - 1);
94-
if (token.getType() == TokenType.SPACE && (prevToken.getType() == TokenType.SYMBOL || prevToken.getType() == TokenType.COMMENT)) {
95-
argList.remove(index);
96-
} else if ((token.getType() == TokenType.SYMBOL || token.getType() == TokenType.COMMENT) && prevToken.getType() == TokenType.SPACE) {
97-
argList.remove(index - 1);
98-
} else if (token.getType() == TokenType.SPACE) {
99-
token.setString(" ");
100-
}
101-
}
90+
Remove_extra_tokens(argList);
91+
92+
extracted2(argList);
93+
94+
int indent = 0;
95+
final List<Integer> bracketIndent = new ArrayList<>();
96+
FormatterToken prev = new FormatterToken(TokenType.SPACE, " ");
97+
boolean encounterBetween = false;
98+
extracted(argList, prev, bracketIndent, indent, encounterBetween);
99+
100+
extracted(argList);
102101

102+
extracted1(argList);
103+
104+
return argList;
105+
}
106+
107+
private static void extracted2(List<FormatterToken> argList) {
103108
for (int index = 0; index < argList.size() - 2; index++) {
104109
FormatterToken t0 = argList.get(index);
105110
FormatterToken t1 = argList.get(index + 1);
@@ -131,11 +136,90 @@ private List<FormatterToken> format(final List<FormatterToken> argList) {
131136
argList.remove(index + 1);
132137
}
133138
}
139+
}
134140

135-
int indent = 0;
136-
final List<Integer> bracketIndent = new ArrayList<>();
137-
FormatterToken prev = new FormatterToken(TokenType.SPACE, " ");
138-
boolean encounterBetween = false;
141+
private static void Remove_extra_tokens(List<FormatterToken> argList) {
142+
FormatterToken token;
143+
// Remove extra tokens (spaces, etc)
144+
for (int index = argList.size() - 1; index >= 1; index--) {
145+
token = argList.get(index);
146+
FormatterToken prevToken = argList.get(index - 1);
147+
if (token.getType() == TokenType.SPACE && (prevToken.getType() == TokenType.SYMBOL || prevToken.getType() == TokenType.COMMENT)) {
148+
argList.remove(index);
149+
} else if ((token.getType() == TokenType.SYMBOL || token.getType() == TokenType.COMMENT) && prevToken.getType() == TokenType.SPACE) {
150+
argList.remove(index - 1);
151+
} else if (token.getType() == TokenType.SPACE) {
152+
token.setString(" ");
153+
}
154+
}
155+
}
156+
157+
private void extracted1(List<FormatterToken> argList) {
158+
FormatterToken prev;
159+
FormatterToken token;
160+
for (int index = 1; index < argList.size(); index++) {
161+
prev = argList.get(index - 1);
162+
token = argList.get(index);
163+
164+
if (prev.getType() != TokenType.SPACE &&
165+
token.getType() != TokenType.SPACE &&
166+
!token.getString().startsWith("(")) {
167+
if (token.getString().equals(",") || statementDelimiters.contains(token.getString())) {
168+
continue;
169+
}
170+
if (isFunction(prev.getString())
171+
&& token.getString().equals("(")) {
172+
continue;
173+
}
174+
if (token.getType() == TokenType.VALUE && prev.getType() == TokenType.NAME) {
175+
// Do not add space between name and value [JDBC:MSSQL]
176+
continue;
177+
}
178+
if (token.getType() == TokenType.SYMBOL && isEmbeddedToken(token) ||
179+
prev.getType() == TokenType.SYMBOL && isEmbeddedToken(prev)) {
180+
// Do not insert spaces around colons
181+
continue;
182+
}
183+
if (token.getType() == TokenType.SYMBOL && prev.getType() == TokenType.SYMBOL) {
184+
// Do not add space between symbols
185+
continue;
186+
}
187+
if (prev.getType() == TokenType.COMMENT) {
188+
// Do not add spaces to comments
189+
continue;
190+
}
191+
argList.add(index, new FormatterToken(TokenType.SPACE, " "));
192+
}
193+
}
194+
}
195+
196+
private static void extracted(List<FormatterToken> argList) {
197+
for (int index = argList.size() - 1; index >= 4; index--) {
198+
if (index >= argList.size()) {
199+
continue;
200+
}
201+
202+
FormatterToken t0 = argList.get(index);
203+
FormatterToken t1 = argList.get(index - 1);
204+
FormatterToken t2 = argList.get(index - 2);
205+
FormatterToken t3 = argList.get(index - 3);
206+
FormatterToken t4 = argList.get(index - 4);
207+
208+
if (t4.getString().equals("(")
209+
&& t3.getString().trim().isEmpty()
210+
&& t1.getString().trim().isEmpty()
211+
&& t0.getString().equalsIgnoreCase(")")) {
212+
t4.setString(t4.getString() + t2.getString() + t0.getString());
213+
argList.remove(index);
214+
argList.remove(index - 1);
215+
argList.remove(index - 2);
216+
argList.remove(index - 3);
217+
}
218+
}
219+
}
220+
221+
private void extracted(List<FormatterToken> argList, FormatterToken prev, List<Integer> bracketIndent, int indent, boolean encounterBetween) {
222+
FormatterToken token;
139223
for (int index = 0; index < argList.size(); index++) {
140224
token = argList.get(index);
141225
String tokenString = token.getString().toUpperCase(Locale.ENGLISH);
@@ -265,66 +349,6 @@ private List<FormatterToken> format(final List<FormatterToken> argList) {
265349
}
266350
prev = token;
267351
}
268-
269-
for (int index = argList.size() - 1; index >= 4; index--) {
270-
if (index >= argList.size()) {
271-
continue;
272-
}
273-
274-
FormatterToken t0 = argList.get(index);
275-
FormatterToken t1 = argList.get(index - 1);
276-
FormatterToken t2 = argList.get(index - 2);
277-
FormatterToken t3 = argList.get(index - 3);
278-
FormatterToken t4 = argList.get(index - 4);
279-
280-
if (t4.getString().equals("(")
281-
&& t3.getString().trim().isEmpty()
282-
&& t1.getString().trim().isEmpty()
283-
&& t0.getString().equalsIgnoreCase(")")) {
284-
t4.setString(t4.getString() + t2.getString() + t0.getString());
285-
argList.remove(index);
286-
argList.remove(index - 1);
287-
argList.remove(index - 2);
288-
argList.remove(index - 3);
289-
}
290-
}
291-
292-
for (int index = 1; index < argList.size(); index++) {
293-
prev = argList.get(index - 1);
294-
token = argList.get(index);
295-
296-
if (prev.getType() != TokenType.SPACE &&
297-
token.getType() != TokenType.SPACE &&
298-
!token.getString().startsWith("(")) {
299-
if (token.getString().equals(",") || statementDelimiters.contains(token.getString())) {
300-
continue;
301-
}
302-
if (isFunction(prev.getString())
303-
&& token.getString().equals("(")) {
304-
continue;
305-
}
306-
if (token.getType() == TokenType.VALUE && prev.getType() == TokenType.NAME) {
307-
// Do not add space between name and value [JDBC:MSSQL]
308-
continue;
309-
}
310-
if (token.getType() == TokenType.SYMBOL && isEmbeddedToken(token) ||
311-
prev.getType() == TokenType.SYMBOL && isEmbeddedToken(prev)) {
312-
// Do not insert spaces around colons
313-
continue;
314-
}
315-
if (token.getType() == TokenType.SYMBOL && prev.getType() == TokenType.SYMBOL) {
316-
// Do not add space between symbols
317-
continue;
318-
}
319-
if (prev.getType() == TokenType.COMMENT) {
320-
// Do not add spaces to comments
321-
continue;
322-
}
323-
argList.add(index, new FormatterToken(TokenType.SPACE, " "));
324-
}
325-
}
326-
327-
return argList;
328352
}
329353

330354
private static boolean isEmbeddedToken(FormatterToken token) {

0 commit comments

Comments
 (0)