diff --git a/formatters/formatter-api/META-INF/MANIFEST.MF b/formatters/formatter-api/META-INF/MANIFEST.MF new file mode 100755 index 000000000..83ad0ce01 --- /dev/null +++ b/formatters/formatter-api/META-INF/MANIFEST.MF @@ -0,0 +1,8 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-SymbolicName: formatter-api +Bundle-Version: 1.9.0.qualifier +Bundle-RequiredExecutionEnvironment: JavaSE-1.8 +Require-Bundle: org.junit;bundle-version="4.12.0", + com.google.guava;bundle-version="21.0.0" +Export-Package: com.marvinformatics.formatter diff --git a/formatters/jdt-core/build.properties b/formatters/formatter-api/build.properties similarity index 100% rename from formatters/jdt-core/build.properties rename to formatters/formatter-api/build.properties diff --git a/formatters/jsdt-core/pom.xml b/formatters/formatter-api/pom.xml similarity index 89% rename from formatters/jsdt-core/pom.xml rename to formatters/formatter-api/pom.xml index 378b5e264..4d44228e3 100755 --- a/formatters/jsdt-core/pom.xml +++ b/formatters/formatter-api/pom.xml @@ -25,7 +25,11 @@ 1.9.0-SNAPSHOT - jsdt-core + formatter-api eclipse-plugin + + formatter.java. + + diff --git a/maven-plugin/src/test/java/com/marvinformatics/formatter/AbstractFormatterTest.java b/formatters/formatter-api/src/com/marvinformatics/formatter/AbstractFormatterTest.java similarity index 65% rename from maven-plugin/src/test/java/com/marvinformatics/formatter/AbstractFormatterTest.java rename to formatters/formatter-api/src/com/marvinformatics/formatter/AbstractFormatterTest.java index 84544931f..1e0c1e52d 100755 --- a/maven-plugin/src/test/java/com/marvinformatics/formatter/AbstractFormatterTest.java +++ b/formatters/formatter-api/src/com/marvinformatics/formatter/AbstractFormatterTest.java @@ -26,8 +26,6 @@ import java.util.Map; import java.util.Random; -import org.apache.maven.plugin.logging.Log; -import org.apache.maven.plugin.logging.SystemStreamLog; import org.junit.Before; import org.junit.Test; @@ -37,6 +35,7 @@ public abstract class AbstractFormatterTest { + private static final String OUTPUT_DIR = "target/formatter-files/"; private Formatter formatter; @Before @@ -51,10 +50,6 @@ public File getTargetDirectory() { return targetDir; } - public Log getLog() { - return new SystemStreamLog(); - } - public Charset getEncoding() { return Charsets.UTF_8; } @@ -80,6 +75,27 @@ public LineEnding lineEnding() { public boolean isDryRun() { return false; } + + @Override + public void info(String message) { + System.out.println("INFO " + message); + } + + @Override + public void error(String message) { + System.out.println("ERROR " + message); + } + + @Override + public void debug(String message) { + System.out.println("DEBUG " + message); + } + + @Override + public void warn(String message, File sourceFile, Exception e) { + System.out.println("WARN " + message); + throw new RuntimeException(e); + } }); } @@ -90,20 +106,25 @@ public void tuneDefaultConfigs(Map options) { @Test public void doTestFormat() throws IOException, NoSuchAlgorithmException { - File originalSourceFile = new File("src/test/resources/", fileUnderTest()); - File sourceFile = createUnformatedFile(originalSourceFile); + new File(OUTPUT_DIR, fileUnderTest()).mkdirs(); - Result r = formatter.formatFile(sourceFile.toPath()); - assertEquals(Result.SUCCESS, r); + File originalFile = new File(OUTPUT_DIR, fileUnderTest() + "/original"); + Files.copy(new File("src/test/resources/sample/", fileUnderTest()), originalFile); - String originalContent = Files.toString(originalSourceFile, Charsets.UTF_8); - String formattedContent = Files.toString(sourceFile, Charsets.UTF_8); + File unformattedFile = createUnformatedFile(originalFile); + String unformattedContent = Files.toString(unformattedFile, Charsets.UTF_8); - String msg = "Files: \n-" + originalSourceFile.getAbsolutePath() + "\n-" + sourceFile.getAbsolutePath(); + String originalContent = Files.toString(originalFile, Charsets.UTF_8); + String formattedContent = formatter.format(unformattedContent); + + File formattedFile = new File(OUTPUT_DIR, fileUnderTest() + "/formatted"); + Files.write(formattedContent, formattedFile, Charsets.UTF_8); + + String msg = "Files: \n-" + originalFile.getAbsolutePath() + "\n-" + formattedFile.getAbsolutePath(); assertEquals(msg, originalContent, formattedContent); - String expectedSha1 = Files.hash(originalSourceFile, Hashing.sha1()).toString(); - String sha1 = Files.hash(sourceFile, Hashing.sha1()).toString(); + String expectedSha1 = Files.hash(originalFile, Hashing.sha1()).toString(); + String sha1 = Files.hash(formattedFile, Hashing.sha1()).toString(); assertEquals(msg, expectedSha1, sha1); } @@ -111,11 +132,9 @@ public void doTestFormat() throws IOException, NoSuchAlgorithmException { public abstract String fileUnderTest(); private File createUnformatedFile(File originalSourceFile) throws IOException { - File unformatedFile = new File("target/test-classes/", fileUnderTest()); + File unformatedFile = new File(OUTPUT_DIR, fileUnderTest() + "/unformatted"); - Files.copy(originalSourceFile, unformatedFile); - - List content = Files.readLines(unformatedFile, Charsets.UTF_8); + List content = Files.readLines(originalSourceFile, Charsets.UTF_8); StringBuilder messedContent = new StringBuilder(); for (String line : content) @@ -136,7 +155,10 @@ private StringBuilder randomSeparator() { StringBuilder separator = new StringBuilder(); for (int i = 0; i < spaces; i++) - separator.append(' '); + if (i % 3 == 0) + separator.append('\t'); + else + separator.append(' '); return separator; } diff --git a/maven-plugin/src/main/java/com/marvinformatics/formatter/ConfigurationSource.java b/formatters/formatter-api/src/com/marvinformatics/formatter/ConfigurationSource.java similarity index 87% rename from maven-plugin/src/main/java/com/marvinformatics/formatter/ConfigurationSource.java rename to formatters/formatter-api/src/com/marvinformatics/formatter/ConfigurationSource.java index 035555ca3..d711152f5 100755 --- a/maven-plugin/src/main/java/com/marvinformatics/formatter/ConfigurationSource.java +++ b/formatters/formatter-api/src/com/marvinformatics/formatter/ConfigurationSource.java @@ -18,15 +18,11 @@ import java.io.File; import java.nio.charset.Charset; -import org.apache.maven.plugin.logging.Log; - /** * @author marvin.froeder */ public interface ConfigurationSource { - Log getLog(); - String getCompilerSources(); String getCompilerCompliance(); @@ -41,4 +37,12 @@ public interface ConfigurationSource { boolean isDryRun(); + void info(String message); + + void error(String message); + + void debug(String message); + + void warn(String message, File sourceFile, Exception e); + } diff --git a/maven-plugin/src/main/java/com/marvinformatics/formatter/Formatter.java b/formatters/formatter-api/src/com/marvinformatics/formatter/Formatter.java similarity index 87% rename from maven-plugin/src/main/java/com/marvinformatics/formatter/Formatter.java rename to formatters/formatter-api/src/com/marvinformatics/formatter/Formatter.java index 4b46b72e7..3e04c55c7 100755 --- a/maven-plugin/src/main/java/com/marvinformatics/formatter/Formatter.java +++ b/formatters/formatter-api/src/com/marvinformatics/formatter/Formatter.java @@ -15,8 +15,6 @@ */ package com.marvinformatics.formatter; -import java.nio.file.Path; - /** * @author marvin.froeder */ @@ -24,10 +22,10 @@ public interface Formatter { /** * Format individual file. - * - * @param file - * @return + * + * @param originalCode code to be formatted + * @return formatted code */ - public abstract Result formatFile(Path file); + String format(String originalCode); } diff --git a/maven-plugin/src/main/java/com/marvinformatics/formatter/LineEnding.java b/formatters/formatter-api/src/com/marvinformatics/formatter/LineEnding.java similarity index 100% rename from maven-plugin/src/main/java/com/marvinformatics/formatter/LineEnding.java rename to formatters/formatter-api/src/com/marvinformatics/formatter/LineEnding.java diff --git a/formatters/groovy-formatter-test/pom.xml b/formatters/groovy-formatter-test/pom.xml new file mode 100755 index 000000000..7e8a7f825 --- /dev/null +++ b/formatters/groovy-formatter-test/pom.xml @@ -0,0 +1,59 @@ + + + + 4.0.0 + + + com.marvinformatics.formatter + formatters + 1.9.0-SNAPSHOT + + + groovy-formatter-test + jar + + + + greclipse + http://dist.springsource.org/snapshot/GRECLIPSE/e4.7/ + p2 + + + + + + com.marvinformatics.formatter + formatter-api + 1.9.0-SNAPSHOT + + + com.marvinformatics.formatter + groovy-formatter + 1.9.0-SNAPSHOT + + + + junit + junit + 4.12 + test + + + + diff --git a/formatters/groovy-formatter-test/src/test/java/com/marvinformatics/formatter/groovy/GroovyFormatterTest.java b/formatters/groovy-formatter-test/src/test/java/com/marvinformatics/formatter/groovy/GroovyFormatterTest.java new file mode 100644 index 000000000..4597f8bdc --- /dev/null +++ b/formatters/groovy-formatter-test/src/test/java/com/marvinformatics/formatter/groovy/GroovyFormatterTest.java @@ -0,0 +1,45 @@ +/** + * Copyright (C) 2010 Marvin Herman Froeder (marvin@marvinformatics.com) + * + * 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.marvinformatics.formatter.groovy; + +import java.util.Map; + +import com.marvinformatics.formatter.AbstractFormatterTest; +import com.marvinformatics.formatter.ConfigurationSource; +import com.marvinformatics.formatter.Formatter; +import com.marvinformatics.formatter.groovy.GroovyFormatter; + +/** + * @author marvin.froeder + */ +public class GroovyFormatterTest extends AbstractFormatterTest { + + @Override + public Formatter createFormatter(Map options, ConfigurationSource configurationSource) { + return new GroovyFormatter(options); + } + + @Override + public void tuneDefaultConfigs(Map options) { + options.put("groovy.formatter.remove.unnecessary.semicolons", "false"); + } + + @Override + public String fileUnderTest() { + return "AnyClass.groovy"; + } + +} diff --git a/formatters/groovy-formatter-test/src/test/resources/sample/AnyClass.groovy b/formatters/groovy-formatter-test/src/test/resources/sample/AnyClass.groovy new file mode 100644 index 000000000..54b122c9d --- /dev/null +++ b/formatters/groovy-formatter-test/src/test/resources/sample/AnyClass.groovy @@ -0,0 +1,37 @@ +/** + * Copyright (C) 2010 Marvin Herman Froeder (marvin@marvinformatics.com) + * + * 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. + */ +/** + * 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() \ No newline at end of file diff --git a/formatters/groovy-formatter/META-INF/MANIFEST.MF b/formatters/groovy-formatter/META-INF/MANIFEST.MF new file mode 100755 index 000000000..72a5896bd --- /dev/null +++ b/formatters/groovy-formatter/META-INF/MANIFEST.MF @@ -0,0 +1,15 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-SymbolicName: groovy-formatter +Bundle-Version: 1.9.0.qualifier +Require-Bundle: formatter-api, + org.codehaus.groovy.eclipse.core;bundle-version="[2.9.0,10.0.0)", + org.codehaus.groovy.eclipse.refactoring;bundle-version="[2.9.0,10.0.0)", + org.eclipse.jdt.groovy.core, + org.eclipse.text, + org.eclipse.jface.text, + org.eclipse.jdt.core, + org.codehaus.groovy, + org.codehaus.groovy.eclipse +Bundle-RequiredExecutionEnvironment: JavaSE-1.8 +Export-Package: com.marvinformatics.formatter.groovy diff --git a/formatters/jsdt-core/build.properties b/formatters/groovy-formatter/build.properties similarity index 100% rename from formatters/jsdt-core/build.properties rename to formatters/groovy-formatter/build.properties diff --git a/formatters/groovy-formatter/pom.xml b/formatters/groovy-formatter/pom.xml new file mode 100755 index 000000000..e0bc4773a --- /dev/null +++ b/formatters/groovy-formatter/pom.xml @@ -0,0 +1,53 @@ + + + + 4.0.0 + + + com.marvinformatics.formatter + formatters + 1.9.0-SNAPSHOT + + + groovy-formatter + eclipse-plugin + + + + + + + + greclipse + http://dist.springsource.org/snapshot/GRECLIPSE/e4.7/ + p2 + + + + + + formatter-jars + + + true + + + + + diff --git a/formatters/groovy-formatter/shade b/formatters/groovy-formatter/shade new file mode 100644 index 000000000..e69de29bb diff --git a/formatters/groovy-formatter/src/com/marvinformatics/formatter/groovy/GroovyFormatter.java b/formatters/groovy-formatter/src/com/marvinformatics/formatter/groovy/GroovyFormatter.java new file mode 100755 index 000000000..d32c443d2 --- /dev/null +++ b/formatters/groovy-formatter/src/com/marvinformatics/formatter/groovy/GroovyFormatter.java @@ -0,0 +1,73 @@ +/** + * Copyright (C) 2010 Marvin Herman Froeder (marvin@marvinformatics.com) + * + * 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.marvinformatics.formatter.groovy; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import org.codehaus.groovy.eclipse.refactoring.formatter.DefaultGroovyFormatter; +import org.codehaus.groovy.eclipse.refactoring.formatter.FormatterPreferencesOnStore; +import org.eclipse.jface.preference.PreferenceStore; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.Document; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.TextSelection; +import org.eclipse.jface.util.ISafeRunnableRunner; +import org.eclipse.text.edits.MalformedTreeException; +import org.eclipse.text.edits.TextEdit; +import org.xml.sax.SAXException; + +import com.marvinformatics.formatter.Formatter; + +public class GroovyFormatter implements Formatter { + + private static final List> RUNTIME_DEPENDENCIES = Arrays.asList(ISafeRunnableRunner.class, + SAXException.class); + + private final DefaultGroovyFormatter formatter; + + public GroovyFormatter(Map options) { + RUNTIME_DEPENDENCIES.forEach(Class::getName); + + IDocument doc = new Document(); + PreferenceStore preferences = createPreferences(options); + FormatterPreferencesOnStore preferencesStore = new FormatterPreferencesOnStore(preferences); + formatter = new DefaultGroovyFormatter(TextSelection.emptySelection(), doc, preferencesStore, false); + } + + public String format(String code) { + TextEdit te = formatter.format(); + if (te == null) + throw new IllegalArgumentException( + "Code cannot be formatted. Possible cause " + "is unmatched source/target/compliance version."); + + IDocument doc = new Document(code); + try { + te.apply(doc); + } catch (MalformedTreeException | BadLocationException e) { + throw new IllegalStateException("Code cannot be formatted. original code:\n" + code); + } + return doc.get(); + } + + private static PreferenceStore createPreferences(final Map options) { + final PreferenceStore preferences = new PreferenceStore(); + options.forEach(preferences::putValue); + return preferences; + } + +} diff --git a/formatters/java-formatter-test/pom.xml b/formatters/java-formatter-test/pom.xml new file mode 100755 index 000000000..2b650127e --- /dev/null +++ b/formatters/java-formatter-test/pom.xml @@ -0,0 +1,51 @@ + + + + 4.0.0 + + + com.marvinformatics.formatter + formatters + 1.9.0-SNAPSHOT + + + java-formatter-test + jar + + + + com.marvinformatics.formatter + formatter-api + 1.9.0-SNAPSHOT + + + com.marvinformatics.formatter + java-formatter + 1.9.0-SNAPSHOT + + + + junit + junit + 4.12 + test + + + + diff --git a/maven-plugin/src/test/java/com/marvinformatics/formatter/java/JavaFormatterTest.java b/formatters/java-formatter-test/src/test/java/com/marvinformatics/formatter/java/JavaFormatterTest.java similarity index 90% rename from maven-plugin/src/test/java/com/marvinformatics/formatter/java/JavaFormatterTest.java rename to formatters/java-formatter-test/src/test/java/com/marvinformatics/formatter/java/JavaFormatterTest.java index a739bc2d4..72a2c6125 100755 --- a/maven-plugin/src/test/java/com/marvinformatics/formatter/java/JavaFormatterTest.java +++ b/formatters/java-formatter-test/src/test/java/com/marvinformatics/formatter/java/JavaFormatterTest.java @@ -18,7 +18,6 @@ import java.util.Map; import com.marvinformatics.formatter.AbstractFormatterTest; -import com.marvinformatics.formatter.CacheableFormatter; import com.marvinformatics.formatter.ConfigurationSource; import com.marvinformatics.formatter.Formatter; @@ -29,12 +28,12 @@ public class JavaFormatterTest extends AbstractFormatterTest { @Override public Formatter createFormatter(Map options, ConfigurationSource configurationSource) { - return new CacheableFormatter(configurationSource, new JavaFormatter( + return new JavaFormatter( options, configurationSource.getCompilerSources(), configurationSource.getCompilerCompliance(), configurationSource.getCompilerCodegenTargetPlatform(), - configurationSource.lineEnding().getChars())::doFormat); + configurationSource.lineEnding()); } @Override diff --git a/maven-plugin/src/test/resources/AnyClass.java b/formatters/java-formatter-test/src/test/resources/sample/AnyClass.java similarity index 97% rename from maven-plugin/src/test/resources/AnyClass.java rename to formatters/java-formatter-test/src/test/resources/sample/AnyClass.java index 183333eb2..517df768e 100755 --- a/maven-plugin/src/test/resources/AnyClass.java +++ b/formatters/java-formatter-test/src/test/resources/sample/AnyClass.java @@ -5,7 +5,7 @@ * 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 + * 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, diff --git a/formatters/java-formatter/META-INF/MANIFEST.MF b/formatters/java-formatter/META-INF/MANIFEST.MF new file mode 100755 index 000000000..abf1d2b98 --- /dev/null +++ b/formatters/java-formatter/META-INF/MANIFEST.MF @@ -0,0 +1,13 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Eclipse JDT core +Bundle-SymbolicName: java-formatter +Bundle-Version: 1.9.0.qualifier +Require-Bundle: formatter-api, + org.eclipse.jdt.core;bundle-version="[3.13.0,10.0.0)", + org.eclipse.text, + org.eclipse.jface.text, + org.eclipse.core.runtime, + org.eclipse.core.resources +Bundle-RequiredExecutionEnvironment: JavaSE-1.8 +Export-Package: com.marvinformatics.formatter.java diff --git a/formatters/java-formatter/build.properties b/formatters/java-formatter/build.properties new file mode 100755 index 000000000..44bffff3b --- /dev/null +++ b/formatters/java-formatter/build.properties @@ -0,0 +1,20 @@ +# +# Copyright (C) 2010 Marvin Herman Froeder (marvin@marvinformatics.com) +# +# 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. +# + +source.. = src/ +output.. = target/classes +bin.includes = META-INF/,\ + . diff --git a/formatters/jdt-core/pom.xml b/formatters/java-formatter/pom.xml similarity index 89% rename from formatters/jdt-core/pom.xml rename to formatters/java-formatter/pom.xml index 437ea4b05..9e5f74118 100755 --- a/formatters/jdt-core/pom.xml +++ b/formatters/java-formatter/pom.xml @@ -25,7 +25,11 @@ 1.9.0-SNAPSHOT - jdt-core + java-formatter eclipse-plugin + + formatter.java. + + diff --git a/formatters/java-formatter/shade b/formatters/java-formatter/shade new file mode 100644 index 000000000..e69de29bb diff --git a/formatters/jdt-core/src/com/marvinformatics/formatter/java/JavaFormatter.java b/formatters/java-formatter/src/com/marvinformatics/formatter/java/JavaFormatter.java similarity index 82% rename from formatters/jdt-core/src/com/marvinformatics/formatter/java/JavaFormatter.java rename to formatters/java-formatter/src/com/marvinformatics/formatter/java/JavaFormatter.java index 69782a3b5..d9079dee0 100755 --- a/formatters/jdt-core/src/com/marvinformatics/formatter/java/JavaFormatter.java +++ b/formatters/java-formatter/src/com/marvinformatics/formatter/java/JavaFormatter.java @@ -15,6 +15,7 @@ */ package com.marvinformatics.formatter.java; +import java.util.HashMap; import java.util.Map; import org.eclipse.jdt.core.JavaCore; @@ -26,27 +27,32 @@ import org.eclipse.text.edits.MalformedTreeException; import org.eclipse.text.edits.TextEdit; -public class JavaFormatter { +import com.marvinformatics.formatter.Formatter; +import com.marvinformatics.formatter.LineEnding; + +public class JavaFormatter implements Formatter { private final CodeFormatter formatter; - private final String lineEnding; + private final LineEnding lineEnding; public JavaFormatter( Map options, String compilerSources, String compilerCompliance, String compilerCodegenTargetPlatform, - String lineEnding) { + LineEnding lineEnding) { + options.put(JavaCore.COMPILER_SOURCE, compilerSources); options.put(JavaCore.COMPILER_COMPLIANCE, compilerCompliance); options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, compilerCodegenTargetPlatform); - formatter = ToolFactory.createCodeFormatter(options); + formatter = ToolFactory.createCodeFormatter(new HashMap<>(options)); this.lineEnding = lineEnding; } - public String doFormat(String code) { - TextEdit te = formatter.format(CodeFormatter.K_COMPILATION_UNIT, code, 0, code.length(), 0, lineEnding); + public String format(String code) { + TextEdit te = formatter.format(CodeFormatter.K_COMPILATION_UNIT, code, 0, code.length(), 0, + lineEnding.getChars()); if (te == null) throw new IllegalArgumentException( "Code cannot be formatted. Possible cause " + "is unmatched source/target/compliance version."); diff --git a/formatters/javascript-formatter-test/pom.xml b/formatters/javascript-formatter-test/pom.xml new file mode 100755 index 000000000..26a235365 --- /dev/null +++ b/formatters/javascript-formatter-test/pom.xml @@ -0,0 +1,59 @@ + + + + + 4.0.0 + + + com.marvinformatics.formatter + formatters + 1.9.0-SNAPSHOT + + + javascript-formatter-test + jar + + + + com.marvinformatics.formatter + formatter-api + 1.9.0-SNAPSHOT + + + com.marvinformatics.formatter + javascript-formatter + 1.9.0-SNAPSHOT + + + + junit + junit + 4.12 + test + + + + diff --git a/maven-plugin/src/test/java/com/marvinformatics/formatter/javascript/JavascriptFormatterTest.java b/formatters/javascript-formatter-test/src/test/java/com/marvinformatics/formatter/javascript/JavascriptFormatterTest.java similarity index 87% rename from maven-plugin/src/test/java/com/marvinformatics/formatter/javascript/JavascriptFormatterTest.java rename to formatters/javascript-formatter-test/src/test/java/com/marvinformatics/formatter/javascript/JavascriptFormatterTest.java index 7f97be05e..f01773fc2 100755 --- a/maven-plugin/src/test/java/com/marvinformatics/formatter/javascript/JavascriptFormatterTest.java +++ b/formatters/javascript-formatter-test/src/test/java/com/marvinformatics/formatter/javascript/JavascriptFormatterTest.java @@ -18,17 +18,17 @@ import java.util.Map; import com.marvinformatics.formatter.AbstractFormatterTest; -import com.marvinformatics.formatter.CacheableFormatter; import com.marvinformatics.formatter.ConfigurationSource; import com.marvinformatics.formatter.Formatter; +import com.marvinformatics.formatter.javascript.JavascriptFormatter; public class JavascriptFormatterTest extends AbstractFormatterTest { @Override public Formatter createFormatter(Map options, ConfigurationSource configurationSource) { - return new CacheableFormatter(configurationSource, new JavascriptFormatter( + return new JavascriptFormatter( options, - configurationSource.lineEnding().getChars())::doFormat); + configurationSource.lineEnding()); } @Override diff --git a/maven-plugin/src/test/resources/AnyJS.js b/formatters/javascript-formatter-test/src/test/resources/sample/AnyJS.js similarity index 52% rename from maven-plugin/src/test/resources/AnyJS.js rename to formatters/javascript-formatter-test/src/test/resources/sample/AnyJS.js index 9b8bef355..78c204687 100755 --- a/maven-plugin/src/test/resources/AnyJS.js +++ b/formatters/javascript-formatter-test/src/test/resources/sample/AnyJS.js @@ -1,3 +1,18 @@ +/* + * Copyright (C) 2010 Marvin Herman Froeder (marvin@marvinformatics.com) + * + * 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. + */ function APITest(expected, actual, message) { it('should implement the API: ' + (message || ''), function() { APITest.testKind(expected, actual, message); diff --git a/formatters/javascript-formatter/META-INF/MANIFEST.MF b/formatters/javascript-formatter/META-INF/MANIFEST.MF new file mode 100755 index 000000000..782590a70 --- /dev/null +++ b/formatters/javascript-formatter/META-INF/MANIFEST.MF @@ -0,0 +1,9 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-SymbolicName: javascript-formatter +Bundle-Version: 1.9.0.qualifier +Require-Bundle: org.eclipse.wst.jsdt.core;bundle-version="[2.0.0,10.0.0)", + org.eclipse.jface.text, + formatter-api +Bundle-RequiredExecutionEnvironment: JavaSE-1.8 +Export-Package: com.marvinformatics.formatter.javascript diff --git a/formatters/javascript-formatter/build.properties b/formatters/javascript-formatter/build.properties new file mode 100755 index 000000000..44bffff3b --- /dev/null +++ b/formatters/javascript-formatter/build.properties @@ -0,0 +1,20 @@ +# +# Copyright (C) 2010 Marvin Herman Froeder (marvin@marvinformatics.com) +# +# 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. +# + +source.. = src/ +output.. = target/classes +bin.includes = META-INF/,\ + . diff --git a/formatters/javascript-formatter/pom.xml b/formatters/javascript-formatter/pom.xml new file mode 100755 index 000000000..335f933f5 --- /dev/null +++ b/formatters/javascript-formatter/pom.xml @@ -0,0 +1,35 @@ + + + + 4.0.0 + + + com.marvinformatics.formatter + formatters + 1.9.0-SNAPSHOT + + + javascript-formatter + eclipse-plugin + + + formatter.javascript. + + + diff --git a/formatters/javascript-formatter/shade b/formatters/javascript-formatter/shade new file mode 100644 index 000000000..e69de29bb diff --git a/formatters/jsdt-core/src/com/marvinformatics/formatter/javascript/JavascriptFormatter.java b/formatters/javascript-formatter/src/com/marvinformatics/formatter/javascript/JavascriptFormatter.java similarity index 83% rename from formatters/jsdt-core/src/com/marvinformatics/formatter/javascript/JavascriptFormatter.java rename to formatters/javascript-formatter/src/com/marvinformatics/formatter/javascript/JavascriptFormatter.java index becd2dbbf..9478c8eb9 100755 --- a/formatters/jsdt-core/src/com/marvinformatics/formatter/javascript/JavascriptFormatter.java +++ b/formatters/javascript-formatter/src/com/marvinformatics/formatter/javascript/JavascriptFormatter.java @@ -25,19 +25,22 @@ import org.eclipse.wst.jsdt.core.ToolFactory; import org.eclipse.wst.jsdt.core.formatter.CodeFormatter; -public class JavascriptFormatter { +import com.marvinformatics.formatter.Formatter; +import com.marvinformatics.formatter.LineEnding; + +public class JavascriptFormatter implements Formatter { private final CodeFormatter formatter; - private final String lineEnding; + private final LineEnding lineEnding; - public JavascriptFormatter(Map options, String lineEnding) { + public JavascriptFormatter(Map options, LineEnding lineEnding) { this.formatter = ToolFactory.createCodeFormatter(options); this.lineEnding = lineEnding; } - public String doFormat(String code) { + public String format(String code) { TextEdit te = formatter.format(CodeFormatter.K_JAVASCRIPT_UNIT, code, 0, code.length(), 0, - lineEnding); + lineEnding.getChars()); if (te == null) throw new IllegalArgumentException( "Code cannot be formatted. Possible cause " + "is unmatched source/target/compliance version."); diff --git a/formatters/jdt-core/META-INF/MANIFEST.MF b/formatters/jdt-core/META-INF/MANIFEST.MF deleted file mode 100755 index f4695e6b6..000000000 --- a/formatters/jdt-core/META-INF/MANIFEST.MF +++ /dev/null @@ -1,9 +0,0 @@ -Manifest-Version: 1.0 -Bundle-ManifestVersion: 2 -Bundle-Name: Eclipse JDT core -Bundle-SymbolicName: jdt-core -Bundle-Version: 1.9.0.qualifier -Require-Bundle: org.eclipse.jdt.core;bundle-version="[3.13.0,10.0.0)", - org.eclipse.text, - org.eclipse.jface.text -Bundle-RequiredExecutionEnvironment: JavaSE-1.7 diff --git a/formatters/jdt-core/proguard.conf b/formatters/jdt-core/proguard.conf deleted file mode 100755 index f4f964238..000000000 --- a/formatters/jdt-core/proguard.conf +++ /dev/null @@ -1,12 +0,0 @@ --dontoptimize --dontobfuscate --dontwarn --ignorewarnings --keepattributes - --keep class org.eclipse.jdt.core.JavaCore --keep class org.eclipse.jdt.core.ToolFactory --keep class org.eclipse.jdt.core.formatter.CodeFormatter --keep class org.eclipse.jface.text.IDocument --keep class org.eclipse.jface.text.Document --keep class org.eclipse.text.edits.TextEdit diff --git a/formatters/jsdt-core/META-INF/MANIFEST.MF b/formatters/jsdt-core/META-INF/MANIFEST.MF deleted file mode 100755 index 9bc99971b..000000000 --- a/formatters/jsdt-core/META-INF/MANIFEST.MF +++ /dev/null @@ -1,8 +0,0 @@ -Manifest-Version: 1.0 -Bundle-ManifestVersion: 2 -Bundle-Name: Eclipse JSDT core -Bundle-SymbolicName: jsdt-core -Bundle-Version: 1.9.0.qualifier -Require-Bundle: org.eclipse.wst.jsdt.core;bundle-version="[2.0.0,10.0.0)", - org.eclipse.jface.text -Bundle-RequiredExecutionEnvironment: JavaSE-1.7 diff --git a/formatters/jsdt-core/proguard.conf b/formatters/jsdt-core/proguard.conf deleted file mode 100755 index 14d25b4c8..000000000 --- a/formatters/jsdt-core/proguard.conf +++ /dev/null @@ -1,11 +0,0 @@ --dontoptimize --dontobfuscate --dontwarn --ignorewarnings --keepattributes - --keep class org.eclipse.wst.jsdt.core.ToolFactory --keep class org.eclipse.wst.jsdt.core.formatter.CodeFormatter --keep class org.eclipse.jface.text.IDocument --keep class org.eclipse.jface.text.Document --keep class org.eclipse.text.edits.TextEdit diff --git a/formatters/pom.xml b/formatters/pom.xml index 7888fbb99..04d02c808 100755 --- a/formatters/pom.xml +++ b/formatters/pom.xml @@ -29,20 +29,28 @@ pom - jdt-core - jsdt-core + formatter-api + java-formatter + java-formatter-test + javascript-formatter + javascript-formatter-test + formatter-jars + - META-INF/MANIFEST.MF + ${basedir}/shade - + org.apache.maven.plugins @@ -104,6 +112,24 @@ + + + org + ${formatter.relocation}org + + org.xml.**.* + org.eclipse.**.* + + + + com.ibm + ${formatter.relocation}com.ibm + + + com.google + ${formatter.relocation}com.google + + diff --git a/maven-plugin/pom.xml b/maven-plugin/pom.xml index 44106006d..d9e681b3c 100644 --- a/maven-plugin/pom.xml +++ b/maven-plugin/pom.xml @@ -16,6 +16,14 @@ limitations under the License. --> + 4.0.0 @@ -51,12 +59,17 @@ com.marvinformatics.formatter - jdt-core + formatter-api ${project.version} com.marvinformatics.formatter - jsdt-core + java-formatter + ${project.version} + + + com.marvinformatics.formatter + javascript-formatter ${project.version} @@ -83,6 +96,19 @@ org.apache.maven.plugins maven-plugin-plugin + + + com.marvinformatics.formatter:java-formatter + + true + + + + com.thoughtworks.qdox + qdox + 2.0-M7 + + maven-dependency-plugin diff --git a/maven-plugin/src/main/java/com/marvinformatics/formatter/AbstractCacheableFormatter.java b/maven-plugin/src/main/java/com/marvinformatics/formatter/AbstractCacheableFormatter.java deleted file mode 100755 index df67098da..000000000 --- a/maven-plugin/src/main/java/com/marvinformatics/formatter/AbstractCacheableFormatter.java +++ /dev/null @@ -1,64 +0,0 @@ -/** - * Copyright (C) 2010 Marvin Herman Froeder (marvin@marvinformatics.com) - * - * 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.marvinformatics.formatter; - -import java.nio.file.Files; -import java.nio.file.Path; - -import org.apache.maven.plugin.logging.Log; - -/** - * @author marvin.froeder - */ -public abstract class AbstractCacheableFormatter { - - protected final Log log; - - protected final ConfigurationSource configurationSource; - - public AbstractCacheableFormatter(ConfigurationSource cfg) { - this.configurationSource = cfg; - this.log = cfg.getLog(); - } - - public Result formatFile(Path file) { - try { - log.debug("Processing file: " + file); - String code = new String(Files.readAllBytes(file), configurationSource.getEncoding()); - String formattedCode = fixLineEnding(doFormat(code)); - - if (code.equals(formattedCode)) { - log.debug("Equal code. Not writing result to file."); - return Result.SKIPPED; - } - - if (!configurationSource.isDryRun()) - Files.write(file, formattedCode.getBytes(configurationSource.getEncoding())); - - return Result.SUCCESS; - } catch (Exception e) { - log.warn("Error formating: " + file.toAbsolutePath(), e); - return Result.FAIL; - } - } - - private String fixLineEnding(String code) { - return configurationSource.lineEnding().fix(code); - } - - protected abstract String doFormat(String code); - -} diff --git a/maven-plugin/src/main/java/com/marvinformatics/formatter/CacheableFormatter.java b/maven-plugin/src/main/java/com/marvinformatics/formatter/CacheableFormatter.java index c141624ff..118d47840 100644 --- a/maven-plugin/src/main/java/com/marvinformatics/formatter/CacheableFormatter.java +++ b/maven-plugin/src/main/java/com/marvinformatics/formatter/CacheableFormatter.java @@ -15,21 +15,44 @@ */ package com.marvinformatics.formatter; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.function.Function; -public class CacheableFormatter extends AbstractCacheableFormatter implements Formatter { +public class CacheableFormatter { - private final Function doFormat; + private final Formatter formatter; + private final ConfigurationSource context; - public CacheableFormatter(ConfigurationSource cfg, Function doFormat) { - super(cfg); + public CacheableFormatter(ConfigurationSource cfg, Formatter formatter) { + this.context = cfg; - this.doFormat = doFormat; + this.formatter = formatter; } - @Override - protected String doFormat(String code) { - return doFormat.apply(code); + public Result formatFile(Path file) { + try { + context.debug("Processing file: " + file); + String code = new String(Files.readAllBytes(file), context.getEncoding()); + String formattedCode = fixLineEnding(formatter.format(code)); + + if (code.equals(formattedCode)) { + context.debug("Equal code. Not writing result to file."); + return Result.SKIPPED; + } + + if (!context.isDryRun()) + Files.write(file, formattedCode.getBytes(context.getEncoding())); + + return Result.SUCCESS; + } catch (Exception e) { + context.warn("Error formating: ", file.toFile().getAbsoluteFile(), e); + return Result.FAIL; + } + } + + private String fixLineEnding(String code) { + return context.lineEnding().fix(code); } } diff --git a/maven-plugin/src/main/java/com/marvinformatics/formatter/FormatterExecuter.java b/maven-plugin/src/main/java/com/marvinformatics/formatter/FormatterExecuter.java index cdc035eab..995e696e0 100644 --- a/maven-plugin/src/main/java/com/marvinformatics/formatter/FormatterExecuter.java +++ b/maven-plugin/src/main/java/com/marvinformatics/formatter/FormatterExecuter.java @@ -115,7 +115,7 @@ private String normalize(String pattern) { return pattern; } - private ThreadLocal createJavaFormatter() { + private ThreadLocal createJavaFormatter() { Supplier> lazyConfig = () -> getFormattingOptions(config.javaConfig()); return ThreadLocal.withInitial(() -> { return new CacheableFormatter(config, new JavaFormatter( @@ -123,16 +123,16 @@ private ThreadLocal createJavaFormatter() { config.getCompilerSources(), config.getCompilerCompliance(), config.getCompilerCodegenTargetPlatform(), - config.lineEnding().getChars())::doFormat); + config.lineEnding())); }); } - private ThreadLocal createJsFormatter() { + private ThreadLocal createJsFormatter() { Supplier> lazyConfig = () -> getFormattingOptions(config.jsConfig()); return ThreadLocal.withInitial(() -> { return new CacheableFormatter(config, new JavascriptFormatter( lazyConfig.get(), - config.lineEnding().getChars())::doFormat); + config.lineEnding())); }); } diff --git a/maven-plugin/src/main/java/com/marvinformatics/formatter/FormatterMojo.java b/maven-plugin/src/main/java/com/marvinformatics/formatter/FormatterMojo.java index cc1806f06..4679b2221 100644 --- a/maven-plugin/src/main/java/com/marvinformatics/formatter/FormatterMojo.java +++ b/maven-plugin/src/main/java/com/marvinformatics/formatter/FormatterMojo.java @@ -190,12 +190,18 @@ public void execute() throws MojoExecutionException, MojoFailureException { // this means m2e is trying to configure itself new M2eConfigurer(this, buildContext).configure(); } else { + getLog().info("Preparing to format files"); + // regular maven execution, just do the work - final ResultCollector rc = new FormatterExecuter(this).execute(); - getLog().info("Successfully formatted: " + rc.successCount() + " file(s)"); - getLog().info("Fail to format : " + rc.failCount() + " file(s)"); - getLog().info("Skipped : " + rc.skippedCount() + " file(s)"); - getLog().info("Approximate time taken: " + rc.getWatch().elapsed(TimeUnit.SECONDS) + "s"); + try { + final ResultCollector rc = new FormatterExecuter(this).execute(); + getLog().info("Successfully formatted: " + rc.successCount() + " file(s)"); + getLog().info("Fail to format : " + rc.failCount() + " file(s)"); + getLog().info("Skipped : " + rc.skippedCount() + " file(s)"); + getLog().info("Approximate time taken: " + rc.getWatch().elapsed(TimeUnit.SECONDS) + "s"); + } catch (Throwable e) { + getLog().error("Error running formatter", e); + } } } @@ -261,4 +267,26 @@ public List directories() { return Lists.newArrayList(this.directories); } + @Override + public void info(String message) { + getLog().info(message); + } + + @Override + public void error(String message) { + getLog().error(message); + } + + @Override + public void debug(String message) { + getLog().debug(message); + } + + @Override + public void warn(String message, File sourceFile, Exception e) { + getLog().warn(message); + buildContext.addMessage(sourceFile, 0, 0, + message, BuildContext.SEVERITY_WARNING, e); + } + } diff --git a/maven-plugin/src/main/java/com/marvinformatics/formatter/RecursiveWalk.java b/maven-plugin/src/main/java/com/marvinformatics/formatter/RecursiveWalk.java index a895ce076..f9fb2e4c1 100644 --- a/maven-plugin/src/main/java/com/marvinformatics/formatter/RecursiveWalk.java +++ b/maven-plugin/src/main/java/com/marvinformatics/formatter/RecursiveWalk.java @@ -32,12 +32,12 @@ public class RecursiveWalk extends RecursiveAction { private static final long serialVersionUID = 6913234076030245489L; private final Stream paths; - private final ThreadLocal javaFormatter; + private final ThreadLocal javaFormatter; private final ResultCollector resultCollector; - private final ThreadLocal jsFormatter; + private final ThreadLocal jsFormatter; private final MatchPatterns excludes; - public RecursiveWalk(ThreadLocal javaFormatter, ThreadLocal jsFormatter, + public RecursiveWalk(ThreadLocal javaFormatter, ThreadLocal jsFormatter, ResultCollector resultCollector, Stream stream, MatchPatterns excludes) { super(); this.paths = stream; @@ -47,7 +47,7 @@ public RecursiveWalk(ThreadLocal javaFormatter, ThreadLocal javaFormatter, ThreadLocal jsFormatter, + public RecursiveWalk(ThreadLocal javaFormatter, ThreadLocal jsFormatter, ResultCollector resultCollector, Path path, MatchPatterns excludes) { this(javaFormatter, jsFormatter, resultCollector, Collections.singletonList(path).stream(), excludes); } diff --git a/pom.xml b/pom.xml index a4d6e05ca..10bbe6318 100644 --- a/pom.xml +++ b/pom.xml @@ -46,7 +46,9 @@ UTF-8 UTF-8 - 3.3 + + 1.0.0 + 3.5 1.8 1.0.0 @@ -192,7 +194,8 @@ ${project.basedir} **/target/** - **/src/test/** + **/sample/** + **/test/projects/** LF ${project.basedir}/src/config/eclipse/formatter/java.xml