Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,12 @@ private static boolean isJBangDirective(String text) {
return matcher.lookingAt() && (matcher.group(1) != null || JBANG_DIRECTIVE_NAMES.contains(matcher.group(2)));
}

private static final Pattern LINE_COMMENT_MISSING_SPACE_PREFIX = Pattern.compile("^(//+)[^\\s/]");

// Preserve special `//noinspection` and `//$NON-NLS-x$` comments used by IDEs, which cannot
// contain leading spaces.
private static final Pattern LINE_COMMENT_MISSING_SPACE_PREFIX =
Pattern.compile("^(//+)(?!noinspection|\\$NON-NLS-\\d+\\$)[^\\s/]");
private static final Pattern LINE_COMMENT_NO_SPACE_PREFIX =
Pattern.compile("^//+(noinspection|\\$NON-NLS-\\d+\\$)");

private static String lineCommentPrefix(String line) {
int prefixLength = 0;
Expand All @@ -191,7 +193,7 @@ private List<String> wrapLineComments(List<String> lines, int column0) {
line = CharMatcher.whitespace().trimLeadingFrom(line);
// Add missing leading spaces to line comments: `//foo` -> `// foo`.
Matcher matcher = LINE_COMMENT_MISSING_SPACE_PREFIX.matcher(line);
if (matcher.find()) {
if (matcher.find() && !LINE_COMMENT_NO_SPACE_PREFIX.matcher(line).find()) {
int length = matcher.group(1).length();
line = "/".repeat(length) + " " + line.substring(length);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,33 @@ void onlyWrapLineCommentOnWhitespace_noLeadingWhitespace() throws Exception {
+ "}\n");
}

@Test
void lineCommentGetsItsMissingSpaceExceptIdeMarkers() throws Exception {
// `//foo` gets a space after any number of slashes, while the IDE markers `//noinspection` and `//$NON-NLS-n$`
// are recognised by their tools only without one and stay as written.
String input = "class T {\n"
+ " //noinspection unchecked\n"
+ " //$NON-NLS-1$ //$NON-NLS-2$\n"
+ " //$NON-NLS-12$ and more\n"
+ " //foo\n"
+ " ///foo\n"
+ " // bar\n"
+ " // two spaces\n"
+ " void m() {}\n"
+ "}\n";
String expected = "class T {\n"
+ " //noinspection unchecked\n"
+ " //$NON-NLS-1$ //$NON-NLS-2$\n"
+ " //$NON-NLS-12$ and more\n"
+ " // foo\n"
+ " /// foo\n"
+ " // bar\n"
+ " // two spaces\n"
+ " void m() {}\n"
+ "}\n";
assertThat(Formatter.create().formatSource(input)).isEqualTo(expected);
}

@Test
void throwsFormatterException() throws Exception {
assertThatThrownBy(() -> Formatter.create().formatSourceAndFixImports("package foo; public class {"))
Expand Down
Loading