-
Couldn't load subscription status.
- Fork 1
Fix #8 Define defaults for Java editorconfig properties #9
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 <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. | ||
|
|
@@ -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: | ||
|
|
@@ -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; | ||
|
|
@@ -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) // | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is where the defaults from |
||
| .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); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| root = true | ||
|
|
||
| [*] | ||
| charset = utf-8 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An empty .editorconfig file to test the defaults |
||
| 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; | ||
| } | ||
|
|
||
| } |
| 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; | ||
| } | ||
|
|
||
| } |
| 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; | ||
| } | ||
|
|
||
| } |
| 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; | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file stores the Java defaults