From 9c1133a292e9326c56b77fb5a4f9ba622ca853e8 Mon Sep 17 00:00:00 2001 From: Abdelilah El Aissaoui Date: Sat, 28 May 2022 16:28:39 +0200 Subject: [PATCH] Added line numbers and text selection to blame --- .../kotlin/app/extensions/IntExtensions.kt | 18 +++ src/main/kotlin/app/ui/Blame.kt | 109 ++++++++++++------ src/main/kotlin/app/ui/Diff.kt | 26 +---- 3 files changed, 93 insertions(+), 60 deletions(-) create mode 100644 src/main/kotlin/app/extensions/IntExtensions.kt diff --git a/src/main/kotlin/app/extensions/IntExtensions.kt b/src/main/kotlin/app/extensions/IntExtensions.kt new file mode 100644 index 0000000..2958e84 --- /dev/null +++ b/src/main/kotlin/app/extensions/IntExtensions.kt @@ -0,0 +1,18 @@ +package app.extensions + +fun Int.toStringWithSpaces(charactersCount: Int): String { + val numberStr = this.toString() + return if (numberStr.count() == charactersCount) + numberStr + else { + val lengthDiff = charactersCount - numberStr.count() + val numberBuilder = StringBuilder() + // Add whitespaces before the numbers + repeat(lengthDiff) { + numberBuilder.append(" ") + } + numberBuilder.append(numberStr) + + numberBuilder.toString() + } +} \ No newline at end of file diff --git a/src/main/kotlin/app/ui/Blame.kt b/src/main/kotlin/app/ui/Blame.kt index d0b87a0..7a92187 100644 --- a/src/main/kotlin/app/ui/Blame.kt +++ b/src/main/kotlin/app/ui/Blame.kt @@ -5,6 +5,8 @@ package app.ui import androidx.compose.foundation.Image import androidx.compose.foundation.background import androidx.compose.foundation.layout.* +import androidx.compose.foundation.text.selection.DisableSelection +import androidx.compose.foundation.text.selection.SelectionContainer import androidx.compose.material.IconButton import androidx.compose.material.MaterialTheme import androidx.compose.material.Text @@ -21,6 +23,7 @@ import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import app.extensions.handMouseClickable import app.extensions.lineAt +import app.extensions.toStringWithSpaces import app.theme.primaryTextColor import app.ui.components.PrimaryButton import app.ui.components.ScrollableLazyColumn @@ -36,58 +39,88 @@ fun Blame( ) { Column { Header(filePath, onClose = onClose) + SelectionContainer { + ScrollableLazyColumn( + modifier = Modifier.fillMaxSize() + ) { + val contents = blameResult.resultContents + val contentsSize = contents.size() + val contentSizeLengthInString = contentsSize.toString().count() - ScrollableLazyColumn( - modifier = Modifier.fillMaxSize() - ) { - val contents = blameResult.resultContents - items(contents.size()) { index -> - val line = contents.lineAt(index) - val author = blameResult.getSourceAuthor(index) - val commit = blameResult.getSourceCommit(index) + items(contentsSize) { index -> + val line = contents.lineAt(index) + val author = blameResult.getSourceAuthor(index) + val commit = blameResult.getSourceCommit(index) - Row( - modifier = Modifier.fillMaxWidth().background(MaterialTheme.colors.background) - .height(IntrinsicSize.Min), - verticalAlignment = Alignment.CenterVertically, - ) { - Column( - modifier = Modifier - .width(200.dp) - .fillMaxHeight() - .background(MaterialTheme.colors.surface) - .handMouseClickable { onSelectCommit(commit) }, - verticalArrangement = Arrangement.Center, + Row( + modifier = Modifier.fillMaxWidth() + .background(MaterialTheme.colors.background) + .height(IntrinsicSize.Min), + verticalAlignment = Alignment.CenterVertically, ) { + + DisableSelection { + LineNumber( + line = index + 1, + highestLineLength = contentSizeLengthInString + ) + + Column( + modifier = Modifier + .width(200.dp) + .fillMaxHeight() + .background(MaterialTheme.colors.surface) + .handMouseClickable { if (commit != null) onSelectCommit(commit) }, + verticalArrangement = Arrangement.Center, + ) { + Text( + text = author?.name.orEmpty(), + color = MaterialTheme.colors.primaryTextColor, + maxLines = 1, + modifier = Modifier.padding(start = 16.dp), + fontSize = 12.sp, + ) + Text( + text = commit?.shortMessage ?: "Uncommited change", + color = MaterialTheme.colors.primaryTextColor, + maxLines = 1, + modifier = Modifier.padding(start = 16.dp), + fontSize = 10.sp, + ) + } + } + Text( - text = author?.name.orEmpty(), + text = line + blameResult.resultContents.lineDelimiter, color = MaterialTheme.colors.primaryTextColor, + modifier = Modifier.padding(vertical = 8.dp, horizontal = 16.dp), + fontFamily = FontFamily.Monospace, maxLines = 1, - modifier = Modifier.padding(start = 16.dp), - fontSize = 12.sp, - ) - Text( - text = commit.shortMessage, - color = MaterialTheme.colors.primaryTextColor, - maxLines = 1, - modifier = Modifier.padding(start = 16.dp), - fontSize = 10.sp, ) } - - Text( - text = line, - color = MaterialTheme.colors.primaryTextColor, - modifier = Modifier.padding(vertical = 8.dp, horizontal = 16.dp), - fontFamily = FontFamily.Monospace, - maxLines = 1, - ) } } } } } +@Composable +private fun LineNumber(line: Int, highestLineLength: Int) { + Box( + modifier = Modifier + .fillMaxHeight() + .background(MaterialTheme.colors.surface) + .padding(start = 4.dp, end = 4.dp), + contentAlignment = Alignment.Center + ) { + Text( + text = line.toStringWithSpaces(highestLineLength), + color = MaterialTheme.colors.primaryTextColor, + fontFamily = FontFamily.Monospace, + ) + } +} + @Composable fun MinimizedBlame( filePath: String, diff --git a/src/main/kotlin/app/ui/Diff.kt b/src/main/kotlin/app/ui/Diff.kt index ce477e3..aa04ecc 100644 --- a/src/main/kotlin/app/ui/Diff.kt +++ b/src/main/kotlin/app/ui/Diff.kt @@ -28,6 +28,7 @@ import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp +import app.extensions.toStringWithSpaces import app.git.DiffEntryType import app.git.EntryContent import app.git.StatusEntry @@ -364,12 +365,12 @@ fun DiffLine(highestLineNumberLength: Int, line: Line) { .height(IntrinsicSize.Min) ) { val oldLineText = if (line.lineType == LineType.REMOVED || line.lineType == LineType.CONTEXT) { - formattedLineNumber(line.displayOldLineNumber, highestLineNumberLength) + line.displayOldLineNumber.toStringWithSpaces(highestLineNumberLength) } else emptyLineNumber(highestLineNumberLength) val newLineText = if (line.lineType == LineType.ADDED || line.lineType == LineType.CONTEXT) { - formattedLineNumber(line.displayNewLineNumber, highestLineNumberLength) + line.displayNewLineNumber.toStringWithSpaces(highestLineNumberLength) } else emptyLineNumber(highestLineNumberLength) @@ -411,24 +412,6 @@ fun LineNumber(text: String) { ) } - -fun formattedLineNumber(number: Int, charactersCount: Int): String { - val numberStr = number.toString() - return if (numberStr.count() == charactersCount) - numberStr - else { - val lengthDiff = charactersCount - numberStr.count() - val numberBuilder = StringBuilder() - // Add whitespaces before the numbers - repeat(lengthDiff) { - numberBuilder.append(" ") - } - numberBuilder.append(numberStr) - - numberBuilder.toString() - } -} - fun emptyLineNumber(charactersCount: Int): String { val numberBuilder = StringBuilder() // Add whitespaces before the numbers @@ -437,5 +420,4 @@ fun emptyLineNumber(charactersCount: Int): String { } return numberBuilder.toString() -} - +} \ No newline at end of file