diff --git a/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocFormatter.java b/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocFormatter.java index bb9f70040..469a60704 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocFormatter.java +++ b/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocFormatter.java @@ -56,7 +56,7 @@ public static String formatJavadoc(String input, int blockIndent) { private static String render(List input, int blockIndent) { JavadocWriter output = new JavadocWriter(blockIndent); for (Token token : input) { - switch (token.getType()) { + switch (token.type()) { case BEGIN_JAVADOC -> output.writeBeginJavadoc(); case END_JAVADOC -> { output.writeEndJavadoc(); @@ -105,7 +105,7 @@ private static Token standardizePToken(Token token) { } private static Token standardize(Token token, Token standardToken) { - return SIMPLE_TAG_PATTERN.matcher(token.getValue()).matches() ? standardToken : token; + return SIMPLE_TAG_PATTERN.matcher(token.value()).matches() ? standardToken : token; } private static final Token STANDARD_BR_TOKEN = new Token(BR_TAG, "
"); diff --git a/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocLexer.java b/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocLexer.java index 393249a5e..546d5df35 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocLexer.java +++ b/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocLexer.java @@ -295,8 +295,8 @@ private static ImmutableList joinAdjacentLiteralsAndAdjacentWhitespace(Li StringBuilder accumulated = new StringBuilder(); for (PeekingIterator tokens = peekingIterator(input.iterator()); tokens.hasNext(); ) { - if (tokens.peek().getType() == LITERAL) { - accumulated.append(tokens.peek().getValue()); + if (tokens.peek().type() == LITERAL) { + accumulated.append(tokens.peek().value()); tokens.next(); continue; } @@ -315,14 +315,14 @@ private static ImmutableList joinAdjacentLiteralsAndAdjacentWhitespace(Li } StringBuilder seenWhitespace = new StringBuilder(); - while (tokens.peek().getType() == WHITESPACE) { - seenWhitespace.append(tokens.next().getValue()); + while (tokens.peek().type() == WHITESPACE) { + seenWhitespace.append(tokens.next().value()); } - if (tokens.peek().getType() == LITERAL && tokens.peek().getValue().startsWith("@")) { + if (tokens.peek().type() == LITERAL && tokens.peek().value().startsWith("@")) { // OK, we're in the case described above. accumulated.append(" "); - accumulated.append(tokens.peek().getValue()); + accumulated.append(tokens.peek().value()); tokens.next(); continue; } @@ -355,14 +355,13 @@ private static ImmutableList inferParagraphTags(List input) { ImmutableList.Builder output = ImmutableList.builder(); for (PeekingIterator tokens = peekingIterator(input.iterator()); tokens.hasNext(); ) { - if (tokens.peek().getType() == LITERAL) { + if (tokens.peek().type() == LITERAL) { output.add(tokens.next()); - if (tokens.peek().getType() == WHITESPACE - && hasMultipleNewlines(tokens.peek().getValue())) { + if (tokens.peek().type() == WHITESPACE && hasMultipleNewlines(tokens.peek().value())) { output.add(tokens.next()); - if (tokens.peek().getType() == LITERAL) { + if (tokens.peek().type() == LITERAL) { output.add(new Token(PARAGRAPH_OPEN_TAG, "

")); } } @@ -393,11 +392,11 @@ private static ImmutableList optionalizeSpacesAfterLinks(List inpu ImmutableList.Builder output = ImmutableList.builder(); for (PeekingIterator tokens = peekingIterator(input.iterator()); tokens.hasNext(); ) { - if (tokens.peek().getType() == LITERAL && tokens.peek().getValue().matches("href=[^>]*>")) { + if (tokens.peek().type() == LITERAL && tokens.peek().value().matches("href=[^>]*>")) { output.add(tokens.next()); - if (tokens.peek().getType() == WHITESPACE) { - output.add(new Token(OPTIONAL_LINE_BREAK, tokens.next().getValue())); + if (tokens.peek().type() == WHITESPACE) { + output.add(new Token(OPTIONAL_LINE_BREAK, tokens.next().value())); } } else { output.add(tokens.next()); @@ -422,18 +421,17 @@ private static ImmutableList deindentPreCodeBlocks(List input) { // TODO: b/323389829 - De-indent {@snippet ...} blocks, too. ImmutableList.Builder output = ImmutableList.builder(); for (PeekingIterator tokens = peekingIterator(input.iterator()); tokens.hasNext(); ) { - if (tokens.peek().getType() != PRE_OPEN_TAG) { + if (tokens.peek().type() != PRE_OPEN_TAG) { output.add(tokens.next()); continue; } output.add(tokens.next()); List initialNewlines = new ArrayList<>(); - while (tokens.hasNext() && tokens.peek().getType() == FORCED_NEWLINE) { + while (tokens.hasNext() && tokens.peek().type() == FORCED_NEWLINE) { initialNewlines.add(tokens.next()); } - if (tokens.peek().getType() != LITERAL - || !tokens.peek().getValue().matches("[ \t]*[{]@code")) { + if (tokens.peek().type() != LITERAL || !tokens.peek().value().matches("[ \t]*[{]@code")) { output.addAll(initialNewlines); output.add(tokens.next()); continue; @@ -447,15 +445,15 @@ private static ImmutableList deindentPreCodeBlocks(List input) { private static void deindentPreCodeBlock( ImmutableList.Builder output, PeekingIterator tokens) { Deque saved = new ArrayDeque<>(); - output.add(new Token(LITERAL, tokens.next().getValue().trim())); - while (tokens.hasNext() && tokens.peek().getType() != PRE_CLOSE_TAG) { + output.add(new Token(LITERAL, tokens.next().value().trim())); + while (tokens.hasNext() && tokens.peek().type() != PRE_CLOSE_TAG) { Token token = tokens.next(); saved.addLast(token); } - while (!saved.isEmpty() && saved.peekFirst().getType() == FORCED_NEWLINE) { + while (!saved.isEmpty() && saved.peekFirst().type() == FORCED_NEWLINE) { saved.removeFirst(); } - while (!saved.isEmpty() && saved.peekLast().getType() == FORCED_NEWLINE) { + while (!saved.isEmpty() && saved.peekLast().type() == FORCED_NEWLINE) { saved.removeLast(); } if (saved.isEmpty()) { @@ -465,11 +463,10 @@ private static void deindentPreCodeBlock( // move the trailing `}` to its own line Token last = saved.peekLast(); boolean trailingBrace = false; - if (last.getType() == LITERAL && last.getValue().endsWith("}")) { + if (last.type() == LITERAL && last.value().endsWith("}")) { saved.removeLast(); if (last.length() > 1) { - saved.addLast( - new Token(LITERAL, last.getValue().substring(0, last.getValue().length() - 1))); + saved.addLast(new Token(LITERAL, last.value().substring(0, last.value().length() - 1))); saved.addLast(new Token(FORCED_NEWLINE, null)); } trailingBrace = true; @@ -477,8 +474,8 @@ private static void deindentPreCodeBlock( int trim = -1; for (Token token : saved) { - if (token.getType() == LITERAL) { - int idx = CharMatcher.isNot(' ').indexIn(token.getValue()); + if (token.type() == LITERAL) { + int idx = CharMatcher.isNot(' ').indexIn(token.value()); if (idx != -1 && (trim == -1 || idx < trim)) { trim = idx; } @@ -487,13 +484,11 @@ private static void deindentPreCodeBlock( output.add(new Token(FORCED_NEWLINE, "\n")); for (Token token : saved) { - if (token.getType() == LITERAL) { + if (token.type() == LITERAL) { output.add( new Token( LITERAL, - trim > 0 && token.length() > trim - ? token.getValue().substring(trim) - : token.getValue())); + trim > 0 && token.length() > trim ? token.value().substring(trim) : token.value())); } else { output.add(token); } diff --git a/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocWriter.java b/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocWriter.java index 5e6af1795..0be30ed57 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocWriter.java +++ b/core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocWriter.java @@ -359,7 +359,7 @@ private void writeToken(Token token) { } if (requestedMoeBeginStripComment != null) { - output.append(requestedMoeBeginStripComment.getValue()); + output.append(requestedMoeBeginStripComment.value()); requestedMoeBeginStripComment = null; indentForMoeEndStripComment = innerIndent(); requestNewline(); @@ -367,9 +367,9 @@ private void writeToken(Token token) { return; } - output.append(token.getValue()); + output.append(token.value()); - if (!START_OF_LINE_TOKENS.contains(token.getType())) { + if (!START_OF_LINE_TOKENS.contains(token.type())) { atStartOfLine = false; } diff --git a/core/src/main/java/com/google/googlejavaformat/java/javadoc/Token.java b/core/src/main/java/com/google/googlejavaformat/java/javadoc/Token.java index f74996060..a08f1d1cb 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/javadoc/Token.java +++ b/core/src/main/java/com/google/googlejavaformat/java/javadoc/Token.java @@ -19,7 +19,7 @@ * naturally expect. The decision is usually pragmatic rather than theoretical. Most of the details * are in {@link JavadocLexer}. */ -final class Token { +record Token(Token.Type type, String value) { /** * Javadoc token type. * @@ -108,28 +108,12 @@ enum Type { ; } - private final Type type; - private final String value; - - Token(Type type, String value) { - this.type = type; - this.value = value; - } - - Type getType() { - return type; - } - - String getValue() { - return value; - } - int length() { return value.length(); } @Override public String toString() { - return "\n" + getType() + ": " + getValue(); + return "\n" + type() + ": " + value(); } }