@exercism/codemirror-lang-erlang 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.
@@ -0,0 +1,152 @@
1
+ import { LRParser } from '@lezer/lr';
2
+ import { LanguageSupport, LRLanguage } from '@codemirror/language';
3
+
4
+ declare const parser: LRParser
5
+
6
+ // ── Snippets ──────────────────────────────────────────────────────────────────
7
+ /**
8
+ Snippets for the constructs people write most often in Erlang.
9
+ */
10
+ declare const snippets = [
11
+ snip("${name}(${Args}) ->\n\t${}.", {
12
+ label: "function",
13
+ detail: "clause",
14
+ type: "keyword"
15
+ }),
16
+ snip("${name}(${Args}) when ${Guard} ->\n\t${}.", {
17
+ label: "function",
18
+ detail: "clause with guard",
19
+ type: "keyword"
20
+ }),
21
+ snip("case ${Expression} of\n\t${Pattern} ->\n\t\t${}\nend", {
22
+ label: "case",
23
+ detail: "expression",
24
+ type: "keyword"
25
+ }),
26
+ snip("if\n\t${Guard} ->\n\t\t${};\n\ttrue ->\n\t\t${}\nend", {
27
+ label: "if",
28
+ detail: "expression",
29
+ type: "keyword"
30
+ }),
31
+ snip("receive\n\t${Pattern} ->\n\t\t${}\nend", {
32
+ label: "receive",
33
+ detail: "expression",
34
+ type: "keyword"
35
+ }),
36
+ snip("receive\n\t${Pattern} ->\n\t\t${}\nafter ${Timeout} ->\n\t${}\nend", {
37
+ label: "receive",
38
+ detail: "with after",
39
+ type: "keyword"
40
+ }),
41
+ snip("fun(${Args}) ->\n\t${}\nend", {
42
+ label: "fun",
43
+ detail: "anonymous function",
44
+ type: "keyword"
45
+ }),
46
+ snip("try ${Expression} of\n\t${Pattern} ->\n\t\t${}\ncatch\n\t${Class}:${Reason} ->\n\t\t${}\nend", {
47
+ label: "try",
48
+ detail: "of catch",
49
+ type: "keyword"
50
+ }),
51
+ snip("begin\n\t${}\nend", {
52
+ label: "begin",
53
+ detail: "block",
54
+ type: "keyword"
55
+ })
56
+ ];
57
+ /**
58
+ Completes Erlang: keywords, guards, built-in functions, types, modules,
59
+ snippets, the functions of common modules after `lists:` and the like, and
60
+ the names declared in the file. After `?` it offers macros, after `#`
61
+ records, and after a record's `.` or inside its braces its fields.
62
+ Variables come from the clause around the cursor. It offers nothing in
63
+ strings and comments.
64
+ */
65
+ declare const erlangCompletionSource = (context) => {
66
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
67
+ const { state, pos } = context;
68
+ const line = state.doc.lineAt(pos);
69
+ const before = state.sliceDoc(line.from, pos);
70
+ // `-mod‸` at the start of a line names an attribute.
71
+ const attribute = /^\s*-\s*([a-z][\w@]*)?$/.exec(before);
72
+ if (attribute) {
73
+ return { from: pos - ((_b = (_a = attribute[1]) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0), options: attributeSnippets, validFor: /^[\w@]*$/ };
74
+ }
75
+ // `lists:fo‸` asks for a function from another module. A module listed
76
+ // here gets its functions, and any other module gets nothing, because the
77
+ // file says nothing about what it exports.
78
+ const remote = /(?:^|[^\w@?#'])([a-z][\w@]*):([a-z][\w@]*)?$/.exec(before);
79
+ if (remote) {
80
+ const options = moduleCompletions[remote[1]];
81
+ if (!options)
82
+ return null;
83
+ return { from: pos - ((_d = (_c = remote[2]) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0), options, validFor: /^[\w@]*$/ };
84
+ }
85
+ const inner = syntaxTree(state).resolveInner(pos, -1);
86
+ // `?MA‸` and `??Arg` ask for a macro.
87
+ const macro = /\?\??([\w@]*)$/.exec(before);
88
+ if (macro) {
89
+ const decls = gatherDeclarations(state);
90
+ const options = [...predefinedMacros];
91
+ for (const [label, args] of decls.macros)
92
+ options.push({ label, type: "constant", detail: args == null ? "macro" : `macro/${args}` });
93
+ return { from: pos - macro[1].length, options, validFor: /^[\w@]*$/ };
94
+ }
95
+ // `#po‸` asks for a record, and `#point.‸` for one of its fields.
96
+ const recordField = /#([a-z][\w@]*)\.([a-z][\w@]*)?$/.exec(before);
97
+ if (recordField) {
98
+ const fields = (_e = gatherDeclarations(state).records.get(recordField[1])) !== null && _e !== void 0 ? _e : [];
99
+ return {
100
+ from: pos - ((_g = (_f = recordField[2]) === null || _f === void 0 ? void 0 : _f.length) !== null && _g !== void 0 ? _g : 0),
101
+ options: fields.map(label => ({ label, type: "property" })),
102
+ validFor: /^[\w@]*$/
103
+ };
104
+ }
105
+ const record = /#([a-z][\w@]*)?$/.exec(before);
106
+ if (record) {
107
+ const records = gatherDeclarations(state).records;
108
+ return {
109
+ from: pos - ((_j = (_h = record[1]) === null || _h === void 0 ? void 0 : _h.length) !== null && _j !== void 0 ? _j : 0),
110
+ options: [...records.keys()].map(label => ({ label, type: "class" })),
111
+ validFor: /^[\w@]*$/
112
+ };
113
+ }
114
+ const typed = context.matchBefore(word);
115
+ if (typed.from == typed.to && !context.explicit)
116
+ return null;
117
+ const from = typed.from;
118
+ const decls = gatherDeclarations(state);
119
+ const formStart = Math.max(0, pos - 2000);
120
+ const recent = state.sliceDoc(formStart, pos);
121
+ // In `-export([ad‸` and after `fun ad‸`, a function is named with its arity.
122
+ if (/-\s*export\s*\(\s*\[[^\]]*$/.test(recent) || /\bfun\s+[a-z][\w@]*$/.test(before)) {
123
+ return { from, options: functionOptions(decls, true), validFor: /^[\w@]*$/ };
124
+ }
125
+ const options = [
126
+ ...variablesInScope(state, inner, { from, to: pos }),
127
+ ...functionOptions(decls, false)
128
+ ];
129
+ const recordName = enclosingRecord(state, inner);
130
+ if (recordName)
131
+ for (const label of (_k = decls.records.get(recordName)) !== null && _k !== void 0 ? _k : [])
132
+ options.push({ label, type: "property" });
133
+ return { from, options, validFor: /^[\w@]*$/ };
134
+ };
135
+ declare const erlangCompletion = context => {
136
+ const before = context.state.sliceDoc(context.state.doc.lineAt(context.pos).from, context.pos);
137
+ if (/^\s*-\s*[\w@]*$/.test(before))
138
+ return null;
139
+ if (/[:?#.][\w@]*$/.test(before))
140
+ return null;
141
+ return fixedList(context);
142
+ };
143
+
144
+ declare const erlangLanguage: LRLanguage;
145
+ /**
146
+ Erlang language support, with completion for keywords, built-in functions,
147
+ the functions of common modules, snippets and the names declared in the
148
+ file.
149
+ */
150
+ declare function erlang(): LanguageSupport;
151
+
152
+ export { erlang, erlangCompletion, erlangCompletionSource, erlangLanguage, parser, snippets };
@@ -0,0 +1,152 @@
1
+ import { LRParser } from '@lezer/lr';
2
+ import { LanguageSupport, LRLanguage } from '@codemirror/language';
3
+
4
+ declare const parser: LRParser
5
+
6
+ // ── Snippets ──────────────────────────────────────────────────────────────────
7
+ /**
8
+ Snippets for the constructs people write most often in Erlang.
9
+ */
10
+ declare const snippets = [
11
+ snip("${name}(${Args}) ->\n\t${}.", {
12
+ label: "function",
13
+ detail: "clause",
14
+ type: "keyword"
15
+ }),
16
+ snip("${name}(${Args}) when ${Guard} ->\n\t${}.", {
17
+ label: "function",
18
+ detail: "clause with guard",
19
+ type: "keyword"
20
+ }),
21
+ snip("case ${Expression} of\n\t${Pattern} ->\n\t\t${}\nend", {
22
+ label: "case",
23
+ detail: "expression",
24
+ type: "keyword"
25
+ }),
26
+ snip("if\n\t${Guard} ->\n\t\t${};\n\ttrue ->\n\t\t${}\nend", {
27
+ label: "if",
28
+ detail: "expression",
29
+ type: "keyword"
30
+ }),
31
+ snip("receive\n\t${Pattern} ->\n\t\t${}\nend", {
32
+ label: "receive",
33
+ detail: "expression",
34
+ type: "keyword"
35
+ }),
36
+ snip("receive\n\t${Pattern} ->\n\t\t${}\nafter ${Timeout} ->\n\t${}\nend", {
37
+ label: "receive",
38
+ detail: "with after",
39
+ type: "keyword"
40
+ }),
41
+ snip("fun(${Args}) ->\n\t${}\nend", {
42
+ label: "fun",
43
+ detail: "anonymous function",
44
+ type: "keyword"
45
+ }),
46
+ snip("try ${Expression} of\n\t${Pattern} ->\n\t\t${}\ncatch\n\t${Class}:${Reason} ->\n\t\t${}\nend", {
47
+ label: "try",
48
+ detail: "of catch",
49
+ type: "keyword"
50
+ }),
51
+ snip("begin\n\t${}\nend", {
52
+ label: "begin",
53
+ detail: "block",
54
+ type: "keyword"
55
+ })
56
+ ];
57
+ /**
58
+ Completes Erlang: keywords, guards, built-in functions, types, modules,
59
+ snippets, the functions of common modules after `lists:` and the like, and
60
+ the names declared in the file. After `?` it offers macros, after `#`
61
+ records, and after a record's `.` or inside its braces its fields.
62
+ Variables come from the clause around the cursor. It offers nothing in
63
+ strings and comments.
64
+ */
65
+ declare const erlangCompletionSource = (context) => {
66
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
67
+ const { state, pos } = context;
68
+ const line = state.doc.lineAt(pos);
69
+ const before = state.sliceDoc(line.from, pos);
70
+ // `-mod‸` at the start of a line names an attribute.
71
+ const attribute = /^\s*-\s*([a-z][\w@]*)?$/.exec(before);
72
+ if (attribute) {
73
+ return { from: pos - ((_b = (_a = attribute[1]) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0), options: attributeSnippets, validFor: /^[\w@]*$/ };
74
+ }
75
+ // `lists:fo‸` asks for a function from another module. A module listed
76
+ // here gets its functions, and any other module gets nothing, because the
77
+ // file says nothing about what it exports.
78
+ const remote = /(?:^|[^\w@?#'])([a-z][\w@]*):([a-z][\w@]*)?$/.exec(before);
79
+ if (remote) {
80
+ const options = moduleCompletions[remote[1]];
81
+ if (!options)
82
+ return null;
83
+ return { from: pos - ((_d = (_c = remote[2]) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0), options, validFor: /^[\w@]*$/ };
84
+ }
85
+ const inner = syntaxTree(state).resolveInner(pos, -1);
86
+ // `?MA‸` and `??Arg` ask for a macro.
87
+ const macro = /\?\??([\w@]*)$/.exec(before);
88
+ if (macro) {
89
+ const decls = gatherDeclarations(state);
90
+ const options = [...predefinedMacros];
91
+ for (const [label, args] of decls.macros)
92
+ options.push({ label, type: "constant", detail: args == null ? "macro" : `macro/${args}` });
93
+ return { from: pos - macro[1].length, options, validFor: /^[\w@]*$/ };
94
+ }
95
+ // `#po‸` asks for a record, and `#point.‸` for one of its fields.
96
+ const recordField = /#([a-z][\w@]*)\.([a-z][\w@]*)?$/.exec(before);
97
+ if (recordField) {
98
+ const fields = (_e = gatherDeclarations(state).records.get(recordField[1])) !== null && _e !== void 0 ? _e : [];
99
+ return {
100
+ from: pos - ((_g = (_f = recordField[2]) === null || _f === void 0 ? void 0 : _f.length) !== null && _g !== void 0 ? _g : 0),
101
+ options: fields.map(label => ({ label, type: "property" })),
102
+ validFor: /^[\w@]*$/
103
+ };
104
+ }
105
+ const record = /#([a-z][\w@]*)?$/.exec(before);
106
+ if (record) {
107
+ const records = gatherDeclarations(state).records;
108
+ return {
109
+ from: pos - ((_j = (_h = record[1]) === null || _h === void 0 ? void 0 : _h.length) !== null && _j !== void 0 ? _j : 0),
110
+ options: [...records.keys()].map(label => ({ label, type: "class" })),
111
+ validFor: /^[\w@]*$/
112
+ };
113
+ }
114
+ const typed = context.matchBefore(word);
115
+ if (typed.from == typed.to && !context.explicit)
116
+ return null;
117
+ const from = typed.from;
118
+ const decls = gatherDeclarations(state);
119
+ const formStart = Math.max(0, pos - 2000);
120
+ const recent = state.sliceDoc(formStart, pos);
121
+ // In `-export([ad‸` and after `fun ad‸`, a function is named with its arity.
122
+ if (/-\s*export\s*\(\s*\[[^\]]*$/.test(recent) || /\bfun\s+[a-z][\w@]*$/.test(before)) {
123
+ return { from, options: functionOptions(decls, true), validFor: /^[\w@]*$/ };
124
+ }
125
+ const options = [
126
+ ...variablesInScope(state, inner, { from, to: pos }),
127
+ ...functionOptions(decls, false)
128
+ ];
129
+ const recordName = enclosingRecord(state, inner);
130
+ if (recordName)
131
+ for (const label of (_k = decls.records.get(recordName)) !== null && _k !== void 0 ? _k : [])
132
+ options.push({ label, type: "property" });
133
+ return { from, options, validFor: /^[\w@]*$/ };
134
+ };
135
+ declare const erlangCompletion = context => {
136
+ const before = context.state.sliceDoc(context.state.doc.lineAt(context.pos).from, context.pos);
137
+ if (/^\s*-\s*[\w@]*$/.test(before))
138
+ return null;
139
+ if (/[:?#.][\w@]*$/.test(before))
140
+ return null;
141
+ return fixedList(context);
142
+ };
143
+
144
+ declare const erlangLanguage: LRLanguage;
145
+ /**
146
+ Erlang language support, with completion for keywords, built-in functions,
147
+ the functions of common modules, snippets and the names declared in the
148
+ file.
149
+ */
150
+ declare function erlang(): LanguageSupport;
151
+
152
+ export { erlang, erlangCompletion, erlangCompletionSource, erlangLanguage, parser, snippets };