@exercism/codemirror-lang-swift 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 +89 -0
- package/LICENSE +21 -0
- package/README.md +168 -2
- package/dist/index.cjs +1080 -0
- package/dist/index.d.cts +28 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +1073 -0
- package/package.json +54 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
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-swift`. It adds completion of keywords, common
|
|
9
|
+
standard library names, snippets and the names declared in the file.
|
|
10
|
+
|
|
11
|
+
## 0.2.0 (2026-08-10)
|
|
12
|
+
|
|
13
|
+
### The language people ship
|
|
14
|
+
|
|
15
|
+
0.1.x was measured on 725 small task-program files, which are small task programs and
|
|
16
|
+
say almost nothing about the Swift in a package. Measured instead on 8620 files
|
|
17
|
+
of Apple's standard library, swift-package-manager, swift-syntax, swift-nio,
|
|
18
|
+
swift-collections, swift-corelibs-foundation, Alamofire, Vapor, RxSwift,
|
|
19
|
+
lottie-ios, Kingfisher, Charts, realm-swift and nine more, 68.84% parsed with
|
|
20
|
+
no error node. That is now 95.01%, and the task-program figure went 94.34% to
|
|
21
|
+
97.24% with it.
|
|
22
|
+
|
|
23
|
+
What was missing was ordinary and nearly all of it recent:
|
|
24
|
+
|
|
25
|
+
* **Conditions match patterns.** `if case .a(let n) = value` and its `guard`
|
|
26
|
+
and `while` twins were 220 files on their own, and a binding may name its
|
|
27
|
+
type: `guard let ref: T = make()`.
|
|
28
|
+
* **Typed throws.** `func f() throws(ParseError) -> Int`, in a declaration, a
|
|
29
|
+
function type and a closure signature alike.
|
|
30
|
+
* **Attributes where a type stands.** `: @unchecked Sendable` on a conformance,
|
|
31
|
+
`@convention(c) (CInt) -> CInt` behind a colon, `[@Sendable () -> Void]`
|
|
32
|
+
inside an array, and `@ReducerBuilder<State, Action>` with type arguments.
|
|
33
|
+
A `TypeAttribute` may now carry its own parenthesised list, which is what a
|
|
34
|
+
type could equally open with, so both readings are kept.
|
|
35
|
+
* **Suppressed conformances.** `~Copyable` is a type like any other, which is
|
|
36
|
+
what `where Element: Equatable & ~Copyable` needs.
|
|
37
|
+
* **More than one trailing closure.** `f { } onCancel: { }` has been in the
|
|
38
|
+
language since 5.3. The label is read by the tokenizer, because a parser
|
|
39
|
+
cannot tell it from the first word of the next statement until the brace
|
|
40
|
+
arrives, and only where a closure has just closed: `name: {` on its own is
|
|
41
|
+
how a computed property is written.
|
|
42
|
+
* **Trailing commas**, allowed behind the last item of any list since 6.1.
|
|
43
|
+
* **Argument labels in a function type.** `(_ response: HTTPURLResponse, _
|
|
44
|
+
done: @escaping () -> Void) -> Void` writes a label and a name.
|
|
45
|
+
* **A compound function reference.** `min(by: closer(lhs:rhs:))` names a
|
|
46
|
+
function by its labels and passes nothing, and the labels are one token for
|
|
47
|
+
the same reason.
|
|
48
|
+
* **`#if` inside a switch.** The same directive wraps statements everywhere
|
|
49
|
+
else, and nothing in it says which; the first word inside does, and the
|
|
50
|
+
tokenizer reads it. `@unknown default` came with it.
|
|
51
|
+
* **`unsafe` in front of an expression**, `nonisolated(unsafe)` in front of a
|
|
52
|
+
declaration, `__owned`, `__shared` and `__consuming`, `borrow` and `mutate`
|
|
53
|
+
accessors, and `if` and `switch` as expressions.
|
|
54
|
+
* **`Any` is a type name**, so `Any.Type` and `[Any]` read the way `Self.Type`
|
|
55
|
+
and `[Int]` do, and every word in the language is a legal member name, since
|
|
56
|
+
a member is looked up by name and nothing is reserved behind a dot.
|
|
57
|
+
|
|
58
|
+
### The C-style `for` loop
|
|
59
|
+
|
|
60
|
+
Swift carried it until 3.0 removed it, and a decade of published Swift is still
|
|
61
|
+
written with it. All three clauses may be empty, and the parentheses around them
|
|
62
|
+
are optional, as they were in the language. A comma in the initialiser belongs
|
|
63
|
+
to the declaration holding it: read as a list of expressions as well, `for (a,
|
|
64
|
+
b` would be the argument list of the tuple those parentheses could equally open,
|
|
65
|
+
and nothing in the text chooses between the two.
|
|
66
|
+
|
|
67
|
+
## 0.1.2 (2026-08-06)
|
|
68
|
+
|
|
69
|
+
The repository link, which npm renders as a link and a browser cannot follow
|
|
70
|
+
with `git+` in front of it.
|
|
71
|
+
|
|
72
|
+
## 0.1.1 (2026-07-29)
|
|
73
|
+
|
|
74
|
+
A line in the README saying what this was written for, and a link to the
|
|
75
|
+
platform it was written for. Nothing in the code changed.
|
|
76
|
+
|
|
77
|
+
## 0.1.0 (2026-07-27)
|
|
78
|
+
|
|
79
|
+
### First release
|
|
80
|
+
|
|
81
|
+
Swift language support: a Lezer grammar covering Swift 5.9, plus highlighting,
|
|
82
|
+
folding, indentation and completion.
|
|
83
|
+
|
|
84
|
+
CodeMirror 6 has never shipped Swift, and no Lezer grammar for it existed
|
|
85
|
+
anywhere, so there was nothing to be compatible with. The CodeMirror 5 stream
|
|
86
|
+
mode that gets used in its place colours keywords and builds no tree.
|
|
87
|
+
|
|
88
|
+
The grammar was developed against 724 small Swift task-program files, of which
|
|
89
|
+
94.34% 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,169 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @exercism/codemirror-lang-swift [](https://www.npmjs.org/package/@exercism/codemirror-lang-swift)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[ [**CHANGELOG**](CHANGELOG.md) ]
|
|
4
|
+
|
|
5
|
+
This package implements Swift language support for the
|
|
6
|
+
[CodeMirror](https://codemirror.net/) code editor, using a
|
|
7
|
+
[Lezer](https://lezer.codemirror.net/) grammar written for this package.
|
|
8
|
+
Exercism uses it in the Swift editor on [exercism.org](https://exercism.org).
|
|
9
|
+
|
|
10
|
+
CodeMirror 6 has never shipped Swift. What it offers is the stream mode ported
|
|
11
|
+
from CodeMirror 5, which colours a list of keywords and leaves no tree behind,
|
|
12
|
+
so nothing above it can fold a function, indent a switch or ask what the word
|
|
13
|
+
under the cursor is. No Lezer grammar for Swift existed anywhere before this
|
|
14
|
+
one.
|
|
15
|
+
|
|
16
|
+
This code is released under an [MIT license](LICENSE).
|
|
17
|
+
|
|
18
|
+
## Install
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install @exercism/codemirror-lang-swift
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
```javascript
|
|
27
|
+
import {EditorView, basicSetup} from "codemirror"
|
|
28
|
+
import {swift} from "@exercism/codemirror-lang-swift"
|
|
29
|
+
|
|
30
|
+
const view = new EditorView({
|
|
31
|
+
parent: document.body,
|
|
32
|
+
doc: `struct Greeter {
|
|
33
|
+
let name: String
|
|
34
|
+
|
|
35
|
+
func greet() {
|
|
36
|
+
print("Hello, \\(name)!")
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
`,
|
|
40
|
+
extensions: [basicSetup, swift()]
|
|
41
|
+
})
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Completion
|
|
45
|
+
|
|
46
|
+
`swift()` includes completion for:
|
|
47
|
+
|
|
48
|
+
- keywords, and the standard library types and functions that Swift solutions
|
|
49
|
+
use most often
|
|
50
|
+
- snippets for functions, `if`, `if let`, `guard`, loops, `switch`, `do` and
|
|
51
|
+
`catch`, and type definitions
|
|
52
|
+
- the names declared in the file: functions, types, enum cases, generic
|
|
53
|
+
parameters, properties, locals, parameters, closure parameters, and the
|
|
54
|
+
names bound by `for`, `if let`, `guard let` and `case let`, plus `error` in
|
|
55
|
+
a `catch` and `newValue` and `oldValue` in accessors
|
|
56
|
+
|
|
57
|
+
Each name is offered inside its scope. Inside a function body, a local
|
|
58
|
+
is offered after its declaration. Members and top-level names are offered
|
|
59
|
+
anywhere, because Swift lets code use them before they are declared.
|
|
60
|
+
Completion stays quiet inside strings and comments, and where a new name is
|
|
61
|
+
being written. Code inside `\( )` in a string still gets names.
|
|
62
|
+
|
|
63
|
+
## Coverage
|
|
64
|
+
|
|
65
|
+
Swift 6.2. Async and await, actors, structured concurrency, opaque and
|
|
66
|
+
existential types (`some` and `any`), property wrappers, result builders,
|
|
67
|
+
macros, parameter packs, pattern matching, generics with where clauses, key
|
|
68
|
+
paths, multi-line and raw strings, operator and precedence group declarations,
|
|
69
|
+
typed throws, suppressed conformances (`~Copyable`), `if` and `switch` as
|
|
70
|
+
expressions, more than one trailing closure on a call, and the trailing comma
|
|
71
|
+
6.1 allows behind the last item of a list.
|
|
72
|
+
|
|
73
|
+
The Swift 1 and 2 spellings a decade of published code still carries are in
|
|
74
|
+
too: `inout` in front of a parameter name, `var` parameters, curried functions,
|
|
75
|
+
constraints written inside the angle brackets, and the C-style `for` loop
|
|
76
|
+
Swift 3 removed, written with its parentheses or without them.
|
|
77
|
+
|
|
78
|
+
Measured against 8620 files of Swift people ship, which is Apple's standard
|
|
79
|
+
library, swift-package-manager, swift-syntax, swift-nio, swift-collections,
|
|
80
|
+
swift-corelibs-foundation, swift-crypto, swift-argument-parser,
|
|
81
|
+
swift-algorithms, swift-composable-architecture, Alamofire, Vapor, RxSwift,
|
|
82
|
+
ReactiveSwift, lottie-ios, Kingfisher, Charts, realm-swift, Moya, SnapKit,
|
|
83
|
+
SQLite.swift, SwiftyJSON and swift-log: 95.01% parse with no error node. A
|
|
84
|
+
set of 725 small task programs reads 97.24%, which is the softer of the two
|
|
85
|
+
measurements, since they are short programs and not whole packages.
|
|
86
|
+
|
|
87
|
+
The compiler's own `test` and `validation-test` trees are left out of that
|
|
88
|
+
corpus on purpose. They are inputs written to be rejected, and a parser is not
|
|
89
|
+
judged on source the language refuses.
|
|
90
|
+
|
|
91
|
+
## What a tokenizer has to settle
|
|
92
|
+
|
|
93
|
+
Almost all of Swift can be written down as a grammar. Eight things cannot, and
|
|
94
|
+
they are the whole of `tokens.ts`.
|
|
95
|
+
|
|
96
|
+
**Whitespace decides what an operator is.** Swift's operator set is open, so
|
|
97
|
+
`a - b`, `a -b` and `a-b` cannot be told apart by spelling. The language settles
|
|
98
|
+
it by spacing: an operator with space on both sides or neither is infix, one
|
|
99
|
+
with space only in front is prefix, and one with space only behind is postfix.
|
|
100
|
+
The same rule separates `x!` from `!x`, and `a ?? b` from `x?.y`.
|
|
101
|
+
|
|
102
|
+
**An angle bracket is two different things.** `Array<Int>` opens a generic
|
|
103
|
+
argument list and `a < b` compares. Where a type is expected the parser has
|
|
104
|
+
already ruled the comparison out. Where an expression is expected, the tokenizer
|
|
105
|
+
reads ahead for the closing bracket and takes the call or member access that
|
|
106
|
+
follows it as the proof, or the end of the line, which is what stands behind
|
|
107
|
+
`typealias Result = Either<A, B>` and behind no chain of comparisons anybody
|
|
108
|
+
writes.
|
|
109
|
+
|
|
110
|
+
**A second trailing closure carries a name.** `f { } onCancel: { }` has been in
|
|
111
|
+
the language since 5.3, and that name stands exactly where the next statement's
|
|
112
|
+
first word would. Only the brace behind the colon tells them apart, so the
|
|
113
|
+
tokenizer reads ahead for it, and asks the parser first: a label can stand only
|
|
114
|
+
where a closure has just closed. Read as an ordinary label it would take every
|
|
115
|
+
`var x: T {` in the language with it.
|
|
116
|
+
|
|
117
|
+
**A function may be named by its argument labels.** `min(by: closer(lhs:rhs:))`
|
|
118
|
+
hands over no arguments at all, and nothing tells that list from an ordinary one
|
|
119
|
+
until the closing bracket, so the whole of it is one token.
|
|
120
|
+
|
|
121
|
+
**A `#if` inside a switch holds whole cases.** The same directive holds
|
|
122
|
+
statements everywhere else, both can stand at the end of a case body, and the
|
|
123
|
+
directive says nothing about which. What settles it is the first word inside.
|
|
124
|
+
|
|
125
|
+
**A brace is two different things.** `if x { }` is a condition and a body;
|
|
126
|
+
`xs.map { }` is a call and a closure. Swift tells them apart by where the brace
|
|
127
|
+
stands, so the tokenizer asks the parser whether a body could stand here and
|
|
128
|
+
lets the answer decide. A closure that opens with a signature (`{ x in ... }`)
|
|
129
|
+
is a third case, found by reading ahead for the `in` over nothing but what a
|
|
130
|
+
signature may hold.
|
|
131
|
+
|
|
132
|
+
**Block comments nest.** A comment opened inside a comment has to be closed
|
|
133
|
+
twice, which no regular token can count.
|
|
134
|
+
|
|
135
|
+
**A string holds code.** `"total: \(a + b)"` is a string with an expression
|
|
136
|
+
inside it, and that expression may hold another string. The tokenizer cuts the
|
|
137
|
+
string into pieces and the grammar parses what lies between them.
|
|
138
|
+
|
|
139
|
+
## What the tree does not say
|
|
140
|
+
|
|
141
|
+
Two things are deliberately shallower than the language reference.
|
|
142
|
+
|
|
143
|
+
**Operator precedence is flat.** Swift lets anybody declare an operator and give
|
|
144
|
+
it a precedence group, so no fixed table here could be right about a program it
|
|
145
|
+
has not seen. Infix operators are one left-associative level, which is wrong
|
|
146
|
+
about the shape of `a + b * c` and right about every operator this grammar has
|
|
147
|
+
never heard of. A highlighter reads tokens rather than shapes.
|
|
148
|
+
|
|
149
|
+
**Patterns are expressions.** `case .some(let x)` holds a binding that only
|
|
150
|
+
appears in a pattern. Rather than carry a second grammar for patterns, `let x`
|
|
151
|
+
is accepted as an expression and the tree says `BindingPattern` where a pattern
|
|
152
|
+
was written.
|
|
153
|
+
|
|
154
|
+
Raw strings (`#"..."#`) are one token rather than a parsed string, because the
|
|
155
|
+
delimiter's pound count would have to be carried from the opening token to the
|
|
156
|
+
closing one. Nothing interpolates inside them anyway.
|
|
157
|
+
|
|
158
|
+
## Building and testing
|
|
159
|
+
|
|
160
|
+
```bash
|
|
161
|
+
npm install # installs the dependencies and builds dist/
|
|
162
|
+
npm run build # regenerates the parser and rebuilds dist/
|
|
163
|
+
npm test # builds, then runs the tests
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## Contributing
|
|
167
|
+
|
|
168
|
+
Please read about how to [contribute to Exercism](https://exercism.org/docs/building).
|
|
169
|
+
Be sure to read the Exercism [Code of Conduct](https://exercism.org/docs/using/legal/code-of-conduct).
|