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