-
Notifications
You must be signed in to change notification settings - Fork 455
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
Changes from 10 commits
952fe05
9635398
0628870
dbfb2c3
115b987
c8377d1
97f06c3
196c29d
4eac44d
b9c1358
4becbed
cdf45fc
25d57ad
b24bf3f
8fd9a2f
cc69b88
f6c1c7a
d78a2b6
5a863b9
027892f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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>' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' | ||
|
@@ -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') | ||
} | ||
}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be formatted like this (if possible): repositories {
maven {
. . .
}
} |
||
} | ||
} | ||
} | ||
|
@@ -143,5 +144,10 @@ if (!ext.isSnapshot) { | |
} | ||
|
||
publish.dependsOn(bintrayUpload) | ||
bintrayUpload.dependsOn(['generatePomFileForPluginMavenPublication', jar, sourcesJar, javadocJar]) | ||
bintrayUpload.dependsOn([ | ||
'generatePomFileForPluginMavenPublication', | ||
jar, | ||
sourcesJar, | ||
javadocJar | ||
]) | ||
} |
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 // | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For array I have only a max length. For example with
|
||
ignoreFailures = false // bug free or it doesn't ship! | ||
reportsDir = file('build/findbugs') | ||
effort = 'max' // min|default|max | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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 { . . . } }
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.