Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ The plugin allows you to tweak Google Java Format options :
<formatterOptions>
<!-- Use AOSP style instead of Google Style (4-space indentation). -->
<googleJavaFormat.aosp>false</googleJavaFormat.aosp>
<!-- Set the maximum line length, between 1 and 999 (google-java-format 2.0.0 or
later). Leave unset to keep the style default of 100 columns. -->
<!-- <googleJavaFormat.maxLineLength>120</googleJavaFormat.maxLineLength> -->
<!-- Format the javadoc -->
<googleJavaFormat.formatJavadoc>true</googleJavaFormat.formatJavadoc>
<!-- Fix import order and remove any unused imports, but do no other formatting. -->
Expand Down
20 changes: 20 additions & 0 deletions google-java-format/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.6.1</version>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,38 @@
*/
class GoogleJavaFormatterOptions {

/** Sentinel value meaning "use the maximum line length of the selected style". */
private static final int STYLE_DEFAULT_MAX_LINE_LENGTH = 0;

/**
* Upper bound accepted by google-java-format: {@code Doc.MAX_LINE_WIDTH - 1}. Layout widths
* saturate at {@code Doc.MAX_LINE_WIDTH}, which is also how forced breaks are represented.
*/
private static final int MAX_SUPPORTED_MAX_LINE_LENGTH = 999;

private final JavaFormatterOptions.Style style;
private final int maxLineLength;
private final boolean formatJavadoc;
private final boolean fixImportsOnly;
private final boolean skipSortingImports;
private final boolean skipRemovingUnusedImports;

public GoogleJavaFormatterOptions(CodeFormatterConfiguration configuration) {
boolean aosp = configuration.getValue("aosp").map(Boolean::parseBoolean).orElse(false);
if (aosp) {
style = AOSP;
maxLineLength =
configuration
.getValue("maxLineLength")
.map(GoogleJavaFormatterOptions::parseMaxLineLength)
.orElse(STYLE_DEFAULT_MAX_LINE_LENGTH);
if (maxLineLength == STYLE_DEFAULT_MAX_LINE_LENGTH) {
style = aosp ? AOSP : GOOGLE;
} else {
style = GOOGLE;
style =
(aosp
? JavaFormatterOptions.Style.builder().aosp()
: JavaFormatterOptions.Style.builder().google())
.maxLineLength(maxLineLength)
.build();
}
formatJavadoc = configuration.getValue("formatJavadoc").map(Boolean::parseBoolean).orElse(true);
fixImportsOnly =
Expand All @@ -36,6 +56,24 @@ public GoogleJavaFormatterOptions(CodeFormatterConfiguration configuration) {
.orElse(false);
}

private static int parseMaxLineLength(String value) {
final int parsed;
try {
parsed = Integer.parseInt(value.trim());
} catch (NumberFormatException e) {
throw new IllegalArgumentException(
"googleJavaFormat.maxLineLength must be an integer, but was '" + value + "'", e);
}
if (parsed < 1 || parsed > MAX_SUPPORTED_MAX_LINE_LENGTH) {
throw new IllegalArgumentException(
"googleJavaFormat.maxLineLength must be between 1 and "
+ MAX_SUPPORTED_MAX_LINE_LENGTH
+ ", but was "
+ parsed);
}
return parsed;
}

public JavaFormatterOptions javaFormatterOptions() {
return JavaFormatterOptions.builder().style(style).formatJavadoc(formatJavadoc).build();
}
Expand All @@ -57,6 +95,8 @@ public String toString() {
return "GoogleJavaFormatterOptions{"
+ "style="
+ style
+ ", maxLineLength="
+ maxLineLength
+ ", fixImportsOnly="
+ fixImportsOnly
+ ", skipSortingImports="
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.cosium.code.format_gjf;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.cosium.code.format_spi.CodeFormatterConfiguration;
import com.google.googlejavaformat.java.JavaFormatterOptions;
import java.util.Map;
import java.util.Optional;
import org.junit.jupiter.api.Test;

class GoogleJavaFormatterOptionsTest {

@Test
void shouldDefaultToGoogleStyleWithTheStyleDefaultMaxLineLength() {
GoogleJavaFormatterOptions options = new GoogleJavaFormatterOptions(configuration(Map.of()));

JavaFormatterOptions javaFormatterOptions = options.javaFormatterOptions();

assertThat(javaFormatterOptions.style()).isEqualTo(JavaFormatterOptions.Style.GOOGLE);
assertThat(javaFormatterOptions.maxLineLength()).isEqualTo(100);
}

@Test
void shouldUseAospStyle() {
GoogleJavaFormatterOptions options =
new GoogleJavaFormatterOptions(configuration(Map.of("aosp", "true")));

assertThat(options.javaFormatterOptions().style()).isEqualTo(JavaFormatterOptions.Style.AOSP);
}

@Test
void shouldApplyMaxLineLengthToTheSelectedStyle() {
GoogleJavaFormatterOptions options =
new GoogleJavaFormatterOptions(
configuration(Map.of("aosp", "true", "maxLineLength", "120")));

JavaFormatterOptions javaFormatterOptions = options.javaFormatterOptions();

assertThat(javaFormatterOptions.maxLineLength()).isEqualTo(120);
assertThat(javaFormatterOptions.style().isAosp()).isTrue();
assertThat(javaFormatterOptions.indentationMultiplier()).isEqualTo(2);
}

@Test
void shouldRejectNonPositiveMaxLineLength() {
assertThatThrownBy(
() -> new GoogleJavaFormatterOptions(configuration(Map.of("maxLineLength", "0"))))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("googleJavaFormat.maxLineLength");
}

@Test
void shouldRejectMaxLineLengthAboveTheSupportedLimit() {
assertThatThrownBy(
() -> new GoogleJavaFormatterOptions(configuration(Map.of("maxLineLength", "1000"))))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("googleJavaFormat.maxLineLength");
}

@Test
void shouldRejectNonNumericMaxLineLength() {
assertThatThrownBy(
() -> new GoogleJavaFormatterOptions(configuration(Map.of("maxLineLength", "wide"))))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("googleJavaFormat.maxLineLength");
}

private static CodeFormatterConfiguration configuration(Map<String, String> values) {
return key -> Optional.ofNullable(values.get(key));
}
}
Loading