Fixed multiple lines shown in changed lines

Also fixed copying not copying line endings of unchanged lines
This commit is contained in:
Abdelilah El Aissaoui 2022-08-06 03:07:32 +02:00
parent 6398244895
commit 3c5b8f7ae6
3 changed files with 51 additions and 23 deletions

View File

@ -30,7 +30,7 @@ val String.dirPath: String
this this
} }
fun String.removeLineEndings(): String { fun String.removeLineDelimiters(): String {
return this.removeSuffix("\n") return this.removeSuffix("\n")
.removeSuffix("\r\n") .removeSuffix("\r\n")
} }

View File

@ -117,7 +117,15 @@ class HunkDiffGenerator @AssistedInject constructor(
while (oldCurrentLine < oldEndLine || newCurrentLine < newEndLine) { while (oldCurrentLine < oldEndLine || newCurrentLine < newEndLine) {
if (oldCurrentLine < curEdit.beginA || endIdx + 1 < curIdx) { if (oldCurrentLine < curEdit.beginA || endIdx + 1 < curIdx) {
val lineText = oldRawText.lineAt(oldCurrentLine) 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()
}
lines.add(Line(lineText, oldCurrentLine, newCurrentLine, LineType.CONTEXT)) lines.add(Line(lineText, oldCurrentLine, newCurrentLine, LineType.CONTEXT))
oldCurrentLine++ oldCurrentLine++

View File

@ -22,8 +22,6 @@ import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onPreviewKeyEvent import androidx.compose.ui.input.key.onPreviewKeyEvent
import androidx.compose.ui.input.pointer.PointerIconDefaults import androidx.compose.ui.input.pointer.PointerIconDefaults
import androidx.compose.ui.input.pointer.pointerHoverIcon import androidx.compose.ui.input.pointer.pointerHoverIcon
@ -33,7 +31,8 @@ import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import app.extensions.removeLineEndings import app.extensions.lineDelimiter
import app.extensions.removeLineDelimiters
import app.extensions.toStringWithSpaces import app.extensions.toStringWithSpaces
import app.git.DiffEntryType import app.git.DiffEntryType
import app.git.EntryContent import app.git.EntryContent
@ -84,7 +83,9 @@ fun Diff(
} }
) { ) {
when (viewDiffResult) { when (viewDiffResult) {
ViewDiffResult.DiffNotFound -> { onCloseDiffView() } ViewDiffResult.DiffNotFound -> {
onCloseDiffView()
}
is ViewDiffResult.Loaded -> { is ViewDiffResult.Loaded -> {
val diffEntryType = viewDiffResult.diffEntryType val diffEntryType = viewDiffResult.diffEntryType
val diffEntry = viewDiffResult.diffResult.diffEntry val diffEntry = viewDiffResult.diffResult.diffEntry
@ -143,7 +144,8 @@ fun NonTextDiff(diffResult: DiffResult.NonText) {
if (showOldAndNew) { if (showOldAndNew) {
Column( Column(
modifier = Modifier.weight(0.5f) modifier = Modifier
.weight(0.5f)
.padding(start = 24.dp, end = 8.dp, top = 24.dp, bottom = 24.dp), .padding(start = 24.dp, end = 8.dp, top = 24.dp, bottom = 24.dp),
horizontalAlignment = Alignment.CenterHorizontally, horizontalAlignment = Alignment.CenterHorizontally,
) { ) {
@ -151,7 +153,8 @@ fun NonTextDiff(diffResult: DiffResult.NonText) {
SideDiff(oldBinaryContent) SideDiff(oldBinaryContent)
} }
Column( Column(
modifier = Modifier.weight(0.5f) modifier = Modifier
.weight(0.5f)
.padding(start = 8.dp, end = 24.dp, top = 24.dp, bottom = 24.dp), .padding(start = 8.dp, end = 24.dp, top = 24.dp, bottom = 24.dp),
horizontalAlignment = Alignment.CenterHorizontally, horizontalAlignment = Alignment.CenterHorizontally,
) { ) {
@ -160,14 +163,16 @@ fun NonTextDiff(diffResult: DiffResult.NonText) {
} }
} else if (oldBinaryContent != EntryContent.Missing) { } else if (oldBinaryContent != EntryContent.Missing) {
Box( Box(
modifier = Modifier.fillMaxSize() modifier = Modifier
.fillMaxSize()
.padding(all = 24.dp), .padding(all = 24.dp),
) { ) {
SideDiff(oldBinaryContent) SideDiff(oldBinaryContent)
} }
} else if (newBinaryContent != EntryContent.Missing) { } else if (newBinaryContent != EntryContent.Missing) {
Column( Column(
modifier = Modifier.fillMaxSize() modifier = Modifier
.fillMaxSize()
.padding(all = 24.dp), .padding(all = 24.dp),
horizontalAlignment = Alignment.CenterHorizontally, horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center, verticalArrangement = Arrangement.Center,
@ -436,11 +441,12 @@ fun DiffLine(
) )
} }
Row {
Text( Text(
text = line.text.replace( // TODO this replace is a workaround until this issue gets fixed https://github.com/JetBrains/compose-jb/issues/615 text = line.text.replace( // TODO this replace is a workaround until this issue gets fixed https://github.com/JetBrains/compose-jb/issues/615
"\t", "\t",
" " " "
).removeLineEndings(), ).removeLineDelimiters(),
modifier = Modifier modifier = Modifier
.padding(start = 8.dp) .padding(start = 8.dp)
.fillMaxSize(), .fillMaxSize(),
@ -448,6 +454,20 @@ fun DiffLine(
style = MaterialTheme.typography.body2, style = MaterialTheme.typography.body2,
overflow = TextOverflow.Visible, overflow = TextOverflow.Visible,
) )
val lineDelimiter = line.text.lineDelimiter
// Display line delimiter in its own text with a maxLines = 1. This will fix the issue
// where copying a line didn't contain the line ending & also fix the issue where the text line would
// display multiple lines even if there is only a single line with a line delimiter at the end
if(lineDelimiter != null) {
Text(
text = lineDelimiter,
maxLines = 1,
)
}
}
} }
} }