Skip to content

Commit 5586346

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 1f9d2ac commit 5586346

2 files changed

Lines changed: 79 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
@@ -447,6 +447,16 @@ private static ImmutableList<Token> joinAdjacentLiteralsAndAdjacentWhitespace(Li
447447
continue;
448448
}
449449

450+
if (seenWhitespace.contains("\n")
451+
&& !hasMultipleNewlines(seenWhitespace)
452+
&& isHyphenatedWordEnd(accumulated)
453+
&& tokens.peek() instanceof Literal literal
454+
&& isWordStart(literal.value())
455+
&& !isSuspendedHyphenConjunction(literal.value())) {
456+
accumulated.append(tokens.next().value());
457+
continue;
458+
}
459+
450460
output.add(new Literal(accumulated.toString()));
451461
accumulated.setLength(0);
452462

@@ -457,13 +467,26 @@ private static ImmutableList<Token> joinAdjacentLiteralsAndAdjacentWhitespace(Li
457467
// We have another token coming. Leave it for the next iteration.
458468
}
459469

460-
/*
461-
* TODO(cpovirk): Another case where we could try to join tokens is if a line ends with
462-
* /[^ -]-/, as in "non-\nblocking."
463-
*/
464470
return output.build();
465471
}
466472

473+
private static boolean isHyphenatedWordEnd(CharSequence cs) {
474+
int length = cs.length();
475+
return length >= 2 && cs.charAt(length - 1) == '-' && isWordChar(cs.charAt(length - 2));
476+
}
477+
478+
private static boolean isWordChar(char c) {
479+
return Character.isLetterOrDigit(c) || c == '_';
480+
}
481+
482+
private static boolean isWordStart(String s) {
483+
return !s.isEmpty() && isWordChar(s.charAt(0));
484+
}
485+
486+
private static boolean isSuspendedHyphenConjunction(String s) {
487+
return s.equals("and") || s.equals("or");
488+
}
489+
467490
private static String consumeAdjacentWhitespace(PeekingIterator<Token> tokens) {
468491
StringBuilder seenWhitespace = new StringBuilder();
469492
while (tokens.peek() instanceof Whitespace) {

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2571,6 +2571,58 @@ class Test {}
25712571
doFormatTest(input, expected);
25722572
}
25732573

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

0 commit comments

Comments
 (0)