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