diff --git a/CHANGES.md b/CHANGES.md
index fffedbbf9b..eac754f5cb 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -12,9 +12,8 @@ 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))
+- `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))
-
-### Fixed
- `forbidWildcardImports` no longer reports import statements inside Java text blocks or comments. ([#2997](https://github.com/diffplug/spotless/issues/2997))
## [4.11.0] - 2026-11-02
diff --git a/lib/src/main/java/com/diffplug/spotless/groovy/RemoveSemicolonsStep.java b/lib/src/main/java/com/diffplug/spotless/groovy/RemoveSemicolonsStep.java
index 8a5c325346..4dd17a93bb 100644
--- a/lib/src/main/java/com/diffplug/spotless/groovy/RemoveSemicolonsStep.java
+++ b/lib/src/main/java/com/diffplug/spotless/groovy/RemoveSemicolonsStep.java
@@ -17,12 +17,20 @@
import java.io.Serial;
import java.io.Serializable;
+import java.util.ArrayDeque;
+import java.util.Deque;
import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.FormatterStep;
/**
- * Removes unnecessary semicolons from Groovy code. Preserves semicolons inside strings and comments.
+ * Removes unnecessary semicolons from Groovy code. A semicolon is only removed when it is the last non-whitespace
+ * character on its line, outside any string literal or comment, and not part of a multi-line {@code for (init; cond;
+ * update)} header.
+ *
+ * Recognised literals: {@code '...'}, {@code "..."}, {@code '''...'''}, {@code """..."""} and dollar-slashy
+ * {@code $/.../$} strings. Plain slashy strings ({@code /.../}) are not recognised, because telling them apart from
+ * division requires a full Groovy lexer.
*
* @author Jose Luis Badano
*/
@@ -46,113 +54,120 @@ private static final class State implements Serializable {
private static final long serialVersionUID = 1L;
FormatterFunc toFormatter() {
- return raw -> {
- StringBuilder result = new StringBuilder(raw.length());
-
- // State tracking
- boolean inSingleQuoteString = false;
- boolean inDoubleQuoteString = false;
- boolean inTripleSingleQuoteString = false;
- boolean inTripleDoubleQuoteString = false;
- boolean inSingleLineComment = false;
- boolean inMultiLineComment = false;
- boolean escaped = false;
+ return State::removeSemicolons;
+ }
- for (int i = 0; i < raw.length(); i++) {
- char c = raw.charAt(i);
+ private enum Mode {
+ CODE, LINE_COMMENT, BLOCK_COMMENT, SINGLE_QUOTE, DOUBLE_QUOTE, TRIPLE_SINGLE_QUOTE, TRIPLE_DOUBLE_QUOTE, DOLLAR_SLASHY
+ }
- // Check for triple quotes first (needs lookahead)
- if (!inSingleLineComment && !inMultiLineComment && i + 2 < raw.length()) {
- String triple = raw.substring(i, i + 3);
- if ("'''".equals(triple) && !inDoubleQuoteString && !inTripleDoubleQuoteString) {
- inTripleSingleQuoteString = !inTripleSingleQuoteString;
- result.append(triple);
- i += 2;
- continue;
- } else if ("\"\"\"".equals(triple) && !inSingleQuoteString && !inTripleSingleQuoteString) {
- inTripleDoubleQuoteString = !inTripleDoubleQuoteString;
- result.append(triple);
- i += 2;
- continue;
- }
+ private static String removeSemicolons(String raw) {
+ StringBuilder result = new StringBuilder(raw.length());
+ Mode mode = Mode.CODE;
+ // one entry per open bracket, true if it is the '(' of a `for` header
+ Deque brackets = new ArrayDeque<>();
+ int i = 0;
+ while (i < raw.length()) {
+ char c = raw.charAt(i);
+ int consumed = 1;
+ switch (mode) {
+ case CODE:
+ if (raw.startsWith("//", i)) {
+ mode = Mode.LINE_COMMENT;
+ consumed = 2;
+ } else if (raw.startsWith("/*", i)) {
+ mode = Mode.BLOCK_COMMENT;
+ consumed = 2;
+ } else if (raw.startsWith("'''", i)) {
+ mode = Mode.TRIPLE_SINGLE_QUOTE;
+ consumed = 3;
+ } else if (raw.startsWith("\"\"\"", i)) {
+ mode = Mode.TRIPLE_DOUBLE_QUOTE;
+ consumed = 3;
+ } else if (raw.startsWith("$/", i) && (i == 0 || !Character.isJavaIdentifierPart(raw.charAt(i - 1)))) {
+ mode = Mode.DOLLAR_SLASHY;
+ consumed = 2;
+ } else if (c == '\'') {
+ mode = Mode.SINGLE_QUOTE;
+ } else if (c == '"') {
+ mode = Mode.DOUBLE_QUOTE;
+ } else if (c == '(') {
+ brackets.push(isPrecededByFor(raw, i));
+ } else if (c == '{' || c == '[') {
+ brackets.push(Boolean.FALSE);
+ } else if (c == ')' || c == '}' || c == ']') {
+ brackets.poll();
+ } else if (c == ';' && !Boolean.TRUE.equals(brackets.peek()) && isLastOnLine(raw, i)) {
+ i++;
+ continue; // drop the semicolon
}
-
- // Handle escaping
- if (c == '\\' && (inSingleQuoteString || inDoubleQuoteString ||
- inTripleSingleQuoteString || inTripleDoubleQuoteString)) {
- escaped = !escaped;
- result.append(c);
- continue;
+ break;
+ case LINE_COMMENT:
+ if (c == '\n' || c == '\r') {
+ mode = Mode.CODE;
}
-
- // Check for comments (only if not in string)
- if (!inSingleQuoteString && !inDoubleQuoteString &&
- !inTripleSingleQuoteString && !inTripleDoubleQuoteString && !escaped) {
-
- // Single line comment
- if (c == '/' && i + 1 < raw.length() && raw.charAt(i + 1) == '/' && !inMultiLineComment) {
- inSingleLineComment = true;
- }
- // Multi-line comment start
- else if (c == '/' && i + 1 < raw.length() && raw.charAt(i + 1) == '*' && !inSingleLineComment) {
- inMultiLineComment = true;
- }
- // Multi-line comment end
- else if (c == '*' && i + 1 < raw.length() && raw.charAt(i + 1) == '/' && inMultiLineComment) {
- inMultiLineComment = false;
- result.append(c);
- if (i + 1 < raw.length()) {
- result.append(raw.charAt(i + 1));
- i++;
- }
- continue;
- }
+ break;
+ case BLOCK_COMMENT:
+ if (raw.startsWith("*/", i)) {
+ mode = Mode.CODE;
+ consumed = 2;
}
-
- // Check for string quotes (only if not in comment and not already in triple quotes)
- if (!inSingleLineComment && !inMultiLineComment && !escaped) {
- if (c == '\'' && !inDoubleQuoteString && !inTripleSingleQuoteString && !inTripleDoubleQuoteString) {
- inSingleQuoteString = !inSingleQuoteString;
- } else if (c == '"' && !inSingleQuoteString && !inTripleSingleQuoteString && !inTripleDoubleQuoteString) {
- inDoubleQuoteString = !inDoubleQuoteString;
- }
+ break;
+ case SINGLE_QUOTE:
+ case DOUBLE_QUOTE:
+ if (c == '\\') {
+ consumed = 2; // skip the escaped character
+ } else if (c == (mode == Mode.SINGLE_QUOTE ? '\'' : '"') || c == '\n' || c == '\r') {
+ // a newline can't occur in a single-line string, so it means we misread the input: resync
+ mode = Mode.CODE;
}
-
- // End single line comment on newline
- if ((c == '\n' || c == '\r') && inSingleLineComment) {
- inSingleLineComment = false;
+ break;
+ case TRIPLE_SINGLE_QUOTE:
+ case TRIPLE_DOUBLE_QUOTE:
+ if (c == '\\') {
+ consumed = 2; // skip the escaped character, so \''' or \""" doesn't end the string
+ } else if (raw.startsWith(mode == Mode.TRIPLE_SINGLE_QUOTE ? "'''" : "\"\"\"", i)) {
+ mode = Mode.CODE;
+ consumed = 3;
}
-
- // Check if we should remove this semicolon
- if (c == ';' && !inSingleQuoteString && !inDoubleQuoteString &&
- !inTripleSingleQuoteString && !inTripleDoubleQuoteString &&
- !inSingleLineComment && !inMultiLineComment) {
-
- // Look ahead to see if this semicolon is at the end of the line
- boolean isEndOfLine = true;
- for (int j = i + 1; j < raw.length(); j++) {
- char next = raw.charAt(j);
- if (next == '\n' || next == '\r') {
- break; // End of line
- } else if (!Character.isWhitespace(next)) {
- isEndOfLine = false;
- break;
- }
- // If it's whitespace but not newline, continue checking
- }
-
- // Only remove if it's at the end of the line
- if (isEndOfLine) {
- continue; // Skip this semicolon
- }
+ break;
+ case DOLLAR_SLASHY:
+ if (raw.startsWith("$$", i) || raw.startsWith("$/", i)) {
+ consumed = 2; // escaped dollar or slash
+ } else if (raw.startsWith("/$", i)) {
+ mode = Mode.CODE;
+ consumed = 2;
}
-
- result.append(c);
- escaped = false;
+ break;
+ default:
+ throw new IllegalStateException("Unknown mode " + mode);
}
+ int end = Math.min(raw.length(), i + consumed);
+ result.append(raw, i, end);
+ i = end;
+ }
+ return result.toString();
+ }
+
+ private static boolean isPrecededByFor(String raw, int parenIndex) {
+ int j = parenIndex - 1;
+ while (j >= 0 && Character.isWhitespace(raw.charAt(j))) {
+ j--;
+ }
+ return j >= 2 && raw.startsWith("for", j - 2)
+ && (j < 3 || !Character.isJavaIdentifierPart(raw.charAt(j - 3)));
+ }
- return result.toString();
- };
+ private static boolean isLastOnLine(String raw, int semicolonIndex) {
+ for (int j = semicolonIndex + 1; j < raw.length(); j++) {
+ char next = raw.charAt(j);
+ if (next == '\n' || next == '\r') {
+ return true;
+ } else if (!Character.isWhitespace(next)) {
+ return false;
+ }
+ }
+ return true;
}
}
}
diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md
index 144e75ebf9..b232fa92e6 100644
--- a/plugin-gradle/CHANGES.md
+++ b/plugin-gradle/CHANGES.md
@@ -5,9 +5,9 @@ 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))
+- 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))
- `removeUnusedImports()` and `googleJavaFormat()` no longer remove imports that are only used by links in Markdown doc comments (`///`, JDK 23+). ([#3161](https://github.com/diffplug/spotless/issues/3161))
-
-### Fixed
- `forbidWildcardImports` no longer reports import statements inside Java text blocks or comments. ([#2997](https://github.com/diffplug/spotless/issues/2997))
## [8.11.0] - 2026-11-02
diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java
index e1cc198067..19ea0ac5a3 100644
--- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java
+++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java
@@ -30,9 +30,12 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
+import java.util.Collections;
+import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
+import java.util.Set;
import java.util.TreeMap;
import java.util.function.Consumer;
import java.util.function.Function;
@@ -227,6 +230,22 @@ public void encoding(String charset) {
protected FileCollection target;
protected FileCollection targetExclude;
+ /*
+ * Bookkeeping which lets String excludes be applied as fileTree exclude patterns on the String targets, so that
+ * excluded directories are pruned while walking the target instead of subtracting a fileTree which walks the whole
+ * project directory, see https://github.com/diffplug/spotless/issues/3147
+ */
+ /** Trees created from String targets, all rooted at the project dir. */
+ private final Set projectDirTargetTrees = Collections.newSetFromMap(new IdentityHashMap<>());
+ /** The parts of each union created by {@link #parseTargetsIsExclude} for a target. */
+ private final Map> targetUnionParts = new IdentityHashMap<>();
+ /** The {@link #targetExclude} which {@link #targetExcludePrunable} and {@link #targetExcludeRest} were split from. */
+ @Nullable private FileCollection targetExcludeSplitFrom;
+ /** String excludes ending in a {@code **} segment, which exclude exactly the same files as a tree exclude. */
+ private List targetExcludePrunable = List.of();
+ /** The rest of {@link #targetExclude}, or null if it consists only of prunable patterns. */
+ @Nullable private FileCollection targetExcludeRest;
+
/** The value from which files will be excluded if their content contain it. */
@Nullable protected String targetExcludeContentPattern;
@@ -263,9 +282,39 @@ public void target(Object... targets) {
* fileTree, with project.rootDir as the dir. List are treated as the
* 'includes' arg to fileTree, with project.rootDir as the dir. Anything else
* gets passed to getProject().files().
+ *
+ * String patterns which end with {@code /**} (e.g. {@code "**\/build/**"}) are
+ * applied directly to String targets, so the excluded directories are skipped
+ * while walking the target rather than walking the whole project directory.
*/
public void targetExclude(Object... targets) {
this.targetExclude = parseTargetsIsExclude(targets, true);
+ List prunable = new ArrayList<>();
+ List