Skip to content
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(printing): 🐛 Simplifies handling of newlines in diff cleaned files #770

Merged
merged 3 commits into from
Jun 24, 2023
Merged
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 @@ -81,7 +81,7 @@ public void clean(Path path, Change change) {
if (change.getModes().contains(DiffCleanModes.NO_WHITESPACE_ADD)) {
String cleanResult = new ExtraWhiteSpaceCleaner()
.clean(Files.readString(filePath), lineChange, change, lineEnding);
printResult(filePath, cleanResult);
printResult(filePath, cleanResult, hasLineEnding, lineEnding);
}
}
}
Expand All @@ -100,7 +100,11 @@ private boolean isDeletedLines(String line) {
return line.startsWith("- ") && !line.startsWith("---");
}

private void printResult(Path filePath, String cleanResult) throws IOException {
private void printResult(Path filePath, String cleanResult, boolean hasLineEnding, String lineEnding)
throws IOException {
if (hasLineEnding) {
cleanResult += lineEnding;
}
Files.writeString(filePath, cleanResult);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class ExtraWhiteSpaceCleaner implements GitDiffCleaner {

@Override
public String clean(@Var String content, GitLineChange gitLineChange, Change change, String lineEnding) {
boolean endsWithLineEnding = endsWithLineEnding(content, lineEnding);
Patch<String> patch = DiffUtils.diffInline(gitLineChange.oldContent(), gitLineChange.newContent());
for (AbstractDelta<String> delta : patch.getDeltas()) {
logger.atInfo().log("Delta: %s", delta);
Expand All @@ -30,19 +29,9 @@ public String clean(@Var String content, GitLineChange gitLineChange, Change cha
.collect(Collectors.joining(lineEnding));
}
}
if (endsWithLineEnding && !patch.getDeltas().isEmpty() && !content.endsWith(lineEnding)) {
content += lineEnding;
}
return content;
}

/**
* Checks if the content ends with the line ending of the file.
*/
private boolean endsWithLineEnding(String content, String lineEnding) {
return content.endsWith(lineEnding);
}

private String changeIfMatches(ChangeDelta<String> delta, String content, GitLineChange gitLineChange) {
if (!gitLineChange.newContent().equals(content)) {
return content;
Expand Down