From ed20be293f7ac54ec6c0316dacb100146c207815 Mon Sep 17 00:00:00 2001 From: Peter Palaga Date: Sun, 3 Feb 2019 22:16:32 +0100 Subject: [PATCH] Fix #8 Define defaults for Java editorconfig properties --- core/pom.xml | 42 ++++++++++++ .../main/resources/java-defaults.editorconfig | 8 +++ jdt/pom.xml | 5 ++ .../ec4j/java/domain/jdt/JdtFormatter.java | 64 +++++++++++++++---- pom.xml | 10 ++- .../build/groovy/generate-test-classes.groovy | 14 ++-- .../resources/basic/defaults/.editorconfig | 4 ++ .../basic/defaults/Good.expected.java | 16 +++++ .../test/resources/basic/defaults/Good.java | 16 +++++ .../basic/defaults/MixedIndent.expected.java | 16 +++++ .../resources/basic/defaults/MixedIndent.java | 16 +++++ 11 files changed, 192 insertions(+), 19 deletions(-) create mode 100644 core/pom.xml create mode 100644 core/src/main/resources/java-defaults.editorconfig create mode 100644 tck/src/test/resources/basic/defaults/.editorconfig create mode 100644 tck/src/test/resources/basic/defaults/Good.expected.java create mode 100644 tck/src/test/resources/basic/defaults/Good.java create mode 100644 tck/src/test/resources/basic/defaults/MixedIndent.expected.java create mode 100644 tck/src/test/resources/basic/defaults/MixedIndent.java diff --git a/core/pom.xml b/core/pom.xml new file mode 100644 index 0000000..4d0ef97 --- /dev/null +++ b/core/pom.xml @@ -0,0 +1,42 @@ + + + 4.0.0 + + + org.ec4j.java-domain + editorconfig-java-domain-parent + 0.0.1-SNAPSHOT + + + editorconfig-java-domain-core + + EditorConfig Java Domain Core + EditorConfig Java Domain Core module + + + + + org.ec4j.core + ec4j-core + + + + + diff --git a/core/src/main/resources/java-defaults.editorconfig b/core/src/main/resources/java-defaults.editorconfig new file mode 100644 index 0000000..f974443 --- /dev/null +++ b/core/src/main/resources/java-defaults.editorconfig @@ -0,0 +1,8 @@ +root = true + +[*.java] +indent_style = space +indent_size = 4 + +# 2147483647 is java.lang.Integer.MAX_VALUE thus actually unlimited +max_line_length = 2147483647 diff --git a/jdt/pom.xml b/jdt/pom.xml index 6563d3e..fcd845d 100644 --- a/jdt/pom.xml +++ b/jdt/pom.xml @@ -37,6 +37,11 @@ ec4j-core + + org.ec4j.java-domain + editorconfig-java-domain-core + + org.ec4j.java-domain editorconfig-java-domain-tck-spi diff --git a/jdt/src/main/java/org/ec4j/java/domain/jdt/JdtFormatter.java b/jdt/src/main/java/org/ec4j/java/domain/jdt/JdtFormatter.java index 974d9fe..3817104 100644 --- a/jdt/src/main/java/org/ec4j/java/domain/jdt/JdtFormatter.java +++ b/jdt/src/main/java/org/ec4j/java/domain/jdt/JdtFormatter.java @@ -27,11 +27,14 @@ import org.ec4j.core.Cache; import org.ec4j.core.Cache.Caches; import org.ec4j.core.EditorConfigLoader; +import org.ec4j.core.PropertyTypeRegistry; import org.ec4j.core.Resource.Resources; import org.ec4j.core.ResourceProperties; import org.ec4j.core.ResourcePropertiesService; +import org.ec4j.core.model.EditorConfig; import org.ec4j.core.model.PropertyType; import org.ec4j.core.model.PropertyType.IndentStyleValue; +import org.ec4j.core.model.Version; import org.ec4j.java.domain.tck.spi.Formatter; import org.eclipse.jdt.core.formatter.CodeFormatter; import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; @@ -49,7 +52,27 @@ * @author Peter Palaga */ public class JdtFormatter implements Formatter { - private static final Integer DEFAULT_JAVA_INDENT_SIZE = Integer.valueOf(4); + + static class WrappedResourceProperties { + private final ResourceProperties properties; + + WrappedResourceProperties(ResourceProperties properties) { + super(); + this.properties = properties; + } + + public T getValue(PropertyType type) { + T result = properties.getValue(type, null, true); + assert result != null : "Value of "+ type.getName() + " property must not be null. Missing a default?"; + return result; + } + + public T getValue(String name) { + T result = properties.getValue(name, null, true); + assert result != null : "Value of "+ name + " property must not be null. Missing a default?"; + return result; + } + } /** * Translated the given EditorConfig {@link ResourceProperties} to a {@link Map} of JDT Formatter's properties. @@ -58,9 +81,9 @@ public class JdtFormatter implements Formatter { * @return a new {@link Map} */ private static Map toJdtFormatterOptions(ResourceProperties properties) { + final WrappedResourceProperties wrappedProperties = new WrappedResourceProperties(properties); Map result = new TreeMap<>(); - final IndentStyleValue indentStyle = properties.getValue(PropertyType.indent_style, IndentStyleValue.space, - false); + final IndentStyleValue indentStyle = wrappedProperties.getValue(PropertyType.indent_style); switch (indentStyle) { case tab: case space: @@ -71,10 +94,10 @@ private static Map toJdtFormatterOptions(ResourceProperties prop String.format("Unexpected %s: [%s]", IndentStyleValue.class.getName(), indentStyle)); } - final Integer indentSize = properties.getValue(PropertyType.indent_size, DEFAULT_JAVA_INDENT_SIZE, false); + final Integer indentSize = wrappedProperties.getValue(PropertyType.indent_size); result.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, indentSize.toString()); - final Integer maxLineLength = properties.getValue(PropertyType.max_line_length, Integer.MAX_VALUE, true); + final Integer maxLineLength = wrappedProperties.getValue(PropertyType.max_line_length); result.put(DefaultCodeFormatterConstants.FORMATTER_LINE_SPLIT, maxLineLength.toString()); return result; @@ -84,18 +107,34 @@ private static Map toJdtFormatterOptions(ResourceProperties prop public JdtFormatter() { final Cache myCache = Caches.permanent(); - EditorConfigLoader myLoader = EditorConfigLoader.default_(); - resourcePropertiesService = ResourcePropertiesService.builder() - .cache(myCache) - .loader(myLoader) + + final PropertyTypeRegistry registry = PropertyTypeRegistry.builder() // + .defaults() // + .type(PropertyType.max_line_length) // .build(); + final EditorConfigLoader myLoader = EditorConfigLoader.of(Version.CURRENT, registry); + try { + final EditorConfig javaDefaults = myLoader.load(Resources.ofClassPath( // + this.getClass().getClassLoader(), // + "/java-defaults.editorconfig", // + StandardCharsets.UTF_8)); + + resourcePropertiesService = ResourcePropertiesService.builder() // + .defaultEditorConfig(javaDefaults) // + .cache(myCache) // + .loader(myLoader) // + .build(); + } catch (IOException e) { + throw new RuntimeException(e); + } } /** {@inheritDoc} */ @Override public String format(Path sourcePath) { try { - final ResourceProperties properties = resourcePropertiesService.queryProperties(Resources.ofPath(sourcePath, StandardCharsets.UTF_8)); + final ResourceProperties properties = resourcePropertiesService + .queryProperties(Resources.ofPath(sourcePath, StandardCharsets.UTF_8)); final Charset charset = Charset.forName(properties.getValue(PropertyType.charset, "utf-8", true)); final String source = new String(Files.readAllBytes(sourcePath), charset); @@ -104,7 +143,10 @@ public String format(Path sourcePath) { final int kind = (sourcePath.getFileName().toString().equals(IModule.MODULE_INFO_JAVA) ? CodeFormatter.K_MODULE_INFO : CodeFormatter.K_COMPILATION_UNIT) | CodeFormatter.F_INCLUDE_COMMENTS; - final PropertyType.EndOfLineValue eol = properties.getValue(PropertyType.end_of_line, null, true); + PropertyType.EndOfLineValue eol = properties.getValue(PropertyType.end_of_line, null, true); + if (eol == null) { + eol = PropertyType.EndOfLineValue.autodetect(source); + } final TextEdit edit = formatter.format(kind, source, 0, source.length(), 0, eol.getEndOfLineString()); final IDocument doc = new Document(source); diff --git a/pom.xml b/pom.xml index 6f54201..b3531da 100644 --- a/pom.xml +++ b/pom.xml @@ -88,9 +88,9 @@ - + core tck-spi jdt ij @@ -101,7 +101,7 @@ 4.12 - 0.0.3 + 0.2.0 182.5107.16 @@ -187,6 +187,12 @@ ${version.org.ec4j.core} + + org.ec4j.java-domain + editorconfig-java-domain-core + 0.0.1-SNAPSHOT + + org.ec4j.java-domain editorconfig-java-domain-tck-spi diff --git a/tck/src/build/groovy/generate-test-classes.groovy b/tck/src/build/groovy/generate-test-classes.groovy index 84a3874..1f70961 100644 --- a/tck/src/build/groovy/generate-test-classes.groovy +++ b/tck/src/build/groovy/generate-test-classes.groovy @@ -31,7 +31,6 @@ Files.createDirectories(generatedClassesDir) /* A Map from class names to lists of expected Java files. One test method will be generated for each expected Java file. */ final Map> testClasses = new TreeMap<>(); -int pathLength = 0; /* Populate testClasses */ testResourcesDir.eachFileRecurse(groovy.io.FileType.FILES) { path -> @@ -44,7 +43,6 @@ testResourcesDir.eachFileRecurse(groovy.io.FileType.FILES) { path -> testClasses.put(className, javaFiles = new ArrayList()) } javaFiles.add(path); - pathLength = path.getNameCount() } } @@ -54,7 +52,6 @@ assert !testClasses.isEmpty() testClasses.each { className, javaFiles -> final StringBuilder testMethods = new StringBuilder() javaFiles.each { expectedJavaFile -> - assert expectedJavaFile.getNameCount() == pathLength final String fileName = expectedJavaFile.getFileName().toString(); final Path javaFile = expectedJavaFile.getParent().resolve(fileName.replace(".expected", "")) final String testMethodName = fileName.replace(".expected.java", "").uncapitalize() @@ -73,8 +70,13 @@ testClasses.each { className, javaFiles -> } String toTestClassName(Path expectedJavaPath) { - final Path valueDir = expectedJavaPath.getParent() - final Path propertyDir = valueDir.getParent() - final String snakeCased = propertyDir.getFileName().toString() + "_" + valueDir.getFileName().toString() + final List segments = new ArrayList<>() + Path dir = expectedJavaPath.getParent() + /* Climb up the path up to the grouping directory, e.g. src/test/resources/basic */ + while (dir.getNameCount() > 4) { + segments.add(dir.getFileName().toString()) + dir = dir.getParent() + } + final String snakeCased = segments.reverse().join("_") return snakeCased.replaceAll(/_\w/){ it[1].toUpperCase() }.capitalize() + "Test" } diff --git a/tck/src/test/resources/basic/defaults/.editorconfig b/tck/src/test/resources/basic/defaults/.editorconfig new file mode 100644 index 0000000..f28239b --- /dev/null +++ b/tck/src/test/resources/basic/defaults/.editorconfig @@ -0,0 +1,4 @@ +root = true + +[*] +charset = utf-8 diff --git a/tck/src/test/resources/basic/defaults/Good.expected.java b/tck/src/test/resources/basic/defaults/Good.expected.java new file mode 100644 index 0000000..83dd46d --- /dev/null +++ b/tck/src/test/resources/basic/defaults/Good.expected.java @@ -0,0 +1,16 @@ +package org.ec4j.java.domain.tck; + +public class Good { + + private final int field; + + public Good(int field) { + super(); + this.field = field; + } + + public int getField() { + return field; + } + +} diff --git a/tck/src/test/resources/basic/defaults/Good.java b/tck/src/test/resources/basic/defaults/Good.java new file mode 100644 index 0000000..83dd46d --- /dev/null +++ b/tck/src/test/resources/basic/defaults/Good.java @@ -0,0 +1,16 @@ +package org.ec4j.java.domain.tck; + +public class Good { + + private final int field; + + public Good(int field) { + super(); + this.field = field; + } + + public int getField() { + return field; + } + +} diff --git a/tck/src/test/resources/basic/defaults/MixedIndent.expected.java b/tck/src/test/resources/basic/defaults/MixedIndent.expected.java new file mode 100644 index 0000000..75d0e84 --- /dev/null +++ b/tck/src/test/resources/basic/defaults/MixedIndent.expected.java @@ -0,0 +1,16 @@ +package org.ec4j.java.domain.tck; + +public class MixedIndent { + + private final int field; + + public MixedIndent(int field) { + super(); + this.field = field; + } + + public int getField() { + return field; + } + +} diff --git a/tck/src/test/resources/basic/defaults/MixedIndent.java b/tck/src/test/resources/basic/defaults/MixedIndent.java new file mode 100644 index 0000000..8345873 --- /dev/null +++ b/tck/src/test/resources/basic/defaults/MixedIndent.java @@ -0,0 +1,16 @@ +package org.ec4j.java.domain.tck; + +public class MixedIndent { + + private final int field; + + public MixedIndent(int field) { + super(); + this.field = field; + } + + public int getField() { + return field; + } + +}