-
Notifications
You must be signed in to change notification settings - Fork 1
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
ppalaga
wants to merge
1
commit into
ec4j:master
Choose a base branch
from
ppalaga:i8
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
tck/src/test/resources/basic/defaults/MixedIndent.expected.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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