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);