Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat(comments): add Kotlin support (#1271)

Elijah Pottera4721f0886c0
diff --git a/Cargo.lock b/Cargo.lock
index 0937618db..ecb46ce3b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -842,6 +842,7 @@ dependencies = [
"tree-sitter-haskell",
"tree-sitter-java",
"tree-sitter-javascript",
+ "tree-sitter-kotlin",
"tree-sitter-lua",
"tree-sitter-nix",
"tree-sitter-php",
@@ -2800,6 +2801,16 @@ dependencies = [
"tree-sitter",
]
+[[package]]
+name = "tree-sitter-kotlin"
+version = "0.3.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8df217a0e1fec649f3e13157de932439f3d37ea4e265038dd0873971ef56e726"
+dependencies = [
+ "cc",
+ "tree-sitter",
+]
+
[[package]]
name = "tree-sitter-lua"
version = "0.0.19"
diff --git a/harper-comments/Cargo.toml b/harper-comments/Cargo.toml
index 7eed50c97..e9dac83e2 100644
--- a/harper-comments/Cargo.toml
+++ b/harper-comments/Cargo.toml
@@ -28,11 +28,12 @@ tree-sitter-lua = "0.0.19"
tree-sitter-bash = "0.20.0"
tree-sitter-java = "0.20.0"
tree-sitter-nix = "0.0.1"
-itertools = "0.14.0"
tree-sitter-haskell = "0.15.0"
tree-sitter-php = "=0.22.2"
tree-sitter-dart = "0.0.4"
tree-sitter-scala = "0.20.0"
+tree-sitter-kotlin = "=0.3.5"
+itertools = "0.14.0"
[dev-dependencies]
paste = "1.0.15"
diff --git a/harper-comments/src/comment_parser.rs b/harper-comments/src/comment_parser.rs
index cd832c251..06e2c00eb 100644
--- a/harper-comments/src/comment_parser.rs
+++ b/harper-comments/src/comment_parser.rs
@@ -44,6 +44,7 @@ impl CommentParser {
"php" => tree_sitter_php::language_php(),
"dart" => tree_sitter_dart::language(),
"scala" => tree_sitter_scala::language(),
+ "kotlin" => tree_sitter_kotlin::language(),
_ => return None,
};
@@ -100,6 +101,7 @@ impl CommentParser {
"php" => "php",
"dart" => "dart",
"scala" | "sbt" | "mill" => "scala",
+ "kt" | "kts" => "kotlin",
_ => return None,
})
}
diff --git a/harper-comments/tests/language_support.rs b/harper-comments/tests/language_support.rs
index 6cdfccbf1..c670ffb2f 100644
--- a/harper-comments/tests/language_support.rs
+++ b/harper-comments/tests/language_support.rs
@@ -55,6 +55,7 @@ create_test!(ignore_shebang_2.sh, 0);
create_test!(ignore_shebang_3.sh, 0);
create_test!(ignore_shebang_4.sh, 1);
create_test!(common.mill, 1);
+create_test!(basic_kotlin.kt, 0);
// Checks that some comments are masked out
create_test!(ignore_comments.rs, 1);
diff --git a/harper-comments/tests/language_support_sources/basic_kotlin.kt b/harper-comments/tests/language_support_sources/basic_kotlin.kt
new file mode 100644
index 000000000..a5fb5db63
--- /dev/null
+++ b/harper-comments/tests/language_support_sources/basic_kotlin.kt
@@ -0,0 +1,130 @@
+// *************************************************************************************************
+// File: RogueScheduler.kt
+//
+// A diminutive but fully-formed demonstration of idiomatic Kotlin.
+//
+// 1. Defines a sealed algebraic hierarchy to represent the discrete states of a task
+// transmogrifying through a rudimentary scheduler.
+// 2. Employs a type-safe builder DSL to assemble a cohort of Task objects succinctly.
+// 3. Utilises coroutines and the structured-concurrency discipline to execute tasks
+// concomitantly while preserving deterministic shutdown semantics.
+// *************************************************************************************************
+
+// ---------- Domain model -------------------------------------------------------------------------
+
+/** Immutable value holder representing a unit of executable labor. */
+data class Task(
+ val id: Int,
+ val description: String,
+ val action: suspend () -> Unit,
+)
+
+/** Exhaustive taxonomy of execution outcomes; the `sealed` modifier ensures compiler‐enforced totality. */
+sealed interface TaskResult {
+ /** Successful completion carrying an optional payload. */
+ data class Success(val id: Int, val elapsedMillis: Long) : TaskResult
+ /** Recoverable misadventure accompanied by the causal `Throwable`. */
+ data class Failure(val id: Int, val cause: Throwable) : TaskResult
+ /** Voluntary cessation initiated by the caller before execution. */
+ data class Cancelled(val id: Int) : TaskResult
+}
+
+// ---------- DSL for declarative task construction -----------------------------------------------
+
+/** Fluent builder furnishing a terse, expressive syntax for batch task definition. */
+@DslMarker
+annotation class TaskDsl
+
+@TaskDsl
+class TaskBatchBuilder {
+ private val tasks = mutableListOf<Task>()
+
+ /** Registers a new task whose body is expressed as a suspending lambda. */
+ fun task(description: String, block: suspend () -> Unit) {
+ tasks += Task(tasks.size + 1, description, block)
+ }
+
+ internal fun build(): List<Task> = tasks.toList()
+}
+
+/** Conveniences the client with type inference and inline lambda to craft a task batch. */
+fun taskBatch(init: TaskBatchBuilder.() -> Unit): List<Task> =
+ TaskBatchBuilder().apply(init).build()
+
+// ---------- Scheduler implementation -------------------------------------------------------------
+
+import kotlinx.coroutines.*
+import kotlin.system.*
+
+/** Executes all tasks concurrently, returning a conglomerate of `TaskResult` artifacts. */
+suspend fun runTasks(tasks: List<Task>): List<TaskResult> = coroutineScope {
+ val startEpoch = System.currentTimeMillis()
+
+ // Launch each task within its own child coroutine; Deferred encapsulates the eventual result.
+ val futures: List<Deferred<TaskResult>> = tasks.map { task ->
+ async {
+ val elapsed = measureTimeMillis {
+ try {
+ task.action()
+ } catch (t: CancellationException) {
+ // Propagate structured-concurrency cancellation upward; annotate as `Cancelled`.
+ return@async TaskResult.Cancelled(task.id)
+ } catch (t: Throwable) {
+ // Swallow domain-level exception, encapsulate in Failure result.
+ return@async TaskResult.Failure(task.id, t)
+ }
+ }
+ // If the lambda returns normally, the endeavor is deemed triumphant.
+ TaskResult.Success(task.id, elapsed)
+ }
+ }
+
+ // Await completion of the entire cohort, preserving result order by task identifier.
+ futures.awaitAll().sortedBy { result ->
+ when (result) {
+ is TaskResult.Success -> result.id
+ is TaskResult.Failure -> result.id
+ is TaskResult.Cancelled -> result.id
+ }
+ }
+}
+
+// ---------- Demonstration entry-point ------------------------------------------------------------
+
+fun main() = runBlocking {
+ // Compose an eclectic suite of tasks via the DSL.
+ val tasks = taskBatch {
+ task("Inconsequential delay") {
+ delay(250)
+ println("Task A executed on thread ${Thread.currentThread().name}")
+ }
+ task("Spurious exception") {
+ delay(100)
+ error("Intentional kaboom")
+ }
+ task("CPU-bound Fibonacci") {
+ val n = 25
+ val fib = generateSequence(0 to 1) { it.second to it.first + it.second }
+ .take(n + 1).last().first
+ println("fib($n) = $fib")
+ }
+ }
+
+ println("Launching ${tasks.size} tasks concurrently…\n")
+
+ // Drive the scheduler and acquire the final ledger.
+ val ledger = runTasks(tasks)
+
+ // Expository epilogue.
+ println("\n────────── Execution Ledger ──────────")
+ ledger.forEach { result ->
+ when (result) {
+ is TaskResult.Success ->
+ println("✔︎ Task ${result.id} succeeded in ${result.elapsedMillis} ms")
+ is TaskResult.Failure ->
+ println("✘ Task ${result.id} failed with ${result.cause::class.simpleName}: ${result.cause.message}")
+ is TaskResult.Cancelled ->
+ println("⚑ Task ${result.id} was cancelled before commencement")
+ }
+ }
+}
diff --git a/harper-core/dictionary.dict b/harper-core/dictionary.dict
index 16d983e9c..7ade35ca4 100644
--- a/harper-core/dictionary.dict
+++ b/harper-core/dictionary.dict
@@ -19080,6 +19080,7 @@ coronation/~1SM
coronavirus/~1MS
coroner/~1MS
coronet/~1MS
+coroutine/~1MS
corp/~1
corpora/9
corporal/~51SM
diff --git a/packages/vscode-plugin/package.json b/packages/vscode-plugin/package.json
index 0f0e480c4..87d4cffd4 100644
--- a/packages/vscode-plugin/package.json
+++ b/packages/vscode-plugin/package.json
@@ -37,6 +37,7 @@
"onLanguage:java",
"onLanguage:javascript",
"onLanguage:javascriptreact",
+ "onLanguage:kotlin",
"onLanguage:literate haskell",
"onLanguage:lua",
"onLanguage:markdown",
diff --git a/packages/web/src/routes/docs/integrations/language-server/+page.md b/packages/web/src/routes/docs/integrations/language-server/+page.md
index b72d3fa6f..b2cb314e7 100644
--- a/packages/web/src/routes/docs/integrations/language-server/+page.md
+++ b/packages/web/src/routes/docs/integrations/language-server/+page.md
@@ -286,6 +286,7 @@ These configs are under the `markdown` key:
| TypeScript | `typescript` | ✅ |
| TypeScript React | `typescriptreact` | ✅ |
| Typst | `typst` | |
+| Kotlin | `kotlin` | ✅ |
Want your language added?
Let us know by [commenting on this issue](https://github.com/Automattic/harper/issues/79).