Fixed line endings not having the right line endings in CRLF files with autoCRLF=false

This also fixes the stage line/hunk issues of #221 and #213
This commit is contained in:
Abdelilah El Aissaoui 2024-08-18 20:44:23 +02:00
parent 8d863492ed
commit 765bae578f
No known key found for this signature in database
GPG Key ID: 7587FC860F594869
2 changed files with 4 additions and 27 deletions

View File

@ -4,7 +4,5 @@ import org.eclipse.jgit.diff.RawText
import java.io.ByteArrayOutputStream
fun RawText.lineAt(line: Int): String {
val outputStream = ByteArrayOutputStream()
this.writeLine(outputStream, line)
return outputStream.toString(Charsets.UTF_8)
return this.getString(line, line + 1, false)
}

View File

@ -67,41 +67,20 @@ class FormatHunksUseCase @Inject constructor() {
while (oldCurrentLine < oldEndLine || newCurrentLine < newEndLine) {
if (oldCurrentLine < curEdit.beginA || endIdx + 1 < curIdx) {
var lineText = oldRawText.lineAt(oldCurrentLine)
if (
oldCurrentLine < oldRawText.size() - 1 || // If it's not the last
(oldCurrentLine == oldRawText.size() - 1 && !oldRawText.isMissingNewlineAtEnd) // Or is the last and contains new line at the end
) {
lineText += oldRawText.lineDelimiter.orEmpty()
}
val lineText = oldRawText.lineAt(oldCurrentLine)
lines.add(Line(lineText, oldCurrentLine, newCurrentLine, LineType.CONTEXT))
oldCurrentLine++
newCurrentLine++
} else if (oldCurrentLine < curEdit.endA) {
var lineText = oldRawText.lineAt(oldCurrentLine)
if (
oldCurrentLine < oldRawText.size() - 1 || // If it's not the last
(oldCurrentLine == oldRawText.size() - 1 && !oldRawText.isMissingNewlineAtEnd) // Or is the last and contains new line at the end
) {
lineText += oldRawText.lineDelimiter.orEmpty()
}
val lineText = oldRawText.lineAt(oldCurrentLine)
lines.add(Line(lineText, oldCurrentLine, newCurrentLine, LineType.REMOVED))
oldCurrentLine++
} else if (newCurrentLine < curEdit.endB) {
var lineText = newRawText.lineAt(newCurrentLine)
if (
newCurrentLine < newRawText.size() - 1 || // If it's not the last
(newCurrentLine == newRawText.size() - 1 && !newRawText.isMissingNewlineAtEnd) // Or is the last and contains new line at the end
) {
lineText += newRawText.lineDelimiter.orEmpty()
}
val lineText = newRawText.lineAt(newCurrentLine)
lines.add(Line(lineText, oldCurrentLine, newCurrentLine, LineType.ADDED))