@exercism/codemirror-lang-scala 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,162 @@
1
+ # Changelog
2
+
3
+ ## 1.0.0
4
+
5
+ This is Exercism's first release of the package, published as
6
+ `@exercism/codemirror-lang-scala`. It adds completion of the names in scope,
7
+ snippets and common methods after a dot, and parses objects named with symbols
8
+ and a lambda passed after a colon.
9
+
10
+ ## 0.4.0 (2026-08-13)
11
+
12
+ The release that takes the grammar over the bar this project holds every grammar
13
+ of ours to. Over 64078 files of the same libraries, now grown, 90.81% parsed
14
+ with no error node before this release and 95.27% do. Error nodes fall from
15
+ 51069 to 24284.
16
+
17
+ Scala 2's postfix call was the largest of them and had been left out on purpose,
18
+ because the rule gives `1 to 10` a second reading and nothing later in a file
19
+ says which was meant. It is a token decision instead. A word is postfix where it
20
+ closes the operand it stands after, in front of a bracket that shuts, a comma, a
21
+ semicolon or the end of its own line, since an infix call needs an operand there
22
+ and a postfix one can take nothing more. A word at the head of its line is
23
+ refused, which is what keeps the ambiguity out. So `Timeout(10 seconds)`,
24
+ `getLong("delay") minutes` and `val timeout: FiniteDuration = 5 second` all read
25
+ where every infix call is untouched.
26
+
27
+ `⇒` was a name one character long, because every character above ASCII was one.
28
+ It beat the arrow it is, so `case A ⇒ 1` read as a pattern, an infix call and a
29
+ number with nothing joining them. The symbol blocks belong to operators here and
30
+ to nothing else.
31
+
32
+ A parameter clause may stand on a line of its own, under what it belongs to:
33
+
34
+ def get(c: String)
35
+ (implicit km: KafkaManager): Future[Result] = ...
36
+
37
+ abstract class Metric
38
+ (
39
+ val name: String
40
+ ) extends EvalMetric
41
+
42
+ The bracket is a token of its own now, so the layout tokenizer can ask whether a
43
+ clause could open where it is looking rather than guess from the character. A
44
+ line beginning on a bracket anywhere else is still the statement it was.
45
+
46
+ A single lambda parameter may carry its type without brackets,
47
+ `tryWith(req) { result: CMResults => ... }`, which is spelled exactly like a
48
+ template body's self type, and only where the body stands says which. The self
49
+ type is weighted above it. The same form with `implicit` in front takes a type
50
+ carrying no arrow of its own, or `implicit request: RequestHeader =>` reads as a
51
+ parameter typed by the function from the header to the body and the block never
52
+ ends. That is how every controller in a Play application is written.
53
+
54
+ The rest are smaller. A script's first line, `#!/usr/bin/env amm`, which every
55
+ Ammonite script opens with. A macro's hole stands where an expression stands, so
56
+ where the parser has no room for one the `$` opens a name, which is
57
+ `import $ivy.` and `import $file.`. The star that ends a parameter list may
58
+ carry a space in front of it. And a type may be named out of operator characters
59
+ and applied in front of its arguments, `\/[E, A]`, which scalaz writes
60
+ throughout.
61
+
62
+ What is left is Scala 2's XML literals, which scalatest, Play and the DBpedia
63
+ extraction framework write pages of, and two directories of code that is not
64
+ meant to parse: Ammonite's test resources, which exist to produce compiler
65
+ errors, and REPL transcripts with the prompts still in them.
66
+
67
+ ## 0.3.0 (2026-08-16)
68
+
69
+ Over 44185 files of cats, fs2, http4s, pekko, Play, scalatest, scalaz, Spark,
70
+ tapir, zio and both compilers, 85.51% of files parsed with no error node before
71
+ this release and 91.34% do now. Play goes 68.17% to 91.24%, Spark 76.29% to
72
+ 94.90%, http4s 86.68% to 95.10%, pekko 88.88% to 98.84% and fs2 92.31% to
73
+ 98.90%.
74
+
75
+ The largest of these was not a missing rule. `if (c)` and `if c then` both open
76
+ on a bracket, and `(c) { ... }` is a call carrying a block, so both readings of
77
+ a Scala 2 `if` survived the whole block after the condition and died on the
78
+ `else`. Lezer carries twelve stacks and prunes on a score once two of them run
79
+ long, and the one it dropped was often the one that was right. One failing file
80
+ in ten failed on a `} else {` a hundred lines in. The tokenizer now reads to
81
+ the bracket's own end and hands the parser a token saying which dialect wrote
82
+ it, so the question is settled in front of the condition.
83
+
84
+ Read now, and not before:
85
+
86
+ * A class's constructor modifiers, `class Api @Inject() (val cache: Cache)`.
87
+ The annotation took every bracket it could reach, so the constructor's
88
+ parameters became its arguments and the `val` in them had nowhere to stand.
89
+ * `$` as the name it is. `$"id"` is a column in Spark and `$(param)` reads one,
90
+ and between them they were in a third of the Spark files that failed.
91
+ * `for x <- xs do f(x)`, Scala 3's loop, written with the word Scala 2 opens a
92
+ do-while with. The word started a do-while that then ate the rest of the
93
+ block looking for a `while` to close it.
94
+ * The argument Scala 3 writes without brackets: a line ending on a colon, and
95
+ the block under it. The form that puts a function's parameters between the
96
+ colon and the end of the line is not read, because it is spelled the same way
97
+ as a template body's self type.
98
+ * An operator imported by name, `import cats.{Applicative, ~>}`.
99
+ * `'['`, which is the character and not the opening of a macro.
100
+ * An operator-named extractor, `case -\/(a) =>`, which is how scalaz writes the
101
+ two sides of a disjunction.
102
+ * A comma ending the indentation region an argument opened over two lines,
103
+ `parent =` and the value under it.
104
+
105
+ `new It[A](x)` no longer opens a body of its own. A template body may be opened
106
+ by a brace or by a colon; the form opened by nothing but the line under it is
107
+ now reachable only from `with` and `extension`, which are the two places Scala
108
+ writes it. Standing at the end of a clause, a `new` was opening one and
109
+ swallowing the clause after it.
110
+
111
+ ## 0.2.1 (2026-08-06)
112
+
113
+ The repository link, which npm renders as a link and a browser cannot follow
114
+ with `git+` in front of it.
115
+
116
+ ## 0.2.0 (2026-08-05)
117
+
118
+ Over 29546 files of cats, zio and the two Scala compilers, 69.72% of files
119
+ parsed with no error node before this release and 87.31% do now. Over the 1072
120
+ files of CodinCod's own pool, 83.68% and 86.29%.
121
+
122
+ Read now, and not before:
123
+
124
+ * `???`, the standard library's value for something nobody has written yet. It
125
+ was read as a prefix operator missing its operand, and the error node it left
126
+ was the largest single shape in the corpus. An operator with an operand on
127
+ neither side is a name.
128
+ * `s"""..."""`. One rule covered both kinds of interpolated string, so both
129
+ contents were shiftable inside either and the tokenizer asking the parser
130
+ which it was in got the wrong answer. The string ended at its first quote and
131
+ the rest of the file went with it.
132
+ * `?`, the type Scala 3 spells where Scala 2 spelled `_`. A method named `?` is
133
+ still read wherever an operator name can stand.
134
+ * Capture checking: `IterableOnce[A]^`, `AnyRef^{ll}`, `class Box[X^]`.
135
+ * Match types, type lambdas, polymorphic function types, and `A Ior B`, the
136
+ infix type application cats and zio are written in.
137
+ * Kind projector's `Map[K, *]`.
138
+ * `inline` and `erased` modifying a parameter, and Scala 2's `= macro impl[A]`.
139
+ * A trailing comma in any bracketed list.
140
+ * A self type. `trait E extends T {` reached the brace as a refinement on `T`
141
+ rather than as E's own body, and the self type inside it went with it.
142
+ * A catch clause on the line of its try, `try f() catch case e: E => g()`.
143
+ * A given's `with` body, and a given that goes unnamed.
144
+ * A lowercase extractor, `case emptyWord(s) =>`, which is how a regular
145
+ expression is matched on.
146
+ * A refinement standing as a type, which is what `({ type l[X] = F[X] })#l`
147
+ needs, `import a.b as c`, `$x` as a hole in quoted code, an annotated package
148
+ clause, and a pattern alternative told apart from a union type.
149
+
150
+ Two things changed shape in the tree. No separator is inserted between an
151
+ opening brace and the first statement of a block, since a separator goes
152
+ between two things. `new Foo { ... }` reaches a `TemplateBody` rather than a
153
+ `RefinementType`.
154
+
155
+ ## 0.1.0 (2026-08-05)
156
+
157
+ First release.
158
+
159
+ A Lezer grammar for Scala 2 and Scala 3, with highlighting, indentation,
160
+ folding and completion for the core library. Braced and indented blocks are both
161
+ read, statements end where a line break ended them, and Scala 3's soft keywords
162
+ are decided in the tokenizer so an ordinary name spelled the same way survives.
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,182 @@
1
- # Temporary Holding Version
1
+ # @exercism/codemirror-lang-scala
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-scala](https://codeberg.org/reeven/codemirror-lang-scala),
5
+ which CodinCod wrote. Exercism uses it for the Scala editor on exercism.org.
6
+ Our changes live on the `main` branch. Upstream's own branch is `master`.
7
+
8
+ The fork adds:
9
+
10
+ * Completion of the names declared in the file. It offers functions, values,
11
+ parameters, types, pattern bindings and imports wherever they are in scope.
12
+ * Snippets for `def`, `val`, `if`, `match`, `for`, `while`, `try`, `class`,
13
+ `case class`, `object`, `trait` and `enum`.
14
+ * Common collection and string methods after a dot, instead of keywords.
15
+ * Completion stays off inside strings and comments. Code inside `${...}` still
16
+ gets it.
17
+ * Parsing of objects named with symbols, such as `object ~`.
18
+ * Parsing of a lambda passed after a colon, with its body on the lines below, as in
19
+ `xs.map: x =>`. Scala 3.3 added this form.
20
+
21
+ Upstream's own README follows.
22
+
23
+ ---
24
+
25
+
26
+ This package implements [Scala](https://www.scala-lang.org/) language support
27
+ for the [CodeMirror](https://codemirror.net/) code editor: a
28
+ [Lezer](https://lezer.codemirror.net/) grammar, highlighting, indentation,
29
+ folding and completion for the core library.
30
+
31
+ The grammar is written from scratch. Scala has never had a Lezer grammar, and
32
+ what a CodeMirror 6 editor could reach for until now was the `clike` stream mode
33
+ in `@codemirror/legacy-modes`, which reads Scala as a curly-braced language with
34
+ a longer keyword list.
35
+
36
+ Both dialects are read by one grammar. A file may brace its blocks or indent
37
+ them, and may do both.
38
+
39
+ Written in part for [CodinCod](https://codincod.com/), a competitive coding
40
+ platform, where it colours the editor people solve puzzles in.
41
+
42
+ This code is released under an
43
+ [MIT license](https://codeberg.org/reeven/codemirror-lang-scala/src/branch/master/LICENSE).
44
+
45
+ ## Install
46
+
47
+ ```bash
48
+ npm install @exercism/codemirror-lang-scala
49
+ ```
50
+
51
+ ## Usage
52
+
53
+ ```javascript
54
+ import {EditorView, basicSetup} from "codemirror"
55
+ import {scala} from "@exercism/codemirror-lang-scala"
56
+
57
+ const view = new EditorView({
58
+ parent: document.body,
59
+ doc: `enum Shape:
60
+ case Circle(radius: Double)
61
+ case Rectangle(width: Double, height: Double)
62
+
63
+ object Shape:
64
+ extension (shape: Shape)
65
+ def area: Double = shape match
66
+ case Circle(r) => math.Pi * r * r
67
+ case Rectangle(w, h) => w * h
68
+
69
+ @main def report(): Unit =
70
+ val shapes = List(Shape.Circle(1.0), Shape.Rectangle(2.0, 3.0))
71
+ for shape <- shapes do
72
+ println(f"\${shape.area}%2.2f")
73
+ `,
74
+ extensions: [basicSetup, scala()]
75
+ })
76
+ ```
77
+
78
+ ## What it reads
79
+
80
+ Measured against the stream mode it replaces, over 1072 files of Scala from
81
+ CodinCod's language-guessing pool: the stream mode carries a highlighting tag on
82
+ **85.76%** of the text in 7 colours, with 43.30% of it in the busiest one. This
83
+ package tags **99.98%** in 11 colours, with 35.71% in the busiest.
84
+
85
+ Over the same 1072 files, **86.29%** parse with no error node. Over 29546 files
86
+ and 59.84 MiB of cats, zio, the Scala 2 compiler and the Scala 3 compiler,
87
+ **87.31%** do: cats 94.85%, zio 90.51%, scala2 91.83%, scala3 84.53%. The two
88
+ compilers carry their own test suites, and 7838 of those files are code that
89
+ was written not to compile, so a share of the remainder is Scala nobody meant
90
+ to be read.
91
+
92
+ Three things took most of the work.
93
+
94
+ * **Scala 3 writes no braces.** A region opens where a line stands further right
95
+ than the one that opened it, and closes at the first line to the left of it.
96
+ The parser is handed zero-width `indent` and `dedent` tokens for the braces
97
+ nobody wrote. A region is only ever opened where the grammar could take a
98
+ body, so an argument list broken over lines opens nothing, and a class header
99
+ continued on the next line is read as the header it still is.
100
+ * **A line break ends a statement.** Scala spends few semicolons, so one is
101
+ inserted wherever a break ended something that could stand alone. The newline
102
+ is kept in the parser's context rather than read back out of the text, because
103
+ a zero-width token leaves the text exactly as it found it, and a token that
104
+ cannot tell it has already fired will fire forever.
105
+ * **Scala 3 reserved nothing.** `end`, `open`, `inline`, `opaque`,
106
+ `transparent`, `infix`, `extension` and `derives` are all ordinary names, and
107
+ in this corpus they are: `end` is somebody's field or method wherever a
108
+ collection has one. Each is decided by one word of lookahead in the tokenizer,
109
+ so where the grammar wants the keyword it gets it, and everywhere else the
110
+ name survives. `case` is decided the same
111
+ way, which is what tells `case class Foo` from the `case Foo` that opens a
112
+ clause, and `@inline` is an annotation rather than a modifier because of the
113
+ character in front of it. `inline` and `erased` modify a parameter as well as
114
+ a definition, and there the word after them is somebody's parameter name, so
115
+ those two are decided by the name and colon standing after them instead.
116
+
117
+ Smaller things worth naming.
118
+
119
+ * **Strings are cut into their pieces.** `s"hi $name, ${1 + 2}"` reaches the
120
+ tree as an interpolator, its text, a variable and a block of code, so a theme
121
+ can colour the hole differently from the string around it. A plain string
122
+ takes escapes and no holes, `"""raw"""` takes neither, and `s"""..."""` takes
123
+ holes and no escapes. Each is a rule of its own, because the tokenizer asks
124
+ the parser which kind of string it is standing in and one rule covering two
125
+ of them answers wrongly.
126
+ * **A name says what it is.** A capitalised name is a type and a lowercase one
127
+ is a value, which is the whole of what tells `case Some(x)` matching from
128
+ `case some` binding. A parameter, a definition and a use each reach a
129
+ different node.
130
+ * **`*` is four tokens.** Multiplication, the star that ends a parameter list,
131
+ the one that spreads a sequence into an argument list, and kind-projector's
132
+ `Map[K, *]`. The middle two are decided by what stands on either side of the
133
+ star, because by the time the grammar sees it the parser has already chosen
134
+ to multiply.
135
+ * **An operator with an operand on neither side is a name.** `???` is the
136
+ standard library's value for something nobody has written yet, and it is a
137
+ method spelled out of the characters an operator is made of. An infix
138
+ operator has something to its left and a prefix operator something to its
139
+ right, so a run with neither is neither, and reaches the tree as the name it
140
+ is.
141
+ * **Capture checking.** `IterableOnce[A]^` captures something and `AnyRef^{ll}`
142
+ says what. dotty's own library is written in it. The caret is an ordinary
143
+ method name as well, and is told apart the same way the splat star is: a
144
+ capture touches the type it closes.
145
+ * **A type computes.** `X match { case String => Char }` is read by the rule a
146
+ pattern match is read by, so its clauses reach the tree as the clauses they
147
+ are. `[X] =>> F[X]` and `[X] => F[X] => G[X]` are one rule told apart by its
148
+ arrow. `A Ior B` is `Ior[A, B]`, which is how cats and zio are written.
149
+ * **A list may end on a comma.** Scala 2.13 lets a bracket broken over several
150
+ lines carry one after its last item, and every build file does.
151
+
152
+ ## What it does not read
153
+
154
+ * **Postfix notation.** `xs head` is legal Scala 2 and is not read as a call.
155
+ Both readings of `1 to 10` parse, and keeping the postfix one would make every
156
+ `x max y` in the corpus ambiguous for the sake of a form Scala 3 has
157
+ deprecated.
158
+ * **XML literals.** Dropped from Scala 3, and 0.6% of the corpus.
159
+ * **`inline x match`.** The word before an inline match is decided a word at a
160
+ time like every other soft keyword, and the `match` that would decide this one
161
+ stands after the thing being matched rather than after the word.
162
+ * **Capture arrows and capture-set bounds.** `List[() ->{C} Unit]` and
163
+ `[A: {Arbitrary, Ordering}]`. The first is dotty-internal; the second collides
164
+ with a refinement type at the brace, and is 0.2% of the corpus.
165
+ * **`given (using c: C): T = ...`, the unnamed given whose signature is nothing
166
+ but a parameter list.** The named form and the one taking type parameters both
167
+ work.
168
+ * **Named tuples**, `(name: String, age: Int)`, which Scala 3.7 added.
169
+
170
+ One thing that looks like a gap and is not a rule. A file deep enough in braced
171
+ blocks inside an indentation region loses the `else` of an `if`, and every
172
+ single reduction of the case makes it parse. That is `@lezer/lr` pruning a
173
+ stack rather than a production nobody wrote; the same file fails the same way
174
+ with two of its patterns removed and passes with three.
175
+
176
+ ## Building and testing
177
+
178
+ ```bash
179
+ npm install # installs the dependencies and builds dist/
180
+ npm run build # regenerates the parser and rebuilds dist/
181
+ npm test # builds, then runs the tests
182
+ ```