Skip to content

Commit f5ccce6

Browse files
committed
Merge pull request #212 from Hccake
* pr/212: Polish 'Add lineSeparator support to Maven Plugin' Add lineSeparator support to Maven Plugin
2 parents 84c456e + dc18d1a commit f5ccce6

File tree

9 files changed

+156
-14
lines changed

9 files changed

+156
-14
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>io.spring.javaformat</groupId>
6+
<artifactId>apply-line-separator</artifactId>
7+
<version>0.0.1.BUILD-SNAPSHOT</version>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<maven.compiler.source>1.8</maven.compiler.source>
11+
<maven.compiler.target>1.8</maven.compiler.target>
12+
</properties>
13+
<build>
14+
<plugins>
15+
<plugin>
16+
<groupId>@project.groupId@</groupId>
17+
<artifactId>@project.artifactId@</artifactId>
18+
<version>@project.version@</version>
19+
<executions>
20+
<execution>
21+
<goals>
22+
<goal>apply</goal>
23+
</goals>
24+
<configuration>
25+
<lineSeparator>CR</lineSeparator>
26+
</configuration>
27+
</execution>
28+
</executions>
29+
</plugin>
30+
</plugins>
31+
</build>
32+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package simple;
2+
3+
/**
4+
* Simple.
5+
* @author Phillip Webb
6+
* @since 1.0.0
7+
*/
8+
public class Simple {
9+
10+
public static void main(String[] args) throws Exception {
11+
// Main method
12+
}
13+
14+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
new io.spring.format.maven.VerifyApply().verify(basedir, "\r")

spring-javaformat-maven/spring-javaformat-maven-plugin/src/main/java/io/spring/format/maven/ApplyMojo.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@
3838
public class ApplyMojo extends FormatMojo {
3939

4040
@Override
41-
protected void execute(List<File> files, Charset encoding) throws MojoExecutionException, MojoFailureException {
41+
protected void execute(List<File> files, Charset encoding, String lineSeparator)
42+
throws MojoExecutionException, MojoFailureException {
4243
try {
4344
FileFormatter formatter = new FileFormatter();
44-
formatter.formatFiles(files, encoding).filter(FileEdit::hasEdits).forEach(this::save);
45+
formatter.formatFiles(files, encoding, lineSeparator).filter(FileEdit::hasEdits).forEach(this::save);
4546
}
4647
catch (FileFormatterException ex) {
4748
throw new MojoExecutionException("Unable to format file " + ex.getFile(), ex);

spring-javaformat-maven/spring-javaformat-maven-plugin/src/main/java/io/spring/format/maven/FormatMojo.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
import java.nio.charset.StandardCharsets;
2323
import java.util.ArrayList;
2424
import java.util.Arrays;
25+
import java.util.Collections;
26+
import java.util.LinkedHashMap;
2527
import java.util.List;
28+
import java.util.Map;
2629
import java.util.stream.Collectors;
2730
import java.util.stream.Stream;
2831

@@ -47,6 +50,15 @@ public abstract class FormatMojo extends AbstractMojo {
4750

4851
private static final String GENERATED_TEST_SOURCES = File.separator + "generated-test-sources" + File.separator;
4952

53+
private static final Map<String, String> LINE_SEPARATOR;
54+
static {
55+
Map<String, String> lineSeparator = new LinkedHashMap<>();
56+
lineSeparator.put("cr", "\r");
57+
lineSeparator.put("lf", "\n");
58+
lineSeparator.put("crlf", "\r\n");
59+
LINE_SEPARATOR = Collections.unmodifiableMap(lineSeparator);
60+
}
61+
5062
/**
5163
* The Maven Project Object.
5264
*/
@@ -88,6 +100,12 @@ public abstract class FormatMojo extends AbstractMojo {
88100
@Parameter(property = "spring-javaformat.includeGeneratedSource", defaultValue = "false")
89101
private boolean includeGeneratedSource;
90102

103+
/**
104+
* Specifies the line separator to use when formatting.
105+
*/
106+
@Parameter(property = "spring-javaformat.lineSeparator")
107+
private String lineSeparator;
108+
91109
@Override
92110
public final void execute() throws MojoExecutionException, MojoFailureException {
93111
List<File> directories = new ArrayList<>();
@@ -98,7 +116,14 @@ public final void execute() throws MojoExecutionException, MojoFailureException
98116
files.addAll(scan(directory));
99117
}
100118
Charset encoding = (this.encoding == null ? StandardCharsets.UTF_8 : Charset.forName(this.encoding));
101-
execute(files, encoding);
119+
String lineSeparator = null;
120+
if (this.lineSeparator != null) {
121+
lineSeparator = LINE_SEPARATOR.get(this.lineSeparator.toLowerCase());
122+
if (lineSeparator == null) {
123+
throw new MojoExecutionException("Unknown lineSeparator " + this.lineSeparator);
124+
}
125+
}
126+
execute(files, encoding, this.lineSeparator != null ? lineSeparator : null);
102127
}
103128

104129
private Stream<File> resolve(List<String> directories) {
@@ -152,10 +177,11 @@ private boolean hasLength(Object[] array) {
152177
* Perform the formatting build-process behavior this {@code Mojo} implements.
153178
* @param files the files to process
154179
* @param encoding the encoding
180+
* @param lineSeparator the line separator
155181
* @throws MojoExecutionException on execution error
156182
* @throws MojoFailureException on failure
157183
*/
158-
protected abstract void execute(List<File> files, Charset encoding)
184+
protected abstract void execute(List<File> files, Charset encoding, String lineSeparator)
159185
throws MojoExecutionException, MojoFailureException;
160186

161187
}

spring-javaformat-maven/spring-javaformat-maven-plugin/src/main/java/io/spring/format/maven/ValidateMojo.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,15 @@ public class ValidateMojo extends FormatMojo {
4545
private boolean skip;
4646

4747
@Override
48-
protected void execute(List<File> files, Charset encoding) throws MojoExecutionException, MojoFailureException {
48+
protected void execute(List<File> files, Charset encoding, String lineSeparator)
49+
throws MojoExecutionException, MojoFailureException {
4950
if (this.skip) {
5051
getLog().debug("skipping validation as per configuration.");
5152
return;
5253
}
5354
FileFormatter formatter = new FileFormatter();
54-
List<File> problems = formatter.formatFiles(files, encoding).filter(FileEdit::hasEdits).map(FileEdit::getFile)
55-
.collect(Collectors.toList());
55+
List<File> problems = formatter.formatFiles(files, encoding, lineSeparator).filter(FileEdit::hasEdits)
56+
.map(FileEdit::getFile).collect(Collectors.toList());
5657
if (!problems.isEmpty()) {
5758
StringBuilder message = new StringBuilder("Formatting violations found in the following files:\n");
5859
problems.stream().forEach((f) -> message.append(" * " + f + "\n"));

spring-javaformat-maven/spring-javaformat-maven-plugin/src/test/java/io/spring/format/maven/VerifyApply.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,18 @@ public class VerifyApply {
3535
private static final String JAVA_FILE = "src/main/java/simple/Simple.java";
3636

3737
public void verify(File base) throws IOException {
38+
verify(base, LF);
39+
}
40+
41+
public void verify(File base, String lineSeparator) throws IOException {
3842
String formated = new String(Files.readAllBytes(base.toPath().resolve(JAVA_FILE)), StandardCharsets.UTF_8);
39-
assertThat(formated).contains("Simple." + LF + " *" + LF + " * @author").contains("public class Simple {");
43+
assertThat(formated).contains("Simple." + lineSeparator + " *" + lineSeparator + " * @author")
44+
.contains("public class Simple {");
4045
}
4146

47+
public static void main(String[] args) throws IOException {
48+
new VerifyApply().verify(new File(
49+
"/Users/pwebb/projects/spring-javaformat/code/spring-javaformat-maven/spring-javaformat-maven-plugin/target/it/apply-line-separator"),
50+
"\r");
51+
}
4252
}

spring-javaformat/spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter/FileFormatter.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,19 @@ public FileFormatter(Formatter formatter) {
5656
* @return a stream of formatted files that have edits
5757
*/
5858
public Stream<FileEdit> formatFiles(Iterable<File> files, Charset encoding) {
59-
return formatFiles(StreamSupport.stream(files.spliterator(), false), encoding);
59+
return formatFiles(files, encoding, Formatter.DEFAULT_LINE_SEPARATOR);
60+
}
61+
62+
/**
63+
* Format the given source files and provide a {@link Stream} of {@link FileEdit}
64+
* instances.
65+
* @param files the files to format
66+
* @param encoding the source encoding
67+
* @param lineSeparator the line separator
68+
* @return a stream of formatted files that have edits
69+
*/
70+
public Stream<FileEdit> formatFiles(Iterable<File> files, Charset encoding, String lineSeparator) {
71+
return formatFiles(StreamSupport.stream(files.spliterator(), false), encoding, lineSeparator);
6072
}
6173

6274
/**
@@ -67,7 +79,19 @@ public Stream<FileEdit> formatFiles(Iterable<File> files, Charset encoding) {
6779
* @return a stream of formatted files that have edits
6880
*/
6981
public Stream<FileEdit> formatFiles(Stream<File> files, Charset encoding) {
70-
return files.map((file) -> formatFile(file, encoding));
82+
return formatFiles(files, encoding, Formatter.DEFAULT_LINE_SEPARATOR);
83+
}
84+
85+
/**
86+
* Format the given source files and provide a {@link Stream} of {@link FileEdit}
87+
* instances.
88+
* @param files the files to format
89+
* @param encoding the source encoding
90+
* @param lineSeparator the line separator
91+
* @return a stream of formatted files that have edits
92+
*/
93+
public Stream<FileEdit> formatFiles(Stream<File> files, Charset encoding, String lineSeparator) {
94+
return files.map((file) -> formatFile(file, encoding, lineSeparator));
7195
}
7296

7397
/**
@@ -77,9 +101,20 @@ public Stream<FileEdit> formatFiles(Stream<File> files, Charset encoding) {
77101
* @return a formatted file
78102
*/
79103
public FileEdit formatFile(File file, Charset encoding) {
104+
return formatFile(file, encoding, Formatter.DEFAULT_LINE_SEPARATOR);
105+
}
106+
107+
/**
108+
* Format the the given source file and return a {@link FileEdit} instance.
109+
* @param file the file to format
110+
* @param encoding the source encoding
111+
* @param lineSeparator the line separator
112+
* @return a formatted file
113+
*/
114+
public FileEdit formatFile(File file, Charset encoding, String lineSeparator) {
80115
try {
81116
String content = new String(Files.readAllBytes(file.toPath()), encoding);
82-
TextEdit edit = this.formatter.format(content);
117+
TextEdit edit = this.formatter.format(content, lineSeparator);
83118
return new FileEdit(file, encoding, content, edit);
84119
}
85120
catch (Exception ex) {

spring-javaformat/spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter/Formatter.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class Formatter extends CodeFormatter {
5454
/**
5555
* The default line separator.
5656
*/
57-
private static final String DEFAULT_LINE_SEPARATOR = null;
57+
public static final String DEFAULT_LINE_SEPARATOR = null;
5858

5959
private final Set<FormatterOption> options;
6060

@@ -81,7 +81,17 @@ public Formatter(FormatterOption... options) {
8181
* @return the text edit
8282
*/
8383
public TextEdit format(String source) {
84-
return format(source, 0, source.length());
84+
return format(source, DEFAULT_LINE_SEPARATOR);
85+
}
86+
87+
/**
88+
* Format the given source content.
89+
* @param source the source content to format
90+
* @param lineSeparator the line separator
91+
* @return the text edit
92+
*/
93+
public TextEdit format(String source, String lineSeparator) {
94+
return format(source, 0, source.length(), lineSeparator);
8595
}
8696

8797
/**
@@ -92,7 +102,19 @@ public TextEdit format(String source) {
92102
* @return the text edit
93103
*/
94104
public TextEdit format(String source, int offset, int length) {
95-
return format(DEFAULT_COMPONENTS, source, offset, length, DEFAULT_INDENTATION_LEVEL, DEFAULT_LINE_SEPARATOR);
105+
return format(source, offset, length, DEFAULT_LINE_SEPARATOR);
106+
}
107+
108+
/**
109+
* Format a specific subsection of the given source content.
110+
* @param source the source content to format
111+
* @param offset the offset to start formatting
112+
* @param length the length to format
113+
* @param lineSeparator the line separator
114+
* @return the text edit
115+
*/
116+
public TextEdit format(String source, int offset, int length, String lineSeparator) {
117+
return format(DEFAULT_COMPONENTS, source, offset, length, DEFAULT_INDENTATION_LEVEL, lineSeparator);
96118
}
97119

98120
@Override

0 commit comments

Comments
 (0)