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
@@ -160,6 +160,7 @@ private val FENCE_LANGUAGES: Map<String, Language> =
|
||||
"shell" to Language.SHELL,
|
||||
"zsh" to Language.SHELL,
|
||||
"console" to Language.SHELL,
|
||||
"diff" to Language.DIFF,
|
||||
"python" to Language.PYTHON,
|
||||
"py" to Language.PYTHON,
|
||||
"javascript" to Language.JAVASCRIPT,
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
@@ -16,6 +16,7 @@ enum class Language {
|
||||
CPP,
|
||||
CSHARP,
|
||||
DART,
|
||||
DIFF,
|
||||
FISH,
|
||||
GO,
|
||||
JAVA,
|
||||
@@ -100,7 +101,7 @@ fun spansOf(code: String, language: Language): List<Span> = SCANNERS.getValue(la
|
||||
// Lazy for the same reason [RULES] is, since it reads it.
|
||||
private val SCANNERS: Map<Language, (String) -> List<Span>> by lazy {
|
||||
RULES.mapValues { (_, rules) -> { code: String -> scan(code, rules) } } +
|
||||
mapOf(Language.MARKDOWN to ::scanMarkdown)
|
||||
mapOf(Language.DIFF to ::scanDiff, Language.MARKDOWN to ::scanMarkdown)
|
||||
}
|
||||
|
||||
private val C_STYLE = BlockComment("/*", "*/", nests = false)
|
||||
|
||||
@@ -213,6 +213,8 @@ val rawSurface: Color
|
||||
*/
|
||||
fun catppuccinSyntax(): SyntaxPalette =
|
||||
SyntaxPalette(
|
||||
addition = Mocha.Green,
|
||||
deletion = Mocha.Red,
|
||||
keyword = Mocha.Mauve,
|
||||
string = Mocha.Green,
|
||||
literal = Mocha.Peach,
|
||||
|
||||
@@ -51,6 +51,8 @@ data class ToolInput(
|
||||
private val SUBJECTS: Map<String, Pair<String, Language?>> =
|
||||
mapOf(
|
||||
"Bash" to ("command" to Language.SHELL),
|
||||
"Shell" to ("command" to Language.SHELL),
|
||||
"Patch" to ("diff" to Language.DIFF),
|
||||
"Read" to ("file_path" to null),
|
||||
"Write" to ("file_path" to null),
|
||||
"Edit" to ("file_path" to null),
|
||||
|
||||
Reference in new issue
Block a user