From 5b1c79eac0f70c4e51babe22d7ce954470c9e752 Mon Sep 17 00:00:00 2001 From: Kevin Chen Date: Thu, 31 Aug 2023 12:42:00 -0700 Subject: [PATCH 1/2] Add option to format javadoc --- .../javaformat/java/JavaFormatterOptions.java | 18 ++++++++-- .../palantir/javaformat/java/Formatter.java | 15 +++----- .../javaformat/java/JavaCommentsHelper.java | 6 ++-- .../java/JavaFormatterInternalOptions.java | 35 ------------------- .../java/FormatterIntegrationTest.java | 3 +- .../java/JavadocFormattingTest.java | 4 +-- 6 files changed, 25 insertions(+), 56 deletions(-) delete mode 100644 palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaFormatterInternalOptions.java diff --git a/palantir-java-format-spi/src/main/java/com/palantir/javaformat/java/JavaFormatterOptions.java b/palantir-java-format-spi/src/main/java/com/palantir/javaformat/java/JavaFormatterOptions.java index d78d8995c..e7be557be 100644 --- a/palantir-java-format-spi/src/main/java/com/palantir/javaformat/java/JavaFormatterOptions.java +++ b/palantir-java-format-spi/src/main/java/com/palantir/javaformat/java/JavaFormatterOptions.java @@ -57,8 +57,11 @@ public int maxLineLength() { private final Style style; - private JavaFormatterOptions(Style style) { + private final boolean formatJavadoc; + + private JavaFormatterOptions(Style style, boolean formatJavadoc) { this.style = style; + this.formatJavadoc = formatJavadoc; } /** Returns the multiplier for the unit of indent. */ @@ -70,6 +73,10 @@ public int maxLineLength() { return style.maxLineLength(); } + public boolean formatJavadoc() { + return formatJavadoc; + } + /** Returns the code style. */ public Style style() { return style; @@ -90,6 +97,8 @@ public static final class Builder { // default is still GOOGLE just because lots of hand-rolled tests rely on this behaviour private Style style = Style.GOOGLE; + private boolean formatJavadoc = false; + private Builder() {} public Builder style(Style style) { @@ -97,8 +106,13 @@ public Builder style(Style style) { return this; } + public Builder formatJavadoc(boolean formatJavadoc) { + this.formatJavadoc = formatJavadoc; + return this; + } + public JavaFormatterOptions build() { - return new JavaFormatterOptions(style); + return new JavaFormatterOptions(style, formatJavadoc); } } } diff --git a/palantir-java-format/src/main/java/com/palantir/javaformat/java/Formatter.java b/palantir-java-format/src/main/java/com/palantir/javaformat/java/Formatter.java index b8565a408..538dc9063 100644 --- a/palantir-java-format/src/main/java/com/palantir/javaformat/java/Formatter.java +++ b/palantir-java-format/src/main/java/com/palantir/javaformat/java/Formatter.java @@ -91,26 +91,20 @@ public final class Formatter { private final JavaFormatterOptions options; private final boolean debugMode; - private final JavaFormatterInternalOptions internalOptions; @VisibleForTesting - Formatter(JavaFormatterOptions options, boolean debugMode, JavaFormatterInternalOptions internalOptions) { + Formatter(JavaFormatterOptions options, boolean debugMode) { this.options = options; this.debugMode = debugMode; - this.internalOptions = internalOptions; } /** A new Formatter instance with default options. */ public static Formatter create() { - return new Formatter( - JavaFormatterOptions.defaultOptions(), - false, - JavaFormatterInternalOptions.builder().build()); + return new Formatter(JavaFormatterOptions.defaultOptions(), false); } public static Formatter createFormatter(JavaFormatterOptions options) { - return new Formatter( - options, false, JavaFormatterInternalOptions.builder().build()); + return new Formatter(options, false); } /** @@ -300,8 +294,7 @@ public ImmutableList getFormatReplacements(String input, Collection // 'de-linting' changes (e.g. import ordering). javaInput = ModifierOrderer.reorderModifiers(javaInput, characterRanges); - JavaCommentsHelper commentsHelper = - new JavaCommentsHelper(javaInput.getLineSeparator(), options, internalOptions); + JavaCommentsHelper commentsHelper = new JavaCommentsHelper(javaInput.getLineSeparator(), options); JavaOutput javaOutput; try { javaOutput = format(javaInput, options, commentsHelper, debugMode); diff --git a/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaCommentsHelper.java b/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaCommentsHelper.java index 1547c28a1..c5a93557a 100644 --- a/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaCommentsHelper.java +++ b/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaCommentsHelper.java @@ -32,12 +32,10 @@ public final class JavaCommentsHelper implements CommentsHelper { private final JavaFormatterOptions options; private final JavadocFormatter javadocFormatter; - public JavaCommentsHelper( - String lineSeparator, JavaFormatterOptions options, JavaFormatterInternalOptions internalOptions) { + public JavaCommentsHelper(String lineSeparator, JavaFormatterOptions options) { this.lineSeparator = lineSeparator; this.options = options; - this.javadocFormatter = - internalOptions.reformatJavadoc() ? new JavadocFormatter(options.maxLineLength()) : null; + this.javadocFormatter = options.formatJavadoc() ? new JavadocFormatter(options.maxLineLength()) : null; } @Override diff --git a/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaFormatterInternalOptions.java b/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaFormatterInternalOptions.java deleted file mode 100644 index 295392478..000000000 --- a/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaFormatterInternalOptions.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.palantir.javaformat.java; - -import com.google.errorprone.annotations.Immutable; -import org.immutables.value.Value; - -@Value.Immutable -@Immutable -interface JavaFormatterInternalOptions { - - @Value.Default - default boolean reformatJavadoc() { - return false; - } - - class Builder extends ImmutableJavaFormatterInternalOptions.Builder {} - - static Builder builder() { - return new Builder(); - } -} diff --git a/palantir-java-format/src/test/java/com/palantir/javaformat/java/FormatterIntegrationTest.java b/palantir-java-format/src/test/java/com/palantir/javaformat/java/FormatterIntegrationTest.java index ae7a4710a..397bd1528 100644 --- a/palantir-java-format/src/test/java/com/palantir/javaformat/java/FormatterIntegrationTest.java +++ b/palantir-java-format/src/test/java/com/palantir/javaformat/java/FormatterIntegrationTest.java @@ -87,8 +87,7 @@ private static Formatter createFormatter() { JavaFormatterOptions.builder() .style(JavaFormatterOptions.Style.PALANTIR) .build(), - isDebugMode(), - JavaFormatterInternalOptions.builder().build()); + isDebugMode()); } @TestTemplate diff --git a/palantir-java-format/src/test/java/com/palantir/javaformat/java/JavadocFormattingTest.java b/palantir-java-format/src/test/java/com/palantir/javaformat/java/JavadocFormattingTest.java index 774746977..95f634808 100644 --- a/palantir-java-format/src/test/java/com/palantir/javaformat/java/JavadocFormattingTest.java +++ b/palantir-java-format/src/test/java/com/palantir/javaformat/java/JavadocFormattingTest.java @@ -31,9 +31,9 @@ public final class JavadocFormattingTest { private final Formatter formatter = new Formatter( JavaFormatterOptions.builder() .style(JavaFormatterOptions.Style.GOOGLE) + .formatJavadoc(true) .build(), - false, - JavaFormatterInternalOptions.builder().reformatJavadoc(true).build()); + false); @Test public void notJavadoc() { From 5c8edf4ff11d9d91890831e7d2eec1fec45ee542 Mon Sep 17 00:00:00 2001 From: Kevin Chen Date: Tue, 5 Sep 2023 11:27:51 -0700 Subject: [PATCH 2/2] manual changelog --- changelog/@unreleased/pr-921.v2.yml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 changelog/@unreleased/pr-921.v2.yml diff --git a/changelog/@unreleased/pr-921.v2.yml b/changelog/@unreleased/pr-921.v2.yml new file mode 100644 index 000000000..131df228d --- /dev/null +++ b/changelog/@unreleased/pr-921.v2.yml @@ -0,0 +1,5 @@ +type: feature +feature: + description: Add option to format javadoc + links: + - https://github.com/palantir/palantir-java-format/pull/921