diff --git a/CHANGES.md b/CHANGES.md index e99fecee87..6919714b80 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,6 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Fixed - GrEclipse no longer leaves a `file%3A...` directory in the working directory. ([#2019](https://github.com/diffplug/spotless/issues/2019)) +- DBeaver SQL formatter no longer prints an `IndexOutOfBoundsException` when a file starts with a keyword like `SET`. ([#1552](https://github.com/diffplug/spotless/issues/1552)) - `ExpandWildcardImportsStep` no longer fails with `Symbol resolution not configured` on records and other syntax newer than Java 11, and leaves files JavaParser cannot validate unchanged. ([#2946](https://github.com/diffplug/spotless/issues/2946)) - `removeSemicolons()` no longer removes semicolons inside dollar-slashy strings (`$/ ... /$`), after escaped triple quotes in multiline strings, or in multi-line `for (init; cond; update)` headers. ([#2780](https://github.com/diffplug/spotless/issues/2780)) - `RemoveUnusedImportsStep` and `GoogleJavaFormatStep` no longer remove imports that are only used by links in Markdown doc comments (`///`, JDK 23+). ([#3161](https://github.com/diffplug/spotless/issues/3161)) diff --git a/lib/src/main/java/com/diffplug/spotless/sql/dbeaver/SQLTokenizedFormatter.java b/lib/src/main/java/com/diffplug/spotless/sql/dbeaver/SQLTokenizedFormatter.java index bad5d4a64a..af38b1631b 100644 --- a/lib/src/main/java/com/diffplug/spotless/sql/dbeaver/SQLTokenizedFormatter.java +++ b/lib/src/main/java/com/diffplug/spotless/sql/dbeaver/SQLTokenizedFormatter.java @@ -431,6 +431,10 @@ && isCommentLine(SQL_DIALECT, token.getString()) boolean isDelimiter = statementDelimiters.contains(token.getString().toUpperCase(Locale.ENGLISH)); if (!isDelimiter) { + if (argIndex == 0) { + // nothing precedes the first token, so there is no line to break + return 0; + } token = argList.get(argIndex - 1); if (token.getType() == TokenType.SPACE) { token.setString(s.toString()); diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 5549a0da75..cf27bf38ca 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -9,6 +9,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( - `SpotlessApply` now only reports that it did work (and is otherwise `UP-TO-DATE`) when it actually rewrote a file. ([#2715](https://github.com/diffplug/spotless/issues/2715)) ### Fixed - GrEclipse no longer leaves a `file%3A...` directory in the working directory. ([#2019](https://github.com/diffplug/spotless/issues/2019)) +- DBeaver SQL formatter no longer prints an `IndexOutOfBoundsException` when a file starts with a keyword like `SET`. ([#1552](https://github.com/diffplug/spotless/issues/1552)) - `expandWildcardImports()` no longer fails with `Symbol resolution not configured` on records and other syntax newer than Java 11, and leaves files JavaParser cannot validate unchanged. ([#2946](https://github.com/diffplug/spotless/issues/2946)) - String `targetExclude` patterns ending in `/**` (e.g. `**/build/**`) are now applied as exclude patterns on String `target`s, so excluded directories are skipped while walking the target instead of walking the whole project directory, which made configuration cache store and reuse scale with the size of the project. ([#3147](https://github.com/diffplug/spotless/issues/3147)) - `removeSemicolons()` no longer removes semicolons inside dollar-slashy strings (`$/ ... /$`), after escaped triple quotes in multiline strings, or in multi-line `for (init; cond; update)` headers. ([#2780](https://github.com/diffplug/spotless/issues/2780)) diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 9e50104b9e..9243f36af2 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -8,6 +8,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( List `spotless-maven-plugin` under `` and run `mvn site`: the page shows which files need formatting, with a colored diff for each, and their lint problems. It works with `maven-site-plugin` 3.12.1 and 3.20.0 or later. ### Fixed - GrEclipse no longer leaves a `file%3A...` directory in the working directory. ([#2019](https://github.com/diffplug/spotless/issues/2019)) +- DBeaver SQL formatter no longer prints an `IndexOutOfBoundsException` when a file starts with a keyword like `SET`. ([#1552](https://github.com/diffplug/spotless/issues/1552)) - `` no longer fails with `Symbol resolution not configured` on records and other syntax newer than Java 11, and leaves files JavaParser cannot validate unchanged. ([#2946](https://github.com/diffplug/spotless/issues/2946)) - `` no longer removes semicolons inside dollar-slashy strings (`$/ ... /$`), after escaped triple quotes in multiline strings, or in multi-line `for (init; cond; update)` headers. ([#2780](https://github.com/diffplug/spotless/issues/2780)) - `` and `` no longer remove imports that are only used by links in Markdown doc comments (`///`, JDK 23+). ([#3161](https://github.com/diffplug/spotless/issues/3161)) diff --git a/testlib/src/main/resources/sql/dbeaver/set-mode.clean b/testlib/src/main/resources/sql/dbeaver/set-mode.clean new file mode 100644 index 0000000000..30e7fdc679 --- /dev/null +++ b/testlib/src/main/resources/sql/dbeaver/set-mode.clean @@ -0,0 +1,2 @@ +SET +MODE PostgreSQL; diff --git a/testlib/src/main/resources/sql/dbeaver/set-mode.dirty b/testlib/src/main/resources/sql/dbeaver/set-mode.dirty new file mode 100644 index 0000000000..81ae754394 --- /dev/null +++ b/testlib/src/main/resources/sql/dbeaver/set-mode.dirty @@ -0,0 +1 @@ +SET MODE PostgreSQL; diff --git a/testlib/src/test/java/com/diffplug/spotless/sql/DBeaverSQLFormatterStepTest.java b/testlib/src/test/java/com/diffplug/spotless/sql/DBeaverSQLFormatterStepTest.java index 648ddc6669..24c6be0b19 100644 --- a/testlib/src/test/java/com/diffplug/spotless/sql/DBeaverSQLFormatterStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/sql/DBeaverSQLFormatterStepTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2024 DiffPlug + * Copyright 2016-2026 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,7 +15,11 @@ */ package com.diffplug.spotless.sql; +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.PrintStream; import java.util.Collections; import java.util.List; @@ -39,6 +43,22 @@ void behavior() { .testResource("sql/dbeaver/jdbi-params.dirty", "sql/dbeaver/jdbi-params.clean"); } + @Test + void keywordAtStartOfFileDoesNotThrow() { + // https://github.com/diffplug/spotless/issues/1552 + PrintStream originalErr = System.err; + ByteArrayOutputStream err = new ByteArrayOutputStream(); + System.setErr(new PrintStream(err, true)); + try { + FormatterStep step = DBeaverSQLFormatterStep.create(Collections.emptySet()); + StepHarness.forStep(step) + .testResource("sql/dbeaver/set-mode.dirty", "sql/dbeaver/set-mode.clean"); + } finally { + System.setErr(originalErr); + } + assertThat(err.toString()).isEmpty(); + } + @Test void behaviorWithConfigFile() { FormatterStep step = DBeaverSQLFormatterStep.create(createTestFiles("sql/dbeaver/sqlConfig.properties"));