From 63b5d18806a4f266f76cd7f0fc64fb554ff874fa Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Wed, 18 Nov 2026 07:10:45 -0800 Subject: [PATCH] Avoid breaking line comments on non-breaking spaces. `JavaCommentsHelper.wrapLineComments` previously wrapped line comments on `CharMatcher.whitespace()` (which matches U+00A0 NO-BREAK SPACE) while `LINE_COMMENT_MISSING_SPACE_PREFIX` matched ASCII `\s` (which does not match U+00A0). Wrapping a comment at `\u00a0` produced a continuation line starting with `//\u00a0`, and subsequent passes inserted a space (`// \u00a0`) and wrapped at `idx = 3`, inserting a new empty `//` line on every run. Break line comments only on `CharMatcher.breakingWhitespace()`, enable `Pattern.UNICODE_CHARACTER_CLASS` on `LINE_COMMENT_MISSING_SPACE_PREFIX`, and use `CharMatcher.whitespace().trimFrom(...)` in `indentLineComments`. Fixes https://github.com/google/google-java-format/issues/562 PiperOrigin-RevId: 1024644076 --- .../googlejavaformat/java/JavaCommentsHelper.java | 15 +++++++++------ .../googlejavaformat/java/FormatterTest.java | 8 ++++---- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaCommentsHelper.java b/core/src/main/java/com/google/googlejavaformat/java/JavaCommentsHelper.java index 475c35d14..8da3fe31e 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaCommentsHelper.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaCommentsHelper.java @@ -112,15 +112,18 @@ private String preserveIndentation(List lines, int column0) { private String indentLineComments(Tok tok, List lines, int column0) { lines = wrapLineComments(tok, lines, column0); StringBuilder builder = new StringBuilder(); - builder.append(lines.get(0).trim()); String indentString = options.indentString(column0); - for (int i = 1; i < lines.size(); ++i) { - builder.append(lineSeparator).append(indentString).append(lines.get(i).trim()); + for (int i = 0; i < lines.size(); ++i) { + if (i > 0) { + builder.append(lineSeparator).append(indentString); + } + builder.append(CharMatcher.whitespace().trimFrom(lines.get(i))); } return builder.toString(); } - private static final Pattern LINE_COMMENT_MISSING_SPACE_PREFIX = Pattern.compile("^(//+)[^\\s/]"); + private static final Pattern LINE_COMMENT_MISSING_SPACE_PREFIX = + Pattern.compile("^(//+)[^\\s/]", Pattern.UNICODE_CHARACTER_CLASS); // Preserve special `//noinspection` and `//$NON-NLS-x$` comments used by IDEs, which cannot // contain leading spaces. @@ -149,8 +152,8 @@ private List wrapLineComments(Tok tok, List lines, int column0) } while (line.length() + column0 > options.maxLineLength()) { int idx = options.maxLineLength() - column0; - // only break on whitespace characters, and ignore the leading `// ` - while (idx >= 2 && !CharMatcher.whitespace().matches(line.charAt(idx))) { + // only break on breaking whitespace characters, and ignore the leading `// ` + while (idx >= 2 && !CharMatcher.breakingWhitespace().matches(line.charAt(idx))) { idx--; } if (idx <= 2) { diff --git a/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java b/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java index 497497515..494c46853 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java @@ -1360,10 +1360,10 @@ class T { String expected = """ class T { - // Copyright (C) 2017 Interactive Brokers LLC. All rights reserved. This code is subject to the - //\u00a0terms and conditions of the IB API Non-Commercial License or the IB API Commercial License, as - // applicable. - // \u00a0non-breaking space after comment prefix + // Copyright (C) 2017 Interactive Brokers LLC. All rights reserved. This code is subject to + // the\u00a0terms and conditions of the IB API Non-Commercial License or the IB API Commercial License, + // as applicable. + //\u00a0non-breaking space after comment prefix } """; assertThat(new Formatter().formatSource(input)).isEqualTo(expected);