diff --git a/spring-javaformat-eclipse/io.spring.javaformat.eclipse.tests/src/io/spring/javaformat/eclipse/projectsettings/ProjectSettingsFilesTests.java b/spring-javaformat-eclipse/io.spring.javaformat.eclipse.tests/src/io/spring/javaformat/eclipse/projectsettings/ProjectSettingsFilesTests.java index a75b92b1..20c06744 100644 --- a/spring-javaformat-eclipse/io.spring.javaformat.eclipse.tests/src/io/spring/javaformat/eclipse/projectsettings/ProjectSettingsFilesTests.java +++ b/spring-javaformat-eclipse/io.spring.javaformat.eclipse.tests/src/io/spring/javaformat/eclipse/projectsettings/ProjectSettingsFilesTests.java @@ -95,7 +95,8 @@ void applyToProjectWithFileMergesToDotSettings() throws Exception { }).given(projectFile).setContents((InputStream) any(), anyInt(), any()); files.applyToProject(project, monitor); verify(projectFile).setContents((InputStream) any(), eq(1), eq(monitor)); - assertThat(out.toString(StandardCharsets.UTF_8)).isEqualTo("a=b\ny=z\n"); + assertThat(out.toString(StandardCharsets.UTF_8)) + .isEqualToNormalizingNewlines("a=b\ny=z\n"); } private ProjectSettingsFile createPrefsFile() throws IOException { diff --git a/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/java/io/spring/javaformat/gradle/CheckTaskTests.java b/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/java/io/spring/javaformat/gradle/CheckTaskTests.java index a5554177..0420e1c2 100644 --- a/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/java/io/spring/javaformat/gradle/CheckTaskTests.java +++ b/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/java/io/spring/javaformat/gradle/CheckTaskTests.java @@ -23,7 +23,7 @@ import java.nio.file.StandardCopyOption; import java.nio.file.StandardOpenOption; import java.util.Arrays; -import java.util.Collections; +import java.util.List; import java.util.stream.Stream; import org.gradle.testkit.runner.BuildResult; @@ -72,8 +72,8 @@ void whenFirstInvocationSucceedsAndSourceIsModifiedThenSecondInvocationSucceeds( GradleBuild gradleBuild = this.gradleBuild.source(this.temp); BuildResult result = gradleBuild.build("check"); assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS); - Files.write(new File(this.temp, "src/main/java/simple/Simple.java").toPath(), - Collections.singletonList("// A change to the file"), StandardOpenOption.APPEND); + appendToFileNormalizingNewlines(new File(this.temp, "src/main/java/simple/Simple.java").toPath(), + "// A change to the file"); result = gradleBuild.build("--debug", "check"); assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS); } @@ -146,4 +146,14 @@ private void copyFolder(Path source, Path target) throws IOException { } } + /** + * Uses a read/modify/truncate approach to append a line to a file. + * This avoids issues where the standard append option results in mixed line-endings. + */ + private void appendToFileNormalizingNewlines(Path sourceFilePath, String lineToAppend) throws IOException { + List lines = Files.readAllLines(sourceFilePath); + lines.add(lineToAppend); + Files.write(sourceFilePath, lines, StandardOpenOption.TRUNCATE_EXISTING); + } + } diff --git a/spring-javaformat-maven/spring-javaformat-maven-plugin/src/it/.gitattributes b/spring-javaformat-maven/spring-javaformat-maven-plugin/src/it/.gitattributes new file mode 100644 index 00000000..525113da --- /dev/null +++ b/spring-javaformat-maven/spring-javaformat-maven-plugin/src/it/.gitattributes @@ -0,0 +1,2 @@ +# Test resources that need a predictable eol +apply*/src/main/java/simple/Simple.java eol=lf diff --git a/spring-javaformat-maven/spring-javaformat-maven-plugin/src/test/java/io/spring/format/maven/VerifyApply.java b/spring-javaformat-maven/spring-javaformat-maven-plugin/src/test/java/io/spring/format/maven/VerifyApply.java index 65fc2ec0..0c21dc7e 100644 --- a/spring-javaformat-maven/spring-javaformat-maven-plugin/src/test/java/io/spring/format/maven/VerifyApply.java +++ b/spring-javaformat-maven/spring-javaformat-maven-plugin/src/test/java/io/spring/format/maven/VerifyApply.java @@ -30,7 +30,7 @@ */ public class VerifyApply { - private static final String LF = System.lineSeparator(); + private static final String LF = "\n"; private static final String JAVA_FILE = "src/main/java/simple/Simple.java"; diff --git a/spring-javaformat/spring-javaformat-formatter-eclipse-jdk11/pom.xml b/spring-javaformat/spring-javaformat-formatter-eclipse-jdk11/pom.xml index 9d7f1a3d..4cfa7455 100644 --- a/spring-javaformat/spring-javaformat-formatter-eclipse-jdk11/pom.xml +++ b/spring-javaformat/spring-javaformat-formatter-eclipse-jdk11/pom.xml @@ -173,6 +173,7 @@ + diff --git a/spring-javaformat/spring-javaformat-formatter-eclipse-jdk8/pom.xml b/spring-javaformat/spring-javaformat-formatter-eclipse-jdk8/pom.xml index 60417036..f3136138 100644 --- a/spring-javaformat/spring-javaformat-formatter-eclipse-jdk8/pom.xml +++ b/spring-javaformat/spring-javaformat-formatter-eclipse-jdk8/pom.xml @@ -173,6 +173,7 @@ + diff --git a/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/.gitattributes b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/.gitattributes new file mode 100644 index 00000000..95175246 --- /dev/null +++ b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/.gitattributes @@ -0,0 +1,4 @@ +# Test resources that need specific eol +**/correct-crlf.txt eol=crlf +**/correct-cr.txt eol=cr +**/correct-lf.txt eol=lf diff --git a/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/expected/correct-cr.txt b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/expected/correct-cr.txt new file mode 100644 index 00000000..a72e4bf3 --- /dev/null +++ b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/expected/correct-cr.txt @@ -0,0 +1 @@ +package correct; public class CorrectCr { public static void main(String[] args) throws Exception { // FIXME } } \ No newline at end of file diff --git a/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/expected/correct-crlf.txt b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/expected/correct-crlf.txt new file mode 100644 index 00000000..5a609678 --- /dev/null +++ b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/expected/correct-crlf.txt @@ -0,0 +1,9 @@ +package correct; + +public class CorrectCrlf { + + public static void main(String[] args) throws Exception { + // FIXME + } + +} diff --git a/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/expected/correct-lf.txt b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/expected/correct-lf.txt new file mode 100644 index 00000000..3b6208a2 --- /dev/null +++ b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/expected/correct-lf.txt @@ -0,0 +1,9 @@ +package correct; + +public class CorrectLf { + + public static void main(String[] args) throws Exception { + // FIXME + } + +} diff --git a/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/source/correct-cr.txt b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/source/correct-cr.txt new file mode 100644 index 00000000..a72e4bf3 --- /dev/null +++ b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/source/correct-cr.txt @@ -0,0 +1 @@ +package correct; public class CorrectCr { public static void main(String[] args) throws Exception { // FIXME } } \ No newline at end of file diff --git a/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/source/correct-crlf.txt b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/source/correct-crlf.txt new file mode 100644 index 00000000..5a609678 --- /dev/null +++ b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/source/correct-crlf.txt @@ -0,0 +1,9 @@ +package correct; + +public class CorrectCrlf { + + public static void main(String[] args) throws Exception { + // FIXME + } + +} diff --git a/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/source/correct-lf.txt b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/source/correct-lf.txt new file mode 100644 index 00000000..3b6208a2 --- /dev/null +++ b/spring-javaformat/spring-javaformat-formatter-tests/src/test/resources/source/correct-lf.txt @@ -0,0 +1,9 @@ +package correct; + +public class CorrectLf { + + public static void main(String[] args) throws Exception { + // FIXME + } + +} diff --git a/spring-javaformat/spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter/Formatter.java b/spring-javaformat/spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter/Formatter.java index 83f6406c..90a7f091 100644 --- a/spring-javaformat/spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter/Formatter.java +++ b/spring-javaformat/spring-javaformat-formatter/src/main/java/io/spring/javaformat/formatter/Formatter.java @@ -19,6 +19,7 @@ import java.util.Map; import org.eclipse.jface.text.IRegion; +import org.eclipse.jface.text.TextUtilities; import org.eclipse.text.edits.TextEdit; import io.spring.javaformat.config.JavaBaseline; @@ -123,6 +124,9 @@ public TextEdit format(String source, int offset, int length, String lineSeparat public TextEdit format(int kind, String source, int offset, int length, int indentationLevel, String lineSeparator) { + if (lineSeparator == null) { + lineSeparator = TextUtilities.determineLineDelimiter(source, DEFAULT_LINE_SEPARATOR); + } return this.delegate.format(kind, source, offset, length, indentationLevel, lineSeparator); } @@ -148,6 +152,9 @@ public TextEdit format(String source, IRegion[] regions, String lineSeparator) { } public TextEdit format(int kind, String source, IRegion[] regions, int indentationLevel, String lineSeparator) { + if (lineSeparator == null) { + lineSeparator = TextUtilities.determineLineDelimiter(source, DEFAULT_LINE_SEPARATOR); + } return this.delegate.format(kind, source, regions, indentationLevel, lineSeparator); }