Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #8 Define defaults for Java editorconfig properties #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!--

Copyright (c) 2018 EditorConfig Java Domain
project contributors as indicated by the @author tags.

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.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.ec4j.java-domain</groupId>
<artifactId>editorconfig-java-domain-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>editorconfig-java-domain-core</artifactId>

<name>EditorConfig Java Domain Core</name>
<description>EditorConfig Java Domain Core module</description>

<dependencies>

<dependency>
<groupId>org.ec4j.core</groupId>
<artifactId>ec4j-core</artifactId>
</dependency>

</dependencies>

</project>
8 changes: 8 additions & 0 deletions core/src/main/resources/java-defaults.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*.java]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This file stores the Java defaults

indent_style = space
indent_size = 4

# 2147483647 is java.lang.Integer.MAX_VALUE thus actually unlimited
max_line_length = 2147483647
5 changes: 5 additions & 0 deletions jdt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
<artifactId>ec4j-core</artifactId>
</dependency>

<dependency>
<groupId>org.ec4j.java-domain</groupId>
<artifactId>editorconfig-java-domain-core</artifactId>
</dependency>

<dependency>
<groupId>org.ec4j.java-domain</groupId>
<artifactId>editorconfig-java-domain-tck-spi</artifactId>
Expand Down
64 changes: 53 additions & 11 deletions jdt/src/main/java/org/ec4j/java/domain/jdt/JdtFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -49,7 +52,27 @@
* @author <a href="https://github.com/ppalaga">Peter Palaga</a>
*/
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> T getValue(PropertyType<T> 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> 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.
Expand All @@ -58,9 +81,9 @@ public class JdtFormatter implements Formatter {
* @return a new {@link Map}
*/
private static Map<String, String> toJdtFormatterOptions(ResourceProperties properties) {
final WrappedResourceProperties wrappedProperties = new WrappedResourceProperties(properties);
Map<String, String> 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:
Expand All @@ -71,10 +94,10 @@ private static Map<String, String> 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;
Expand All @@ -84,18 +107,34 @@ private static Map<String, String> 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) //
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is where the defaults from java-defaults.editorconfig are used in the JdtFormatter

.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);

Expand All @@ -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);
Expand Down
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@
</snapshots>
</repository>
</repositories>


<modules>
<module>core</module>
<module>tck-spi</module>
<module>jdt</module>
<module>ij</module>
Expand All @@ -101,7 +101,7 @@

<!-- Dependency versions in alphabectic order -->
<version.junit>4.12</version.junit>
<version.org.ec4j.core>0.0.3</version.org.ec4j.core>
<version.org.ec4j.core>0.2.0</version.org.ec4j.core>
<version.ij>182.5107.16</version.ij>

<!-- Plugins and their dependencies -->
Expand Down Expand Up @@ -187,6 +187,12 @@
<version>${version.org.ec4j.core}</version>
</dependency>

<dependency>
<groupId>org.ec4j.java-domain</groupId>
<artifactId>editorconfig-java-domain-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.ec4j.java-domain</groupId>
<artifactId>editorconfig-java-domain-tck-spi</artifactId>
Expand Down
14 changes: 8 additions & 6 deletions tck/src/build/groovy/generate-test-classes.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, List<Path>> testClasses = new TreeMap<>();
int pathLength = 0;

/* Populate testClasses */
testResourcesDir.eachFileRecurse(groovy.io.FileType.FILES) { path ->
Expand All @@ -44,7 +43,6 @@ testResourcesDir.eachFileRecurse(groovy.io.FileType.FILES) { path ->
testClasses.put(className, javaFiles = new ArrayList<Path>())
}
javaFiles.add(path);
pathLength = path.getNameCount()
}
}

Expand All @@ -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()
Expand All @@ -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<String> 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"
}
4 changes: 4 additions & 0 deletions tck/src/test/resources/basic/defaults/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
root = true

[*]
charset = utf-8
Copy link
Contributor Author

Choose a reason for hiding this comment

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

An empty .editorconfig file to test the defaults

16 changes: 16 additions & 0 deletions tck/src/test/resources/basic/defaults/Good.expected.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
16 changes: 16 additions & 0 deletions tck/src/test/resources/basic/defaults/Good.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
16 changes: 16 additions & 0 deletions tck/src/test/resources/basic/defaults/MixedIndent.expected.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
16 changes: 16 additions & 0 deletions tck/src/test/resources/basic/defaults/MixedIndent.java
Original file line number Diff line number Diff line change
@@ -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;
}

}