Skip to content

Detect and preserve line separators instead of using system default #340

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

Closed
wants to merge 6 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<String> lines = Files.readAllLines(sourceFilePath);
lines.add(lineToAppend);
Files.write(sourceFilePath, lines, StandardOpenOption.TRUNCATE_EXISTING);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Test resources that need a predictable eol
apply*/src/main/java/simple/Simple.java eol=lf
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
<option>-keep class org.eclipse.jdt.core.formatter.** {*;}</option>
<option>-keep class org.eclipse.jdt.internal.formatter.** {*;}</option>
<option>-keep class org.eclipse.jface.text.Document {*;}</option>
<option>-keep class org.eclipse.jface.text.TextUtilities {*;}</option>
<option>-keep class
org.eclipse.jdt.internal.compiler.parser.TerminalTokens {*;}</option>
<option>-keep class org.eclipse.jdt.core.dom.TagElement {*;}</option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
<option>-keep class org.eclipse.jdt.core.formatter.** {*;}</option>
<option>-keep class org.eclipse.jdt.internal.formatter.** {*;}</option>
<option>-keep class org.eclipse.jface.text.Document {*;}</option>
<option>-keep class org.eclipse.jface.text.TextUtilities {*;}</option>
<option>-keep class
org.eclipse.jdt.internal.compiler.parser.TerminalTokens {*;}</option>
<option>-keep class org.eclipse.jdt.core.dom.TagElement {*;}</option>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package correct;public class CorrectCr { public static void main(String[] args) throws Exception { // FIXME }}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package correct;

public class CorrectCrlf {

public static void main(String[] args) throws Exception {
// FIXME
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package correct;

public class CorrectLf {

public static void main(String[] args) throws Exception {
// FIXME
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package correct;public class CorrectCr { public static void main(String[] args) throws Exception { // FIXME }}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package correct;

public class CorrectCrlf {

public static void main(String[] args) throws Exception {
// FIXME
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package correct;

public class CorrectLf {

public static void main(String[] args) throws Exception {
// FIXME
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down