@exercism/codemirror-lang-kotlin 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,42 @@
1
+ ## 1.0.0
2
+
3
+ This is Exercism's first release of the package, published as
4
+ `@exercism/codemirror-lang-kotlin`. It adds completion of the names in scope,
5
+ snippets and standard library functions, and parses a primary constructor that
6
+ starts on the line after the class name.
7
+
8
+ ### New features
9
+
10
+ Completion for the names declared in the file, offered only where each one is
11
+ in scope, through the new `localCompletionSource`.
12
+
13
+ Snippets for functions, classes, `when`, loops, `if` and `try`, exported as
14
+ `snippets`.
15
+
16
+ After a `.`, `kotlinCompletion` offers the functions called on a value and
17
+ leaves out keywords. It offers nothing inside strings or comments.
18
+
19
+ ### Bug fixes
20
+
21
+ A primary constructor may start on the line after the class name.
22
+
23
+ ## 0.1.1 (2026-08-06)
24
+
25
+ The repository link, which npm renders as a link and a browser cannot follow
26
+ with `git+` in front of it.
27
+
28
+ ## 0.1.0 (2026-08-02)
29
+
30
+ ### First release
31
+
32
+ Kotlin language support: a Lezer grammar covering Kotlin 2.2, plus
33
+ highlighting, folding, indentation and completion.
34
+
35
+ CodeMirror has never shipped Kotlin and no Lezer grammar for it existed. What
36
+ stands in for one is the C-family stream mode ported from CodeMirror 5, which
37
+ colours a keyword list, builds no tree, and predates string templates, raw
38
+ strings, labels and annotations.
39
+
40
+ The grammar was developed against 6 589 files from ktor, detekt,
41
+ kotlinx.coroutines, Exposed, kotlinx.serialization and okio, of which 97.33%
42
+ parse with no error node.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 CodinCod
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,149 @@
1
- # Temporary Holding Version
1
+ # @exercism/codemirror-lang-kotlin [![NPM version](https://img.shields.io/npm/v/@exercism/codemirror-lang-kotlin.svg)](https://www.npmjs.org/package/@exercism/codemirror-lang-kotlin)
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ This is Exercism's fork of
4
+ [@codincod/codemirror-lang-kotlin](https://codeberg.org/reeven/codemirror-lang-kotlin),
5
+ written by CodinCod. Exercism uses it for Kotlin in the editor on
6
+ [exercism.org](https://exercism.org). Our changes are on the `main` branch,
7
+ and the rest of this README is upstream's, apart from the sections on
8
+ completion and the package name.
9
+
10
+ The fork adds:
11
+
12
+ - completion for the names declared in the file (functions, classes,
13
+ properties, parameters, locals, loop variables and imports), offered only
14
+ where each name is in scope.
15
+ - snippets for functions, classes, `when`, loops, `if` and `try`.
16
+ - completion for standard library functions. Functions called on their own are
17
+ offered in ordinary positions, and functions called on a value are offered
18
+ after a `.`. Nothing is offered inside strings or comments.
19
+ - parsing of a primary constructor that starts on the line after the class
20
+ name.
21
+
22
+ ---
23
+
24
+ This package implements Kotlin language support for the
25
+ [CodeMirror](https://codemirror.net/) code editor, using a
26
+ [Lezer](https://lezer.codemirror.net/) grammar written for this package.
27
+
28
+ CodeMirror 6 has never shipped Kotlin, and no Lezer grammar for it existed
29
+ before this one. What gets used instead is the C-family stream mode ported from
30
+ CodeMirror 5, which colours a list of keywords and builds no tree, so nothing
31
+ above it can fold a function, indent a `when` or ask what the word under the
32
+ cursor is. It also predates most of the language: string templates, raw
33
+ strings, labels and annotations all come out as plain text.
34
+
35
+ Written in part for [CodinCod](https://codincod.com/), a competitive coding
36
+ platform, where it colours the editor people solve puzzles in.
37
+
38
+ This code is released under an
39
+ [MIT license](https://codeberg.org/reeven/codemirror-lang-kotlin/src/branch/master/LICENSE).
40
+
41
+ ## Install
42
+
43
+ ```bash
44
+ npm install @exercism/codemirror-lang-kotlin
45
+ ```
46
+
47
+ ## Usage
48
+
49
+ ```javascript
50
+ import {EditorView, basicSetup} from "codemirror"
51
+ import {kotlin} from "@exercism/codemirror-lang-kotlin"
52
+
53
+ const view = new EditorView({
54
+ parent: document.body,
55
+ doc: `data class Greeter(val name: String) {
56
+ fun greet(): String = "Hello, $name!"
57
+ }
58
+ `,
59
+ extensions: [basicSetup, kotlin()]
60
+ })
61
+ ```
62
+
63
+ ## Completion
64
+
65
+ `kotlin()` registers two completion sources, and both are exported.
66
+
67
+ - `kotlinCompletion` offers keywords, snippets, common standard library types
68
+ and functions. After a `.` or `?.` it offers only the functions called on a
69
+ value, such as `map` and `joinToString`.
70
+ - `localCompletionSource` offers the names declared in the file. A parameter
71
+ is offered inside its function, a local below the line that declares it, a
72
+ class member anywhere in its class, and a top level declaration anywhere in
73
+ the file. A lambda with no parameters offers `it`.
74
+
75
+ Neither source offers anything inside a string, a comment or a name being
76
+ declared. `snippets` holds the snippets on their own.
77
+
78
+ ## Coverage
79
+
80
+ Kotlin 2.2. Classes, interfaces, objects and companions, data and value
81
+ classes, sealed hierarchies, enums, annotations and their use site targets,
82
+ typealiases, extension functions and properties, generics with variance and
83
+ `where` clauses, delegation, property accessors and delegates, coroutines and
84
+ `suspend`, lambdas and trailing lambdas, callable references, destructuring,
85
+ labels and labelled jumps, `when` with and without a subject, and every way
86
+ Kotlin writes a string, including the multi dollar raw strings 2.2 added.
87
+
88
+ Measured against 6 589 Kotlin files from six open source projects (ktor,
89
+ detekt, kotlinx.coroutines, Exposed, kotlinx.serialization and okio), which is
90
+ 26 MiB of code, of which 97.33% parse with no error node.
91
+
92
+ ## What the grammar cannot say
93
+
94
+ Four things about Kotlin are settled in `src/tokens.ts` rather than in the
95
+ grammar, because no context free rule can decide them.
96
+
97
+ **A newline ends a statement, except when it does not.** Kotlin has no
98
+ semicolons and no statement terminator, so `a\nb` is two statements and `a +\nb`
99
+ is one. The tokenizer emits a zero width `insertSemi` when a line break stands
100
+ between two things that could each begin a statement, and holds it back when
101
+ the next line opens with something that can only continue the one before: an
102
+ operator, a dot, a closing bracket, `as`, `by`, `else`, `finally`, a `catch`
103
+ followed by its parenthesis, a `where` followed by a constraint, or a `get` or
104
+ `set` that turns out to be an accessor rather than a call. That last list is
105
+ the whole difficulty. `set.addAll(x)` at the start of a line is a call to a
106
+ set, and `set(value) { }` under a property is an accessor, and only what
107
+ follows the word tells them apart.
108
+
109
+ **A `<` is a comparison or a type argument list.** `a < b` and `Map<K, V>` are
110
+ the same two characters. When the parser could shift a comparison the tokenizer
111
+ scans ahead over what a type argument list is allowed to contain, up to two
112
+ hundred characters, and only emits the bracket tokens if it reaches a matching
113
+ `>` followed by something that could stand after a type. A `*` inside the
114
+ brackets settles it immediately, because a star projection cannot be anything
115
+ else.
116
+
117
+ **A string is four different languages.** `"a"`, `"""a"""`, `$"a"` and their
118
+ templates disagree about what ends a string, what an escape is, and whether a
119
+ `$` opens a hole. The tokenizer cuts each one into a start, its content and an
120
+ end, and the grammar parses the expressions between the pieces. A raw string is
121
+ read whole, because its content is whatever stands before three quotes.
122
+
123
+ **A block comment nests.** `/* /* */ */` is one comment in Kotlin and two in
124
+ most of its relatives, so the tokenizer counts depth rather than searching for
125
+ the first `*/`.
126
+
127
+ One more is settled in the grammar, and is worth knowing about.
128
+
129
+ **Casing decides what a name is.** After one identifier no amount of lookahead
130
+ separates a type from a variable, so the parser follows the convention every
131
+ Kotlin codebase keeps: `Foo` is read as a type, `FOO_BAR` as a constant, and
132
+ `foo` as an ordinary name. Both readings stay alive where the grammar allows
133
+ either, and the one that fits wins. The trick is
134
+ [@lezer/java](https://github.com/lezer-parser/java)'s.
135
+
136
+ ## Known gaps
137
+
138
+ `when` guards, the `if` that Kotlin 2.1 allows after a branch condition, are
139
+ not parsed. The guard and a branch whose condition is itself an `if` expression
140
+ share a prefix that an LR parser cannot tell apart, and resolving it in the
141
+ guard's favour cost more real files than it fixed.
142
+
143
+ ## Building and testing
144
+
145
+ ```bash
146
+ npm install # installs the dependencies and builds dist/
147
+ npm run build # regenerates the parser and rebuilds dist/
148
+ npm test # builds, then runs the tests
149
+ ```