Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: formatter no longer adds whitespace to blank lines #800

Merged
merged 10 commits into from
Sep 11, 2019
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-800.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: fix
fix:
description: 'Don''t add whitespace to blank lines inside comments. Fixes #799'
links:
- https://github.com/palantir/gradle-baseline/pull/800
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ public void apply(Project project) {
java.removeUnusedImports();
// use empty string to specify one group for all non-static imports
java.importOrder("");
java.trimTrailingWhitespace();

if (eclipseFormattingEnabled(project)) {
java.eclipse().configFile(project.file(eclipseXml.toString()));
}

java.trimTrailingWhitespace();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is gonna be different in an IDE vs from gradlew??

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, but formatting from within Intellij didn't produce the whitespace. I suspect that's due to how IntelliJ automatically trims trailing whitespace upon saving.

});

// necessary because SpotlessPlugin creates tasks in an afterEvaluate block
Expand All @@ -65,7 +66,7 @@ public void apply(Project project) {
Task spotlessJava = project.getTasks().getByName("spotlessJava");
Task spotlessApply = project.getTasks().getByName("spotlessApply");
if (eclipseFormattingEnabled(project) && !Files.exists(eclipseXml)) {
spotlessJava.dependsOn(project.getTasks().findByPath(":baselineUpdateConfig"));
spotlessJava.dependsOn(":baselineUpdateConfig");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just style - no need to resolve the actual task ourselves since dependsOn takes a string

}
formatTask.dependsOn(spotlessApply);
project.getTasks().withType(JavaCompile.class).configureEach(spotlessJava::mustRunAfter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.palantir.baseline

import com.google.common.io.Resources
import java.nio.charset.Charset
import org.apache.commons.io.FileUtils
import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.TaskOutcome
Expand Down Expand Up @@ -152,4 +154,23 @@ class BaselineFormatIntegrationTest extends AbstractPluginTest {
BuildResult result = with('spotlessJavaCheck').build()
result.task(":spotlessJava").outcome == TaskOutcome.SUCCESS
}

def 'eclipse format trims blank lines in block or javadoc comment'() {
when:
buildFile << standardBuildFile
file('gradle.properties') << """
com.palantir.baseline-format.eclipse=true
""".stripIndent()
file('src/main/java/test/Test.java').text = resourceAsString("blank-lines-in-comments.java")

then:
BuildResult result = with('format').build()
result.task(":spotlessJavaApply").outcome == TaskOutcome.SUCCESS
file('src/main/java/test/Test.java').text == resourceAsString("blank-lines-in-comments-fixed.java")
}

private String resourceAsString(String fileName) {
Resources.toString(Resources.getResource(this.class, fileName), Charset.defaultCharset())
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package test;

public class Test {
/**
Docstring that looks like a list:

1. hey
2. there

with blank line.
*/
Void test() {
/*
Normal comment

with blank line.
*/
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package test;

public class Test {
/**
Docstring that looks like a list:

1. hey
2. there

with blank line.
*/
Void test() {
/*
Normal comment

with blank line.
*/
}
}