Skip to content

Commit

Permalink
fix(printing): šŸ› Simplifies handling of newlines in diff cleaned files (
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinWitt authored Jun 24, 2023
1 parent 4eb95ab commit 2d17740
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
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

0 comments on commit 2d17740

Please sign in to comment.