@exercism/codemirror-lang-zig 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/LICENSE +21 -0
- package/README.md +25 -2
- package/lib/complete.d.ts +7 -0
- package/lib/complete.js +335 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +33 -0
- package/lib/std.d.ts +8 -0
- package/lib/std.js +865 -0
- package/package.json +37 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Joel Gustafson
|
|
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,26 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @exercism/codemirror-lang-zig
|
|
2
2
|
|
|
3
|
-
This
|
|
3
|
+
This package gives CodeMirror 6 support for [Zig](https://ziglang.org/): highlighting, indentation, folding and completion. It is used by the editor on [Exercism](https://exercism.org).
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @exercism/codemirror-lang-zig
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { EditorView, basicSetup } from "codemirror"
|
|
15
|
+
import { indentUnit } from "@codemirror/language"
|
|
16
|
+
import { zig } from "@exercism/codemirror-lang-zig"
|
|
17
|
+
|
|
18
|
+
new EditorView({
|
|
19
|
+
extensions: [basicSetup, zig(), indentUnit.of(" ")],
|
|
20
|
+
parent: document.body,
|
|
21
|
+
})
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
`zig fmt` indents with 4 spaces, so the editor should set a 4-space indent unit.
|
|
25
|
+
|
|
26
|
+
The source and tests are in [exercism/codemirror-lang-zig](https://github.com/exercism/codemirror-lang-zig).
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Completion, CompletionSource } from "@codemirror/autocomplete";
|
|
2
|
+
export declare const keywords: Completion[];
|
|
3
|
+
export declare const snippets: Completion[];
|
|
4
|
+
export declare const builtinCompletionSource: CompletionSource;
|
|
5
|
+
export declare const localCompletionSource: CompletionSource;
|
|
6
|
+
export declare const memberCompletionSource: CompletionSource;
|
|
7
|
+
export declare const zigCompletionSources: CompletionSource[];
|
package/lib/complete.js
ADDED
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
import { completeFromList, ifNotIn, snippetCompletion, } from "@codemirror/autocomplete";
|
|
2
|
+
import { syntaxTree } from "@codemirror/language";
|
|
3
|
+
import { IterMode, NodeWeakMap } from "@lezer/common";
|
|
4
|
+
import { builtins, stdNamespaces } from "./std.js";
|
|
5
|
+
const keywordNames = "addrspace align allowzero and anyframe anytype asm break callconv catch comptime const continue defer else enum errdefer error export extern fn for if inline linksection noalias noinline nosuspend opaque or orelse packed pub resume return struct suspend switch test threadlocal try union unreachable var volatile while";
|
|
6
|
+
const valueNames = "true false null undefined";
|
|
7
|
+
const primitiveTypes = "i8 u8 i16 u16 i32 u32 i64 u64 i128 u128 isize usize u1 u2 u3 u4 u5 u6 u7 u21 c_char c_short c_ushort c_int c_uint c_long c_ulong c_longlong c_ulonglong c_longdouble f16 f32 f64 f80 f128 bool anyopaque void noreturn type anyerror comptime_int comptime_float";
|
|
8
|
+
export const keywords = [
|
|
9
|
+
...keywordNames.split(" ").map((label) => ({ label, type: "keyword" })),
|
|
10
|
+
...valueNames.split(" ").map((label) => ({ label, type: "constant" })),
|
|
11
|
+
...primitiveTypes.split(" ").map((label) => ({ label, type: "type" })),
|
|
12
|
+
];
|
|
13
|
+
export const snippets = [
|
|
14
|
+
snippetCompletion('const std = @import("std");', { label: "import std", detail: "declaration", type: "keyword" }),
|
|
15
|
+
snippetCompletion("fn ${name}(${params}) ${void} {\n\t${}\n}", { label: "fn", detail: "definition", type: "keyword" }),
|
|
16
|
+
snippetCompletion("pub fn ${name}(${params}) ${void} {\n\t${}\n}", {
|
|
17
|
+
label: "pub fn",
|
|
18
|
+
detail: "definition",
|
|
19
|
+
type: "keyword",
|
|
20
|
+
}),
|
|
21
|
+
snippetCompletion("const ${Name} = struct {\n\t${}\n};", { label: "struct", detail: "definition", type: "keyword" }),
|
|
22
|
+
snippetCompletion("const ${Name} = enum {\n\t${}\n};", { label: "enum", detail: "definition", type: "keyword" }),
|
|
23
|
+
snippetCompletion("const ${Name} = union(enum) {\n\t${}\n};", {
|
|
24
|
+
label: "union",
|
|
25
|
+
detail: "definition",
|
|
26
|
+
type: "keyword",
|
|
27
|
+
}),
|
|
28
|
+
snippetCompletion("const ${Name} = error{\n\t${},\n};", { label: "error", detail: "error set", type: "keyword" }),
|
|
29
|
+
snippetCompletion("if (${condition}) {\n\t${}\n}", { label: "if", detail: "block", type: "keyword" }),
|
|
30
|
+
snippetCompletion("if (${condition}) {\n\t${}\n} else {\n\t\n}", { label: "if", detail: "/ else block", type: "keyword" }),
|
|
31
|
+
snippetCompletion("if (${optional}) |${value}| {\n\t${}\n}", { label: "if", detail: "capture", type: "keyword" }),
|
|
32
|
+
snippetCompletion("while (${condition}) {\n\t${}\n}", { label: "while", detail: "loop", type: "keyword" }),
|
|
33
|
+
snippetCompletion("while (${i} < ${n}) : (${i} += 1) {\n\t${}\n}", {
|
|
34
|
+
label: "while",
|
|
35
|
+
detail: "counter loop",
|
|
36
|
+
type: "keyword",
|
|
37
|
+
}),
|
|
38
|
+
snippetCompletion("for (${items}) |${item}| {\n\t${}\n}", { label: "for", detail: "loop", type: "keyword" }),
|
|
39
|
+
snippetCompletion("for (0..${n}) |${i}| {\n\t${}\n}", { label: "for", detail: "range loop", type: "keyword" }),
|
|
40
|
+
snippetCompletion("switch (${value}) {\n\t${} => ${},\n\telse => ${},\n}", {
|
|
41
|
+
label: "switch",
|
|
42
|
+
detail: "expression",
|
|
43
|
+
type: "keyword",
|
|
44
|
+
}),
|
|
45
|
+
snippetCompletion("try ${expression}", { label: "try", detail: "error union", type: "keyword" }),
|
|
46
|
+
snippetCompletion("catch |${err}| {\n\t${}\n}", { label: "catch", detail: "error union", type: "keyword" }),
|
|
47
|
+
snippetCompletion("catch |${err}| switch (${err}) {\n\terror.${} => ${},\n\telse => |e| return e,\n}", {
|
|
48
|
+
label: "catch",
|
|
49
|
+
detail: "switch on error",
|
|
50
|
+
type: "keyword",
|
|
51
|
+
}),
|
|
52
|
+
snippetCompletion("defer ${};", { label: "defer", detail: "statement", type: "keyword" }),
|
|
53
|
+
snippetCompletion("errdefer ${};", { label: "errdefer", detail: "statement", type: "keyword" }),
|
|
54
|
+
snippetCompletion('test "${name}" {\n\t${}\n}', { label: "test", detail: "block", type: "keyword" }),
|
|
55
|
+
];
|
|
56
|
+
const builtinCompletions = builtins.map(([label, args]) => snippetCompletion(args == 0 ? `${label}()` : `${label}(\${})`, {
|
|
57
|
+
label,
|
|
58
|
+
type: "function",
|
|
59
|
+
detail: args < 0 ? "(...)" : `(${args} argument${args == 1 ? "" : "s"})`,
|
|
60
|
+
}));
|
|
61
|
+
// Positions where nothing should be completed: in strings, comments and names being declared.
|
|
62
|
+
const dontComplete = [
|
|
63
|
+
"StringLiteral",
|
|
64
|
+
"StringLiteralSingle",
|
|
65
|
+
"CharLiteral",
|
|
66
|
+
"LineComment",
|
|
67
|
+
"DocComment",
|
|
68
|
+
"ContainerDocComment",
|
|
69
|
+
"Name",
|
|
70
|
+
"FnName",
|
|
71
|
+
"ParamName",
|
|
72
|
+
"PayloadName",
|
|
73
|
+
"FieldName",
|
|
74
|
+
"BlockLabel",
|
|
75
|
+
"BreakLabel",
|
|
76
|
+
"TestName",
|
|
77
|
+
];
|
|
78
|
+
function inDontComplete(context) {
|
|
79
|
+
for (let node = syntaxTree(context.state).resolveInner(context.pos, -1); node; node = node.parent)
|
|
80
|
+
if (dontComplete.includes(node.name))
|
|
81
|
+
return true;
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
/// Completes builtins such as `@import` and `@intCast` after `@`.
|
|
85
|
+
export const builtinCompletionSource = (context) => {
|
|
86
|
+
let word = context.matchBefore(/@\w*/);
|
|
87
|
+
if (!word || inDontComplete(context))
|
|
88
|
+
return null;
|
|
89
|
+
return { from: word.from, options: builtinCompletions, validFor: /^@\w*$/ };
|
|
90
|
+
};
|
|
91
|
+
//// Names declared in the file.
|
|
92
|
+
// Nodes that open a scope. A name declared directly in one is visible inside it.
|
|
93
|
+
const ScopeNodes = new Set([
|
|
94
|
+
"Program",
|
|
95
|
+
"ContainerBlock",
|
|
96
|
+
"Block",
|
|
97
|
+
"Decl",
|
|
98
|
+
"ForStatement",
|
|
99
|
+
"ForExpr",
|
|
100
|
+
"ForTypeExpr",
|
|
101
|
+
"WhileStatement",
|
|
102
|
+
"WhileExpr",
|
|
103
|
+
"WhileTypeExpr",
|
|
104
|
+
"IfStatement",
|
|
105
|
+
"IfExpr",
|
|
106
|
+
"IfTypeExpr",
|
|
107
|
+
"SwitchProng",
|
|
108
|
+
"BinaryExpr",
|
|
109
|
+
]);
|
|
110
|
+
// In a block, locals are visible only after their declaration. In a container, order doesn't matter.
|
|
111
|
+
const OrderedScopes = new Set(["Block"]);
|
|
112
|
+
function varType(proto, doc) {
|
|
113
|
+
let value = proto.nextSibling;
|
|
114
|
+
if (value?.name == "Expr") {
|
|
115
|
+
let text = doc.sliceString(value.from, Math.min(value.to, value.from + 40));
|
|
116
|
+
if (/^(extern\s+|packed\s+)?(struct|enum|union|opaque)\b|^error\s*\{/.test(text))
|
|
117
|
+
return "class";
|
|
118
|
+
}
|
|
119
|
+
return proto.firstChild?.name == "var" ? "variable" : "constant";
|
|
120
|
+
}
|
|
121
|
+
const cache = new NodeWeakMap();
|
|
122
|
+
function getScope(doc, node) {
|
|
123
|
+
let cached = cache.get(node);
|
|
124
|
+
if (cached)
|
|
125
|
+
return cached;
|
|
126
|
+
let defs = [];
|
|
127
|
+
let top = true;
|
|
128
|
+
let def = (n, type) => defs.push({ label: doc.sliceString(n.from, n.to), type, pos: n.from });
|
|
129
|
+
node.cursor(IterMode.IncludeAnonymous).iterate((n) => {
|
|
130
|
+
if (top) {
|
|
131
|
+
top = false;
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
if (n.name == "Name") {
|
|
135
|
+
let proto = n.node.parent;
|
|
136
|
+
def(n, varType(proto, doc));
|
|
137
|
+
}
|
|
138
|
+
else if (n.name == "FnName") {
|
|
139
|
+
def(n, "function");
|
|
140
|
+
}
|
|
141
|
+
else if (n.name == "ParamName") {
|
|
142
|
+
def(n, "variable");
|
|
143
|
+
}
|
|
144
|
+
else if (n.name == "PayloadName") {
|
|
145
|
+
def(n, "variable");
|
|
146
|
+
}
|
|
147
|
+
else if (n.name == "Decl") {
|
|
148
|
+
// A function's name belongs to the outer scope, and its parameters to the Decl.
|
|
149
|
+
let fn = n.node.getChild("FnProto");
|
|
150
|
+
let name = fn?.getChild("FnName");
|
|
151
|
+
if (name)
|
|
152
|
+
def(name, "function");
|
|
153
|
+
else {
|
|
154
|
+
let proto = n.node.getChild("GlobalVarDecl")?.getChild("VarDeclProto");
|
|
155
|
+
let varName = proto?.getChild("Name");
|
|
156
|
+
if (proto && varName)
|
|
157
|
+
def(varName, varType(proto, doc));
|
|
158
|
+
}
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
else if (ScopeNodes.has(n.name)) {
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
else if (!n.name && n.to - n.from > 8192) {
|
|
165
|
+
for (let d of getScope(doc, n.node))
|
|
166
|
+
defs.push(d);
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
cache.set(node, defs);
|
|
171
|
+
return defs;
|
|
172
|
+
}
|
|
173
|
+
function scopeDefs(doc, node, pos) {
|
|
174
|
+
let defs = getScope(doc, node);
|
|
175
|
+
return OrderedScopes.has(node.name) ? defs.filter((d) => d.pos < pos) : defs;
|
|
176
|
+
}
|
|
177
|
+
const identifier = /^[A-Za-z_][\w]*$/;
|
|
178
|
+
/// Completes the names declared in the file that are in scope at the cursor.
|
|
179
|
+
export const localCompletionSource = (context) => {
|
|
180
|
+
let inner = syntaxTree(context.state).resolveInner(context.pos, -1);
|
|
181
|
+
if (inDontComplete(context))
|
|
182
|
+
return null;
|
|
183
|
+
if (context.matchBefore(/[.@]\w*$/))
|
|
184
|
+
return null;
|
|
185
|
+
let word = context.matchBefore(/[A-Za-z_]\w*$/);
|
|
186
|
+
if (!word && !context.explicit)
|
|
187
|
+
return null;
|
|
188
|
+
let seen = new Set();
|
|
189
|
+
let options = [];
|
|
190
|
+
for (let pos = inner; pos; pos = pos.parent) {
|
|
191
|
+
if (!ScopeNodes.has(pos.name))
|
|
192
|
+
continue;
|
|
193
|
+
for (let d of scopeDefs(context.state.doc, pos, context.pos)) {
|
|
194
|
+
if (seen.has(d.label))
|
|
195
|
+
continue;
|
|
196
|
+
seen.add(d.label);
|
|
197
|
+
options.push({ label: d.label, type: d.type });
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return { options, from: word ? word.from : context.pos, validFor: identifier };
|
|
201
|
+
};
|
|
202
|
+
//// Members after a dot.
|
|
203
|
+
const memberLists = {
|
|
204
|
+
...stdNamespaces,
|
|
205
|
+
"std.StringHashMap": stdNamespaces["std.AutoHashMap"],
|
|
206
|
+
"std.array_list.Managed": stdNamespaces["std.ArrayList"],
|
|
207
|
+
};
|
|
208
|
+
// Turns `std.mem`, `@import("std").mem` or `std.ArrayList(u8)` into a key such as "std.mem".
|
|
209
|
+
function normalisePath(path) {
|
|
210
|
+
return path
|
|
211
|
+
.replace(/@import\(\s*"std"\s*\)/g, "std")
|
|
212
|
+
.replace(/\([^()]*\)/g, "")
|
|
213
|
+
.replace(/\s+/g, "");
|
|
214
|
+
}
|
|
215
|
+
// Finds what names in the file stand for, such as `const mem = std.mem;` or `var list: std.ArrayList(u8) = .empty;`.
|
|
216
|
+
function fileAliases(text) {
|
|
217
|
+
let aliases = new Map();
|
|
218
|
+
let path = String.raw `((?:@import\(\s*"std"\s*\)|[A-Za-z_]\w*)(?:\s*\.\s*[A-Za-z_]\w*|\([^()]*\))*)`;
|
|
219
|
+
let decl = new RegExp(String.raw `\b(?:const|var)\s+([A-Za-z_]\w*)\s*(?::\s*${path}\s*)?(?:=\s*(?:try\s+)?${path})?`, "g");
|
|
220
|
+
for (let [, name, typePath, valuePath] of text.matchAll(decl)) {
|
|
221
|
+
if (typePath)
|
|
222
|
+
aliases.set(name, normalisePath(typePath));
|
|
223
|
+
else if (valuePath)
|
|
224
|
+
aliases.set(name, normalisePath(valuePath));
|
|
225
|
+
}
|
|
226
|
+
// Parameters such as `list: *std.ArrayList(u8)`.
|
|
227
|
+
let param = new RegExp(String.raw `[(,]\s*([A-Za-z_]\w*)\s*:\s*\*?(?:const\s+)?${path}`, "g");
|
|
228
|
+
for (let [, name, typePath] of text.matchAll(param))
|
|
229
|
+
if (!aliases.has(name))
|
|
230
|
+
aliases.set(name, normalisePath(typePath));
|
|
231
|
+
return aliases;
|
|
232
|
+
}
|
|
233
|
+
function resolve(path, aliases) {
|
|
234
|
+
for (let i = 0; i < 8; i++) {
|
|
235
|
+
if (memberLists[path])
|
|
236
|
+
return path;
|
|
237
|
+
let [head, ...rest] = path.split(".");
|
|
238
|
+
let target = aliases.get(head);
|
|
239
|
+
if (target && target != head) {
|
|
240
|
+
path = [target, ...rest].join(".");
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
// `std.ArrayList(u8).empty` is an ArrayList value, so drop the last segment and try again.
|
|
244
|
+
if (rest.length && memberLists[[head, ...rest.slice(0, -1)].join(".")]) {
|
|
245
|
+
let base = [head, ...rest.slice(0, -1)].join(".");
|
|
246
|
+
if (/^std\.(ArrayList|AutoHashMap|StringHashMap|array_list\.Managed)$/.test(base))
|
|
247
|
+
return base;
|
|
248
|
+
}
|
|
249
|
+
return null;
|
|
250
|
+
}
|
|
251
|
+
return null;
|
|
252
|
+
}
|
|
253
|
+
// The pub declarations and fields of a struct, enum or union body.
|
|
254
|
+
function containerMembers(doc, block) {
|
|
255
|
+
let options = [];
|
|
256
|
+
for (let child = block.firstChild; child; child = child.nextSibling) {
|
|
257
|
+
if (child.name == "ContainerField") {
|
|
258
|
+
let name = child.getChild("FieldName") ?? child.getChild("TypeExpr");
|
|
259
|
+
if (name)
|
|
260
|
+
options.push({ label: doc.sliceString(name.from, name.to), type: "property" });
|
|
261
|
+
}
|
|
262
|
+
else if (child.name == "Decl") {
|
|
263
|
+
let fnName = child.getChild("FnProto")?.getChild("FnName");
|
|
264
|
+
let varName = child.getChild("GlobalVarDecl")?.getChild("VarDeclProto")?.getChild("Name");
|
|
265
|
+
if (fnName)
|
|
266
|
+
options.push({ label: doc.sliceString(fnName.from, fnName.to), type: "method" });
|
|
267
|
+
else if (varName)
|
|
268
|
+
options.push({ label: doc.sliceString(varName.from, varName.to), type: "constant" });
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return options.filter((o) => identifier.test(o.label));
|
|
272
|
+
}
|
|
273
|
+
// The ContainerBlock that a declared name such as `Point` stands for, if the file declares one.
|
|
274
|
+
function findContainer(doc, tree, name) {
|
|
275
|
+
let found = null;
|
|
276
|
+
tree.cursor().iterate((n) => {
|
|
277
|
+
if (found)
|
|
278
|
+
return false;
|
|
279
|
+
if (n.name == "Name" && doc.sliceString(n.from, n.to) == name) {
|
|
280
|
+
let value = n.node.parent?.nextSibling;
|
|
281
|
+
let block = value?.getChild("TypeExpr")?.getChild("ContainerDecl")?.getChild("ContainerBlock");
|
|
282
|
+
if (block)
|
|
283
|
+
found = block;
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
return found;
|
|
287
|
+
}
|
|
288
|
+
/// Completes members after a dot: `std.mem.`, `std.fmt.`, `std.ArrayList(u8).`, a list variable,
|
|
289
|
+
/// `self.` inside a struct, or the name of a struct declared in the file.
|
|
290
|
+
export const memberCompletionSource = (context) => {
|
|
291
|
+
let match = context.matchBefore(/(?:@import\(\s*"std"\s*\)|[A-Za-z_]\w*)(?:\s*\.\s*[A-Za-z_]\w*|\([^()]*\))*\s*\.\s*\w*$/);
|
|
292
|
+
if (!match || inDontComplete(context))
|
|
293
|
+
return null;
|
|
294
|
+
let dot = match.text.lastIndexOf(".");
|
|
295
|
+
let word = match.text.slice(dot + 1).trimStart();
|
|
296
|
+
let from = match.to - word.length;
|
|
297
|
+
let path = normalisePath(match.text.slice(0, dot));
|
|
298
|
+
let doc = context.state.doc;
|
|
299
|
+
let aliases = fileAliases(doc.toString());
|
|
300
|
+
let key = resolve(path, aliases);
|
|
301
|
+
if (key) {
|
|
302
|
+
let options = memberLists[key].map((e) => ({ ...e }));
|
|
303
|
+
return { from, options, validFor: identifier };
|
|
304
|
+
}
|
|
305
|
+
// Members of a struct declared in the file.
|
|
306
|
+
let tree = syntaxTree(context.state);
|
|
307
|
+
let head = path.split(".")[0];
|
|
308
|
+
let block = null;
|
|
309
|
+
if ((head == "self" || head == "Self") && !path.includes(".")) {
|
|
310
|
+
for (let n = tree.resolveInner(context.pos, -1); n; n = n.parent)
|
|
311
|
+
if (n.name == "ContainerBlock") {
|
|
312
|
+
block = n;
|
|
313
|
+
break;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
else if (!path.includes(".")) {
|
|
317
|
+
// A variable such as `var p = Point.init();` or `p: Point` has the members of `Point`.
|
|
318
|
+
let typeName = aliases.get(head)?.split(".")[0];
|
|
319
|
+
block = findContainer(doc, tree.topNode, head) ?? (typeName ? findContainer(doc, tree.topNode, typeName) : null);
|
|
320
|
+
}
|
|
321
|
+
if (!block)
|
|
322
|
+
return null;
|
|
323
|
+
return { from, options: containerMembers(doc, block), validFor: identifier };
|
|
324
|
+
};
|
|
325
|
+
/// The completion sources used by `zig()`.
|
|
326
|
+
export const zigCompletionSources = [
|
|
327
|
+
ifNotIn(dontComplete, (context) => {
|
|
328
|
+
if (context.matchBefore(/[.@]\w*$/))
|
|
329
|
+
return null;
|
|
330
|
+
return completeFromList([...snippets, ...keywords])(context);
|
|
331
|
+
}),
|
|
332
|
+
builtinCompletionSource,
|
|
333
|
+
memberCompletionSource,
|
|
334
|
+
localCompletionSource,
|
|
335
|
+
];
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { LRLanguage, LanguageSupport } from "@codemirror/language";
|
|
2
|
+
export { keywords, snippets, builtinCompletionSource, localCompletionSource, memberCompletionSource, zigCompletionSources, } from "./complete.js";
|
|
3
|
+
export declare const zigLanguage: LRLanguage;
|
|
4
|
+
export declare function zig(): LanguageSupport;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { LRLanguage, indentNodeProp, continuedIndent, delimitedIndent, foldNodeProp, foldInside, LanguageSupport, } from "@codemirror/language";
|
|
2
|
+
import { parser } from "@exercism/lezer-zig";
|
|
3
|
+
import { zigCompletionSources } from "./complete.js";
|
|
4
|
+
export { keywords, snippets, builtinCompletionSource, localCompletionSource, memberCompletionSource, zigCompletionSources, } from "./complete.js";
|
|
5
|
+
export const zigLanguage = LRLanguage.define({
|
|
6
|
+
name: "zig",
|
|
7
|
+
parser: parser.configure({
|
|
8
|
+
props: [
|
|
9
|
+
indentNodeProp.add({
|
|
10
|
+
"InitList Block ErrBlock SwitchBlock ContainerBlock": delimitedIndent({ closing: "}" }),
|
|
11
|
+
"ParamDeclList FnCallArgs": delimitedIndent({ closing: ")", align: true }),
|
|
12
|
+
AsmParams: delimitedIndent({ closing: ")", align: false }),
|
|
13
|
+
"AsmOutList AsmInList AsmClobList": (context) => context.baseIndent + 2, // ': '
|
|
14
|
+
SwitchProng: (context) => context.baseIndent + context.unit,
|
|
15
|
+
IfStatement: continuedIndent({ except: /^\s*({|else\b)/ }),
|
|
16
|
+
MultiStringLiteral: continuedIndent(),
|
|
17
|
+
WhileStatement: continuedIndent(),
|
|
18
|
+
}),
|
|
19
|
+
foldNodeProp.add({
|
|
20
|
+
"InitList Block ErrBlock SwitchBlock ContainerBlock ParamDeclList FnCallArgs AsmParams": foldInside,
|
|
21
|
+
}),
|
|
22
|
+
],
|
|
23
|
+
}),
|
|
24
|
+
languageData: {
|
|
25
|
+
commentTokens: { line: "//" },
|
|
26
|
+
closeBrackets: { brackets: ["(", "[", "{", "'", '"'] },
|
|
27
|
+
indentOnInput: /^\s*(?:\{|\})$/,
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
/// Zig support, with highlighting, indentation, folding and completion.
|
|
31
|
+
export function zig() {
|
|
32
|
+
return new LanguageSupport(zigLanguage, zigCompletionSources.map((autocomplete) => zigLanguage.data.of({ autocomplete })));
|
|
33
|
+
}
|