Normalize shell and patch tool cards
This commit is contained in:
1 parent
4dc3e3d784
commit
b507656abd
11 files changed
+327
-16
No files matched your search
@@ -7,6 +7,8 @@ import androidx.compose.ui.text.buildAnnotatedString
|
||||
|
||||
/** What a span of code is, in the terms the palette has a colour for. */
|
||||
enum class Kind {
|
||||
ADDITION,
|
||||
DELETION,
|
||||
KEYWORD,
|
||||
STRING,
|
||||
LITERAL,
|
||||
@@ -24,6 +26,8 @@ data class Span(val start: Int, val end: Int, val kind: Kind)
|
||||
* one instance and lives with the rest of the palette.
|
||||
*/
|
||||
data class SyntaxPalette(
|
||||
val addition: Color,
|
||||
val deletion: Color,
|
||||
val keyword: Color,
|
||||
val string: Color,
|
||||
val literal: Color,
|
||||
@@ -34,6 +38,8 @@ data class SyntaxPalette(
|
||||
) {
|
||||
fun of(kind: Kind): Color =
|
||||
when (kind) {
|
||||
Kind.ADDITION -> addition
|
||||
Kind.DELETION -> deletion
|
||||
Kind.KEYWORD -> keyword
|
||||
Kind.STRING -> string
|
||||
Kind.LITERAL -> literal
|
||||
@@ -44,6 +50,26 @@ data class SyntaxPalette(
|
||||
}
|
||||
}
|
||||
|
||||
/** A unified diff is line-oriented: colour the changed lines and leave context untouched. */
|
||||
fun scanDiff(code: String): List<Span> {
|
||||
val spans = ArrayList<Span>()
|
||||
var start = 0
|
||||
while (start < code.length) {
|
||||
val end = code.indexOf('\n', start).let { if (it == -1) code.length else it }
|
||||
val kind =
|
||||
when {
|
||||
code.startsWith("+++", start) || code.startsWith("---", start) -> Kind.METADATA
|
||||
code.startsWith("+", start) -> Kind.ADDITION
|
||||
code.startsWith("-", start) -> Kind.DELETION
|
||||
code.startsWith("@@", start) -> Kind.METADATA
|
||||
else -> null
|
||||
}
|
||||
if (kind != null) spans.add(Span(start, end, kind))
|
||||
start = if (end == code.length) end else end + 1
|
||||
}
|
||||
return spans
|
||||
}
|
||||
|
||||
/**
|
||||
* [code] with its keywords, strings and comments coloured, or plain if there is no language for it.
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user