@exercism/codemirror-lang-kotlin 0.0.0-stage → 1.0.1
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 +46 -0
- package/LICENSE +21 -0
- package/README.md +131 -2
- package/dist/index.cjs +1074 -0
- package/dist/index.d.cts +31 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +1067 -0
- package/package.json +56 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
## 1.0.1
|
|
2
|
+
|
|
3
|
+
The README is restructured. This release is the first published from GitHub Actions, with npm provenance.
|
|
4
|
+
|
|
5
|
+
## 1.0.0
|
|
6
|
+
|
|
7
|
+
This is Exercism's first release of the package, published as
|
|
8
|
+
`@exercism/codemirror-lang-kotlin`. It adds completion of the names in scope,
|
|
9
|
+
snippets and standard library functions, and parses a primary constructor that
|
|
10
|
+
starts on the line after the class name.
|
|
11
|
+
|
|
12
|
+
### New features
|
|
13
|
+
|
|
14
|
+
Completion for the names declared in the file, offered only where each one is
|
|
15
|
+
in scope, through the new `localCompletionSource`.
|
|
16
|
+
|
|
17
|
+
Snippets for functions, classes, `when`, loops, `if` and `try`, exported as
|
|
18
|
+
`snippets`.
|
|
19
|
+
|
|
20
|
+
After a `.`, `kotlinCompletion` offers the functions called on a value and
|
|
21
|
+
leaves out keywords. It offers nothing inside strings or comments.
|
|
22
|
+
|
|
23
|
+
### Bug fixes
|
|
24
|
+
|
|
25
|
+
A primary constructor may start on the line after the class name.
|
|
26
|
+
|
|
27
|
+
## 0.1.1 (2026-08-06)
|
|
28
|
+
|
|
29
|
+
The repository link, which npm renders as a link and a browser cannot follow
|
|
30
|
+
with `git+` in front of it.
|
|
31
|
+
|
|
32
|
+
## 0.1.0 (2026-08-02)
|
|
33
|
+
|
|
34
|
+
### First release
|
|
35
|
+
|
|
36
|
+
Kotlin language support: a Lezer grammar covering Kotlin 2.2, plus
|
|
37
|
+
highlighting, folding, indentation and completion.
|
|
38
|
+
|
|
39
|
+
CodeMirror has never shipped Kotlin and no Lezer grammar for it existed. What
|
|
40
|
+
stands in for one is the C-family stream mode ported from CodeMirror 5, which
|
|
41
|
+
colours a keyword list, builds no tree, and predates string templates, raw
|
|
42
|
+
strings, labels and annotations.
|
|
43
|
+
|
|
44
|
+
The grammar was developed against 6 589 files from ktor, detekt,
|
|
45
|
+
kotlinx.coroutines, Exposed, kotlinx.serialization and okio, of which 97.33%
|
|
46
|
+
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,132 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @exercism/codemirror-lang-kotlin [](https://www.npmjs.org/package/@exercism/codemirror-lang-kotlin)
|
|
2
2
|
|
|
3
|
-
This
|
|
3
|
+
This package implements Kotlin language support for the
|
|
4
|
+
[CodeMirror](https://codemirror.net/) code editor, using a
|
|
5
|
+
[Lezer](https://lezer.codemirror.net/) grammar written for this package.
|
|
6
|
+
Exercism uses it in the Kotlin editor on [exercism.org](https://exercism.org).
|
|
7
|
+
|
|
8
|
+
CodeMirror 6 has never shipped Kotlin, and no Lezer grammar for it existed
|
|
9
|
+
before this one. What gets used instead is the C-family stream mode ported from
|
|
10
|
+
CodeMirror 5, which colours a list of keywords and builds no tree, so nothing
|
|
11
|
+
above it can fold a function, indent a `when` or ask what the word under the
|
|
12
|
+
cursor is. It also predates most of the language: string templates, raw
|
|
13
|
+
strings, labels and annotations all come out as plain text.
|
|
14
|
+
|
|
15
|
+
This code is released under an [MIT license](LICENSE).
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @exercism/codemirror-lang-kotlin
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
```javascript
|
|
26
|
+
import {EditorView, basicSetup} from "codemirror"
|
|
27
|
+
import {kotlin} from "@exercism/codemirror-lang-kotlin"
|
|
28
|
+
|
|
29
|
+
const view = new EditorView({
|
|
30
|
+
parent: document.body,
|
|
31
|
+
doc: `data class Greeter(val name: String) {
|
|
32
|
+
fun greet(): String = "Hello, $name!"
|
|
33
|
+
}
|
|
34
|
+
`,
|
|
35
|
+
extensions: [basicSetup, kotlin()]
|
|
36
|
+
})
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Completion
|
|
40
|
+
|
|
41
|
+
`kotlin()` registers two completion sources, and both are exported.
|
|
42
|
+
|
|
43
|
+
- `kotlinCompletion` offers keywords, snippets, common standard library types
|
|
44
|
+
and functions. After a `.` or `?.` it offers only the functions called on a
|
|
45
|
+
value, such as `map` and `joinToString`.
|
|
46
|
+
- `localCompletionSource` offers the names declared in the file. A parameter
|
|
47
|
+
is offered inside its function, a local below the line that declares it, a
|
|
48
|
+
class member anywhere in its class, and a top level declaration anywhere in
|
|
49
|
+
the file. A lambda with no parameters offers `it`.
|
|
50
|
+
|
|
51
|
+
Neither source offers anything inside a string, a comment, or where you
|
|
52
|
+
declare a new name. The snippets cover functions, classes, `when`, loops, `if` and
|
|
53
|
+
`try`. The package also exports them on their own as `snippets`.
|
|
54
|
+
|
|
55
|
+
## Coverage
|
|
56
|
+
|
|
57
|
+
Kotlin 2.2. Classes, interfaces, objects and companions, data and value
|
|
58
|
+
classes, sealed hierarchies, enums, annotations and their use site targets,
|
|
59
|
+
typealiases, extension functions and properties, generics with variance and
|
|
60
|
+
`where` clauses, delegation, property accessors and delegates, coroutines and
|
|
61
|
+
`suspend`, lambdas and trailing lambdas, callable references, destructuring,
|
|
62
|
+
labels and labelled jumps, `when` with and without a subject, and every way
|
|
63
|
+
Kotlin writes a string, including the multi dollar raw strings 2.2 added.
|
|
64
|
+
A primary constructor may start on the line after the class name.
|
|
65
|
+
|
|
66
|
+
Measured against 6 589 Kotlin files from six open source projects (ktor,
|
|
67
|
+
detekt, kotlinx.coroutines, Exposed, kotlinx.serialization and okio), which is
|
|
68
|
+
26 MiB of code, of which 97.33% parse with no error node.
|
|
69
|
+
|
|
70
|
+
## What the grammar cannot say
|
|
71
|
+
|
|
72
|
+
Four things about Kotlin are settled in `src/tokens.ts` rather than in the
|
|
73
|
+
grammar, because no context free rule can decide them.
|
|
74
|
+
|
|
75
|
+
**A newline ends a statement, except when it does not.** Kotlin has no
|
|
76
|
+
semicolons and no statement terminator, so `a\nb` is two statements and `a +\nb`
|
|
77
|
+
is one. The tokenizer emits a zero width `insertSemi` when a line break stands
|
|
78
|
+
between two things that could each begin a statement, and holds it back when
|
|
79
|
+
the next line opens with something that can only continue the one before: an
|
|
80
|
+
operator, a dot, a closing bracket, `as`, `by`, `else`, `finally`, a `catch`
|
|
81
|
+
followed by its parenthesis, a `where` followed by a constraint, or a `get` or
|
|
82
|
+
`set` that turns out to be an accessor rather than a call. That last list is
|
|
83
|
+
the whole difficulty. `set.addAll(x)` at the start of a line is a call to a
|
|
84
|
+
set, and `set(value) { }` under a property is an accessor, and only what
|
|
85
|
+
follows the word tells them apart.
|
|
86
|
+
|
|
87
|
+
**A `<` is a comparison or a type argument list.** `a < b` and `Map<K, V>` are
|
|
88
|
+
the same two characters. When the parser could shift a comparison the tokenizer
|
|
89
|
+
scans ahead over what a type argument list is allowed to contain, up to two
|
|
90
|
+
hundred characters, and only emits the bracket tokens if it reaches a matching
|
|
91
|
+
`>` followed by something that could stand after a type. A `*` inside the
|
|
92
|
+
brackets settles it immediately, because a star projection cannot be anything
|
|
93
|
+
else.
|
|
94
|
+
|
|
95
|
+
**A string is four different languages.** `"a"`, `"""a"""`, `$"a"` and their
|
|
96
|
+
templates disagree about what ends a string, what an escape is, and whether a
|
|
97
|
+
`$` opens a hole. The tokenizer cuts each one into a start, its content and an
|
|
98
|
+
end, and the grammar parses the expressions between the pieces. A raw string is
|
|
99
|
+
read whole, because its content is whatever stands before three quotes.
|
|
100
|
+
|
|
101
|
+
**A block comment nests.** `/* /* */ */` is one comment in Kotlin and two in
|
|
102
|
+
most of its relatives, so the tokenizer counts depth rather than searching for
|
|
103
|
+
the first `*/`.
|
|
104
|
+
|
|
105
|
+
One more is settled in the grammar, and is worth knowing about.
|
|
106
|
+
|
|
107
|
+
**Casing decides what a name is.** After one identifier no amount of lookahead
|
|
108
|
+
separates a type from a variable, so the parser follows the convention every
|
|
109
|
+
Kotlin codebase keeps: `Foo` is read as a type, `FOO_BAR` as a constant, and
|
|
110
|
+
`foo` as an ordinary name. Both readings stay alive where the grammar allows
|
|
111
|
+
either, and the one that fits wins. The trick is
|
|
112
|
+
[@lezer/java](https://github.com/lezer-parser/java)'s.
|
|
113
|
+
|
|
114
|
+
## Known gaps
|
|
115
|
+
|
|
116
|
+
`when` guards, the `if` that Kotlin 2.1 allows after a branch condition, are
|
|
117
|
+
not parsed. The guard and a branch whose condition is itself an `if` expression
|
|
118
|
+
share a prefix that an LR parser cannot tell apart, and resolving it in the
|
|
119
|
+
guard's favour cost more real files than it fixed.
|
|
120
|
+
|
|
121
|
+
## Building and testing
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
npm install # installs the dependencies and builds dist/
|
|
125
|
+
npm run build # regenerates the parser and rebuilds dist/
|
|
126
|
+
npm test # builds, then runs the tests
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Contributing
|
|
130
|
+
|
|
131
|
+
Please read about how to [contribute to Exercism](https://exercism.org/docs/building).
|
|
132
|
+
Be sure to read the Exercism [Code of Conduct](https://exercism.org/docs/using/legal/code-of-conduct).
|