From c31b1a9d6bb31ce6f91977fbaad6e5c87ab48fea Mon Sep 17 00:00:00 2001 From: Abdelilah El Aissaoui Date: Thu, 7 Apr 2022 23:06:20 +0200 Subject: [PATCH] Fixed hunk stage on files with a single line without lineDelimiter --- src/main/kotlin/app/git/StatusManager.kt | 15 ++++++++++----- src/main/kotlin/app/git/diff/HunkDiffGenerator.kt | 4 ++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/app/git/StatusManager.kt b/src/main/kotlin/app/git/StatusManager.kt index 3c4042f..1fdf581 100644 --- a/src/main/kotlin/app/git/StatusManager.kt +++ b/src/main/kotlin/app/git/StatusManager.kt @@ -148,16 +148,21 @@ class StatusManager @Inject constructor( private fun getTextLines(rawFile: RawText): List { val content = rawFile.rawContent.toString(Charsets.UTF_8)//.removeSuffix(rawFile.lineDelimiter) + val lineDelimiter: String? = rawFile.lineDelimiter - var splitted: List = content.split(rawFile.lineDelimiter).toMutableList().apply { - if(this.last() == "") - removeLast() + var splitted: List = if (lineDelimiter != null) { + content.split(lineDelimiter).toMutableList().apply { + if (this.last() == "") + removeLast() + } + } else { + listOf(content) } splitted = splitted.mapIndexed { index, line -> - val lineWithBreak = line + rawFile.lineDelimiter + val lineWithBreak = line +lineDelimiter.orEmpty() - if(index == splitted.count() - 1 && !content.endsWith(lineWithBreak)) { + if (index == splitted.count() - 1 && !content.endsWith(lineWithBreak)) { line } else lineWithBreak diff --git a/src/main/kotlin/app/git/diff/HunkDiffGenerator.kt b/src/main/kotlin/app/git/diff/HunkDiffGenerator.kt index 01accf4..84baa83 100644 --- a/src/main/kotlin/app/git/diff/HunkDiffGenerator.kt +++ b/src/main/kotlin/app/git/diff/HunkDiffGenerator.kt @@ -129,7 +129,7 @@ class HunkDiffGenerator @AssistedInject constructor( 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 + lineText += oldRawText.lineDelimiter.orEmpty() } lines.add(Line(lineText, oldCurrentLine, newCurrentLine, LineType.REMOVED)) @@ -142,7 +142,7 @@ class HunkDiffGenerator @AssistedInject constructor( 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 + lineText += newRawText.lineDelimiter.orEmpty() } lines.add(Line(lineText, oldCurrentLine, newCurrentLine, LineType.ADDED))