Skip to content

Commit

Permalink
add Move syntax highlighting...
Browse files Browse the repository at this point in the history
  • Loading branch information
astinz committed Sep 4, 2023
1 parent 67db72c commit b9d762e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Project(
loadCohesiveProject()
}

createCohesiveProjectListener(parentDir.absolutePath) {
/*createCohesiveProjectListener(parentDir.absolutePath) {
onCreate {
//TODO handle modify file
}
Expand All @@ -42,7 +42,7 @@ class Project(
GlobalScope.launch {
startListening()
}
}
}*/
projectLoaded = true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,32 +198,87 @@ fun codeString(

val punctuationCharacters = listOf(":", "=", "\"", "[", "]", "{", "}", "(", ")", ",")
val keywords = listOf(
"fun", "val", "var", "private", "internal", "for",
"expect", "actual", "import", "package",
"script",
"module",
"self",
"const",
"native",
"public",
"public(friend)",
"internal",
"entry",
"fun",
"has",
"drop",
"key",
"store",
"acquires",
"struct",
"use",
"as",
"mut",
"copy",
"move",
"return",
"abort",
"break",
"continue",
"if",
"else",
"loop",
"while",
"let",
"phantom",
"spec",
)


// Identify and store the ranges of all comments
val commentRanges = mutableListOf<IntRange>()
for (result in """^\s*//.*""".toRegex(RegexOption.MULTILINE).findAll(strFormatted)) {
commentRanges.add(result.range)
addStyle(theme.code.comment, result.range.first, result.range.last + 1) // Style the comment immediately
}

for (punctuation in punctuationCharacters) {
addStyle(theme.code.punctuation, strFormatted, punctuation)
val matches = Regex.escape(punctuation).toRegex().findAll(strFormatted)
for (match in matches) {
if (!isInCommentRange(match.range.first, commentRanges)) {
addStyle(theme.code.punctuation, match.range.first, match.range.last + 1)
}
}
}

for (keyword in keywords) {
addStyle(theme.code.keyword, strFormatted, keyword)
val matches = """\b$keyword\b""".toRegex().findAll(strFormatted)
for (match in matches) {
if (!isInCommentRange(match.range.first, commentRanges)) {
addStyle(theme.code.keyword, match.range.first, match.range.last + 1)
}
}
}

addStyle(theme.code.value, strFormatted, "true")
addStyle(theme.code.value, strFormatted, "false")
addStyle(theme.code.value, strFormatted, Regex("[0-9]*"))
addStyle(theme.code.annotation, strFormatted, Regex("^@[a-zA-Z_]*"))
addStyle(theme.code.comment, strFormatted, Regex("^\\s*//.*"))
addStyle(theme.code.value, strFormatted, """\btrue\b""")
addStyle(theme.code.value, strFormatted, """\bfalse\b""")
addStyle(theme.code.value, strFormatted, """\b[0-9]+\b""")
addStyle(theme.code.annotation, strFormatted, """\b@[a-zA-Z_]+\b""")
}
}

private fun isInCommentRange(index: Int, commentRanges: List<IntRange>): Boolean {
for (range in commentRanges) {
if (range.contains(index)) {
return true
}
}
return false
}

private fun AnnotatedString.Builder.addStyle(
style: SpanStyle,
text: String,
regexp: String,
) {
addStyle(style, text, Regex.fromLiteral(regexp))
addStyle(style, text, Regex(regexp))
}

private fun AnnotatedString.Builder.addStyle(
Expand All @@ -236,6 +291,7 @@ private fun AnnotatedString.Builder.addStyle(
}
}


@Composable
fun EditorEmpty(
text: String = "No file opened",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fun java.io.File.toProjectFile(): File =
return object : TextLines {
override val size
get() = size
override var isCode: Boolean = name.endsWith(".kt", ignoreCase = true)
override var isCode: Boolean = name.endsWith(".move", ignoreCase = true)
override val text: State<String>
get() {
return if (byteBuffer.hasArray()) {
Expand Down
2 changes: 1 addition & 1 deletion cmpe/csp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ sourceSets.main { java.srcDirs("src/main/kotlin") }
publishing {
publications {
create<MavenPublication>("maven") {
groupId = "com.mcxross.cohesive"
groupId = "xyz.mcxross.cohesive"
artifactId = "cohesive-csp"
from(components["java"])
}
Expand Down

0 comments on commit b9d762e

Please sign in to comment.