Added sample of basic syntax highlight for Kotlin

This commit is contained in:
Abdelilah El Aissaoui 2024-03-01 15:29:15 +01:00
parent d97ac18676
commit 6011b620f9
No known key found for this signature in database
GPG Key ID: 7587FC860F594869
2 changed files with 142 additions and 10 deletions

View File

@ -23,8 +23,8 @@ val lightTheme = ColorsScheme(
dialogOverlay = Color(0xAA000000), dialogOverlay = Color(0xAA000000),
normalScrollbar = Color(0xFFCCCCCC), normalScrollbar = Color(0xFFCCCCCC),
hoverScrollbar = Color(0xFF0070D8), hoverScrollbar = Color(0xFF0070D8),
diffLineAdded = Color(0xFFd7ebd0), diffLineAdded = Color(0xAAd7ebd0),
diffLineRemoved = Color(0xFFf0d4d4), diffLineRemoved = Color(0xAAf0d4d4),
isLight = true, isLight = true,
) )
@ -50,8 +50,8 @@ val darkBlueTheme = ColorsScheme(
dialogOverlay = Color(0xAA000000), dialogOverlay = Color(0xAA000000),
normalScrollbar = Color(0xFF888888), normalScrollbar = Color(0xFF888888),
hoverScrollbar = Color(0xFFCCCCCC), hoverScrollbar = Color(0xFFCCCCCC),
diffLineAdded = Color(0xFF566f5a), diffLineAdded = Color(0xAA566f5a),
diffLineRemoved = Color(0xFF6f585e), diffLineRemoved = Color(0xAA6f585e),
isLight = false, isLight = false,
) )
@ -76,7 +76,7 @@ val darkGrayTheme = ColorsScheme(
dialogOverlay = Color(0xAA000000), dialogOverlay = Color(0xAA000000),
normalScrollbar = Color(0xFF888888), normalScrollbar = Color(0xFF888888),
hoverScrollbar = Color(0xFFCCCCCC), hoverScrollbar = Color(0xFFCCCCCC),
diffLineAdded = Color(0xFF5b7059), diffLineAdded = Color(0xAA5b7059),
diffLineRemoved = Color(0xFF74595c), diffLineRemoved = Color(0xAA74595c),
isLight = false, isLight = false,
) )

View File

@ -1136,6 +1136,7 @@ fun SplitDiffLine(
@Composable @Composable
fun DiffLineText(line: Line, diffType: DiffType, onActionTriggered: () -> Unit) { fun DiffLineText(line: Line, diffType: DiffType, onActionTriggered: () -> Unit) {
val fileExtension = diffType.filePath.split(".").lastOrNull()
val text = line.text val text = line.text
val hoverInteraction = remember { MutableInteractionSource() } val hoverInteraction = remember { MutableInteractionSource() }
val isHovered by hoverInteraction.collectIsHoveredAsState() val isHovered by hoverInteraction.collectIsHoveredAsState()
@ -1170,10 +1171,7 @@ fun DiffLineText(line: Line, diffType: DiffType, onActionTriggered: () -> Unit)
Row { Row {
Text( Text(
text = text.replace( text = syntaxHighlight(fileExtension, text),
"\t",
" "
).removeLineDelimiters(),
modifier = Modifier modifier = Modifier
.padding(start = 16.dp) .padding(start = 16.dp)
.fillMaxSize(), .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 @Composable
fun LineNumber(text: String, remarked: Boolean) { fun LineNumber(text: String, remarked: Boolean) {
Text( Text(