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

Providing explicit support for Groovy source code (fixes #13) #94

Merged
merged 20 commits into from
Apr 10, 2017
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
952fe05
Providing explicit support for Groovy source code (fixes #13)
fvgh Mar 20, 2017
9635398
SkipFilesNamed's equality is based on its serialized representation. …
nedtwigg Apr 3, 2017
0628870
Fixed some grammar and spelling errors in README. Also gave each sec…
nedtwigg Apr 3, 2017
dbfb2c3
Reduced visibility of GroovyExtension.NAME and EXCLUDE_JAVA_DEFAULT.
nedtwigg Apr 3, 2017
115b987
Improved formatting for the gradle plugin README block.
nedtwigg Apr 3, 2017
c8377d1
Publish ext-greclipse 2.3.0.
nedtwigg Apr 3, 2017
97f06c3
Now that greclipse is in jcenter, we can strip out the snapshot stuff.
nedtwigg Apr 3, 2017
196c29d
Added a workaround for the new varargs ambiguity for GroovyExtension …
nedtwigg Apr 3, 2017
4eac44d
Supporting dedicated configuration for groovy source.
fvgh Apr 5, 2017
b9c1358
Fixed greclipseFormat ping/pong. Provided proposal for format.
fvgh Apr 8, 2017
4becbed
Fixes according to review by @jbduncan (as far as possible, using the…
fvgh Apr 8, 2017
cdf45fc
As agreed in previous review: Femoval of unnecessary '\f'
fvgh Apr 8, 2017
25d57ad
Fixed description/values of Groovy-Eclipse configuration example and …
fvgh Apr 9, 2017
b24bf3f
Make GroovyDefaultTargetTest platform independent. Focus on the subje…
fvgh Apr 9, 2017
8fd9a2f
Changed excludeJava to use the same idiom we use for paddedCell().
nedtwigg Apr 9, 2017
cc69b88
ext-greclipse is now in mavenCentral, so the tests can be against mav…
nedtwigg Apr 9, 2017
f6c1c7a
Rather than copy-pasting JavaExtension.LICENSE_HEADER_DELIMITER, we c…
nedtwigg Apr 9, 2017
d78a2b6
Renamed greclipseFormat to just greclipse.
nedtwigg Apr 9, 2017
5a863b9
Minor corrections to groovy section of plugin-gradle/README.md
nedtwigg Apr 9, 2017
027892f
Update changelog and congratulate Frank on his contribution!!
nedtwigg Apr 9, 2017
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ lib('generic.LicenseHeaderStep') +'{{yes}} | {{no}}
lib('generic.ReplaceRegexStep') +'{{yes}} | {{no}} | {{no}} |',
lib('generic.ReplaceStep') +'{{yes}} | {{no}} | {{no}} |',
lib('generic.TrimTrailingWhitespaceStep') +'{{yes}} | {{no}} | {{no}} |',
extra('groovy.GrEclipseFormatterStep') +'{{yes}} | {{no}} | {{no}} |',
lib('java.GoogleJavaFormatStep') +'{{yes}} | {{no}} | {{no}} |',
lib('java.ImportOrderStep') +'{{yes}} | {{no}} | {{no}} |',
extra('java.EclipseFormatterStep') +'{{yes}} | {{no}} | {{no}} |',
Expand All @@ -58,6 +59,7 @@ lib('scala.ScalaFmtStep') +'{{yes}} | {{no}}
| [`generic.ReplaceRegexStep`](lib/src/main/java/com/diffplug/spotless/generic/ReplaceRegexStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [`generic.ReplaceStep`](lib/src/main/java/com/diffplug/spotless/generic/ReplaceStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [`generic.TrimTrailingWhitespaceStep`](lib/src/main/java/com/diffplug/spotless/generic/TrimTrailingWhitespaceStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [`groovy.GrEclipseFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/groovy/GrEclipseFormatterStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [`java.GoogleJavaFormatStep`](lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [`java.ImportOrderStep`](lib/src/main/java/com/diffplug/spotless/java/ImportOrderStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [`java.EclipseFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/java/EclipseFormatterStep.java) | :+1: | :white_large_square: | :white_large_square: |
Expand Down
2 changes: 1 addition & 1 deletion _ext/greclipse/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Mayor/Minor versions are in line with the Groovy version values.
# Patch version is an incremental counter for the Spotless plugin.
grec_version=2.3.0-SNAPSHOT
grec_version=2.3.0
grec_artifactId=spotless-ext-greclipse
grec_description=Groovy Eclipse's formatter bundled for Spotless

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
repositories { maven { url 'https://plugins.gradle.org/m2/' } }
repositories { maven { url 'https://plugins.gradle.org/m2/' }}
Copy link
Member

Choose a reason for hiding this comment

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

This should be formatted like the following (if possible):

repositories {
    maven {
        . . .
    }
}

Copy link
Member Author

Choose a reason for hiding this comment

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

That should work. The formatter should accept the format.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry, but the formatter always replaces nested closures by } } and then starting the ping-pong, unless I reformat it to }}.
Furthermore it cannot convinced to leave closures with one entry split over multiple lines.

dependencies {
classpath "com.diffplug.gradle:goomph:${VER_GOOMPH}"
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:${VER_BINTRAY}"
Expand Down
24 changes: 15 additions & 9 deletions gradle/java-publish.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ javadoc {
// use markdown in javadoc
def makeLink = { url, text -> "<a href=\"${url}\" style=\"text-transform: none;\">${text}</a>" }
def javadocInfo = '<h2>' + makeLink("https://github.com/${org}/${name}", "${group}:${project.ext.artifactId}:${ext.version}") +
' by ' + makeLink('http://www.diffplug.com', 'DiffPlug') + '</h2>'
' by ' + makeLink('http://www.diffplug.com', 'DiffPlug') + '</h2>'
Copy link
Member

Choose a reason for hiding this comment

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

Nice change! 👍


String version_str = ext.version.endsWith('-SNAPSHOT') ? 'snapshot' : ext.version
apply plugin: 'ch.raffael.pegdown-doclet'
Expand Down Expand Up @@ -110,13 +110,14 @@ model {
}
if (project.ext.isSnapshot) {
// upload snapshots to oss.sonatype.org
repositories { maven {
url = 'https://oss.sonatype.org/content/repositories/snapshots'
credentials {
username = cred('nexus_user')
password = cred('nexus_pass')
}
} }
repositories {
maven {
url = 'https://oss.sonatype.org/content/repositories/snapshots'
credentials {
username = cred('nexus_user')
password = cred('nexus_pass')
}
}}
Copy link
Member

Choose a reason for hiding this comment

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

It should be formatted like this (if possible):

repositories {
    maven {
        . . .
    }
}

}
}
}
Expand All @@ -143,5 +144,10 @@ if (!ext.isSnapshot) {
}

publish.dependsOn(bintrayUpload)
bintrayUpload.dependsOn(['generatePomFileForPluginMavenPublication', jar, sourcesJar, javadocJar])
bintrayUpload.dependsOn([
'generatePomFileForPluginMavenPublication',
jar,
sourcesJar,
javadocJar
])
}
11 changes: 4 additions & 7 deletions gradle/java-setup.gradle
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
//////////
// JAVA //
//////////
repositories {
jcenter()
}
repositories { jcenter() }

// setup java
apply plugin: 'java'

sourceCompatibility = VER_JAVA
targetCompatibility = VER_JAVA
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
tasks.withType(JavaCompile) { options.encoding = 'UTF-8' }

/////////////
// ECLIPSE //
Expand All @@ -38,7 +34,8 @@ eclipseResourceFilters {
apply plugin: 'findbugs'
findbugs {
toolVersion = VER_FINDBUGS
sourceSets = [sourceSets.main] // don't check the test code
sourceSets = [
sourceSets.main] // don't check the test code
Copy link
Member

Choose a reason for hiding this comment

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

You should manually reformat this line like this (if possible):

sourceSets = [
    sourceSets.main  // don't check the test code
]

Copy link
Member Author

@fvgh fvgh Apr 8, 2017

Choose a reason for hiding this comment

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

For array I have only a max length. For example with groovy.formatter.longListLength=1, we can assure that all entries are in separate lines. Hence we can archive:

	sourceSets = [
		// don't check the test code
		sourceSets.main
	]

ignoreFailures = false // bug free or it doesn't ship!
reportsDir = file('build/findbugs')
effort = 'max' // min|default|max
Expand Down
3 changes: 1 addition & 2 deletions lib-extra/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,5 @@ dependencies {
}

// we'll hold the core lib to a high standard
findbugs {
reportLevel = 'low' // low|medium|high (low = sensitive to even minor mistakes)
findbugs { reportLevel = 'low' // low|medium|high (low = sensitive to even minor mistakes)
}
Copy link
Member

Choose a reason for hiding this comment

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

It's not clear to me why Greclipse formatted this section like this.

Can we prevent it with a setting?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it is the comment, which is in the same line. The Groovy-Formatter seems only to accept this or method calls.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, that's a shame.

Could we work around this problem with the following manual formatting?

findbugs { reportLevel = 'low' } // low|medium|high (low = sensitive to even minor mistakes)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, that works.

Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2016 DiffPlug
*
* 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.diffplug.spotless.extra.groovy;

import java.io.File;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Objects;
import java.util.Properties;

import com.diffplug.spotless.FileSignature;
import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.FormatterProperties;
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.JarState;
import com.diffplug.spotless.Provisioner;

/** Formatter step which calls out to the Groovy-Eclipse formatter. */
public class GrEclipseFormatterStep {
public static final String defaultVersion() {
return DEFAULT_VERSION;
}

private static final String NAME = "groovy-eclipse formatter";
private static final String FORMATTER_CLASS = "com.diffplug.gradle.spotless.groovy.eclipse.GrEclipseFormatterStepImpl";
private static final String DEFAULT_VERSION = "2.3.0";
private static final String MAVEN_COORDINATE = "com.diffplug.spotless:spotless-ext-greclipse:";
private static final String FORMATTER_METHOD = "format";

/** Creates a formatter step using the default version for the given settings file. */
public static FormatterStep create(Iterable<File> settingsFiles, Provisioner provisioner) {
return create(defaultVersion(), settingsFiles, provisioner);
}

/** Creates a formatter step for the given version and settings file. */
public static FormatterStep create(String version, Iterable<File> settingsFiles, Provisioner provisioner) {
return FormatterStep.createLazy(
NAME,
() -> new State(JarState.from(MAVEN_COORDINATE + version, provisioner), settingsFiles),
State::createFormat);
}

private static class State implements Serializable {
private static final long serialVersionUID = 1L;

/** The jar that contains the eclipse formatter. */
final JarState jarState;
/** The signature of the settings file. */
final FileSignature settings;

State(JarState jar, final Iterable<File> settingsFiles) throws Exception {
this.jarState = Objects.requireNonNull(jar);
this.settings = FileSignature.signAsList(settingsFiles);
}

FormatterFunc createFormat() throws Exception {
FormatterProperties preferences = FormatterProperties.from(settings.files());

ClassLoader classLoader = jarState.getClassLoader();

// instantiate the formatter and get its format method
Class<?> formatterClazz = classLoader.loadClass(FORMATTER_CLASS);
Object formatter = formatterClazz.getConstructor(Properties.class).newInstance(preferences.getProperties());
Method method = formatterClazz.getMethod(FORMATTER_METHOD, String.class);
return input -> {
try {
return (String) method.invoke(formatter, input);
} catch (InvocationTargetException exceptionWrapper) {
Throwable throwable = exceptionWrapper.getTargetException();
Exception exception = (throwable instanceof Exception) ? (Exception) throwable : null;
throw (null == exception) ? exceptionWrapper : exception;
}
};
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@ParametersAreNonnullByDefault
@ReturnValuesAreNonnullByDefault
package com.diffplug.spotless.extra.groovy;

import javax.annotation.ParametersAreNonnullByDefault;

import com.diffplug.spotless.annotations.ReturnValuesAreNonnullByDefault;
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2016 DiffPlug
*
* 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.diffplug.spotless.extra.groovy;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.junit.Test;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.Provisioner;
import com.diffplug.spotless.ResourceHarness;
import com.diffplug.spotless.SerializableEqualityTester;
import com.diffplug.spotless.StepHarness;
import com.diffplug.spotless.TestProvisioner;

public class GrEclipseFormatterStepTest extends ResourceHarness {

private static final String RESOURCE_PATH = "groovy/greclipse/format/";
private static final String CONFIG_FILE = RESOURCE_PATH + "greclipse.properties";

//String is hard-coded in the GrEclipseFormatter
private static final String FORMATTER_FILENAME_REPALCEMENT = "Hello.groovy";

private static Provisioner provisioner() {
return TestProvisioner.jcenter();
}

@Test
public void nominal() throws Throwable {
List<File> config = createTestFiles(CONFIG_FILE);
StepHarness.forStep(GrEclipseFormatterStep.create(config, provisioner()))
.testResource(RESOURCE_PATH + "unformatted.test", RESOURCE_PATH + "formatted.test");
}

@Test
public void formatterException() throws Throwable {
List<File> config = createTestFiles(CONFIG_FILE);
StepHarness.forStep(GrEclipseFormatterStep.create(config, provisioner()))
.testException(RESOURCE_PATH + "exception.test", assertion -> {
assertion.isInstanceOf(IllegalArgumentException.class);
assertion.hasMessageContaining(FORMATTER_FILENAME_REPALCEMENT);
});
}

@Test
public void configurationException() throws Throwable {
String configFileName = "greclipse.exception";
List<File> config = createTestFiles(RESOURCE_PATH + configFileName);
StepHarness.forStep(GrEclipseFormatterStep.create(config, provisioner()))
.testException(RESOURCE_PATH + "unformatted.test", assertion -> {
assertion.isInstanceOf(IllegalArgumentException.class);
assertion.hasMessageContaining(configFileName);
});
}

@Test
public void equality() throws IOException {
List<File> configFile = createTestFiles(CONFIG_FILE);
new SerializableEqualityTester() {

@Override
protected void setupTest(API api) {
api.areDifferentThan();
}

@Override
protected FormatterStep create() {
return GrEclipseFormatterStep.create(configFile, provisioner());
}
}.testEquals();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Class description
*/
class Foo{
String foo

/* Method */
def callBar() {
new Bar().bar(foo)
}
//Compiler error. Missing close-brace.
//
//Note that the error message is additionally logged to system-err
//within GrEclipse itself. That error message is just a dump of a RAW data report.
//The whole error handling therefore looks not nicely readable.
//Since these errors should only occur if the input file is not compile-able,
//the error format is considered acceptable, since the spotless task runs
//in general only after a successful compilation.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Class description
*/
class Foo{
String foo

/* Method */
def callBar() {
new Bar().bar(foo)
}

/** Inner class */
class Bar {

def bar(foo) {
println "${foo}Bar"
}
}
}

def foo = new Foo(foo: 'Foo')
foo.callBar()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The file name extension 'exception' not supported for GrEclipse configuration files.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#Whether to use 'space', 'tab' or 'mixed' (both) characters for indentation.
#The default value is 'tab'.
org.eclipse.jdt.core.formatter.tabulation.char=space

#Number of spaces used for indentation in case 'space' characters
#have been selected. The default value is 4.
org.eclipse.jdt.core.formatter.tabulation.size=2

#Number of spaces used for indentation in case 'mixed' characters
#have been selected. The default value is 4.
org.eclipse.jdt.core.formatter.indentation.size=2

#Whether or not indentation characters are inserted into empty lines.
#The default value is 'true'.
org.eclipse.jdt.core.formatter.indent_empty_lines=false

#Number of spaces used for multiline indentation.
#The default value is 2.
groovy.formatter.multiline.indentation=1

#Length after which list are considered too long. These will be wrapped.
#The default value is 30.
groovy.formatter.longListLength=30

#Whether opening braces position shall be the next line.
#The default value is 'false'.
groovy.formatter.braces.start=true

#Whether closing braces position shall be the next line.
#The default value is 'true'.
groovy.formatter.braces.end=true

#Remove unnecessary semicolons. The default value is 'false'.
groovy.formatter.remove.unnecessary.semicolons=true
Loading