From 6011b620f9a315a419e7c6d854716c1525fe4bab Mon Sep 17 00:00:00 2001 From: Abdelilah El Aissaoui Date: Fri, 1 Mar 2024 15:29:15 +0100 Subject: [PATCH] Added sample of basic syntax highlight for Kotlin --- .../com/jetpackduba/gitnuro/theme/Color.kt | 12 +- .../com/jetpackduba/gitnuro/ui/diff/Diff.kt | 140 +++++++++++++++++- 2 files changed, 142 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/theme/Color.kt b/src/main/kotlin/com/jetpackduba/gitnuro/theme/Color.kt index 705a50b..076bb59 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/theme/Color.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/theme/Color.kt @@ -23,8 +23,8 @@ val lightTheme = ColorsScheme( dialogOverlay = Color(0xAA000000), normalScrollbar = Color(0xFFCCCCCC), hoverScrollbar = Color(0xFF0070D8), - diffLineAdded = Color(0xFFd7ebd0), - diffLineRemoved = Color(0xFFf0d4d4), + diffLineAdded = Color(0xAAd7ebd0), + diffLineRemoved = Color(0xAAf0d4d4), isLight = true, ) @@ -50,8 +50,8 @@ val darkBlueTheme = ColorsScheme( dialogOverlay = Color(0xAA000000), normalScrollbar = Color(0xFF888888), hoverScrollbar = Color(0xFFCCCCCC), - diffLineAdded = Color(0xFF566f5a), - diffLineRemoved = Color(0xFF6f585e), + diffLineAdded = Color(0xAA566f5a), + diffLineRemoved = Color(0xAA6f585e), isLight = false, ) @@ -76,7 +76,7 @@ val darkGrayTheme = ColorsScheme( dialogOverlay = Color(0xAA000000), normalScrollbar = Color(0xFF888888), hoverScrollbar = Color(0xFFCCCCCC), - diffLineAdded = Color(0xFF5b7059), - diffLineRemoved = Color(0xFF74595c), + diffLineAdded = Color(0xAA5b7059), + diffLineRemoved = Color(0xAA74595c), isLight = false, ) \ No newline at end of file diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/ui/diff/Diff.kt b/src/main/kotlin/com/jetpackduba/gitnuro/ui/diff/Diff.kt index 715e458..fad9352 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/ui/diff/Diff.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/ui/diff/Diff.kt @@ -1136,6 +1136,7 @@ fun SplitDiffLine( @Composable fun DiffLineText(line: Line, diffType: DiffType, onActionTriggered: () -> Unit) { + val fileExtension = diffType.filePath.split(".").lastOrNull() val text = line.text val hoverInteraction = remember { MutableInteractionSource() } val isHovered by hoverInteraction.collectIsHoveredAsState() @@ -1170,10 +1171,7 @@ fun DiffLineText(line: Line, diffType: DiffType, onActionTriggered: () -> Unit) Row { Text( - text = text.replace( - "\t", - " " - ).removeLineDelimiters(), + text = syntaxHighlight(fileExtension, text), modifier = Modifier .padding(start = 16.dp) .fillMaxSize(), @@ -1199,6 +1197,140 @@ fun DiffLineText(line: Line, diffType: DiffType, onActionTriggered: () -> Unit) } } + +fun syntaxHighlight(fileExtension: String?, text: String): AnnotatedString { + val cleanText = text.replace( + "\t", + " " + ).removeLineDelimiters() + + return if (cleanText.trimStart().startsWith("//")) { + AnnotatedString(cleanText, spanStyle = SpanStyle(color = Color(0xFF70C290))) + } else { + val words = cleanText.split(" ") + + val builder = AnnotatedString.Builder() + val keywords = listOf( + "as", + "as?", + "break", + "by", + "catch", + "class", + "constructor", + "continue", + "do", + "dynamic", + "else", + "false", + "finally", + "for", + "fun", + "if", + "import", + "in", + "!in", + "interface", + "is", + "!is", + "null", + "object", + "package", + "return", + "super", + "this", + "throw", + "true", + "try", + "val", + "var", + "when", + "where", + "while", + + // Modifiers + "actual", + "abstract", + "annotation", + "companion", + "const", + "crossinline", + "data", + "enum", + "expect", + "external", + "final", + "infix", + "inline", + "inner", + "internal", + "lateinit", + "noinline", + "open", + "operator", + "out", + "override", + "private", + "protected", + "public", + "reified", + "sealed", + "suspend", + "tailrec", + "vararg", + ) + + fun isAnnotation(word: String): Boolean = word.startsWith("@") + + words.forEachIndexed { index, word -> + if (keywords.contains(word)) { + builder.append( + AnnotatedString( + word, + spanStyle = SpanStyle( +// color = Color(0xFF669ACD) + color = Color(0xFF90c0f0) + ) + ) + ) + } else if (isAnnotation(word)) { + builder.append( + AnnotatedString( + word, + spanStyle = SpanStyle( + color = Color(0xFFB3AE5F) + ) + ) + ) + } else { + builder.append(word) + } + + if (index < words.lastIndex) { + builder.append(" ") + } + } + + builder.toAnnotatedString() + } +} + +class Testtt { + companion object { + @JvmStatic + external fun test1() + } +} +// +//class Pancakes private constructor(val pointer: Long) { +// constructor(message: String, num: Int) : this(initialize(message, num)) +// companion object { +// fun initialize(message: String, num: Int): Long { +// return 0L +// } +// } +//} + @Composable fun LineNumber(text: String, remarked: Boolean) { Text(