Skip to content

Commit cb4063b

Browse files
cushongoogle-java-format Team
authored andcommitted
Join hyphenated words broken across lines in Javadoc comments.
When a compound word was hyphenated across a line break in Javadoc (e.g., `infinite-\n * precision`), the formatter previously joined the tokens with a space, resulting in `infinite- precision`. Join adjacent word literals across a line break when the preceding literal ends in a hyphen following a word character, while preserving spaces after suspended hyphens (e.g., `pre- and post-`). PiperOrigin-RevId: 993629839
1 parent 0452ffd commit cb4063b

2 files changed

Lines changed: 80 additions & 4 deletions

File tree

‎core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocLexer.java‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,16 @@ private static ImmutableList<Token> joinAdjacentLiteralsAndAdjacentWhitespace(Li
438438
continue;
439439
}
440440

441+
if (seenWhitespace.contains("\n")
442+
&& !hasMultipleNewlines(seenWhitespace)
443+
&& isHyphenatedWordEnd(accumulated)
444+
&& tokens.peek() instanceof Literal literal
445+
&& isWordStart(literal.value())
446+
&& !isSuspendedHyphenConjunction(literal.value())) {
447+
accumulated.append(tokens.next().value());
448+
continue;
449+
}
450+
441451
output.add(new Literal(accumulated.toString()));
442452
accumulated.setLength(0);
443453

@@ -448,13 +458,26 @@ private static ImmutableList<Token> joinAdjacentLiteralsAndAdjacentWhitespace(Li
448458
// We have another token coming. Leave it for the next iteration.
449459
}
450460

451-
/*
452-
* TODO(cpovirk): Another case where we could try to join tokens is if a line ends with
453-
* /[^ -]-/, as in "non-\nblocking."
454-
*/
455461
return output.build();
456462
}
457463

464+
private static boolean isHyphenatedWordEnd(CharSequence cs) {
465+
int length = cs.length();
466+
return length >= 2 && cs.charAt(length - 1) == '-' && isWordChar(cs.charAt(length - 2));
467+
}
468+
469+
private static boolean isWordChar(char c) {
470+
return Character.isLetterOrDigit(c) || c == '_';
471+
}
472+
473+
private static boolean isWordStart(String s) {
474+
return !s.isEmpty() && isWordChar(s.charAt(0));
475+
}
476+
477+
private static boolean isSuspendedHyphenConjunction(String s) {
478+
return s.equals("and") || s.equals("or");
479+
}
480+
458481
private static String consumeAdjacentWhitespace(PeekingIterator<Token> tokens) {
459482
StringBuilder seenWhitespace = new StringBuilder();
460483
while (tokens.peek() instanceof Whitespace) {

‎core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2569,6 +2569,59 @@ class Test {}
25692569
doFormatTest(input, expected);
25702570
}
25712571

2572+
@Test
2573+
public void hyphenatedLineBreakJoined() {
2574+
String input =
2575+
"""
2576+
/**
2577+
* This calculation requires an infinite-
2578+
* precision number.
2579+
*/
2580+
class Test {}
2581+
""";
2582+
String expected =
2583+
"""
2584+
/** This calculation requires an infinite-precision number. */
2585+
class Test {}
2586+
""";
2587+
doFormatTest(input, expected);
2588+
}
2589+
2590+
@Test
2591+
public void suspendedHyphenPreserved() {
2592+
String input =
2593+
"""
2594+
/**
2595+
* Both pre-
2596+
* and post-processing steps.
2597+
*/
2598+
class Test {}
2599+
""";
2600+
String expected =
2601+
"""
2602+
/** Both pre- and post-processing steps. */
2603+
class Test {}
2604+
""";
2605+
doFormatTest(input, expected);
2606+
}
2607+
2608+
@Test
2609+
public void markdownHyphenatedLineBreakJoined() {
2610+
assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue();
2611+
String input =
2612+
"""
2613+
/// This calculation requires an infinite-
2614+
/// precision number.
2615+
class Test {}
2616+
""";
2617+
String expected =
2618+
"""
2619+
/// This calculation requires an infinite-precision number.
2620+
class Test {}
2621+
""";
2622+
doFormatTest(input, expected);
2623+
}
2624+
25722625
// TODO: b/346668798 - Test the following Markdown constructs, and make the tests work as needed.
25732626
// We can assume that the CommonMark parser correctly handles Markdown, so the question is whether
25742627
// they are subsequently mishandled by our formatting logic. So for example the CommonMark parser

0 commit comments

Comments
 (0)