Skip to content

Commit

Permalink
Refactored syntax highlight to support multiple languages
Browse files Browse the repository at this point in the history
  • Loading branch information
JetpackDuba committed Jul 15, 2024
1 parent 6011b62 commit 284597c
Show file tree
Hide file tree
Showing 9 changed files with 304 additions and 135 deletions.
9 changes: 9 additions & 0 deletions src/main/kotlin/com/jetpackduba/gitnuro/theme/Color.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ val lightTheme = ColorsScheme(
hoverScrollbar = Color(0xFF0070D8),
diffLineAdded = Color(0xAAd7ebd0),
diffLineRemoved = Color(0xAAf0d4d4),
diffKeyword = Color(0xff3b83cf),
diffAnnotation = Color(0xff9f9927),
diffComment = Color(0xff0d9141),
isLight = true,
)

Expand Down Expand Up @@ -52,6 +55,9 @@ val darkBlueTheme = ColorsScheme(
hoverScrollbar = Color(0xFFCCCCCC),
diffLineAdded = Color(0xAA566f5a),
diffLineRemoved = Color(0xAA6f585e),
diffKeyword = Color(0xFF90c0f0),
diffAnnotation = Color(0xFFB3AE5F),
diffComment = Color(0xFF70C290),
isLight = false,
)

Expand All @@ -78,5 +84,8 @@ val darkGrayTheme = ColorsScheme(
hoverScrollbar = Color(0xFFCCCCCC),
diffLineAdded = Color(0xAA5b7059),
diffLineRemoved = Color(0xAA74595c),
diffKeyword = Color(0xFF90c0f0),
diffAnnotation = Color(0xFFB3AE5F),
diffComment = Color(0xFF70C290),
isLight = false,
)
3 changes: 3 additions & 0 deletions src/main/kotlin/com/jetpackduba/gitnuro/theme/ColorsScheme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ data class ColorsScheme(
val hoverScrollbar: Color,
val diffLineAdded: Color,
val diffLineRemoved: Color,
val diffKeyword: Color,
val diffAnnotation: Color,
val diffComment: Color,
val isLight: Boolean,
) {
fun toComposeColors(): Colors {
Expand Down
9 changes: 9 additions & 0 deletions src/main/kotlin/com/jetpackduba/gitnuro/theme/Theme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ val Colors.diffLineAdded: Color
val Colors.diffLineRemoved: Color
get() = appTheme.diffLineRemoved

val Colors.diffKeyword: Color
get() = appTheme.diffKeyword

val Colors.diffAnnotation: Color
get() = appTheme.diffAnnotation

val Colors.diffComment: Color
get() = appTheme.diffComment

val Colors.isDark: Boolean
get() = !this.isLight

Expand Down
139 changes: 4 additions & 135 deletions src/main/kotlin/com/jetpackduba/gitnuro/ui/diff/Diff.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import com.jetpackduba.gitnuro.ui.components.tooltip.DelayedTooltip
import com.jetpackduba.gitnuro.ui.context_menu.ContextMenu
import com.jetpackduba.gitnuro.ui.context_menu.ContextMenuElement
import com.jetpackduba.gitnuro.ui.context_menu.SelectionAwareTextContextMenu
import com.jetpackduba.gitnuro.ui.diff.syntax_highlighter.getSyntaxHighlighterFromExtension
import com.jetpackduba.gitnuro.viewmodels.DiffViewModel
import com.jetpackduba.gitnuro.viewmodels.TextDiffType
import com.jetpackduba.gitnuro.viewmodels.ViewDiffResult
Expand Down Expand Up @@ -1137,6 +1138,8 @@ fun SplitDiffLine(
@Composable
fun DiffLineText(line: Line, diffType: DiffType, onActionTriggered: () -> Unit) {
val fileExtension = diffType.filePath.split(".").lastOrNull()
val syntaxHighlighter = getSyntaxHighlighterFromExtension(fileExtension)

val text = line.text
val hoverInteraction = remember { MutableInteractionSource() }
val isHovered by hoverInteraction.collectIsHoveredAsState()
Expand Down Expand Up @@ -1171,7 +1174,7 @@ fun DiffLineText(line: Line, diffType: DiffType, onActionTriggered: () -> Unit)

Row {
Text(
text = syntaxHighlight(fileExtension, text),
text = syntaxHighlighter.syntaxHighlight(text),
modifier = Modifier
.padding(start = 16.dp)
.fillMaxSize(),
Expand All @@ -1197,140 +1200,6 @@ 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(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.jetpackduba.gitnuro.ui.diff.syntax_highlighter

class DefaultSyntaxHighlighter : SyntaxHighlighter() {
override fun loadKeywords(): List<String> = emptyList()
override fun isAnnotation(word: String): Boolean = false
override fun isComment(line: String): Boolean = false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.jetpackduba.gitnuro.ui.diff.syntax_highlighter

class KotlinSyntaxHighlighter : SyntaxHighlighter() {
override fun loadKeywords(): List<String> = 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",
"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",
)

override fun isAnnotation(word: String): Boolean = word.startsWith("@")
override fun isComment(line: String): Boolean = line.startsWith("//")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.jetpackduba.gitnuro.ui.diff.syntax_highlighter

class RustSyntaxHighlighter : SyntaxHighlighter() {
override fun loadKeywords(): List<String> = listOf(
"as",
"async",
"await",
"break",
"const",
"continue",
"crate",
"dyn",
"else",
"enum",
"extern",
"false",
"fn",
"for",
"if",
"impl",
"in",
"let",
"loop",
"match",
"mod",
"move",
"mut",
"pub",
"ref",
"return",
"Self",
"self",
"static",
"struct",
"super",
"trait",
"true",
"type",
"union",
"unsafe",
"use",
"where",
"while",
)

override fun isAnnotation(word: String): Boolean = word.startsWith("#[") && word.endsWith("]")

override fun isComment(line: String): Boolean = line.startsWith("//")
}
Loading

0 comments on commit 284597c

Please sign in to comment.