From f70e1c7d8ad46ffd7ab12aa633cd6b99b0d0cd6e Mon Sep 17 00:00:00 2001 From: Frank Vennemeyer Date: Sun, 5 Aug 2018 20:20:53 +0200 Subject: [PATCH] Providing WTP formatters, excluding JSP (#241) Providing WTP formatters, excluding JSP --- _ext/eclipse-groovy/build.gradle | 157 ++------------ _ext/eclipse-wtp/LICENSE.txt | 70 ++++++ _ext/eclipse-wtp/README.md | 11 + _ext/eclipse-wtp/build.gradle | 112 ++++++++++ _ext/eclipse-wtp/gradle.properties | 22 ++ .../wtp/EclipseCssFormatterStepImpl.java | 56 +++++ .../wtp/EclipseHtmlFormatterStepImpl.java | 176 +++++++++++++++ .../wtp/EclipseJsFormatterStepImpl.java | 157 ++++++++++++++ .../wtp/EclipseJsonFormatterStepImpl.java | 68 ++++++ .../wtp/EclipseXmlFormatterStepImpl.java | 161 ++++++++++++++ .../eclipse/wtp/html/JsRegionProcessor.java | 86 ++++++++ .../wtp/html/StructuredDocumentProcessor.java | 205 ++++++++++++++++++ .../extra/eclipse/wtp/html/package-info.java | 20 ++ .../extra/eclipse/wtp/package-info.java | 20 ++ .../extra/eclipse/wtp/sse/CleanupStep.java | 153 +++++++++++++ .../eclipse/wtp/sse/ContentTypeManager.java | 182 ++++++++++++++++ .../eclipse/wtp/sse/SpotlessPreferences.java | 93 ++++++++ .../extra/eclipse/wtp/sse/package-info.java | 20 ++ .../wtp/EclipseCssFormatterStepImplTest.java | 79 +++++++ .../wtp/EclipseHtmlFormatterStepImplTest.java | 144 ++++++++++++ .../wtp/EclipseJsFormatterStepImplTest.java | 101 +++++++++ .../wtp/EclipseJsonFormatterStepImplTest.java | 69 ++++++ .../wtp/EclipseXmlFormatterStepImplTest.java | 139 ++++++++++++ .../spotless/extra/eclipse/wtp/TestData.java | 77 +++++++ .../src/test/resources/html/expected/css.html | 39 ++++ .../test/resources/html/expected/html4.html | 10 + .../test/resources/html/expected/html5.html | 11 + .../resources/html/expected/html5_upper.html | 11 + .../html/expected/invalid_syntax.html | 7 + .../resources/html/expected/javascript.html | 41 ++++ .../html/expected/javascript_semicolon.html | 41 ++++ .../src/test/resources/html/input/css.html | 23 ++ .../src/test/resources/html/input/html4.html | 10 + .../src/test/resources/html/input/html5.html | 10 + .../resources/html/input/invalid_syntax.html | 4 + .../test/resources/html/input/javascript.html | 34 +++ .../resources/html/restrictions/ReadMe.txt | 9 + .../resources/xml/expected/dtd_relative.xml | 8 + .../test/resources/xml/expected/xml_space.xml | 4 + .../resources/xml/expected/xsd_not_found.xml | 7 + .../resources/xml/expected/xsd_relative.xml | 7 + .../test/resources/xml/input/dtd_relative.xml | 8 + .../test/resources/xml/input/xml_space.xml | 1 + .../resources/xml/input/xsd_not_found.xml | 4 + .../test/resources/xml/input/xsd_relative.xml | 4 + .../resources/xml/restrictions/catalog.xml | 5 + .../test/resources/xml/restrictions/test.dtd | 4 + .../test/resources/xml/restrictions/test.xsd | 23 ++ _ext/gradle/p2-fat-jar-setup.gradle | 178 +++++++++++++++ 49 files changed, 2737 insertions(+), 144 deletions(-) create mode 100644 _ext/eclipse-wtp/LICENSE.txt create mode 100644 _ext/eclipse-wtp/README.md create mode 100644 _ext/eclipse-wtp/build.gradle create mode 100644 _ext/eclipse-wtp/gradle.properties create mode 100644 _ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseCssFormatterStepImpl.java create mode 100644 _ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseHtmlFormatterStepImpl.java create mode 100644 _ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseJsFormatterStepImpl.java create mode 100644 _ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseJsonFormatterStepImpl.java create mode 100644 _ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseXmlFormatterStepImpl.java create mode 100644 _ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/html/JsRegionProcessor.java create mode 100644 _ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/html/StructuredDocumentProcessor.java create mode 100644 _ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/html/package-info.java create mode 100644 _ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/package-info.java create mode 100644 _ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/sse/CleanupStep.java create mode 100644 _ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/sse/ContentTypeManager.java create mode 100644 _ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/sse/SpotlessPreferences.java create mode 100644 _ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/sse/package-info.java create mode 100644 _ext/eclipse-wtp/src/test/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseCssFormatterStepImplTest.java create mode 100644 _ext/eclipse-wtp/src/test/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseHtmlFormatterStepImplTest.java create mode 100644 _ext/eclipse-wtp/src/test/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseJsFormatterStepImplTest.java create mode 100644 _ext/eclipse-wtp/src/test/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseJsonFormatterStepImplTest.java create mode 100644 _ext/eclipse-wtp/src/test/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseXmlFormatterStepImplTest.java create mode 100644 _ext/eclipse-wtp/src/test/java/com/diffplug/spotless/extra/eclipse/wtp/TestData.java create mode 100644 _ext/eclipse-wtp/src/test/resources/html/expected/css.html create mode 100644 _ext/eclipse-wtp/src/test/resources/html/expected/html4.html create mode 100644 _ext/eclipse-wtp/src/test/resources/html/expected/html5.html create mode 100644 _ext/eclipse-wtp/src/test/resources/html/expected/html5_upper.html create mode 100644 _ext/eclipse-wtp/src/test/resources/html/expected/invalid_syntax.html create mode 100644 _ext/eclipse-wtp/src/test/resources/html/expected/javascript.html create mode 100644 _ext/eclipse-wtp/src/test/resources/html/expected/javascript_semicolon.html create mode 100644 _ext/eclipse-wtp/src/test/resources/html/input/css.html create mode 100644 _ext/eclipse-wtp/src/test/resources/html/input/html4.html create mode 100644 _ext/eclipse-wtp/src/test/resources/html/input/html5.html create mode 100644 _ext/eclipse-wtp/src/test/resources/html/input/invalid_syntax.html create mode 100644 _ext/eclipse-wtp/src/test/resources/html/input/javascript.html create mode 100644 _ext/eclipse-wtp/src/test/resources/html/restrictions/ReadMe.txt create mode 100644 _ext/eclipse-wtp/src/test/resources/xml/expected/dtd_relative.xml create mode 100644 _ext/eclipse-wtp/src/test/resources/xml/expected/xml_space.xml create mode 100644 _ext/eclipse-wtp/src/test/resources/xml/expected/xsd_not_found.xml create mode 100644 _ext/eclipse-wtp/src/test/resources/xml/expected/xsd_relative.xml create mode 100644 _ext/eclipse-wtp/src/test/resources/xml/input/dtd_relative.xml create mode 100644 _ext/eclipse-wtp/src/test/resources/xml/input/xml_space.xml create mode 100644 _ext/eclipse-wtp/src/test/resources/xml/input/xsd_not_found.xml create mode 100644 _ext/eclipse-wtp/src/test/resources/xml/input/xsd_relative.xml create mode 100644 _ext/eclipse-wtp/src/test/resources/xml/restrictions/catalog.xml create mode 100644 _ext/eclipse-wtp/src/test/resources/xml/restrictions/test.dtd create mode 100644 _ext/eclipse-wtp/src/test/resources/xml/restrictions/test.xsd create mode 100644 _ext/gradle/p2-fat-jar-setup.gradle diff --git a/_ext/eclipse-groovy/build.gradle b/_ext/eclipse-groovy/build.gradle index bb2c6a9a22..5e0042a765 100644 --- a/_ext/eclipse-groovy/build.gradle +++ b/_ext/eclipse-groovy/build.gradle @@ -1,47 +1,21 @@ -import java.io.File - -import org.apache.commons.io.filefilter.DirectoryFileFilter - -plugins { - // p2 dependencies - id 'com.diffplug.gradle.p2.asmaven' version '3.9.0' -} - -apply from: rootProject.file('../gradle/java-setup.gradle') -apply from: rootProject.file('../gradle/java-publish.gradle') - -// The dependencies to pull from GrEclipse's p2 repositories -def grEclipseDeps = [ - 'org.codehaus.groovy.eclipse.refactoring':'+', // GroovyFormatter and related - - // The following lists does not reflect the complete transitive required packages, but - // the once used during code formatting - 'org.codehaus.groovy':'+', // Groovy compiler patches supporting use within GrEclipse and Groovy itself - 'org.codehaus.groovy.eclipse.core':'+', // Groovy core classes (provides central logging used by formatter) - 'org.eclipse.jdt.core':"${VER_JDT_PATCH}", // Patches org.eclipse.jdt.core classes supporting use within GrEclipse (provides AST generator) - 'org.eclipse.jdt.groovy.core':'+' // Extends org.eclipse.jdt.core for Groovy -] - ext { developers = [ fvgh: [ name: 'Frank Vennemeyer', email: 'frankgh@zoho.com' ], ] - //Include/Excludes form the JARs, which goes into a fat-jar with the spottless formatter interface. - jarInclude = [ - '**/*.class', // Take all classes - '**/*.java', // ... and sources. - '**/*.properties', // Text resources (for messages, etc) - '**/*.xml', // Plugin XML and other resources - '*.html', // License information about the included JARs, - 'META-INF/**' // Information about the origin of the individual class files - ] - jarExclude = [ - 'META-INF/*.RSA', // The eclipse jars are signed, and our fat-jar breaks the signatures - 'META-INF/*.SF', // ... so all signatures are filtered + p2Repository = "http://dist.springsource.org/release/GRECLIPSE/e${VER_ECLIPSE}" + + p2Dependencies = [ + 'org.codehaus.groovy.eclipse.refactoring':'+', // GroovyFormatter and related + + // The following lists does not reflect the complete transitive required packages, but + // the once used during code formatting + 'org.codehaus.groovy':'+', // Groovy compiler patches supporting use within GrEclipse and Groovy itself + 'org.codehaus.groovy.eclipse.core':'+', // Groovy core classes (provides central logging used by formatter) + //'org.eclipse.jdt.core':"${VER_JDT_PATCH}", // Patches org.eclipse.jdt.core classes supporting use within GrEclipse (provides AST generator) + 'org.eclipse.jdt.groovy.core':'+' // Extends org.eclipse.jdt.core for Groovy ] - //Some JARs include JARs themselfs internalJars = [ //Jars included by org.codehaus.groovy "**/groovy-all-${VER_GROOVY}-indy", // Use Groovy compiler compatible with GrEclipse instead of localGroovy @@ -52,33 +26,12 @@ ext { '**/nlcl' //Non locking class loader used by groovy compiler ] - // The directory contains all external classes for the fat-jar - embeddedClassesDirName = 'build/embeddedClasses' - embeddedClassesDir = project.file(embeddedClassesDirName) - embeddedClassesLibDirName = 'build/embeddedClasses/lib' - embeddedClassesLibDir = project.file(embeddedClassesLibDirName) } -// build a maven repo in our build folder containing these artifacts -p2AsMaven { - group 'p2', { - repo "http://dist.springsource.org/release/GRECLIPSE/e${VER_ECLIPSE}" - grEclipseDeps.keySet.each { p2.addIU(it) } - } -} - -configurations { - embeddedJars // GrEclipse JARs the fat-jar is based uppon -} +apply from: rootProject.file('../gradle/p2-fat-jar-setup.gradle') +apply from: rootProject.file('../gradle/java-publish.gradle') dependencies { - grEclipseDeps.each { groupArtifact, version -> - embeddedJars "p2:${groupArtifact}:${version}" - } - - // The resulting fat-jar includes the classes from GRECLIPSE. - compile files(embeddedClassesDir) - compile "com.diffplug.spotless:spotless-eclipse-base:${VER_SPOTLESS_ECLISPE_BASE}" // Provides text partitioners for formatters compile ("org.eclipse.platform:org.eclipse.jface.text:${VER_ECLISPE_JFACE}") { @@ -86,11 +39,6 @@ dependencies { } } -jar { - // this embeds the Eclipse-Groovy clases into our "fat JAR" - from embeddedClassesDir -} - ////////// // Test // ////////// @@ -98,82 +46,3 @@ sourceSets { // Use JAR file with all resources for Eclipse-Groovy integration-tests test.runtimeClasspath = jar.outputs.files + sourceSets.test.output + sourceSets.test.compileClasspath } - -/////////////////// -// External Deps // -/////////////////// - -task unjarEmbeddedClasses { - description = "Copies filtered set of embedded classes from the Eclise/GrEclipse dependencies to '${project.relativePath(embeddedClassesDir)}'." - inputs.files(configurations.embeddedJars) - inputs.property('internalJars', internalJars) - inputs.property('jarInclude', jarInclude) - inputs.property('jarExclude', jarExclude) - outputs.file(embeddedClassesDir) - - doLast { - embeddedClassesDir.deleteDir() - embeddedClassesDir.mkdirs() - embeddedClassesLibDir.deleteDir() - embeddedClassesLibDir.mkdirs() - configurations.embeddedJars.each { - unjar(it, embeddedClassesDir) - } - //Unpack internal JARs. Maintain the order defined in internalJars - internalJars.each { - fileTree(embeddedClassesDir).include("${it}.jar").each { - unjar(it, embeddedClassesDir) - delete(it) - } - } - } -} - -def unjar(File jarFile, File destDir) { - ant.unjar(src: jarFile, dest: destDir) { - patternset { - jarInclude.each { - include(name: "${it}") - } - internalJars.each { - include(name: "**/${it}.jar") - } - jarExclude.each { - exclude(name: "${it}") - } - } - } - //Provide Fat JAR resources (following naming convention of spotless-eclipse-base) - def fat_jar_resource_dir = jarFile.getName().split('-')[0] - ant.move(todir: "${destDir}/${fat_jar_resource_dir}/META-INF", quiet: 'true', failonerror: 'false') { - fileset(dir: "${destDir}/META-INF") - } - //Keep licenses and other human readable information for transparency - ant.move(todir: "${destDir}/${fat_jar_resource_dir}", quiet: 'true') { - fileset(dir: destDir) { - include(name: 'META-INF') - include(name: '*') - type(type: 'file') - exclude(name: '*jar-*') - exclude(name: '*.jar') - } - } -} - -tasks.compileJava.dependsOn(unjarEmbeddedClasses) - -///////// -// IDE // -///////// - -apply plugin: 'eclipse' - -// always create fresh projects -tasks.eclipse.dependsOn(cleanEclipse) -// Encure that the dependent classes are preovided for compilation if project is build via Eclipse instead of command line -tasks.eclipseClasspath.dependsOn(unjarEmbeddedClasses) - -apply plugin: 'idea' - -// Encure that the dependent classes are preovided for compilation if project is build via Eclipse instead of command line -tasks.idea.dependsOn(unjarEmbeddedClasses) diff --git a/_ext/eclipse-wtp/LICENSE.txt b/_ext/eclipse-wtp/LICENSE.txt new file mode 100644 index 0000000000..3d967aee74 --- /dev/null +++ b/_ext/eclipse-wtp/LICENSE.txt @@ -0,0 +1,70 @@ +Eclipse Public License - v 1.0 + +THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. + +1. DEFINITIONS + +"Contribution" means: + +a) in the case of the initial Contributor, the initial code and documentation distributed under this Agreement, and +b) in the case of each subsequent Contributor: +i) changes to the Program, and +ii) additions to the Program; +where such changes and/or additions to the Program originate from and are distributed by that particular Contributor. A Contribution 'originates' from a Contributor if it was added to the Program by such Contributor itself or anyone acting on such Contributor's behalf. Contributions do not include additions to the Program which: (i) are separate modules of software distributed in conjunction with the Program under their own license agreement, and (ii) are not derivative works of the Program. +"Contributor" means any person or entity that distributes the Program. + +"Licensed Patents" mean patent claims licensable by a Contributor which are necessarily infringed by the use or sale of its Contribution alone or when combined with the Program. + +"Program" means the Contributions distributed in accordance with this Agreement. + +"Recipient" means anyone who receives the Program under this Agreement, including all Contributors. + +2. GRANT OF RIGHTS + +a) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free copyright license to reproduce, prepare derivative works of, publicly display, publicly perform, distribute and sublicense the Contribution of such Contributor, if any, and such derivative works, in source code and object code form. +b) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free patent license under Licensed Patents to make, use, sell, offer to sell, import and otherwise transfer the Contribution of such Contributor, if any, in source code and object code form. This patent license shall apply to the combination of the Contribution and the Program if, at the time the Contribution is added by the Contributor, such addition of the Contribution causes such combination to be covered by the Licensed Patents. The patent license shall not apply to any other combinations which include the Contribution. No hardware per se is licensed hereunder. +c) Recipient understands that although each Contributor grants the licenses to its Contributions set forth herein, no assurances are provided by any Contributor that the Program does not infringe the patent or other intellectual property rights of any other entity. Each Contributor disclaims any liability to Recipient for claims brought by any other entity based on infringement of intellectual property rights or otherwise. As a condition to exercising the rights and licenses granted hereunder, each Recipient hereby assumes sole responsibility to secure any other intellectual property rights needed, if any. For example, if a third party patent license is required to allow Recipient to distribute the Program, it is Recipient's responsibility to acquire that license before distributing the Program. +d) Each Contributor represents that to its knowledge it has sufficient copyright rights in its Contribution, if any, to grant the copyright license set forth in this Agreement. +3. REQUIREMENTS + +A Contributor may choose to distribute the Program in object code form under its own license agreement, provided that: + +a) it complies with the terms and conditions of this Agreement; and +b) its license agreement: +i) effectively disclaims on behalf of all Contributors all warranties and conditions, express and implied, including warranties or conditions of title and non-infringement, and implied warranties or conditions of merchantability and fitness for a particular purpose; +ii) effectively excludes on behalf of all Contributors all liability for damages, including direct, indirect, special, incidental and consequential damages, such as lost profits; +iii) states that any provisions which differ from this Agreement are offered by that Contributor alone and not by any other party; and +iv) states that source code for the Program is available from such Contributor, and informs licensees how to obtain it in a reasonable manner on or through a medium customarily used for software exchange. +When the Program is made available in source code form: + +a) it must be made available under this Agreement; and +b) a copy of this Agreement must be included with each copy of the Program. +Contributors may not remove or alter any copyright notices contained within the Program. + +Each Contributor must identify itself as the originator of its Contribution, if any, in a manner that reasonably allows subsequent Recipients to identify the originator of the Contribution. + +4. COMMERCIAL DISTRIBUTION + +Commercial distributors of software may accept certain responsibilities with respect to end users, business partners and the like. While this license is intended to facilitate the commercial use of the Program, the Contributor who includes the Program in a commercial product offering should do so in a manner which does not create potential liability for other Contributors. Therefore, if a Contributor includes the Program in a commercial product offering, such Contributor ("Commercial Contributor") hereby agrees to defend and indemnify every other Contributor ("Indemnified Contributor") against any losses, damages and costs (collectively "Losses") arising from claims, lawsuits and other legal actions brought by a third party against the Indemnified Contributor to the extent caused by the acts or omissions of such Commercial Contributor in connection with its distribution of the Program in a commercial product offering. The obligations in this section do not apply to any claims or Losses relating to any actual or alleged intellectual property infringement. In order to qualify, an Indemnified Contributor must: a) promptly notify the Commercial Contributor in writing of such claim, and b) allow the Commercial Contributor to control, and cooperate with the Commercial Contributor in, the defense and any related settlement negotiations. The Indemnified Contributor may participate in any such claim at its own expense. + +For example, a Contributor might include the Program in a commercial product offering, Product X. That Contributor is then a Commercial Contributor. If that Commercial Contributor then makes performance claims, or offers warranties related to Product X, those performance claims and warranties are such Commercial Contributor's responsibility alone. Under this section, the Commercial Contributor would have to defend claims against the other Contributors related to those performance claims and warranties, and if a court requires any other Contributor to pay any damages as a result, the Commercial Contributor must pay those damages. + +5. NO WARRANTY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the appropriateness of using and distributing the Program and assumes all risks associated with its exercise of rights under this Agreement , including but not limited to the risks and costs of program errors, compliance with applicable laws, damage to or loss of data, programs or equipment, and unavailability or interruption of operations. + +6. DISCLAIMER OF LIABILITY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + +7. GENERAL + +If any provision of this Agreement is invalid or unenforceable under applicable law, it shall not affect the validity or enforceability of the remainder of the terms of this Agreement, and without further action by the parties hereto, such provision shall be reformed to the minimum extent necessary to make such provision valid and enforceable. + +If Recipient institutes patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Program itself (excluding combinations of the Program with other software or hardware) infringes such Recipient's patent(s), then such Recipient's rights granted under Section 2(b) shall terminate as of the date such litigation is filed. + +All Recipient's rights under this Agreement shall terminate if it fails to comply with any of the material terms or conditions of this Agreement and does not cure such failure in a reasonable period of time after becoming aware of such noncompliance. If all Recipient's rights under this Agreement terminate, Recipient agrees to cease use and distribution of the Program as soon as reasonably practicable. However, Recipient's obligations under this Agreement and any licenses granted by Recipient relating to the Program shall continue and survive. + +Everyone is permitted to copy and distribute copies of this Agreement, but in order to avoid inconsistency the Agreement is copyrighted and may only be modified in the following manner. The Agreement Steward reserves the right to publish new versions (including revisions) of this Agreement from time to time. No one other than the Agreement Steward has the right to modify this Agreement. The Eclipse Foundation is the initial Agreement Steward. The Eclipse Foundation may assign the responsibility to serve as the Agreement Steward to a suitable separate entity. Each new version of the Agreement will be given a distinguishing version number. The Program (including Contributions) may always be distributed subject to the version of the Agreement under which it was received. In addition, after a new version of the Agreement is published, Contributor may elect to distribute the Program (including its Contributions) under the new version. Except as expressly stated in Sections 2(a) and 2(b) above, Recipient receives no rights or licenses to the intellectual property of any Contributor under this Agreement, whether expressly, by implication, estoppel or otherwise. All rights in the Program not expressly granted under this Agreement are reserved. + +This Agreement is governed by the laws of the State of New York and the intellectual property laws of the United States of America. No party to this Agreement will bring a legal action under this Agreement more than one year after the cause of action arose. Each party waives its rights to a jury trial in any resulting litigation. \ No newline at end of file diff --git a/_ext/eclipse-wtp/README.md b/_ext/eclipse-wtp/README.md new file mode 100644 index 0000000000..8555163acf --- /dev/null +++ b/_ext/eclipse-wtp/README.md @@ -0,0 +1,11 @@ +# spotless-eclipse-wtp + +Eclipse WTP is not available in a form which can be easily consumed by maven or gradle. To fix this, we publish Eclipse's WTP formatters, along with a small amount of glue code, into the `com.diffplug.spotless.extra:spotless-eclipse-wtp` artifact. + +To publish a new version, update the `_ext/eclipse-wtp/gradle.properties` appropriately and run this from the root directory: + +``` +gradlew -b _ext/eclipse-wtp/build.gradle publish +``` + +Spotless at large is under the Apache 2.0 license, but this jar is under the EPL v1. diff --git a/_ext/eclipse-wtp/build.gradle b/_ext/eclipse-wtp/build.gradle new file mode 100644 index 0000000000..050534a49f --- /dev/null +++ b/_ext/eclipse-wtp/build.gradle @@ -0,0 +1,112 @@ +ext { + developers = [ + fvgh: [ name: 'Frank Vennemeyer', email: 'frankgh@zoho.com' ], + ] + + p2Repository = "http://download.eclipse.org/webtools/repository/${VER_ECLIPSE_WTP}" + + p2Dependencies = [ + // XML/HTML Formatter - Dependencies + 'org.eclipse.wst.xml.core':'+', // DefaultXMLPartitionFormatter and XMLAssociationProvider + 'org.eclipse.wst.sse.core':'+', // Structure models + 'org.eclipse.wst.common.uriresolver':'+', // URI resolver for model queries + 'org.eclipse.wst.dtd.core':'+', // Support DTD extensions + + // XML Formatter - Dependencies + 'org.eclipse.wst.xsd.core':'+', // Support XSD extensions + + // JS Formatter - Dependencies + 'org.eclipse.wst.jsdt.core':'+', // DefaultCodeFormatter and related + 'org.eclipse.wst.jsdt.ui':'+', // Functionality to format comments + + // JSON Formatter - Dependencies + 'org.eclipse.wst.json.core':'+', // FormatProcessorJSON and related + 'org.eclipse.json':'+', // Provides JSON node interfaces + + // CSS Formatter - Dependencies + 'org.eclipse.wst.css.core':'+', // FormatProcessorCSS and related + + // HTML Formatter - Dependencies + 'org.eclipse.wst.html.core':'+', // HTMLFormatProcessorImpl and related + ] + + jarInclude = [ + '**/*.class', // Take all classes + '**/*.properties', // Text resources (for messages, etc) + '**/*.rsc', // JSDT requires gramar files + '**/*.xml', // Plugin XML and other resources + '*.html', // License information about the included JARs, + 'META-INF/**' // Plugin manifest and addtional information + ] + + fatJarResourcesMap = [ + 'org.eclipse.wst.common.uriresolver': 'org.eclipse.wst.common.uriresolver.internal.provisional', + 'org.eclipse.wst.css.core': 'org.eclipse.wst.css.core.internal', + 'org.eclipse.wst.dtd.core': 'org.eclipse.wst.dtd.core.internal', + 'org.eclipse.wst.html.core': 'org.eclipse.wst.html.core.internal', + 'org.eclipse.wst.sse.core': 'org.eclipse.wst.sse.core.internal.encoding.util', + 'org.eclipse.wst.xml.core': 'org.eclipse.wst.xml.core.internal', + 'org.eclipse.wst.xsd.core': 'org.eclipse.wst.xsd.core.internal' + ] +} + +apply from: rootProject.file('../gradle/p2-fat-jar-setup.gradle') +apply from: rootProject.file('../gradle/java-publish.gradle') + +dependencies { + compile "com.diffplug.spotless:spotless-eclipse-base:${VER_SPOTLESS_ECLISPE_BASE}" + // Required by most WPT formatters + compile "com.ibm.icu:icu4j:${VER_IBM_ICU}" + // The XSD/DTD and other models are defined with EMF. + compile "org.eclipse.emf:org.eclipse.emf.common:${VER_ECLISPE_EMF}" + compile "org.eclipse.emf:org.eclipse.emf.ecore:${VER_ECLISPE_EMF}" + // Some WPT plugins requires OSGI bundle interfaces (but not effectively used) + compile "org.eclipse.platform:org.eclipse.osgi.services:${VER_ECLISPE_PLATFORM}" + // Provides document data structure and file buffers for formatters + compile "org.eclipse.platform:org.eclipse.core.filebuffers:${VER_ECLISPE_PLATFORM}" + // Provides text partitioners for formatters + compile ("org.eclipse.platform:org.eclipse.jface.text:${VER_ECLISPE_JFACE}") { + exclude group: 'org.eclipse.platform', module: 'org.eclipse.swt' + } + // Some WPT plugins use the EFS for storing temporary worspace data + compile "org.eclipse.platform:org.eclipse.core.filesystem:${VER_ECLISPE_EFS}" + // Required by org.eclipse.wst.xsd.core + compile "org.eclipse.xsd:org.eclipse.xsd:${VER_ECLISPE_XSD}" +} + +////////// +// Test // +////////// +sourceSets { + // Use JAR file with all resources for Eclipse-WTP integration-tests + test.runtimeClasspath = jar.outputs.files + sourceSets.test.output + sourceSets.test.compileClasspath +} + +/* + * All test classes need to run separately since they all instatiate different setups of the + * Eclipse framework. + */ +test { + //Skip default tests, which would run every test case. + exclude '**' +} + +//Instead make a separate test task per case +def testLocation = 'src/test/java' +fileTree(dir: testLocation).include('**/*Test.java').each { file -> + def testFile = file.getName().replace(".java", "") + def filePath = file.getAbsolutePath().replace(".java", "**") //Don't ask me why the task is not happy when it gets no asterisk + filePath = filePath.substring(filePath.lastIndexOf(testLocation) + testLocation.length() + 1) + task "${testFile}"(type: Test) { + group = LifecycleBasePlugin.VERIFICATION_GROUP + description = "Runs ${testFile} integration test." + include "${filePath}" + reports { + html.destination = new File("$buildDir/reports/${testFile}") + junitXml.destination = new File("$buildDir/${testFile}") + } + //classpath = jar.outputs.files + sourceSets.test.output + sourceSets.test.compileClasspath + mustRunAfter tasks.jar + } + test.dependsOn "${testFile}" +} diff --git a/_ext/eclipse-wtp/gradle.properties b/_ext/eclipse-wtp/gradle.properties new file mode 100644 index 0000000000..6a19fcbeff --- /dev/null +++ b/_ext/eclipse-wtp/gradle.properties @@ -0,0 +1,22 @@ +# Versions correspond to the Eclipse-WTP version used for th FAT JAR +# See https://www.eclipse.org/webtools/ for further information about Eclipse-Groovy versions. +# Patch version can be is incremented independently for backward compatible patches of this library. +ext_version=3.9.5 +ext_artifactId=spotless-eclipse-wtp +ext_description=Eclipse's WTP formatters bundled for Spotless + +ext_org=diffplug +ext_group=com.diffplug.spotless + +# Build requirements +ext_VER_JAVA=1.8 + +# Compile +VER_ECLIPSE_WTP=oxygen +VER_SPOTLESS_ECLISPE_BASE=[3.0.0,4.0.0[ +VER_IBM_ICU=[61,62[ +VER_ECLISPE_EMF=[2.12.0,3.0.0[ +VER_ECLISPE_PLATFORM=[3.6.0,4.0.0[ +VER_ECLISPE_JFACE=[3.12.0,4.0.0[ +VER_ECLISPE_EFS=[1.6.0,2.0.0[ +VER_ECLISPE_XSD=[2.12.0,3.0.0[ \ No newline at end of file diff --git a/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseCssFormatterStepImpl.java b/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseCssFormatterStepImpl.java new file mode 100644 index 0000000000..ead6d4da91 --- /dev/null +++ b/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseCssFormatterStepImpl.java @@ -0,0 +1,56 @@ +/* + * 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.eclipse.wtp; + +import java.util.Properties; + +import org.eclipse.wst.css.core.internal.CSSCorePlugin; +import org.eclipse.wst.css.core.internal.cleanup.CleanupProcessorCSS; +import org.eclipse.wst.css.core.internal.preferences.CSSCorePreferenceInitializer; +import org.eclipse.wst.sse.core.internal.format.IStructuredFormatProcessor; + +import com.diffplug.spotless.extra.eclipse.wtp.sse.CleanupStep; + +/** Formatter step which calls out to the Eclipse CSS cleanup and formatter. */ +public class EclipseCssFormatterStepImpl extends CleanupStep { + + public EclipseCssFormatterStepImpl(Properties properties) throws Exception { + super(new SpotlessCssCleanup(), plugin -> plugin.add(new CSSCorePlugin())); + configure(properties, true, CSSCorePlugin.getDefault(), new CSSCorePreferenceInitializer()); + } + + /** + * The FormatProcessorCSS does not allow a strict case formatting. + * Hence additionally the CleanupProcessorCSS is used. + */ + public static class SpotlessCssCleanup extends CleanupProcessorCSS implements CleanupStep.ProcessorAccessor { + @Override + public String getThisContentType() { + return getContentType(); + } + + @Override + public IStructuredFormatProcessor getThisFormatProcessor() { + return getFormatProcessor(); + } + + @Override + public void refreshThisCleanupPreferences() { + refreshCleanupPreferences(); + } + + } +} diff --git a/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseHtmlFormatterStepImpl.java b/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseHtmlFormatterStepImpl.java new file mode 100644 index 0000000000..97b8b4ca61 --- /dev/null +++ b/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseHtmlFormatterStepImpl.java @@ -0,0 +1,176 @@ +/* + * 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.eclipse.wtp; + +import static com.diffplug.spotless.extra.eclipse.base.SpotlessEclipseFramework.LINE_DELIMITER; +import static com.diffplug.spotless.extra.eclipse.wtp.EclipseJsFormatterStepImpl.JS_CORE_CONFIG; +import static org.eclipse.wst.css.core.internal.preferences.CSSCorePreferenceNames.*; +import static org.eclipse.wst.xml.core.internal.preferences.XMLCorePreferenceNames.CMDOCUMENT_GLOBAL_CACHE_ENABLED; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Map; +import java.util.Properties; +import java.util.Set; + +import org.eclipse.wst.common.uriresolver.internal.provisional.URIResolverPlugin; +import org.eclipse.wst.css.core.internal.CSSCorePlugin; +import org.eclipse.wst.css.core.internal.preferences.CSSCorePreferenceInitializer; +import org.eclipse.wst.dtd.core.internal.DTDCorePlugin; +import org.eclipse.wst.html.core.internal.HTMLCorePlugin; +import org.eclipse.wst.html.core.internal.cleanup.HTMLCleanupProcessorImpl; +import org.eclipse.wst.html.core.internal.encoding.HTMLDocumentLoader; +import org.eclipse.wst.html.core.internal.format.HTMLFormatProcessorImpl; +import org.eclipse.wst.html.core.internal.preferences.HTMLCorePreferenceInitializer; +import org.eclipse.wst.html.core.text.IHTMLPartitions; +import org.eclipse.wst.jsdt.core.JavaScriptCore; +import org.eclipse.wst.jsdt.core.ToolFactory; +import org.eclipse.wst.jsdt.core.formatter.CodeFormatter; +import org.eclipse.wst.sse.core.internal.format.IStructuredFormatProcessor; +import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument; +import org.eclipse.wst.xml.core.internal.XMLCorePlugin; +import org.eclipse.wst.xml.core.internal.preferences.XMLCorePreferenceInitializer; + +import com.diffplug.spotless.extra.eclipse.wtp.html.JsRegionProcessor; +import com.diffplug.spotless.extra.eclipse.wtp.html.StructuredDocumentProcessor; +import com.diffplug.spotless.extra.eclipse.wtp.sse.CleanupStep; +import com.diffplug.spotless.extra.eclipse.wtp.sse.SpotlessPreferences; + +/** Formatter step which calls out to the Eclipse HTML cleanup and formatter. */ +public class EclipseHtmlFormatterStepImpl extends CleanupStep { + + private final String htmlFormatterIndent; + private final CodeFormatter jsFormatter; + + public EclipseHtmlFormatterStepImpl(Properties properties) throws Exception { + super(new SpotlessHtmlCleanup(), JS_CORE_CONFIG, additionalPlugins -> { + additionalPlugins.add(new CSSCorePlugin()); + additionalPlugins.add(new XMLCorePlugin()); + //DTDs must be resolved by URI + additionalPlugins.add(new URIResolverPlugin()); + //Support parsing of the DTD (required, though only the internal EMF models are used) + additionalPlugins.add(new DTDCorePlugin()); + // The JS core uses EFS for determination of temporary storage location + additionalPlugins.add(new org.eclipse.core.internal.filesystem.Activator()); + additionalPlugins.add(new JavaScriptCore()); + additionalPlugins.add(new HTMLCorePlugin()); + }); + /* + * The cleanup processor tries to load DTDs into the cache (which we have not setup). + * Anyhow, the attempt is bogus since it anyway just silently fails to read the internal DTDs. + * So we forbid to use the cache in the first place. + */ + properties.setProperty(CMDOCUMENT_GLOBAL_CACHE_ENABLED, Boolean.toString(false)); + configure(getCSSFormattingProperties(properties), true, CSSCorePlugin.getDefault(), new CSSCorePreferenceInitializer()); + configure(properties, false, XMLCorePlugin.getDefault(), new XMLCorePreferenceInitializer()); + configure(properties, false, HTMLCorePlugin.getDefault(), new HTMLCorePreferenceInitializer()); + htmlFormatterIndent = processor.getIndent(); + + //Create JS formatter + Map jsOptions = EclipseJsFormatterStepImpl.createFormatterOptions(properties); + jsFormatter = ToolFactory.createCodeFormatter(jsOptions, ToolFactory.M_FORMAT_EXISTING); + SpotlessPreferences.configurePluginPreferences(CSSCorePlugin.getDefault(), properties); + } + + @Override + public String format(String raw) throws Exception { + raw = super.format(raw); + + // Not sure how Eclipse binds the JS formatter to HTML. The formatting is accomplished manually instead. + IStructuredDocument document = (IStructuredDocument) new HTMLDocumentLoader().createNewStructuredDocument(); + document.setPreferredLineDelimiter(LINE_DELIMITER); + document.set(raw); + StructuredDocumentProcessor jsProcessor = new StructuredDocumentProcessor( + document, IHTMLPartitions.SCRIPT, JsRegionProcessor.createFactory(htmlFormatterIndent)); + jsProcessor.apply(jsFormatter); + + return document.get(); + } + + /** + * * The WTP {@link HTMLFormatProcessorImpl} does not allow a strict case formatting. + * Hence additionally the {@link HTMLCleanupProcessorImpl} is used. + *

+ * Note that a preferences like {@code TAG_NAME_CASE} are not used by the + * formatter, though configurable in the formatters preference GUI. + * The user must instead configure for example {@code CLEANUP_TAG_NAME_CASE} + * in the cleanup GUI. + *

+ */ + public static class SpotlessHtmlCleanup extends HTMLCleanupProcessorImpl implements CleanupStep.ProcessorAccessor { + private HTMLFormatProcessorImpl processor = null; + + @Override + public String getThisContentType() { + return getContentType(); + } + + @Override + public IStructuredFormatProcessor getThisFormatProcessor() { + return getFormatProcessor(); + } + + @Override + public void refreshThisCleanupPreferences() { + refreshCleanupPreferences(); + } + + @Override + protected IStructuredFormatProcessor getFormatProcessor() { + if (null == processor) { + processor = new HTMLFormatProcessorImpl(); + } + return processor; + } + + String getIndent() { + /* + * The processor must not be null, + * otherwise it has not been configured yet, + * and the result would be incorrect. + */ + return processor.getFormatPreferences().getIndent(); + } + + } + + private final static Set SUPPORTED_CSS_FORMAT_PREFS = new HashSet(Arrays.asList( + CASE_IDENTIFIER, + CASE_SELECTOR, + CASE_PROPERTY_NAME, + CASE_PROPERTY_VALUE, + FORMAT_BETWEEN_VALUE, + FORMAT_PROP_POST_DELIM, + FORMAT_PROP_PRE_DELIM, + FORMAT_QUOTE, + FORMAT_QUOTE_IN_URI, + FORMAT_SPACE_BETWEEN_SELECTORS, + WRAPPING_NEWLINE_ON_OPEN_BRACE, + WRAPPING_ONE_PER_LINE, + WRAPPING_PROHIBIT_WRAP_ON_ATTR, + LINE_WIDTH, + INDENTATION_CHAR, + INDENTATION_SIZE, + QUOTE_ATTR_VALUES, + CLEAR_ALL_BLANK_LINES)); + + private static Properties getCSSFormattingProperties(final Properties properties) { + Properties filteredProperties = new Properties(); + properties.entrySet().stream().filter( + entry -> SUPPORTED_CSS_FORMAT_PREFS.contains(entry.getKey())).forEach(entry -> filteredProperties.put(entry.getKey(), entry.getValue())); + return filteredProperties; + } +} diff --git a/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseJsFormatterStepImpl.java b/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseJsFormatterStepImpl.java new file mode 100644 index 0000000000..36d25860bc --- /dev/null +++ b/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseJsFormatterStepImpl.java @@ -0,0 +1,157 @@ +/* + * 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.eclipse.wtp; + +import static com.diffplug.spotless.extra.eclipse.base.SpotlessEclipseFramework.DefaultBundles.*; +import static com.diffplug.spotless.extra.eclipse.base.SpotlessEclipseFramework.LINE_DELIMITER; + +import java.util.AbstractMap.SimpleEntry; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; +import java.util.Set; +import java.util.function.Consumer; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.Document; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IDocumentPartitioner; +import org.eclipse.jface.text.ITypedRegion; +import org.eclipse.jface.text.TextUtilities; +import org.eclipse.jface.text.TypedPosition; +import org.eclipse.jface.text.formatter.FormattingContextProperties; +import org.eclipse.jface.text.formatter.IFormattingContext; +import org.eclipse.jface.text.rules.FastPartitioner; +import org.eclipse.text.edits.MultiTextEdit; +import org.eclipse.text.edits.TextEdit; +import org.eclipse.wst.jsdt.core.JavaScriptCore; +import org.eclipse.wst.jsdt.core.ToolFactory; +import org.eclipse.wst.jsdt.core.formatter.CodeFormatter; +import org.eclipse.wst.jsdt.core.formatter.DefaultCodeFormatterConstants; +import org.eclipse.wst.jsdt.internal.ui.text.FastJavaPartitionScanner; +import org.eclipse.wst.jsdt.internal.ui.text.comment.CommentFormattingContext; +import org.eclipse.wst.jsdt.internal.ui.text.comment.CommentFormattingStrategy; +import org.eclipse.wst.jsdt.ui.text.IJavaScriptPartitions; +import org.osgi.framework.Bundle; + +import com.diffplug.spotless.extra.eclipse.base.SpotlessEclipseCoreConfig; +import com.diffplug.spotless.extra.eclipse.base.SpotlessEclipseFramework; + +/** Formatter step which calls out to the Eclipse JS formatter. */ +public class EclipseJsFormatterStepImpl { + + /** Spotless Eclipse framework core setup for JS formatter support.*/ + public static final Consumer JS_CORE_CONFIG = (core) -> { + //The JS model requires the JDT indexer, hence a headless Eclipse cannot be used. + core.add(PLATFORM, Bundle.ACTIVE); + core.add(REGISTRY, PREFERENCES, COMMON); + }; + + private final static String[] COMMENT_TYPES = { + IJavaScriptPartitions.JAVA_DOC, + IJavaScriptPartitions.JAVA_MULTI_LINE_COMMENT, + IJavaScriptPartitions.JAVA_SINGLE_LINE_COMMENT, + IJavaScriptPartitions.JAVA_STRING, + IJavaScriptPartitions.JAVA_CHARACTER + }; + + private final static Map OPTION_2_COMMENT_TYPE = Collections.unmodifiableMap(Stream.of( + new SimpleEntry<>(DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_LINE_COMMENT, IJavaScriptPartitions.JAVA_SINGLE_LINE_COMMENT), + new SimpleEntry<>(DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_BLOCK_COMMENT, IJavaScriptPartitions.JAVA_MULTI_LINE_COMMENT), + new SimpleEntry<>(DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_JAVADOC_COMMENT, IJavaScriptPartitions.JAVA_DOC)).collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue()))); + + private final CodeFormatter formatter; + private final Map options; + private final Set commentTypesToBeFormatted; + + public EclipseJsFormatterStepImpl(Properties properties) throws Exception { + SpotlessEclipseFramework.setup( + JS_CORE_CONFIG, + config -> config.applyDefault(), + plugins -> { + plugins.applyDefault(); + // The JS core uses EFS for determination of temporary storage location + plugins.add(new org.eclipse.core.internal.filesystem.Activator()); + // The JS core provides the JSDT formatter + plugins.add(new org.eclipse.wst.jsdt.core.JavaScriptCore()); + }); + options = createFormatterOptions(properties); + commentTypesToBeFormatted = OPTION_2_COMMENT_TYPE.entrySet().stream().filter(x -> DefaultCodeFormatterConstants.TRUE.equals(options.get(x.getKey()))).map(x -> x.getValue()).collect(Collectors.toSet()); + formatter = ToolFactory.createCodeFormatter(options, ToolFactory.M_FORMAT_EXISTING); + } + + @SuppressWarnings("unchecked") + static Map createFormatterOptions(Properties properties) { + Map options = JavaScriptCore.getDefaultOptions(); + options.putAll(DefaultCodeFormatterConstants.getJSLintConventionsSettings()); + options.putAll(new HashMap(properties)); + return options; + } + + /** Formatting JavaScript string */ + public String format(String raw) throws Exception { + raw = formatComments(raw); + // The comment formatter messed up the code a little bit (adding some line breaks). Now we format the code. + IDocument doc = new Document(raw); + TextEdit edit = formatter.format(CodeFormatter.K_JAVASCRIPT_UNIT, raw, 0, raw.length(), 0, LINE_DELIMITER); + if (edit == null) { + throw new IllegalArgumentException("Invalid JavaScript syntax for formatting."); + } else { + edit.apply(doc); + } + return doc.get(); + } + + /** + * Comment formats like it would be accomplished by the JDTS UI, without setting up the UI. + * @see org.eclipse.wst.jsdt.internal.ui.fix.CommentFormatFix + */ + private String formatComments(String raw) { + Document doc = new Document(raw); + IDocumentPartitioner commentPartitioner = new FastPartitioner(new FastJavaPartitionScanner(), COMMENT_TYPES); + doc.setDocumentPartitioner(IJavaScriptPartitions.JAVA_PARTITIONING, commentPartitioner); + commentPartitioner.connect(doc); + CommentFormattingStrategy commentFormatter = new CommentFormattingStrategy(); + IFormattingContext context = new CommentFormattingContext(); + context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, options); + context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.TRUE); + context.setProperty(FormattingContextProperties.CONTEXT_MEDIUM, doc); + try { + ITypedRegion[] regions = TextUtilities.computePartitioning(doc, IJavaScriptPartitions.JAVA_PARTITIONING, 0, doc.getLength(), false); + MultiTextEdit resultEdit = new MultiTextEdit(); + Arrays.asList(regions).stream().filter(reg -> commentTypesToBeFormatted.contains(reg.getType())).forEach(region -> { + TypedPosition typedPosition = new TypedPosition(region.getOffset(), region.getLength(), region.getType()); + context.setProperty(FormattingContextProperties.CONTEXT_PARTITION, typedPosition); + commentFormatter.formatterStarts(context); + TextEdit edit = commentFormatter.calculateTextEdit(); + commentFormatter.formatterStops(); + if (null != edit && edit.hasChildren()) { + resultEdit.addChild(edit); + } + }); + resultEdit.apply(doc); + return doc.get(); + } catch (BadLocationException e) { + //Silently ignore comment formatting exceptions and return the original string + return raw; + } + } + +} diff --git a/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseJsonFormatterStepImpl.java b/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseJsonFormatterStepImpl.java new file mode 100644 index 0000000000..f77f0d6f27 --- /dev/null +++ b/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseJsonFormatterStepImpl.java @@ -0,0 +1,68 @@ +/* + * 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.eclipse.wtp; + +import java.util.Properties; + +import org.eclipse.wst.json.core.JSONCorePlugin; +import org.eclipse.wst.json.core.cleanup.CleanupProcessorJSON; +import org.eclipse.wst.json.core.internal.preferences.JSONCorePreferenceInitializer; +import org.eclipse.wst.sse.core.internal.format.IStructuredFormatProcessor; + +import com.diffplug.spotless.extra.eclipse.wtp.sse.CleanupStep; + +/** Formatter step which calls out to the Eclipse JSON cleanup processor and formatter. */ +public class EclipseJsonFormatterStepImpl extends CleanupStep { + + public EclipseJsonFormatterStepImpl(Properties properties) throws Exception { + super(new SpotlessJsonCleanup(), plugin -> plugin.add(new JSONCorePlugin())); + configure(properties, true, JSONCorePlugin.getDefault(), new JSONCorePreferenceInitializer()); + } + + /** + * The JSON CleanUp is partly implemented. + *

+ * For example the abstract formatter supports the + * CASE_PROPERTY_NAME configuration item. + * However, this seems to be all dead code and there seems + * to be no way in the latest Eclipse GUI to configure + * or trigger the clean-up process. + *

+ *

+ * Here we just use the CleanupProcessorJSON to reuse the common + * interface to trigger the formatting. + *

+ * See {@code org.eclipse.wst.json.core.internal.format.AbstractJSONSourceFormatter} for details. + */ + public static class SpotlessJsonCleanup extends CleanupProcessorJSON implements CleanupStep.ProcessorAccessor { + @Override + public String getThisContentType() { + return getContentType(); + } + + @Override + public IStructuredFormatProcessor getThisFormatProcessor() { + return getFormatProcessor(); + } + + @Override + public void refreshThisCleanupPreferences() { + refreshCleanupPreferences(); + } + + } + +} diff --git a/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseXmlFormatterStepImpl.java b/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseXmlFormatterStepImpl.java new file mode 100644 index 0000000000..45d4ee8972 --- /dev/null +++ b/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseXmlFormatterStepImpl.java @@ -0,0 +1,161 @@ +/* + * 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.eclipse.wtp; + +import static com.diffplug.spotless.extra.eclipse.base.SpotlessEclipseFramework.LINE_DELIMITER; +import static org.eclipse.wst.xml.core.internal.preferences.XMLCorePreferenceNames.*; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Properties; +import java.util.Set; + +import org.eclipse.core.runtime.Plugin; +import org.eclipse.jface.text.IDocumentPartitioner; +import org.eclipse.text.edits.TextEdit; +import org.eclipse.wst.common.uriresolver.internal.provisional.URIResolverPlugin; +import org.eclipse.wst.dtd.core.internal.DTDCorePlugin; +import org.eclipse.wst.sse.core.internal.provisional.INodeAdapterFactory; +import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument; +import org.eclipse.wst.sse.core.internal.text.BasicStructuredDocument; +import org.eclipse.wst.xml.core.internal.XMLCorePlugin; +import org.eclipse.wst.xml.core.internal.contentmodel.modelquery.CMDocumentManager; +import org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery; +import org.eclipse.wst.xml.core.internal.document.DOMModelImpl; +import org.eclipse.wst.xml.core.internal.formatter.DefaultXMLPartitionFormatter; +import org.eclipse.wst.xml.core.internal.formatter.XMLFormattingPreferences; +import org.eclipse.wst.xml.core.internal.modelquery.ModelQueryAdapterFactoryForXML; +import org.eclipse.wst.xml.core.internal.modelquery.ModelQueryUtil; +import org.eclipse.wst.xml.core.internal.parser.XMLSourceParser; +import org.eclipse.wst.xml.core.internal.preferences.XMLCorePreferenceInitializer; +import org.eclipse.wst.xml.core.internal.text.rules.StructuredTextPartitionerForXML; +import org.eclipse.wst.xsd.core.internal.XSDCorePlugin; +import org.eclipse.xsd.util.XSDSchemaBuildingTools; +import org.osgi.framework.BundleException; + +import com.diffplug.spotless.extra.eclipse.base.SpotlessEclipseFramework; +import com.diffplug.spotless.extra.eclipse.wtp.sse.SpotlessPreferences; + +/** Formatter step which calls out to the Eclipse XML formatter. */ +public class EclipseXmlFormatterStepImpl { + private static XmlFormattingPreferencesFactory PREFERENCE_FACTORY = null; + + private final DefaultXMLPartitionFormatter formatter; + private final XMLFormattingPreferences preferences; + private final INodeAdapterFactory xmlAdapterFactory; + + public EclipseXmlFormatterStepImpl(Properties properties) throws Exception { + setupFramework(); + preferences = PREFERENCE_FACTORY.create(properties); + formatter = new DefaultXMLPartitionFormatter(); + //The adapter factory maintains the common CMDocumentCache + xmlAdapterFactory = new ModelQueryAdapterFactoryForXML(); + } + + private static void setupFramework() throws BundleException { + if (SpotlessEclipseFramework.setup( + plugins -> { + plugins.applyDefault(); + //The WST XML formatter + plugins.add(new XMLCorePlugin()); + //XSDs/DTDs must be resolved by URI + plugins.add(new URIResolverPlugin()); + //Support formatting based on DTD restrictions + plugins.add(new DTDCorePlugin()); + //Support formatting based on XSD restrictions + plugins.add(new XSDCorePlugin()); + })) { + PREFERENCE_FACTORY = new XmlFormattingPreferencesFactory(); + //Register required EMF factories + XSDSchemaBuildingTools.getXSDFactory(); + } + } + + /** Formatting XML string resolving URIs according its base location */ + public String format(String raw, String baseLocation) throws Exception { + IStructuredDocument document = new BasicStructuredDocument(new XMLSourceParser()); + document.setPreferredLineDelimiter(LINE_DELIMITER); + IDocumentPartitioner partitioner = new StructuredTextPartitionerForXML(); + document.setDocumentPartitioner(new StructuredTextPartitionerForXML()); + partitioner.connect(document); + document.set(raw); + DOMModelImpl xmlDOM = new DOMModelImpl(); + xmlDOM.setBaseLocation(baseLocation); + xmlDOM.getFactoryRegistry().addFactory(xmlAdapterFactory); + xmlDOM.setStructuredDocument(document); + ModelQuery modelQuery = ModelQueryUtil.getModelQuery(xmlDOM); + modelQuery.getCMDocumentManager().setPropertyEnabled(CMDocumentManager.PROPERTY_USE_CACHED_RESOLVED_URI, true); + TextEdit formatterChanges = formatter.format(xmlDOM, 0, document.getLength(), preferences); + formatterChanges.apply(document); + return document.get(); + } + + private static class XmlFormattingPreferencesFactory { + + private final static Set SUPPORTED_XML_FORMAT_PREFS = new HashSet(Arrays.asList( + FORMAT_COMMENT_TEXT, + FORMAT_COMMENT_JOIN_LINES, + LINE_WIDTH, + SPLIT_MULTI_ATTRS, + ALIGN_END_BRACKET, + SPACE_BEFORE_EMPTY_CLOSE_TAG, + PRESERVE_CDATACONTENT, + INDENTATION_CHAR, + INDENTATION_SIZE, + CLEAR_ALL_BLANK_LINES)); + + XmlFormattingPreferencesFactory() { + XMLCorePreferenceInitializer initializer = new XMLCorePreferenceInitializer(); + initializer.initializeDefaultPreferences(); + /* + * The cache is only used for system catalogs, but not for user catalogs. + * It requires the SSECorePLugin, which has either a big performance overhead, + * or needs a dirty mocking (we don't really require its functions but it needs to be there). + * So we disable the cache for now. + * This might cause a performance drop in case for example XSDs are formatted. + * But these standard/system restriction files contain anyway no formatting rules. + * So in case of performance inconveniences, we could add a Spotless property to disable the + * XSD/DTD parsing entirely (for example by adding an own URI resolver). + */ + Properties properties = new Properties(); + properties.setProperty(CMDOCUMENT_GLOBAL_CACHE_ENABLED, Boolean.toString(false)); + Plugin plugin = XMLCorePlugin.getDefault(); + SpotlessPreferences.configurePluginPreferences(plugin, properties); + } + + XMLFormattingPreferences create(final Properties properties) { + SpotlessPreferences.configureCatalog(properties); + return createFormattingPreference(properties); + } + + private XMLFormattingPreferences createFormattingPreference(final Properties properties) { + Properties newXmlProperties = getXMLFormattingProperties(properties); + Plugin plugin = XMLCorePlugin.getDefault(); + Properties defaultXmlProperties = SpotlessPreferences.configurePluginPreferences(plugin, newXmlProperties); + XMLFormattingPreferences xmlPreferences = new XMLFormattingPreferences(); + SpotlessPreferences.configurePluginPreferences(plugin, defaultXmlProperties); + return xmlPreferences; + } + + private Properties getXMLFormattingProperties(final Properties properties) { + Properties filteredProperties = new Properties(); + properties.entrySet().stream().filter( + entry -> SUPPORTED_XML_FORMAT_PREFS.contains(entry.getKey())).forEach(entry -> filteredProperties.put(entry.getKey(), entry.getValue())); + return filteredProperties; + } + } + +} diff --git a/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/html/JsRegionProcessor.java b/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/html/JsRegionProcessor.java new file mode 100644 index 0000000000..d856335c1b --- /dev/null +++ b/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/html/JsRegionProcessor.java @@ -0,0 +1,86 @@ +/* + * 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.eclipse.wtp.html; + +import static com.diffplug.spotless.extra.eclipse.base.SpotlessEclipseFramework.LINE_DELIMITER; + +import java.util.function.BiFunction; + +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.ITypedRegion; +import org.eclipse.text.edits.MalformedTreeException; +import org.eclipse.text.edits.MultiTextEdit; +import org.eclipse.text.edits.TextEdit; +import org.eclipse.wst.jsdt.core.formatter.CodeFormatter; +import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument; + +import com.diffplug.spotless.extra.eclipse.wtp.html.StructuredDocumentProcessor.RegionProcessor; + +/** + * Provides additional formating to the plain JS {@link CodeFormatter}: + *
    + *
  • Eclipse HTML places the embedded JS in separated lines by adding a line break after/before <script/> tag.
  • + *
  • Eclipse HTML treats the text before the closing </script> tag as part of the script region.
  • + *
+ *

+ * Note that the closing tag is indented by Eclipse using the embedded formatters indentation, + * whereas the opening tag indentation is configured by the HTML preferences. + * This is more a bug than a feature, but Spotless formatter output shall be identical + * to the one of Eclipse. + *

+ */ +public class JsRegionProcessor extends RegionProcessor { + public JsRegionProcessor(IStructuredDocument document, ITypedRegion scriptRegion, String htmlIndent) { + super(document, scriptRegion, htmlIndent); + } + + @Override + protected void applyFirst(CodeFormatter formatter) throws MalformedTreeException, BadLocationException { + MultiTextEdit modifications = new MultiTextEdit(); + String jsSource = document.get(region.getOffset(), region.getLength()); + TextEdit jsEdit = formatter.format(CodeFormatter.K_JAVASCRIPT_UNIT, jsSource, 0, jsSource.length(), indentationLevel + 1, LINE_DELIMITER); + if (null != jsEdit) { + jsEdit.moveTree(region.getOffset()); + modifications.addChild(jsEdit); + } + modifications.apply(document); + } + + @Override + protected void applySecond(CodeFormatter formatter) throws MalformedTreeException, BadLocationException { + MultiTextEdit modifications = new MultiTextEdit(); + int regionEnd = region.getOffset() + region.getLength(); + regionEnd += fixDelimiter(modifications, region.getOffset(), false); + regionEnd += fixDelimiter(modifications, region.getOffset() + region.getLength() - 1, true); + modifications.apply(document); + modifications.removeChildren(); + fixTagIndent(modifications, regionEnd, formatter.createIndentationString(indentationLevel)); + modifications.apply(document); + } + + /** Factory for {@link StructuredDocumentProcessor}*/ + public static BiFunction createFactory(String htmlIndent) { + return new BiFunction() { + + @Override + public JsRegionProcessor apply(IStructuredDocument document, ITypedRegion region) { + return new JsRegionProcessor(document, region, htmlIndent); + } + + }; + } + +} diff --git a/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/html/StructuredDocumentProcessor.java b/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/html/StructuredDocumentProcessor.java new file mode 100644 index 0000000000..6adc258c78 --- /dev/null +++ b/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/html/StructuredDocumentProcessor.java @@ -0,0 +1,205 @@ +/* + * 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.eclipse.wtp.html; + +import static com.diffplug.spotless.extra.eclipse.base.SpotlessEclipseFramework.LINE_DELIMITER; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.function.BiFunction; +import java.util.stream.Collectors; + +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.ITypedRegion; +import org.eclipse.text.edits.InsertEdit; +import org.eclipse.text.edits.MalformedTreeException; +import org.eclipse.text.edits.MultiTextEdit; +import org.eclipse.text.edits.ReplaceEdit; +import org.eclipse.text.edits.TextEdit; +import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument; + +/** + * The HTML formatter uses for different regions of the structured document, + * other formatters, explicitly for CSS and JS. + * Adaptations of the current formatter modifications are tedious, + * since the adaptations e.g. overlap with the required additional modifications. + * Hence the modifications is split in steps, whereas the regions of the + * structured documents and their offsets are regenerate between the first + * ad second step. + */ +public class StructuredDocumentProcessor { + private final String type; + private final BiFunction> factory; + private final IStructuredDocument document; + private final int numberOfRegions; + + /** + * Constructs a document processor + * @param document Document to be processed + * @param type Document type ID recognized by {@code IContentTypeManager} service + * @param factory Factory for structured document processor + */ + public StructuredDocumentProcessor(IStructuredDocument document, String type, + BiFunction> factory) { + this.type = type; + this.factory = factory; + this.document = document; + numberOfRegions = getRegions().size(); + } + + /** Applies processor on document, using a given formatter */ + public void apply(T formatter) { + for (int currentRegionId = 0; currentRegionId < numberOfRegions; currentRegionId++) { + applyOnRegion(currentRegionId, formatter); + } + } + + private List getRegions() { + try { + return Arrays.asList(document.computePartitioning(0, document.getLength())).stream().filter(reg -> type == reg.getType()).collect(Collectors.toList()); + } catch (BadLocationException e) { + /* + * This prevents the processing in case the entire document + * cannot be processed (e.g. the document is incomplete). + */ + return new ArrayList(0); + } + } + + private void applyOnRegion(int number, T formatter) { + RegionProcessor adapter = getRegionProcessor(number); + try { + adapter.applyFirst(formatter); + adapter = getRegionProcessor(number); + adapter.applySecond(formatter); + } catch (MalformedTreeException | BadLocationException e) { + throw new IllegalArgumentException( + String.format("%s formatting failed between lines %d and %d. Most likely the syntax is not recognized.", + type, adapter.getFirstLine(), adapter.getLastLine()), + e); + } + } + + private RegionProcessor getRegionProcessor(int number) { + List regions = getRegions(); + if (numberOfRegions != regions.size()) { + //Don't catch this. This is a severe internal bug! + throw new IllegalArgumentException( + String.format( + "During first '%s' formatting step, the number of detected regions changed from '%d' to '%d'", + type, numberOfRegions, regions.size())); + } + ITypedRegion region = regions.get(number); + return factory.apply(document, region); + } + + /** Base class for region adaptations. */ + public static abstract class RegionProcessor { + protected final IStructuredDocument document; + protected final ITypedRegion region; + protected final int indentationLevel; + protected final int firstLine; + protected final int lastLine; + + protected RegionProcessor(IStructuredDocument document, ITypedRegion region, String htmlIndent) { + this.document = document; + this.region = region; + indentationLevel = computeIndent(document, region, htmlIndent); + firstLine = document.getLineOfOffset(region.getOffset()); + lastLine = document.getLineOfOffset(region.getOffset() + region.getLength()); + } + + public int getFirstLine() { + return firstLine; + } + + public int getLastLine() { + return lastLine; + } + + private static int computeIndent(IStructuredDocument document, ITypedRegion region, String htmlIndent) { + int indent = 0; + try { + int lineNumber = document.getLineOfOffset(region.getOffset()); + document.getNumberOfLines(); + int lineOffset = document.getLineOffset(lineNumber); + String lineStart = document.get(lineOffset, region.getOffset() - lineOffset); + while (lineStart.length() > htmlIndent.length()) { + if (lineStart.startsWith(htmlIndent)) { + indent++; + } else { + break; + } + lineStart = lineStart.substring(htmlIndent.length()); + } + } catch (BadLocationException e) { + /* + * Skip addition indentation. This normally indicates a malformed HTML + * outside of this region, which cannot be handled here. + */ + indent = 0; + } + return indent; + } + + /** Add delimiter at or after given offset, if there is none at the offset position. Returns the number of characters inserted. */ + protected int fixDelimiter(MultiTextEdit modifications, int offset, boolean addAfter) throws BadLocationException { + int delimiterLength = LINE_DELIMITER.length(); + String delimiter = document.get(offset, delimiterLength); + if (!LINE_DELIMITER.equals(delimiter)) { + if (addAfter) { + offset += 1; + } + modifications.addChild(new InsertEdit(offset, LINE_DELIMITER)); + return LINE_DELIMITER.length(); + } + return 0; + } + + /** Fix the tag indentation at a given position with a predefined indentation. */ + protected void fixTagIndent(MultiTextEdit modifications, int offset, String indentString) throws BadLocationException { + int lineNumber = document.getLineOfOffset(offset); + if (lineNumber >= document.getNumberOfLines()) { + //Nothing to change for last line. If syntax is correct, there is no indentation. If syntax is not correct, there is nothing to do. + return; + } + int lineStart = document.getLineOffset(lineNumber); + int lineEnd = document.getLineOffset(lineNumber + 1); + String lineContent = document.get(lineStart, lineEnd - lineStart); + StringBuilder currentIndent = new StringBuilder(); + lineContent.chars().filter(c -> { + if (c == ' ' || c == '\t') { + currentIndent.append(c); + return false; + } + return true; + }).findFirst(); + if (!indentString.equals(currentIndent.toString())) { + TextEdit replaceIndent = new ReplaceEdit(lineStart, currentIndent.length(), indentString); + replaceIndent.apply(document); + } + } + + /** First application of modifications */ + abstract protected void applyFirst(T formatter) throws MalformedTreeException, BadLocationException; + + /** Second application of modifications (based on new regions) */ + abstract protected void applySecond(T formatter) throws MalformedTreeException, BadLocationException; + + } + +} diff --git a/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/html/package-info.java b/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/html/package-info.java new file mode 100644 index 0000000000..770e126a1b --- /dev/null +++ b/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/html/package-info.java @@ -0,0 +1,20 @@ +/* + * 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. + */ +/** Eclipse WTP HTML formatter helper */ +@ParametersAreNonnullByDefault +package com.diffplug.spotless.extra.eclipse.wtp.html; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/package-info.java b/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/package-info.java new file mode 100644 index 0000000000..6cf6f0bfff --- /dev/null +++ b/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/package-info.java @@ -0,0 +1,20 @@ +/* + * 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. + */ +/** Eclipse WTP based Spotless formatters */ +@ParametersAreNonnullByDefault +package com.diffplug.spotless.extra.eclipse.wtp; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/sse/CleanupStep.java b/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/sse/CleanupStep.java new file mode 100644 index 0000000000..9668bdc664 --- /dev/null +++ b/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/sse/CleanupStep.java @@ -0,0 +1,153 @@ +/* + * 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.eclipse.wtp.sse; + +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; +import java.util.Properties; +import java.util.function.Consumer; + +import org.eclipse.core.internal.preferences.PreferencesService; +import org.eclipse.core.runtime.Plugin; +import org.eclipse.core.runtime.content.IContentTypeManager; +import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer; +import org.eclipse.core.runtime.preferences.IPreferencesService; +import org.eclipse.wst.sse.core.internal.cleanup.AbstractStructuredCleanupProcessor; +import org.eclipse.wst.sse.core.internal.format.AbstractStructuredFormatProcessor; +import org.eclipse.wst.sse.core.internal.format.IStructuredFormatProcessor; +import org.osgi.framework.BundleActivator; + +import com.diffplug.spotless.extra.eclipse.base.SpotlessEclipseCoreConfig; +import com.diffplug.spotless.extra.eclipse.base.SpotlessEclipseFramework; + +/** + * Common base class for step implementations based on an SSE cleanup processor. + *

+ * Some WTP formatters do not apply all formatting within a formatter process, + * but provide a cleanup and a formatter process, whereas the cleanup process + * calls the formatter process. + *

+ *

+ * Not all cleanup processes provided by WTP are suitable for Spotless formatting. + * For example the XML cleanup processor focus on syntax and line delimiter + * corrections. The syntax correction is based on the corresponding XSD/DTD, + * but it must be assumed by the Spotless formatter that the provided files + * are valid. It cannot be the purpose of a formatter to correct syntax. + * A Java formatter should also not attempt to correct compilation errors. + * A line delimiter correction would furthermore violate the Spotless rule + * that the strings processed by a Spotless formatter chain must use + * UNIX style delimiters. + *

+ * @see org.eclipse.wst.sse.core.internal.cleanup.AbstractStructuredCleanupProcessor + */ +public class CleanupStep { + + /** + * Some of the SEE AbstractStructuredCleanupProcessor interface shall be + * made public to provide cleaner interfaces. */ + public interface ProcessorAccessor { + /** Returns this.getContentType() */ + String getThisContentType(); + + /** Returns this.getFormatProcessor() */ + IStructuredFormatProcessor getThisFormatProcessor(); + + /** Calls this.refreshCleanupPreferences() */ + void refreshThisCleanupPreferences(); + } + + // The formatter cannot per configured per instance + private final static Properties CONFIG = new Properties(); + private static boolean FIRST_CONFIG = true; + + protected final T processor; + + protected CleanupStep(T processor, Consumer> addptionalPlugins) throws Exception { + this(processor, core -> core.applyDefault(), addptionalPlugins); + } + + protected CleanupStep(T processor, Consumer core, Consumer> addptionalPlugins) throws Exception { + SpotlessEclipseFramework.setup( + core, + config -> { + config.disableDebugging(); + config.hideEnvironment(); + config.useTemporaryLocations(); + config.changeSystemLineSeparator(); + //Allow association of string passed in the CleanupStep.format to its type/plugin + config.add(IContentTypeManager.class, new ContentTypeManager(processor)); + //The preference lookup via the ContentTypeManager, requires a preference service + config.add(IPreferencesService.class, PreferencesService.getDefault()); + }, + plugins -> { + plugins.applyDefault(); + List additional = new LinkedList(); + addptionalPlugins.accept(additional); + plugins.add(additional); + /* + * The core preferences require do lookup the resources "config/override.properties" + * from the plugin ID. + * The values are never used, nor do we require the complete SSE core plugin to be started. + * Hence we just provide the internal plugin. + */ + plugins.add(new org.eclipse.wst.sse.core.internal.encoding.util.CodedResourcePlugin()); + }); + this.processor = processor; + /* + * Don't refresh the preferences every time a clone of the processor is created. + * All processors shall use the preferences of its parent. + */ + this.processor.refreshCleanupPreferences = false; + } + + protected final void configure(Properties properties, boolean usesPreferenceService, Plugin plugin, AbstractPreferenceInitializer preferencesInit) { + synchronized (CONFIG) { + if (usesPreferenceService) { + assertConfigHasNotChanged(properties); + } + preferencesInit.initializeDefaultPreferences(); + SpotlessPreferences.configurePluginPreferences(plugin, properties); + processor.refreshThisCleanupPreferences(); // Initialize cleanup processor preferences (if there are any) + IStructuredFormatProcessor formatter = processor.getThisFormatProcessor(); // Initialize processor preferences by creating the formatter for the first time + if (formatter instanceof AbstractStructuredFormatProcessor) { + ((AbstractStructuredFormatProcessor) formatter).refreshFormatPreferences = false; + } + } + } + + private static void assertConfigHasNotChanged(final Properties properties) { + synchronized (CONFIG) { + if (FIRST_CONFIG) { + FIRST_CONFIG = false; + CONFIG.putAll(properties); + } else if (!CONFIG.equals(properties)) { + throw new IllegalArgumentException("The Eclipse formatter does not support multiple configurations."); + } + } + } + + /** + * Calls the cleanup and formatting task of the processor and returns the formatted string. + * @param raw Dirty string + * @return Formatted string + * @throws Exception All exceptions are considered fatal to the build process (Gradle, Maven, ...) and should not be caught. + */ + public String format(String raw) throws Exception { + return processor.cleanupContent(raw); + } + +} diff --git a/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/sse/ContentTypeManager.java b/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/sse/ContentTypeManager.java new file mode 100644 index 0000000000..d60d3c6e02 --- /dev/null +++ b/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/sse/ContentTypeManager.java @@ -0,0 +1,182 @@ +/* + * 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.eclipse.wtp.sse; + +import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.QualifiedName; +import org.eclipse.core.runtime.content.IContentDescription; +import org.eclipse.core.runtime.content.IContentType; +import org.eclipse.core.runtime.content.IContentTypeSettings; +import org.eclipse.core.runtime.preferences.IScopeContext; +import org.eclipse.wst.css.core.internal.provisional.contenttype.ContentTypeIdForCSS; +import org.eclipse.wst.html.core.internal.provisional.contenttype.ContentTypeIdForHTML; +import org.eclipse.wst.json.core.contenttype.ContentTypeIdForJSON; +import org.eclipse.wst.xml.core.internal.provisional.contenttype.ContentTypeIdForXML; + +import com.diffplug.spotless.extra.eclipse.base.service.NoContentTypeSpecificHandling; + +/** + * For some embedded formatters, the WTP uses the content type ID for + * preferences lookup. + *

+ * The preference lookup is accomplished via the Eclipse preference service, + * which must be provided in combination with this service. + * For cleanup tasks, the ID mapping is also used by the model handler + * to determine the model which a string stream requires. + *

+ * @see org.eclipse.wst.sse.core.internal.modelhandler.ModelHandlerRegistry + */ +class ContentTypeManager extends NoContentTypeSpecificHandling { + private final Map id2Object; + private final IContentType processorStepType; + + /** + * Content type manager as required for cleanup steps. + * @param formatterContentTypeID The content type of the formatter step + */ + ContentTypeManager(CleanupStep.ProcessorAccessor processor) { + id2Object = new HashMap(); + Arrays.asList( + ContentTypeIdForCSS.ContentTypeID_CSS, + ContentTypeIdForXML.ContentTypeID_XML, + ContentTypeIdForHTML.ContentTypeID_HTML, + ContentTypeIdForJSON.ContentTypeID_JSON) + .stream().forEach(id -> id2Object.put(id, new ContentTypeId(id))); + processorStepType = id2Object.get(processor.getThisContentType()); + if (null == processorStepType) { + throw new IllegalArgumentException( + String.format( + "The manager does not support content type '%s' of processor '%s'.", + processor.getThisContentType(), processor.getClass().getName())); + } + } + + @Override + public IContentType getContentType(String contentTypeIdentifier) { + /* + * It is OK to return null here since the manager is only used as an additional + * helper to alter default behavior. + */ + return id2Object.get(contentTypeIdentifier); + } + + @Override + public IContentType findContentTypeFor(InputStream contents, String fileName) throws IOException { + //We only format things here with the given processor, so this answer is always correct. + return processorStepType; + } + + /** + * The WTP uses the manager only for ID mapping, so most of the methods are not used. + * Actually it has a hand stitched way for transforming the content type ID + * {@code org.eclipse.wst...source} to the plugin ID {@code org.eclipse.wst...core}. + * @see org.eclipse.wst.sse.core.internal.encoding.ContentBasedPreferenceGateway + */ + private static class ContentTypeId implements IContentType { + + private final String id; + + ContentTypeId(String id) { + this.id = id; + } + + @Override + public void addFileSpec(String fileSpec, int type) throws CoreException {} + + @Override + public void removeFileSpec(String fileSpec, int type) throws CoreException {} + + @Override + public void setDefaultCharset(String userCharset) throws CoreException {} + + @Override + public boolean isUserDefined() { + return false; + } + + @Override + public IContentType getBaseType() { + return null; + } + + @Override + public IContentDescription getDefaultDescription() { + return null; + } + + @Override + public IContentDescription getDescriptionFor(InputStream contents, QualifiedName[] options) throws IOException { + return null; + } + + @Override + public IContentDescription getDescriptionFor(Reader contents, QualifiedName[] options) throws IOException { + return null; + } + + @Override + public String getDefaultCharset() { + return null; + } + + @Override + public String[] getFileSpecs(int type) { + return null; + } + + @Override + public String getId() { + return id; + } + + @Override + public String getName() { + return id; + } + + @Override + public boolean isAssociatedWith(String fileName) { + return false; + } + + @Override + public boolean isAssociatedWith(String fileName, IScopeContext context) { + return false; + } + + @Override + public boolean isKindOf(IContentType another) { + if (null == another) { + return false; + } + return this.id.equals(another.getId()); + } + + @Override + public IContentTypeSettings getSettings(IScopeContext context) throws CoreException { + return null; + } + + } + +} diff --git a/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/sse/SpotlessPreferences.java b/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/sse/SpotlessPreferences.java new file mode 100644 index 0000000000..7b372492fa --- /dev/null +++ b/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/sse/SpotlessPreferences.java @@ -0,0 +1,93 @@ +/* + * 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.eclipse.wtp.sse; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Optional; +import java.util.Properties; + +import org.eclipse.core.runtime.Plugin; +import org.eclipse.core.runtime.preferences.DefaultScope; +import org.eclipse.core.runtime.preferences.IEclipsePreferences; +import org.eclipse.wst.xml.core.internal.XMLCorePlugin; +import org.eclipse.wst.xml.core.internal.catalog.Catalog; +import org.eclipse.wst.xml.core.internal.catalog.CatalogReader; +import org.eclipse.wst.xml.core.internal.catalog.provisional.ICatalog; + +/** Adaptations of the Eclipse WTP environment for Spotless. */ +public class SpotlessPreferences { + /** + * Optional XML catalog for XSD/DTD lookup. + * Catalog versions 1.0 and 1.1 are supported. + *

+ * Value is of type {@code Path}. + *

+ */ + public static final String USER_CATALOG = "userCatalog"; + + /** Configures the Eclipse properties for a Plugin and returns its previous values. */ + public static Properties configurePluginPreferences(Plugin plugin, Properties newValues) { + IEclipsePreferences globalPreferences = DefaultScope.INSTANCE.getNode(plugin.getBundle().getSymbolicName()); + Properties oldValues = new Properties(); + newValues.forEach((key, value) -> { + String oldValue = globalPreferences.get((String) key, null); + if (null != oldValue) { + oldValues.put(key, oldValue); + } + globalPreferences.put((String) key, (String) value); + }); + return oldValues; + } + + public static void configureCatalog(final Properties config) { + Optional catalog = getCatalogConfig(config); + Catalog defaultCatalog = getDefaultCatalog(); + if (catalog.isPresent()) { + try { + InputStream inputStream = new FileInputStream(catalog.get()); + String orgBase = defaultCatalog.getBase(); + defaultCatalog.setBase(catalog.get().toURI().toString()); + CatalogReader.read((Catalog) defaultCatalog, inputStream); + defaultCatalog.setBase(orgBase); + } catch (IOException e) { + throw new IllegalArgumentException( + String.format("Value of '%s' refers to '%s', which cannot be read.", USER_CATALOG, catalog.get())); + } + } else { + defaultCatalog.clear(); + } + } + + private static Catalog getDefaultCatalog() { + ICatalog defaultCatalogI = XMLCorePlugin.getDefault().getDefaultXMLCatalog(); + if (defaultCatalogI instanceof Catalog) { + return (Catalog) defaultCatalogI; + } + throw new IllegalArgumentException("Internal error: Catalog implementation '" + defaultCatalogI.getClass().getCanonicalName() + "' unsupported."); + } + + private static Optional getCatalogConfig(Properties config) { + String newLocation = config.getProperty(USER_CATALOG); + if (newLocation != null && !newLocation.isEmpty()) { + return Optional.of(new File(newLocation)); + } + return Optional.empty(); + } + +} diff --git a/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/sse/package-info.java b/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/sse/package-info.java new file mode 100644 index 0000000000..c986262050 --- /dev/null +++ b/_ext/eclipse-wtp/src/main/java/com/diffplug/spotless/extra/eclipse/wtp/sse/package-info.java @@ -0,0 +1,20 @@ +/* + * 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. + */ +/** Eclipse WTP SEE formatter helper */ +@ParametersAreNonnullByDefault +package com.diffplug.spotless.extra.eclipse.wtp.sse; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/_ext/eclipse-wtp/src/test/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseCssFormatterStepImplTest.java b/_ext/eclipse-wtp/src/test/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseCssFormatterStepImplTest.java new file mode 100644 index 0000000000..6df20f2388 --- /dev/null +++ b/_ext/eclipse-wtp/src/test/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseCssFormatterStepImplTest.java @@ -0,0 +1,79 @@ +/* + * 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.eclipse.wtp; + +import static com.diffplug.spotless.extra.eclipse.base.SpotlessEclipseFramework.LINE_DELIMITER; +import static org.assertj.core.api.Assertions.assertThat; +import static org.eclipse.wst.css.core.internal.preferences.CSSCorePreferenceNames.*; +import static org.junit.Assert.*; + +import java.util.Properties; + +import org.junit.Before; +import org.junit.Test; + +public class EclipseCssFormatterStepImplTest { + + private final static String ILLEGAL_CHAR = Character.toString((char) 254); + private final static String UNFORMATTED = " body {a: v; b: v;}\n".replaceAll("\n", LINE_DELIMITER); + private final static String FORMATTED = "BODY {\n a: v;\n b: v;\n}".replaceAll("\n", LINE_DELIMITER); + private final static String PRE_CODE_UNFORMATTED = "/**
\"Hello\"
*/\n".replaceAll("\n", LINE_DELIMITER); + + private EclipseCssFormatterStepImpl formatter; + + @Before + public void initialize() throws Exception { + /* + * The instantiation can be repeated for each step, but only with the same configuration + * All formatter configuration is stored in + * org.eclipse.core.runtime/.settings/org.eclipse.wst.css.core.prefs. + * So a simple test of one configuration item change is considered sufficient. + */ + Properties properties = new Properties(); + properties.put(INDENTATION_SIZE, "3"); //Default is 1 + properties.put(INDENTATION_CHAR, SPACE); //Default is TAB + properties.put(CLEANUP_CASE_SELECTOR, Integer.toString(UPPER)); //Done by cleanup + formatter = new EclipseCssFormatterStepImpl(properties); + } + + @Test + public void format() throws Exception { + String output = formatter.format(UNFORMATTED); + assertEquals("Unexpected formatting with default preferences.", + FORMATTED, output); + } + + @Test + public void illegalCharacter() throws Exception { + String output = formatter.format(ILLEGAL_CHAR + UNFORMATTED); + assertThat(output).as("Illeagl characters are not handled on best effort basis.").contains("BODY {"); + } + + @Test + public void illegalSyntax() throws Exception { + String output = formatter.format("{" + UNFORMATTED); + assertEquals("Illeagl syntax is not handled on best effort basis.", + "{" + LINE_DELIMITER + FORMATTED, output); + } + + @Test + public void formatComment() throws Exception { + String output = formatter.format(PRE_CODE_UNFORMATTED + UNFORMATTED); + assertEquals("Unexpected formatting of cpomments.", + PRE_CODE_UNFORMATTED + FORMATTED, output); + } + +} diff --git a/_ext/eclipse-wtp/src/test/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseHtmlFormatterStepImplTest.java b/_ext/eclipse-wtp/src/test/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseHtmlFormatterStepImplTest.java new file mode 100644 index 0000000000..e7c02af172 --- /dev/null +++ b/_ext/eclipse-wtp/src/test/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseHtmlFormatterStepImplTest.java @@ -0,0 +1,144 @@ +/* + * 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.eclipse.wtp; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.eclipse.wst.html.core.internal.preferences.HTMLCorePreferenceNames.*; +import static org.eclipse.wst.jsdt.core.formatter.DefaultCodeFormatterConstants.*; +import static org.junit.Assert.*; + +import java.util.Arrays; +import java.util.Properties; +import java.util.function.Consumer; + +import org.eclipse.wst.html.core.internal.preferences.HTMLCorePreferenceNames; +import org.eclipse.wst.jsdt.core.JavaScriptCore; +import org.junit.BeforeClass; +import org.junit.Test; + +//@RunWith(QuarantiningRunner.class) +//@Quarantine({"org.eclipse", "org.osgi", "com.diffplug"}) +public class EclipseHtmlFormatterStepImplTest { + + private static TestData TEST_DATA = null; + + @BeforeClass + public static void initializeStatic() throws Exception { + TEST_DATA = TestData.getTestDataOnFileSystem("html"); + } + + @Test + public void formatHtml4() throws Exception { + String output = format(TEST_DATA.input("html4.html"), config -> {}); + assertEquals("Unexpected HTML4 formatting.", + TEST_DATA.expected("html4.html"), output); + } + + @Test + public void formatHtml5() throws Exception { + String output = format(TEST_DATA.input("html5.html"), config -> {}); + assertEquals("Unexpected HTML5 formatting.", + TEST_DATA.expected("html5.html"), output); + } + + @Test + public void changeHtmlConfiguration() throws Exception { + String output = format(TEST_DATA.input("html5.html"), config -> {}); + assertEquals("Unexpected HTML5 formatting with default configuration.", + TEST_DATA.expected("html5.html"), output); + output = format(TEST_DATA.input("html5.html"), config -> { + config.put(CLEANUP_TAG_NAME_CASE, Integer.toString(HTMLCorePreferenceNames.UPPER)); + }); + assertEquals("Unexpected HTML5 formatting with custom configuration.", + TEST_DATA.expected("html5_upper.html"), output); + output = format(TEST_DATA.input("html5.html"), config -> {}); + assertEquals("Unexpected HTML5 formatting after reset of default configuration.", + TEST_DATA.expected("html5.html"), output); + } + + @Test + public void invalidConfiguration() throws Throwable { + String output = format(TEST_DATA.input("html5.html"), config -> { + config.put(TAG_NAME_CASE, "Not an integer"); + }); + assertEquals("Unexpected HTML5 formatting with invlaid configuration.", + TEST_DATA.expected("html5.html"), output); + } + + @Test + public void invalidSyntax() throws Exception { + String output = format(TEST_DATA.input("invalid_syntax.html"), config -> {}); + assertEquals("Unexpected HTML formatting in case syntax is not valid.", + TEST_DATA.expected("invalid_syntax.html"), output); + } + + @Test + public void formatJavaScript() throws Exception { + String output = format(TEST_DATA.input("javascript.html"), config -> {}); + assertEquals("Unexpected JS formatting.", + TEST_DATA.expected("javascript.html"), output); + } + + @Test + public void changeJsConfiguration() throws Exception { + String output = format(TEST_DATA.input("javascript.html"), config -> {}); + assertEquals("Unexpected JS formatting with default configuration.", + TEST_DATA.expected("javascript.html"), output); + output = format(TEST_DATA.input("javascript.html"), config -> { + config.put(FORMATTER_INSERT_SPACE_BEFORE_SEMICOLON, JavaScriptCore.INSERT); + }); + assertEquals("Unexpected JS formatting with custom configuration.", + TEST_DATA.expected("javascript_semicolon.html"), output); + output = format(TEST_DATA.input("javascript.html"), config -> {}); + assertEquals("Unexpected HTML5 formatting after reset of default configuration.", + TEST_DATA.expected("javascript.html"), output); + } + + @Test + public void formatCSS() throws Exception { + String output = format(TEST_DATA.input("css.html"), config -> { + + }); + assertEquals("Unexpected CSS formatting.", + TEST_DATA.expected("css.html"), output); + } + + @Test + public void changeCssConfiguration() throws Exception { + format(TEST_DATA.input("css.html"), config -> {}); + boolean exceptionCaught = false; + try { + format(TEST_DATA.input("css.html"), config -> { + config.put(QUOTE_ATTR_VALUES, "TRUE"); + }); + } catch (IllegalArgumentException e) { + exceptionCaught = true; + assertThat(e.getMessage()).as("Exception has no hint about multiple configurations.").contains(Arrays.asList("multiple", "configurations")); + } + assertThat(exceptionCaught).as("No IllegalArgumentException thrown for reconfiguration of CSS formatter.").isTrue(); + } + + private static String format(final String[] input, final Consumer config) throws Exception { + return format(input[0], config); + } + + private static String format(final String input, final Consumer config) throws Exception { + Properties properties = new Properties(); + config.accept(properties); + EclipseHtmlFormatterStepImpl formatter = new EclipseHtmlFormatterStepImpl(properties); + return formatter.format(input); + } +} diff --git a/_ext/eclipse-wtp/src/test/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseJsFormatterStepImplTest.java b/_ext/eclipse-wtp/src/test/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseJsFormatterStepImplTest.java new file mode 100644 index 0000000000..344b4c707e --- /dev/null +++ b/_ext/eclipse-wtp/src/test/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseJsFormatterStepImplTest.java @@ -0,0 +1,101 @@ +/* + * 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.eclipse.wtp; + +import static com.diffplug.spotless.extra.eclipse.base.SpotlessEclipseFramework.LINE_DELIMITER; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.*; + +import java.util.Arrays; +import java.util.Properties; +import java.util.function.Consumer; + +import org.eclipse.wst.jsdt.core.JavaScriptCore; +import org.eclipse.wst.jsdt.core.formatter.DefaultCodeFormatterConstants; +import org.junit.Test; + +public class EclipseJsFormatterStepImplTest { + private final static String ILLEGAL_CHAR = Character.toString((char) 254); + private final static String UNFORMATTED = "var TEST = TEST || {};\n" + + "TEST.say = function() {\n" + + " console.log(\"Hello world!\"); }\n".replaceAll("\n", LINE_DELIMITER); + private final static String FORMATTED = "var TEST = TEST || {};\n" + + "TEST.say = function () {\n" + + " console.log(\"Hello world!\");\n}\n".replaceAll("\n", LINE_DELIMITER); + // Single line comment remains untouched + private final static String SINGLE_LINE_COMMENT = "// One line \"hello world\"\n".replaceAll("\n", LINE_DELIMITER); + // JavaDoc comment get indentation. Within PPRE code, HTML entities are escaped. + private final static String PRE_CODE_UNFORMATTED = "/**
\"Hello\"
*/\n".replaceAll("\n", LINE_DELIMITER); + private final static String PRE_CODE_FORMATTED = "/**\n *
\n * "Hello"\n * 
\n */\n".replaceAll("\n", LINE_DELIMITER); + + @Test + public void defaultFormat() throws Exception { + String output = format(UNFORMATTED, config -> {}); + assertEquals("Unexpected formatting with default preferences.", + FORMATTED, output); + } + + @Test + public void invalidSyntax() throws Exception { + boolean exceptionCaught = false; + try { + format(UNFORMATTED.replace("()", ""), config -> {}); + } catch (IllegalArgumentException e) { + exceptionCaught = true; + assertThat(e.getMessage()).as("Exception has no hint about invalid syntax.").contains(Arrays.asList("Invalid", "syntax")); + } + assertThat(exceptionCaught).as("No IllegalArgumentException thrown for invalid syntax.").isTrue(); + } + + @Test + public void illegalCharacter() throws Exception { + String output = format(UNFORMATTED.replace("function", "function" + ILLEGAL_CHAR), config -> {}); + assertThat(output).as("Illegal ASCII charactes are not treated on best effort basis.").contains("function" + ILLEGAL_CHAR); + } + + @Test + public void validConfiguration() throws Exception { + String output = format(UNFORMATTED, config -> { + config.setProperty(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaScriptCore.TAB); + }); + assertEquals("User configuration ignored by formatter.", + FORMATTED.replace(" ", "\t"), output); + } + + @Test + public void invalidConfiguration() throws Exception { + String output = format(UNFORMATTED, config -> { + config.setProperty(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, "-1"); + }); + assertEquals("Invalid user configuration not treated on best effort basis.", + FORMATTED.replace(" ", ""), output); + } + + @Test + public void formatComments() throws Exception { + String output = format(SINGLE_LINE_COMMENT + PRE_CODE_UNFORMATTED + UNFORMATTED, config -> {}); + assertEquals("Invalid user configuration not treated on best effort basis.", + SINGLE_LINE_COMMENT + PRE_CODE_FORMATTED + FORMATTED, output); + } + + private static String format(final String input, final Consumer config) throws Exception { + Properties properties = new Properties(); + config.accept(properties); + EclipseJsFormatterStepImpl formatter = new EclipseJsFormatterStepImpl(properties); + return formatter.format(input); + } + +} diff --git a/_ext/eclipse-wtp/src/test/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseJsonFormatterStepImplTest.java b/_ext/eclipse-wtp/src/test/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseJsonFormatterStepImplTest.java new file mode 100644 index 0000000000..35e06ec2f4 --- /dev/null +++ b/_ext/eclipse-wtp/src/test/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseJsonFormatterStepImplTest.java @@ -0,0 +1,69 @@ +/* + * 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.eclipse.wtp; + +import static com.diffplug.spotless.extra.eclipse.base.SpotlessEclipseFramework.LINE_DELIMITER; +import static org.eclipse.wst.json.core.preferences.JSONCorePreferenceNames.*; +import static org.junit.Assert.*; + +import java.util.Properties; + +import org.junit.Before; +import org.junit.Test; + +public class EclipseJsonFormatterStepImplTest { + private final static String ILLEGAL_CHAR = Character.toString((char) 254); + private final static String UNFORMATTED = "{\n \"x\": { \"a\" : \"v\",\"properties\" : \"v\" }}".replaceAll("\n", LINE_DELIMITER); + private final static String FORMATTED = "{\n \"x\": {\n \"a\": \"v\",\n \"properties\": \"v\"\n }\n}".replaceAll("\n", LINE_DELIMITER); + + private static EclipseJsonFormatterStepImpl formatter; + + @Before + public void initialize() throws Exception { + /* + * The instantiation can be repeated for each step, but only with the same configuration + * All formatter configuration is stored in + * org.eclipse.core.runtime/.settings/org.eclipse.wst.json.core.prefs. + * So a simple test of one configuration item change is considered sufficient. + */ + Properties properties = new Properties(); + properties.put(INDENTATION_SIZE, "3"); //Default is 1 + properties.put(INDENTATION_CHAR, SPACE); //Default is TAB + properties.put(CASE_PROPERTY_NAME, Integer.toString(UPPER)); //Dead code, ignored + formatter = new EclipseJsonFormatterStepImpl(properties); + } + + @Test + public void format() throws Exception { + String output = formatter.format(UNFORMATTED); + assertEquals("Unexpected formatting with default preferences.", + FORMATTED, output); + } + + @Test + public void illegalCharacter() throws Exception { + String output = formatter.format(ILLEGAL_CHAR + UNFORMATTED); + assertEquals("Illeagl characteds are not ignored.", + ILLEGAL_CHAR + FORMATTED, output); + } + + @Test + public void illegalSyntax() throws Exception { + String output = formatter.format("{" + UNFORMATTED); + assertEquals("Illeagl syntax is not handled on best effort basis.", + FORMATTED, output); + } +} diff --git a/_ext/eclipse-wtp/src/test/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseXmlFormatterStepImplTest.java b/_ext/eclipse-wtp/src/test/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseXmlFormatterStepImplTest.java new file mode 100644 index 0000000000..ac159e72f5 --- /dev/null +++ b/_ext/eclipse-wtp/src/test/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseXmlFormatterStepImplTest.java @@ -0,0 +1,139 @@ +/* + * 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.eclipse.wtp; + +import static com.diffplug.spotless.extra.eclipse.base.SpotlessEclipseFramework.LINE_DELIMITER; +import static org.eclipse.wst.xml.core.internal.preferences.XMLCorePreferenceNames.*; +import static org.junit.Assert.*; + +import java.util.Properties; +import java.util.function.Consumer; + +import org.junit.BeforeClass; +import org.junit.Test; + +import com.diffplug.spotless.extra.eclipse.wtp.sse.SpotlessPreferences; + +/** Eclipse WST wrapper integration tests */ +public class EclipseXmlFormatterStepImplTest { + + private final static String INCOMPLETE = ""; + private final static String ILLEGAL_CHAR = "\0"; + private static TestData TEST_DATA = null; + + @BeforeClass + public static void initializeStatic() throws Exception { + TEST_DATA = TestData.getTestDataOnFileSystem("xml"); + } + + @Test + public void simpleDefaultFormat() throws Throwable { + String output = format(TEST_DATA.input("xml_space.xml"), config -> {}); + assertEquals("Unexpected formatting with default preferences.", + TEST_DATA.expected("xml_space.xml"), output); + } + + @Test + public void invalidXmlFormat() throws Throwable { + String[] input = TEST_DATA.input("xml_space.xml"); + input[0] += INCOMPLETE; + String output = format(input, config -> {}); + String expected = TEST_DATA.expected("xml_space.xml") + LINE_DELIMITER + INCOMPLETE; + assertEquals("Incomplete XML not formatted on best effort basis.", + expected, output); + } + + @Test + public void illegalXmlCharater() throws Throwable { + String[] input = TEST_DATA.input("xml_space.xml"); + input[0] = ILLEGAL_CHAR + input[0]; + String output = format(input, config -> {}); + String expected = LINE_DELIMITER + LINE_DELIMITER + TEST_DATA.expected("xml_space.xml"); + assertEquals("Illegal character not replaced by line delimiter.", expected, output); + } + + @Test + public void multipleConfigurations() throws Throwable { + String output = format(TEST_DATA.input("xml_space.xml"), config -> { + config.setProperty(INDENTATION_SIZE, "2"); + config.setProperty(INDENTATION_CHAR, SPACE); + }); + String expected = TEST_DATA.expected("xml_space.xml").replace("\t", " "); + assertEquals("Custom indentation configuration not applied.", expected, output); + + output = format(TEST_DATA.input("xml_space.xml"), config -> { + config.setProperty(SPLIT_MULTI_ATTRS, Boolean.toString(true)); + }); + expected = TEST_DATA.expected("xml_space.xml"); + expected = expected.replace(" a=", LINE_DELIMITER + "\ta="); + expected = expected.replace(" b=", LINE_DELIMITER + "\tb="); + assertEquals("Custom indentation configuration not reverted or custom multiple argument configuration not applied.", expected, output); + } + + @Test + public void invalidConfiguration() throws Throwable { + String output = format(TEST_DATA.input("xml_space.xml"), config -> { + config.setProperty(INDENTATION_SIZE, "Not an integer"); + config.setProperty(INDENTATION_CHAR, SPACE); + }); + assertEquals("Invalid indentation configuration not replaced by default value (0 spaces)", + TEST_DATA.expected("xml_space.xml").replace("\t", ""), output); + } + + @Test + public void dtdRelativePath() throws Throwable { + String output = format(TEST_DATA.input("dtd_relative.xml"), config -> {}); + assertEquals("Relative DTD not resolved. Restrictions are not applied by formatter.", + TEST_DATA.expected("dtd_relative.xml"), output); + } + + @Test + public void xsdRelativePath() throws Throwable { + String output = format(TEST_DATA.input("xsd_relative.xml"), config -> {}); + assertEquals("Relative XSD not resolved. Restrictions are not applied by formatter.", + TEST_DATA.expected("xsd_relative.xml"), output); + } + + @Test + public void xsdNotFound() throws Throwable { + String output = format(TEST_DATA.input("xsd_not_found.xml"), config -> {}); + assertEquals("Unresolved XSD/DTD not silently ignored.", + TEST_DATA.expected("xsd_not_found.xml"), output); + } + + @Test + public void catalogLookup() throws Throwable { + String output = format(TEST_DATA.input("xsd_not_found.xml"), config -> { + config.setProperty( + SpotlessPreferences.USER_CATALOG, + TEST_DATA.getRestrictionsPath("catalog.xml").toString()); + }); + assertEquals("XSD not resolved by catalog. Restrictions are not applied by formatter.", + TEST_DATA.expected("xsd_not_found.xml").replace(" remove spaces ", "remove spaces"), output); + } + + private static String format(final String[] input, final Consumer config) throws Exception { + return format(input[0], input[1], config); + } + + private static String format(final String input, final String location, final Consumer config) throws Exception { + Properties properties = new Properties(); + config.accept(properties); + EclipseXmlFormatterStepImpl formatter = new EclipseXmlFormatterStepImpl(properties); + return formatter.format(input, location); + } + +} diff --git a/_ext/eclipse-wtp/src/test/java/com/diffplug/spotless/extra/eclipse/wtp/TestData.java b/_ext/eclipse-wtp/src/test/java/com/diffplug/spotless/extra/eclipse/wtp/TestData.java new file mode 100644 index 0000000000..d767337970 --- /dev/null +++ b/_ext/eclipse-wtp/src/test/java/com/diffplug/spotless/extra/eclipse/wtp/TestData.java @@ -0,0 +1,77 @@ +/* + * 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.eclipse.wtp; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class TestData { + public static TestData getTestDataOnFileSystem(String kind) { + final String userDir = System.getProperty("user.dir", "."); + Path dataPath = Paths.get(userDir, "src", "test", "resources", kind); + if (Files.isDirectory(dataPath)) { + return new TestData(dataPath); + } + return null; + } + + private final Path inputPath; + private final Path expectedPath; + private final Path restrictionsPath; + + private TestData(Path dataPath) { + inputPath = dataPath.resolve("input").toAbsolutePath(); + expectedPath = dataPath.resolve("expected").toAbsolutePath(); + restrictionsPath = dataPath.resolve("restrictions").toAbsolutePath(); + for (Path testDataDir : new Path[]{inputPath, expectedPath, restrictionsPath}) { + if (!Files.isDirectory(testDataDir)) { + throw new IllegalArgumentException(String.format("'%1$s' is not a directory.", testDataDir)); + } + } + } + + public String[] input(final String fileName) throws Exception { + Path xmlPath = inputPath.resolve(fileName); + return new String[]{read(xmlPath), xmlPath.toString()}; + } + + public String expected(final String fileName) { + Path xmlPath = expectedPath.resolve(fileName); + return read(xmlPath); + } + + private String read(final Path xmlPath) { + if (!Files.isRegularFile(xmlPath)) { + throw new IllegalArgumentException(String.format("'%1$s' is not a regular file.", xmlPath)); + } + try { + String checkedOutFileContent = new String(java.nio.file.Files.readAllBytes(xmlPath), "UTF8"); + return checkedOutFileContent.replace("\r", ""); //Align GIT end-of-line normalization + } catch (IOException e) { + throw new IllegalArgumentException(String.format("Failed to read '%1$s'.", xmlPath), e); + } + } + + public Path getRestrictionsPath(String fileName) { + Path filePath = restrictionsPath.resolve(fileName); + if (!Files.exists(filePath)) { + throw new IllegalArgumentException(String.format("'%1$s' is not a restrictions file.", fileName)); + } + return filePath; + } +} diff --git a/_ext/eclipse-wtp/src/test/resources/html/expected/css.html b/_ext/eclipse-wtp/src/test/resources/html/expected/css.html new file mode 100644 index 0000000000..90e85f617b --- /dev/null +++ b/_ext/eclipse-wtp/src/test/resources/html/expected/css.html @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/_ext/eclipse-wtp/src/test/resources/html/expected/html4.html b/_ext/eclipse-wtp/src/test/resources/html/expected/html4.html new file mode 100644 index 0000000000..32dfd43590 --- /dev/null +++ b/_ext/eclipse-wtp/src/test/resources/html/expected/html4.html @@ -0,0 +1,10 @@ + + + + Before +
After + + + + \ No newline at end of file diff --git a/_ext/eclipse-wtp/src/test/resources/html/expected/html5.html b/_ext/eclipse-wtp/src/test/resources/html/expected/html5.html new file mode 100644 index 0000000000..bb63831f09 --- /dev/null +++ b/_ext/eclipse-wtp/src/test/resources/html/expected/html5.html @@ -0,0 +1,11 @@ + + + + Before +
After + + + + \ No newline at end of file diff --git a/_ext/eclipse-wtp/src/test/resources/html/expected/html5_upper.html b/_ext/eclipse-wtp/src/test/resources/html/expected/html5_upper.html new file mode 100644 index 0000000000..c79dfdce62 --- /dev/null +++ b/_ext/eclipse-wtp/src/test/resources/html/expected/html5_upper.html @@ -0,0 +1,11 @@ + + + + Before +
After + + + + \ No newline at end of file diff --git a/_ext/eclipse-wtp/src/test/resources/html/expected/invalid_syntax.html b/_ext/eclipse-wtp/src/test/resources/html/expected/invalid_syntax.html new file mode 100644 index 0000000000..004ca3cc0e --- /dev/null +++ b/_ext/eclipse-wtp/src/test/resources/html/expected/invalid_syntax.html @@ -0,0 +1,7 @@ + + + + +T<whatsoever></whatsoever> + + \ No newline at end of file diff --git a/_ext/eclipse-wtp/src/test/resources/html/expected/javascript.html b/_ext/eclipse-wtp/src/test/resources/html/expected/javascript.html new file mode 100644 index 0000000000..e774fbed4a --- /dev/null +++ b/_ext/eclipse-wtp/src/test/resources/html/expected/javascript.html @@ -0,0 +1,41 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/_ext/eclipse-wtp/src/test/resources/html/expected/javascript_semicolon.html b/_ext/eclipse-wtp/src/test/resources/html/expected/javascript_semicolon.html new file mode 100644 index 0000000000..8468e71d64 --- /dev/null +++ b/_ext/eclipse-wtp/src/test/resources/html/expected/javascript_semicolon.html @@ -0,0 +1,41 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/_ext/eclipse-wtp/src/test/resources/html/input/css.html b/_ext/eclipse-wtp/src/test/resources/html/input/css.html new file mode 100644 index 0000000000..8ccbc4dcc8 --- /dev/null +++ b/_ext/eclipse-wtp/src/test/resources/html/input/css.html @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/_ext/eclipse-wtp/src/test/resources/html/input/html4.html b/_ext/eclipse-wtp/src/test/resources/html/input/html4.html new file mode 100644 index 0000000000..45aa7b56f7 --- /dev/null +++ b/_ext/eclipse-wtp/src/test/resources/html/input/html4.html @@ -0,0 +1,10 @@ + + + +Before +
+After + + + + \ No newline at end of file diff --git a/_ext/eclipse-wtp/src/test/resources/html/input/html5.html b/_ext/eclipse-wtp/src/test/resources/html/input/html5.html new file mode 100644 index 0000000000..216c53e8e3 --- /dev/null +++ b/_ext/eclipse-wtp/src/test/resources/html/input/html5.html @@ -0,0 +1,10 @@ + + + +Before +
+After + + + + \ No newline at end of file diff --git a/_ext/eclipse-wtp/src/test/resources/html/input/invalid_syntax.html b/_ext/eclipse-wtp/src/test/resources/html/input/invalid_syntax.html new file mode 100644 index 0000000000..04c6cf7f2a --- /dev/null +++ b/_ext/eclipse-wtp/src/test/resources/html/input/invalid_syntax.html @@ -0,0 +1,4 @@ + + + +T</whatsoever> \ No newline at end of file diff --git a/_ext/eclipse-wtp/src/test/resources/html/input/javascript.html b/_ext/eclipse-wtp/src/test/resources/html/input/javascript.html new file mode 100644 index 0000000000..dccde2c2dd --- /dev/null +++ b/_ext/eclipse-wtp/src/test/resources/html/input/javascript.html @@ -0,0 +1,34 @@ +<!DOCTYPE html> +<html> +<head> +<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> +<script type="text/javascript">console.log("One line")</script> +<script type="text/javascript"> +console.log("Line break, no indent"); +</script> +<script type="text/javascript"> + console.log("Line break, wrong indent") +</script> +</head> +<body> +<nav> +<script type="text/javascript">console.log("One line, nested")</script> +<script type="text/javascript"> +console.log("Line break, no indent, nested") +</script> +<script type="text/javascript"> + console.log("Line break, wrong indent, nested"); +</script> + <script type="text/javascript"> +console.log("Wrong tag indents") + </script> +<script type="text/javascript"> +console.log("Empty lines when closing."); + + </script> +<script>console.log("Script without type")</script> +<script type="text/javascript"></script> +<script type="text/javascript"> if (condition) { console.log("Missing end of JS block")</script> +</nav> +</body> +</html> \ No newline at end of file diff --git a/_ext/eclipse-wtp/src/test/resources/html/restrictions/ReadMe.txt b/_ext/eclipse-wtp/src/test/resources/html/restrictions/ReadMe.txt new file mode 100644 index 0000000000..37c58641d8 --- /dev/null +++ b/_ext/eclipse-wtp/src/test/resources/html/restrictions/ReadMe.txt @@ -0,0 +1,9 @@ +Various manual tests have been performed with external and internal DTDs. +They seem not to be used by the HTML formatter. Instead they are a +helper to parse the DOCTYPE and select the right content model. +The DTDParser.parse throws a SAXParseException when parsing the +HTML DTDs provided with the WST JARs (accessed by the System catalog). +This nominal and expected exception is eaten (DTDParser.currentDTD not set). + +Since currently there seems to be no use case for DTD/XSD/catalog, +we omit it for Spotless integration. \ No newline at end of file diff --git a/_ext/eclipse-wtp/src/test/resources/xml/expected/dtd_relative.xml b/_ext/eclipse-wtp/src/test/resources/xml/expected/dtd_relative.xml new file mode 100644 index 0000000000..9e6403e8bd --- /dev/null +++ b/_ext/eclipse-wtp/src/test/resources/xml/expected/dtd_relative.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE r SYSTEM "../restrictions/test.dtd"> +<r> + <x>indent + this text</x> + <y>preserve + spaces</y> +</r> \ No newline at end of file diff --git a/_ext/eclipse-wtp/src/test/resources/xml/expected/xml_space.xml b/_ext/eclipse-wtp/src/test/resources/xml/expected/xml_space.xml new file mode 100644 index 0000000000..2d8b374851 --- /dev/null +++ b/_ext/eclipse-wtp/src/test/resources/xml/expected/xml_space.xml @@ -0,0 +1,4 @@ +<r a="1" b="2"> + <x> foo bar </x> + <y xml:space="preserve"> preserve space </y> +</r> \ No newline at end of file diff --git a/_ext/eclipse-wtp/src/test/resources/xml/expected/xsd_not_found.xml b/_ext/eclipse-wtp/src/test/resources/xml/expected/xsd_not_found.xml new file mode 100644 index 0000000000..9513be0005 --- /dev/null +++ b/_ext/eclipse-wtp/src/test/resources/xml/expected/xsd_not_found.xml @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8"?> +<t:r xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:t="http://foo.bar/test" + xsi:schemaLocation="http://foo.bar/test http://foo.bar/test.xsd"> + <t:x> remove spaces </t:x> + <t:y> preserve spaces </t:y> +</t:r> \ No newline at end of file diff --git a/_ext/eclipse-wtp/src/test/resources/xml/expected/xsd_relative.xml b/_ext/eclipse-wtp/src/test/resources/xml/expected/xsd_relative.xml new file mode 100644 index 0000000000..2477f56594 --- /dev/null +++ b/_ext/eclipse-wtp/src/test/resources/xml/expected/xsd_relative.xml @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8"?> +<t:r xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:t="http://foo.bar/test" + xsi:schemaLocation="http://foo.bar/test ../restrictions/test.xsd"> + <t:x>remove spaces</t:x> + <t:y> preserve spaces </t:y> +</t:r> \ No newline at end of file diff --git a/_ext/eclipse-wtp/src/test/resources/xml/input/dtd_relative.xml b/_ext/eclipse-wtp/src/test/resources/xml/input/dtd_relative.xml new file mode 100644 index 0000000000..431354868c --- /dev/null +++ b/_ext/eclipse-wtp/src/test/resources/xml/input/dtd_relative.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE r SYSTEM "../restrictions/test.dtd"> +<r> + <x>indent + this text</x> + <y>preserve + spaces</y> +</r> \ No newline at end of file diff --git a/_ext/eclipse-wtp/src/test/resources/xml/input/xml_space.xml b/_ext/eclipse-wtp/src/test/resources/xml/input/xml_space.xml new file mode 100644 index 0000000000..2ddd865f1c --- /dev/null +++ b/_ext/eclipse-wtp/src/test/resources/xml/input/xml_space.xml @@ -0,0 +1 @@ +<r a="1" b="2"><x> foo bar </x><y xml:space="preserve"> preserve space </y></r> \ No newline at end of file diff --git a/_ext/eclipse-wtp/src/test/resources/xml/input/xsd_not_found.xml b/_ext/eclipse-wtp/src/test/resources/xml/input/xsd_not_found.xml new file mode 100644 index 0000000000..483be84785 --- /dev/null +++ b/_ext/eclipse-wtp/src/test/resources/xml/input/xsd_not_found.xml @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="UTF-8"?> +<t:r xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:t="http://foo.bar/test" xsi:schemaLocation="http://foo.bar/test http://foo.bar/test.xsd"> +<t:x> remove spaces </t:x><t:y> preserve spaces </t:y> +</t:r> \ No newline at end of file diff --git a/_ext/eclipse-wtp/src/test/resources/xml/input/xsd_relative.xml b/_ext/eclipse-wtp/src/test/resources/xml/input/xsd_relative.xml new file mode 100644 index 0000000000..ce8392adf8 --- /dev/null +++ b/_ext/eclipse-wtp/src/test/resources/xml/input/xsd_relative.xml @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="UTF-8"?> +<t:r xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:t="http://foo.bar/test" xsi:schemaLocation="http://foo.bar/test ../restrictions/test.xsd"> +<t:x> remove spaces </t:x><t:y> preserve spaces </t:y> +</t:r> \ No newline at end of file diff --git a/_ext/eclipse-wtp/src/test/resources/xml/restrictions/catalog.xml b/_ext/eclipse-wtp/src/test/resources/xml/restrictions/catalog.xml new file mode 100644 index 0000000000..4ebd39144a --- /dev/null +++ b/_ext/eclipse-wtp/src/test/resources/xml/restrictions/catalog.xml @@ -0,0 +1,5 @@ +<?xml version="1.0" encoding="UTF-8"?> +<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog"> + <!-- OASIS: If the catalog entry file is specified with a relative URI, it is relative to the base URI of the document that contains the processing instruction. --> + <system systemId="http://foo.bar/test.xsd" uri="./test.xsd"/> +</catalog> \ No newline at end of file diff --git a/_ext/eclipse-wtp/src/test/resources/xml/restrictions/test.dtd b/_ext/eclipse-wtp/src/test/resources/xml/restrictions/test.dtd new file mode 100644 index 0000000000..5076fd91e0 --- /dev/null +++ b/_ext/eclipse-wtp/src/test/resources/xml/restrictions/test.dtd @@ -0,0 +1,4 @@ +<!ELEMENT r (x,y)> +<!ELEMENT x (#PCDATA)> +<!ELEMENT y ANY> +<!ATTLIST y xml:space (preserve) #FIXED 'preserve'> diff --git a/_ext/eclipse-wtp/src/test/resources/xml/restrictions/test.xsd b/_ext/eclipse-wtp/src/test/resources/xml/restrictions/test.xsd new file mode 100644 index 0000000000..30a77f23e8 --- /dev/null +++ b/_ext/eclipse-wtp/src/test/resources/xml/restrictions/test.xsd @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="UTF-8"?> +<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http:/foo.bar/test" elementFormDefault="qualified"> + <xs:element name="r"> + <xs:complexType> + <xs:sequence> + <xs:element name="x"> + <xs:simpleType> + <xs:restriction base="xs:string"> + <xs:whiteSpace value="collapse"/> + </xs:restriction> + </xs:simpleType> + </xs:element> + <xs:element name="y"> + <xs:simpleType> + <xs:restriction base="xs:string"> + <xs:whiteSpace value="preserve"/> + </xs:restriction> + </xs:simpleType> + </xs:element> + </xs:sequence> + </xs:complexType> + </xs:element> +</xs:schema> \ No newline at end of file diff --git a/_ext/gradle/p2-fat-jar-setup.gradle b/_ext/gradle/p2-fat-jar-setup.gradle new file mode 100644 index 0000000000..40d303f93f --- /dev/null +++ b/_ext/gradle/p2-fat-jar-setup.gradle @@ -0,0 +1,178 @@ +apply from: rootProject.file('../gradle/java-setup.gradle') + +buildscript { + repositories { + maven { + url "https://plugins.gradle.org/m2/" + } + } + dependencies { + classpath "com.diffplug.gradle:goomph:3.15.0" + } +} +apply plugin: com.diffplug.gradle.p2.AsMavenPlugin + +ext { + // P2 Repository URL + if (!project.hasProperty('p2Repository')) { + p2Repository = "p2Repository not defined in project" + } + // P2 dependencies + if (!project.hasProperty('p2Dependencies')) { + p2Dependencies = "p2Dependencies not defined in project" + } + + // Some JARs may include JARs themselfs, which shall be unpacked and added to the embedded class folder. + if (!project.hasProperty('internalJars')) { + internalJars = [] + } + + // Include form the JARs, which goes into a fat-jar with the spottless formatter interface. + if (!project.hasProperty('jarInclude')) { + jarInclude = [ + '**/*.class', // Take all classes + '**/*.properties', // Text resources (for messages, etc) + '**/*.xml', // Plugin XML and other resources + '*.html', // License information about the included JARs, + 'META-INF/**' // Plugin manifest and addtional information + ] + } + + // Exclude form the JARs, which goes into a fat-jar with the spottless formatter interface. + if (!project.hasProperty('jarExclude')) { + jarExclude = [ + 'META-INF/*.RSA', // The eclipse jars are signed, and our fat-jar breaks the signatures + 'META-INF/*.SF', // ... so all signatures are filtered + ] + } + + // Exclude form the JARs, which goes into a fat-jar with the spottless formatter interface. + if (!project.hasProperty('jarExclude')) { + jarExclude = [ + 'META-INF/*.RSA', // The eclipse jars are signed, and our fat-jar breaks the signatures + 'META-INF/*.SF', // ... so all signatures are filtered + ] + } + + // Map fat-JAR resources path if JAR file name does not correspond to plugin package name (e.g. required for internal plugins) + if (!project.hasProperty('fatJarResourcesMap')) { + fatJarResourcesMap = [:] + } + + + // The directory contains all external classes for the fat-jar + embeddedClassesDirName = 'build/embeddedClasses' + embeddedClassesDir = project.file(embeddedClassesDirName) +} + +// build a maven repo in our build folder containing these artifacts +p2AsMaven { + group 'p2', { + repo project.p2Repository + p2Dependencies.keySet.each { p2.addIU(it) } + p2ant { + if (!project.hasProperty('p2AntProxy')) { + setP2AntProxy(it) + } + } + } +} + +configurations { + embeddedJars // P2 JARs the fat-JAR is based uppon +} + +dependencies { + p2Dependencies.each { groupArtifact, version -> + embeddedJars "p2:${groupArtifact}:${version}" + } + // Includes the classes from P2 JARs during compilation + compile files(embeddedClassesDir) +} + +jar { + // Add P2 clases to fat-JAR + from embeddedClassesDir +} + +////////////////////////// +// Unpack External Deps // +////////////////////////// +import java.io.File +import org.apache.commons.io.filefilter.DirectoryFileFilter + +task unjarEmbeddedClasses { + description = "Copies filtered set of embedded classes from the Eclise/GrEclipse dependencies to '${project.relativePath(embeddedClassesDir)}'." + inputs.files(configurations.embeddedJars) + inputs.property('internalJars', internalJars) + inputs.property('jarInclude', jarInclude) + inputs.property('jarExclude', jarExclude) + inputs.property('fatJarResourcesMap', fatJarResourcesMap) + outputs.file(embeddedClassesDir) + + doLast { + embeddedClassesDir.deleteDir() + embeddedClassesDir.mkdirs() + configurations.embeddedJars.each { + unjar(it, embeddedClassesDir) + } + // Unpack internal JARs. Maintain the order defined in internalJars + internalJars.each { + fileTree(embeddedClassesDir).include("${it}.jar").each { + unjar(it, embeddedClassesDir) + delete(it) + } + } + } +} + +def unjar(File jarFile, File destDir) { + ant.unjar(src: jarFile, dest: destDir) { + patternset { + jarInclude.each { + include(name: "${it}") + } + internalJars.each { + include(name: "**/${it}.jar") + } + jarExclude.each { + exclude(name: "${it}") + } + } + } + // Provide Fat JAR resources (following naming convention of spotless-eclipse-base) + def fat_jar_resource_dir = jarFile.getName().split('-')[0] + fat_jar_resource_dir = fatJarResourcesMap.getOrDefault(fat_jar_resource_dir, fat_jar_resource_dir) + ant.move(todir: "${destDir}/${fat_jar_resource_dir}/META-INF", quiet: 'true', failonerror: 'false') { + fileset(dir: "${destDir}/META-INF") + } + //Keep licenses and other human readable information for transparency + ant.move(todir: "${destDir}/${fat_jar_resource_dir}", quiet: 'true') { + fileset(dir: destDir) { + include(name: '*') + type(type: 'file') + exclude(name: '*jar-*') + exclude(name: '*.jar') + } + } + +} + +tasks.compileJava.dependsOn(unjarEmbeddedClasses) + +///////// +// IDE // +///////// + +apply plugin: 'eclipse' + +// always create fresh projects +tasks.eclipse.dependsOn(cleanEclipse) +// Encure that the dependent classes are preovided for compilation if project is build via Eclipse instead of command line +tasks.eclipseClasspath.dependsOn(unjarEmbeddedClasses) + +apply plugin: 'idea' + +// Encure that the dependent classes are preovided for compilation if project is build via Eclipse instead of command line +tasks.idea.dependsOn(unjarEmbeddedClasses) +