Skip to content

Commit 6d17c15

Browse files
authored
Merge pull request #104 from openjavaformat/gjf-1460-no-lookahead-regex
Rewrite the missing-space rule without a negative lookahead (google-java-format palantir#1460)
2 parents 825828a + 369d3bb commit 6d17c15

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

‎open-java-format/src/main/java/com/palantir/javaformat/java/JavaCommentsHelper.java‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,12 @@ private static boolean isJBangDirective(String text) {
167167
return matcher.lookingAt() && (matcher.group(1) != null || JBANG_DIRECTIVE_NAMES.contains(matcher.group(2)));
168168
}
169169

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

175177
private static String lineCommentPrefix(String line) {
176178
int prefixLength = 0;
@@ -191,7 +193,7 @@ private List<String> wrapLineComments(List<String> lines, int column0) {
191193
line = CharMatcher.whitespace().trimLeadingFrom(line);
192194
// Add missing leading spaces to line comments: `//foo` -> `// foo`.
193195
Matcher matcher = LINE_COMMENT_MISSING_SPACE_PREFIX.matcher(line);
194-
if (matcher.find()) {
196+
if (matcher.find() && !LINE_COMMENT_NO_SPACE_PREFIX.matcher(line).find()) {
195197
int length = matcher.group(1).length();
196198
line = "/".repeat(length) + " " + line.substring(length);
197199
}

‎open-java-format/src/test/java/com/palantir/javaformat/java/FormatterTest.java‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,33 @@ void onlyWrapLineCommentOnWhitespace_noLeadingWhitespace() throws Exception {
368368
+ "}\n");
369369
}
370370

371+
@Test
372+
void lineCommentGetsItsMissingSpaceExceptIdeMarkers() throws Exception {
373+
// `//foo` gets a space after any number of slashes, while the IDE markers `//noinspection` and `//$NON-NLS-n$`
374+
// are recognised by their tools only without one and stay as written.
375+
String input = "class T {\n"
376+
+ " //noinspection unchecked\n"
377+
+ " //$NON-NLS-1$ //$NON-NLS-2$\n"
378+
+ " //$NON-NLS-12$ and more\n"
379+
+ " //foo\n"
380+
+ " ///foo\n"
381+
+ " // bar\n"
382+
+ " // two spaces\n"
383+
+ " void m() {}\n"
384+
+ "}\n";
385+
String expected = "class T {\n"
386+
+ " //noinspection unchecked\n"
387+
+ " //$NON-NLS-1$ //$NON-NLS-2$\n"
388+
+ " //$NON-NLS-12$ and more\n"
389+
+ " // foo\n"
390+
+ " /// foo\n"
391+
+ " // bar\n"
392+
+ " // two spaces\n"
393+
+ " void m() {}\n"
394+
+ "}\n";
395+
assertThat(Formatter.create().formatSource(input)).isEqualTo(expected);
396+
}
397+
371398
@Test
372399
void throwsFormatterException() throws Exception {
373400
assertThatThrownBy(() -> Formatter.create().formatSourceAndFixImports("package foo; public class {"))

0 commit comments

Comments
 (0)