@exercism/codemirror-lang-elixir 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/dist/index.cjs ADDED
@@ -0,0 +1,715 @@
1
+ 'use strict';
2
+
3
+ var language = require('@codemirror/language');
4
+ var state = require('@codemirror/state');
5
+ var view = require('@codemirror/view');
6
+ var lezerElixir = require('lezer-elixir');
7
+ var autocomplete = require('@codemirror/autocomplete');
8
+ var common = require('@lezer/common');
9
+
10
+ /**
11
+ A syntax provider based on the [Lezer Elixir parser](https://github.com/lezer-parser/elixir),
12
+ extended with highlighting and indentation information.
13
+ */
14
+ const elixirLanguage = language.LRLanguage.define({
15
+ name: "elixir",
16
+ parser: lezerElixir.parser.configure({
17
+ props: [
18
+ language.indentNodeProp.add({
19
+ "DoBlock AfterBlock ElseBlock CatchBlock RescueBlock": withContinuedStabClause(language.continuedIndent({
20
+ except: /^\s*(after|else|catch|rescue|end)\b/,
21
+ })),
22
+ AnonymousFunction: withContinuedStabClause(language.delimitedIndent({ closing: "end", align: false })),
23
+ Block: withContinuedStabClause(language.delimitedIndent({ closing: ")", align: false })),
24
+ StabClause: language.continuedIndent(),
25
+ List: language.delimitedIndent({ closing: "]", align: false }),
26
+ Tuple: language.delimitedIndent({ closing: "}", align: false }),
27
+ Bitstring: language.delimitedIndent({ closing: ">>", align: false }),
28
+ Arguments: language.delimitedIndent({ closing: ")", align: false }),
29
+ Map: language.delimitedIndent({ closing: "}", align: false }),
30
+ "String Charlist Sigil": language.flatIndent,
31
+ BinaryOperator: language.continuedIndent(),
32
+ Pair: language.continuedIndent(),
33
+ }),
34
+ language.foldNodeProp.add({
35
+ "DoBlock Block List Tuple Bitstring AnonymousFunction": language.foldInside,
36
+ Map: foldMap,
37
+ }),
38
+ ],
39
+ }),
40
+ languageData: {
41
+ commentTokens: { line: "#" },
42
+ closeBrackets: {
43
+ brackets: ["(", "[", "{", "'", '"', "'''", '"""'],
44
+ // Built-in sigils
45
+ stringPrefixes: ["~s", "~S", "~r", "~R", "~c", "~C", "~D", "~N"],
46
+ },
47
+ indentOnInput: /^\s*([\}\]\)]|>>|after|else|catch|rescue|end)|.*->$/,
48
+ },
49
+ });
50
+ function withContinuedStabClause(baseStrategy) {
51
+ return (context) => {
52
+ var _a, _b, _c, _d;
53
+ const before = context.node.childBefore(context.pos);
54
+ // If end is being introduced, use the top-level node indentation
55
+ if (((_b = (_a = context.node.lastChild) === null || _a === void 0 ? void 0 : _a.type) === null || _b === void 0 ? void 0 : _b.name) === "end" &&
56
+ context.textAfter.endsWith("end")) {
57
+ return context.baseIndentFor(context.node);
58
+ }
59
+ // If a new stab clause is introduced, we give it the same indentation
60
+ // as the previous one
61
+ if (((_c = before === null || before === void 0 ? void 0 : before.type) === null || _c === void 0 ? void 0 : _c.name) === "StabClause" &&
62
+ context.textAfter.endsWith("->")) {
63
+ return context.baseIndentFor(before);
64
+ }
65
+ // If we are positioned right after a child stab clause, we keep
66
+ // that stab clause's indentation
67
+ if (((_d = before === null || before === void 0 ? void 0 : before.type) === null || _d === void 0 ? void 0 : _d.name) === "StabClause") {
68
+ return context.baseIndentFor(before) + context.unit;
69
+ }
70
+ return baseStrategy(context);
71
+ };
72
+ }
73
+ function foldMap(context) {
74
+ const open = context.getChild("{");
75
+ const close = context.getChild("}");
76
+ if (open && close) {
77
+ return { from: open.to, to: close.from };
78
+ }
79
+ else {
80
+ return null;
81
+ }
82
+ }
83
+
84
+ /**
85
+ This command, when invoked inside a do-block or an anonymous
86
+ function, inserts the closing end when applicable.
87
+ */
88
+ const insertNewlineCloseEnd = (view) => {
89
+ const { state: state$1, dispatch } = view;
90
+ const tree = language.syntaxTree(state$1);
91
+ const indentationUnit = language.getIndentUnit(state$1);
92
+ let apply = true;
93
+ const changes = state$1.changeByRange((range) => {
94
+ if (!range.empty || !elixirLanguage.isActiveAt(state$1, range.from)) {
95
+ apply = false;
96
+ return { range };
97
+ }
98
+ const node = tree.resolve(range.from, -1);
99
+ const parent = endDelimitedParent(node);
100
+ if (parent && !checkAllClosed(parent)) {
101
+ const indentation = language.getIndentation(state$1, parent.from + 1) || indentationUnit;
102
+ const innerIndent = language.indentString(state$1, indentation);
103
+ const outerIndent = language.indentString(state$1, indentation - indentationUnit);
104
+ const insertPre = `\n${innerIndent}`;
105
+ const insertPost = `\n${outerIndent}end`;
106
+ const newPos = range.from + insertPre.length;
107
+ return {
108
+ range: state.EditorSelection.cursor(newPos),
109
+ changes: [
110
+ { from: range.from, to: range.to, insert: insertPre + insertPost },
111
+ ],
112
+ };
113
+ }
114
+ apply = false;
115
+ return { range };
116
+ });
117
+ if (apply) {
118
+ dispatch(state$1.update(changes, { scrollIntoView: true, userEvent: "input" }));
119
+ return true;
120
+ }
121
+ else {
122
+ return false;
123
+ }
124
+ };
125
+ function endDelimitedParent(node) {
126
+ let cur = node;
127
+ if (cur && cur.type.name === "do") {
128
+ cur = cur.parent;
129
+ }
130
+ if (cur && cur.type.name === "Operator") {
131
+ cur = cur.parent;
132
+ }
133
+ if (cur && (cur.type.name === "StabClause" || cur.type.name === "fn")) {
134
+ cur = cur.parent;
135
+ }
136
+ if (cur && isEndDelimited(cur)) {
137
+ return cur;
138
+ }
139
+ else {
140
+ return null;
141
+ }
142
+ }
143
+ function isEndDelimited(node) {
144
+ return node.type.name === "DoBlock" || node.type.name === "AnonymousFunction";
145
+ }
146
+ function checkAllClosed(node) {
147
+ var _a, _b;
148
+ let cur = node;
149
+ while (cur) {
150
+ if (isEndDelimited(cur)) {
151
+ if (((_b = (_a = cur.lastChild) === null || _a === void 0 ? void 0 : _a.type) === null || _b === void 0 ? void 0 : _b.name) !== "end") {
152
+ return false;
153
+ }
154
+ }
155
+ cur = cur.parent;
156
+ }
157
+ return true;
158
+ }
159
+
160
+ // Public functions and macros of common standard library modules, with their arities.
161
+ // bin/stdlib.exs generates this file from Elixir 1.20.1. Hidden and deprecated ones are left out.
162
+ const stdlib = {
163
+ "Access": [["all", [0]], ["at", [1]], ["at!", [1]], ["elem", [1]], ["fetch", [2]], ["fetch!", [2]], ["filter", [1]], ["find", [1]], ["get", [2, 3]], ["get_and_update", [3]], ["key", [1, 2]], ["key!", [1]], ["pop", [2]], ["slice", [1]], ["values", [0]]],
164
+ "Agent": [["cast", [2, 4]], ["child_spec", [1]], ["get", [2, 3, 4, 5]], ["get_and_update", [2, 3, 4, 5]], ["start", [1, 2, 3, 4]], ["start_link", [1, 2, 3, 4]], ["stop", [1, 2, 3]], ["update", [2, 3, 4, 5]]],
165
+ "Atom": [["to_charlist", [1]], ["to_string", [1]]],
166
+ "Base": [["decode16", [1, 2]], ["decode16!", [1, 2]], ["decode32", [1, 2]], ["decode32!", [1, 2]], ["decode64", [1, 2]], ["decode64!", [1, 2]], ["encode16", [1, 2]], ["encode32", [1, 2]], ["encode64", [1, 2]], ["hex_decode32", [1, 2]], ["hex_decode32!", [1, 2]], ["hex_encode32", [1, 2]], ["hex_valid32?", [1, 2]], ["url_decode64", [1, 2]], ["url_decode64!", [1, 2]], ["url_encode64", [1, 2]], ["url_valid64?", [1, 2]], ["valid16?", [1, 2]], ["valid32?", [1, 2]], ["valid64?", [1, 2]]],
167
+ "Bitwise": [["band", [2]], ["bnot", [1]], ["bor", [2]], ["bsl", [2]], ["bsr", [2]], ["bxor", [2]]],
168
+ "Date": [["add", [2]], ["after?", [2]], ["before?", [2]], ["beginning_of_month", [1]], ["beginning_of_week", [1, 2]], ["compare", [2]], ["convert", [2]], ["convert!", [2]], ["day_of_era", [1]], ["day_of_week", [1, 2]], ["day_of_year", [1]], ["days_in_month", [1]], ["diff", [2]], ["end_of_month", [1]], ["end_of_week", [1, 2]], ["from_erl", [1, 2]], ["from_erl!", [1, 2]], ["from_gregorian_days", [1, 2]], ["from_iso8601", [1, 2]], ["from_iso8601!", [1, 2]], ["leap_year?", [1]], ["months_in_year", [1]], ["new", [3, 4]], ["new!", [3, 4]], ["quarter_of_year", [1]], ["range", [2, 3]], ["shift", [2]], ["to_erl", [1]], ["to_gregorian_days", [1]], ["to_iso8601", [1, 2]], ["to_string", [1]], ["utc_today", [0, 1]], ["year_of_era", [1]]],
169
+ "DateTime": [["add", [2, 3, 4]], ["after?", [2]], ["before?", [2]], ["compare", [2]], ["convert", [2]], ["convert!", [2]], ["diff", [2, 3]], ["from_gregorian_seconds", [1, 2, 3]], ["from_iso8601", [1, 2, 3]], ["from_naive", [2, 3]], ["from_naive!", [2, 3]], ["from_unix", [1, 2, 3]], ["from_unix!", [1, 2, 3]], ["new", [2, 3, 4]], ["new!", [2, 3, 4]], ["now", [1, 2]], ["now!", [1, 2]], ["shift", [2, 3]], ["shift_zone", [2, 3]], ["shift_zone!", [2, 3]], ["to_date", [1]], ["to_gregorian_seconds", [1]], ["to_iso8601", [1, 2, 3]], ["to_naive", [1]], ["to_string", [1]], ["to_time", [1]], ["to_unix", [1, 2]], ["truncate", [2]], ["utc_now", [0, 1, 2]]],
170
+ "Enum": [["all?", [1, 2]], ["any?", [1, 2]], ["at", [2, 3]], ["chunk_by", [2]], ["chunk_every", [2, 3, 4]], ["chunk_while", [4]], ["concat", [1, 2]], ["count", [1, 2]], ["count_until", [2, 3]], ["dedup", [1]], ["dedup_by", [2]], ["drop", [2]], ["drop_every", [2]], ["drop_while", [2]], ["each", [2]], ["empty?", [1]], ["fetch", [2]], ["fetch!", [2]], ["filter", [2]], ["find", [2, 3]], ["find_index", [2]], ["find_value", [2, 3]], ["flat_map", [2]], ["flat_map_reduce", [3]], ["frequencies", [1]], ["frequencies_by", [2]], ["group_by", [2, 3]], ["intersperse", [2]], ["into", [2, 3]], ["join", [1, 2]], ["map", [2]], ["map_every", [3]], ["map_intersperse", [3]], ["map_join", [2, 3]], ["map_reduce", [3]], ["max", [1, 2, 3]], ["max_by", [2, 3, 4]], ["member?", [2]], ["min", [1, 2, 3]], ["min_by", [2, 3, 4]], ["min_max", [1, 2, 3]], ["min_max_by", [2, 3, 4]], ["product", [1]], ["product_by", [2]], ["random", [1]], ["reduce", [2, 3]], ["reduce_while", [3]], ["reject", [2]], ["reverse", [1, 2]], ["reverse_slice", [3]], ["scan", [2, 3]], ["shuffle", [1]], ["slice", [2, 3]], ["slide", [3]], ["sort", [1, 2]], ["sort_by", [2, 3]], ["split", [2]], ["split_while", [2]], ["split_with", [2]], ["sum", [1]], ["sum_by", [2]], ["take", [2]], ["take_every", [2]], ["take_random", [2]], ["take_while", [2]], ["to_list", [1]], ["uniq", [1]], ["uniq_by", [2]], ["unzip", [1]], ["with_index", [1, 2]], ["zip", [1, 2]], ["zip_reduce", [3, 4]], ["zip_with", [2, 3]]],
171
+ "Float": [["ceil", [1, 2]], ["floor", [1, 2]], ["max_finite", [0]], ["min_finite", [0]], ["parse", [1]], ["pow", [2]], ["ratio", [1]], ["round", [1, 2]], ["to_charlist", [1]], ["to_string", [1]]],
172
+ "Function": [["capture", [3]], ["identity", [1]], ["info", [1, 2]]],
173
+ "GenServer": [["abcast", [2, 3]], ["call", [2, 3]], ["cast", [2]], ["multi_call", [2, 3, 4]], ["reply", [2]], ["start", [2, 3]], ["start_link", [2, 3]], ["stop", [1, 2, 3]], ["whereis", [1]]],
174
+ "IO": [["binread", [1, 2]], ["binstream", [0, 1, 2]], ["binwrite", [1, 2]], ["chardata_to_string", [1]], ["getn", [1, 2, 3]], ["gets", [1, 2]], ["inspect", [1, 2, 3]], ["iodata_empty?", [1]], ["iodata_length", [1]], ["iodata_to_binary", [1]], ["puts", [1, 2]], ["read", [1, 2]], ["stream", [0, 1, 2]], ["warn", [1, 2]], ["write", [1, 2]]],
175
+ "Integer": [["ceil_div", [2]], ["digits", [1, 2]], ["extended_gcd", [2]], ["floor_div", [2]], ["gcd", [2]], ["is_even", [1]], ["is_odd", [1]], ["mod", [2]], ["parse", [1, 2]], ["popcount", [1]], ["pow", [2]], ["to_charlist", [1, 2]], ["to_string", [1, 2]], ["undigits", [1, 2]]],
176
+ "Kernel": [["abs", [1]], ["alias!", [1]], ["and", [2]], ["apply", [2, 3]], ["binary_part", [3]], ["binary_slice", [2, 3]], ["binding", [0, 1]], ["bit_size", [1]], ["byte_size", [1]], ["ceil", [1]], ["dbg", [0, 1, 2]], ["def", [1, 2]], ["defdelegate", [2]], ["defexception", [1]], ["defguard", [1]], ["defguardp", [1]], ["defimpl", [2, 3]], ["defmacro", [1, 2]], ["defmacrop", [1, 2]], ["defmodule", [2]], ["defoverridable", [1]], ["defp", [1, 2]], ["defprotocol", [2]], ["defstruct", [1]], ["destructure", [2]], ["div", [2]], ["elem", [2]], ["exit", [1]], ["floor", [1]], ["function_exported?", [3]], ["get_and_update_in", [2, 3]], ["get_in", [1, 2]], ["hd", [1]], ["if", [2]], ["in", [2]], ["inspect", [1, 2]], ["is_atom", [1]], ["is_binary", [1]], ["is_bitstring", [1]], ["is_boolean", [1]], ["is_exception", [1, 2]], ["is_float", [1]], ["is_function", [1, 2]], ["is_integer", [1]], ["is_list", [1]], ["is_map", [1]], ["is_map_key", [2]], ["is_nil", [1]], ["is_non_struct_map", [1]], ["is_number", [1]], ["is_pid", [1]], ["is_port", [1]], ["is_reference", [1]], ["is_struct", [1, 2]], ["is_tuple", [1]], ["length", [1]], ["macro_exported?", [3]], ["make_ref", [0]], ["map_size", [1]], ["match?", [2]], ["max", [2]], ["min", [2]], ["node", [0, 1]], ["not", [1]], ["or", [2]], ["pop_in", [1, 2]], ["put_elem", [3]], ["put_in", [2, 3]], ["raise", [1, 2]], ["rem", [2]], ["reraise", [2, 3]], ["round", [1]], ["self", [0]], ["send", [2]], ["spawn", [1, 3]], ["spawn_link", [1, 3]], ["spawn_monitor", [1, 3]], ["struct", [1, 2]], ["struct!", [1, 2]], ["tap", [2]], ["then", [2]], ["throw", [1]], ["tl", [1]], ["to_charlist", [1]], ["to_string", [1]], ["to_timeout", [1]], ["trunc", [1]], ["tuple_size", [1]], ["update_in", [2, 3]], ["use", [1, 2]], ["var!", [1, 2]]],
177
+ "Keyword": [["delete", [2]], ["delete_first", [2]], ["drop", [2]], ["equal?", [2]], ["fetch", [2]], ["fetch!", [2]], ["filter", [2]], ["from_keys", [2]], ["get", [2, 3]], ["get_and_update", [3]], ["get_and_update!", [3]], ["get_lazy", [3]], ["get_values", [2]], ["has_key?", [2]], ["intersect", [2, 3]], ["keys", [1]], ["keyword?", [1]], ["merge", [2, 3]], ["new", [0, 1, 2]], ["pop", [2, 3]], ["pop!", [2]], ["pop_first", [2, 3]], ["pop_lazy", [3]], ["pop_values", [2]], ["put", [3]], ["put_new", [3]], ["put_new_lazy", [3]], ["reject", [2]], ["replace", [3]], ["replace!", [3]], ["replace_lazy", [3]], ["split", [2]], ["split_with", [2]], ["take", [2]], ["to_list", [1]], ["update", [4]], ["update!", [3]], ["validate", [2]], ["validate!", [2]], ["values", [1]]],
178
+ "List": [["ascii_printable?", [1, 2]], ["delete", [2]], ["delete_at", [2]], ["duplicate", [2]], ["ends_with?", [2]], ["first", [1, 2]], ["first!", [1]], ["flatten", [1, 2]], ["foldl", [3]], ["foldr", [3]], ["improper?", [1]], ["insert_at", [3]], ["keydelete", [3]], ["keyfind", [3, 4]], ["keyfind!", [3]], ["keymember?", [3]], ["keyreplace", [4]], ["keysort", [2, 3]], ["keystore", [4]], ["keytake", [3]], ["last", [1, 2]], ["last!", [1]], ["myers_difference", [2, 3]], ["pop_at", [2, 3]], ["replace_at", [3]], ["starts_with?", [2]], ["to_atom", [1]], ["to_charlist", [1]], ["to_existing_atom", [1]], ["to_float", [1]], ["to_integer", [1, 2]], ["to_string", [1]], ["to_tuple", [1]], ["update_at", [3]], ["wrap", [1]]],
179
+ "Map": [["delete", [2]], ["drop", [2]], ["equal?", [2]], ["fetch", [2]], ["fetch!", [2]], ["filter", [2]], ["from_keys", [2]], ["from_struct", [1]], ["get", [2, 3]], ["get_and_update", [3]], ["get_and_update!", [3]], ["get_lazy", [3]], ["has_key?", [2]], ["intersect", [2, 3]], ["keys", [1]], ["merge", [2, 3]], ["new", [0, 1, 2]], ["pop", [2, 3]], ["pop!", [2]], ["pop_lazy", [3]], ["put", [3]], ["put_new", [3]], ["put_new_lazy", [3]], ["reject", [2]], ["replace", [3]], ["replace!", [3]], ["replace_lazy", [3]], ["split", [2]], ["split_with", [2]], ["take", [2]], ["to_list", [1]], ["update", [4]], ["update!", [3]], ["values", [1]]],
180
+ "MapSet": [["delete", [2]], ["difference", [2]], ["disjoint?", [2]], ["equal?", [2]], ["filter", [2]], ["intersection", [2]], ["member?", [2]], ["new", [0, 1, 2]], ["put", [2]], ["reject", [2]], ["size", [1]], ["split_with", [2]], ["subset?", [2]], ["symmetric_difference", [2]], ["to_list", [1]], ["union", [2]]],
181
+ "NaiveDateTime": [["add", [2, 3]], ["after?", [2]], ["before?", [2]], ["beginning_of_day", [1]], ["compare", [2]], ["convert", [2]], ["convert!", [2]], ["diff", [2, 3]], ["end_of_day", [1]], ["from_erl", [1, 2, 3]], ["from_erl!", [1, 2, 3]], ["from_gregorian_seconds", [1, 2, 3]], ["from_iso8601", [1, 2]], ["from_iso8601!", [1, 2]], ["local_now", [0, 1]], ["new", [2, 6, 7, 8]], ["new!", [2, 6, 7, 8]], ["shift", [2]], ["to_date", [1]], ["to_erl", [1]], ["to_gregorian_seconds", [1]], ["to_iso8601", [1, 2]], ["to_string", [1]], ["to_time", [1]], ["truncate", [2]], ["utc_now", [0, 1, 2]]],
182
+ "Process": [["alias", [0, 1]], ["alive?", [1]], ["cancel_timer", [1, 2]], ["delete", [1]], ["demonitor", [1, 2]], ["exit", [2]], ["flag", [2, 3]], ["get", [0, 1, 2]], ["get_keys", [0, 1]], ["get_label", [0, 1]], ["group_leader", [0, 2]], ["hibernate", [3]], ["info", [1, 2]], ["link", [1]], ["list", [0]], ["monitor", [1, 2]], ["put", [2]], ["read_timer", [1]], ["register", [2]], ["registered", [0]], ["send", [3]], ["send_after", [3, 4]], ["set_label", [1]], ["sleep", [1]], ["spawn", [2, 4]], ["unalias", [1]], ["unlink", [1]], ["unregister", [1]], ["whereis", [1]]],
183
+ "Range": [["disjoint?", [2]], ["new", [2, 3]], ["shift", [2]], ["size", [1]], ["split", [2]], ["to_list", [1]]],
184
+ "Regex": [["compile", [1, 2]], ["compile!", [1, 2]], ["escape", [1]], ["import", [1]], ["match?", [2]], ["named_captures", [2, 3]], ["names", [1]], ["opts", [1]], ["re_pattern", [1]], ["replace", [3, 4]], ["run", [2, 3]], ["scan", [2, 3]], ["source", [1]], ["split", [2, 3]], ["to_embed", [1, 2]]],
185
+ "Stream": [["chunk_by", [2]], ["chunk_every", [2, 3, 4]], ["chunk_while", [4]], ["concat", [1, 2]], ["cycle", [1]], ["dedup", [1]], ["dedup_by", [2]], ["drop", [2]], ["drop_every", [2]], ["drop_while", [2]], ["duplicate", [2]], ["each", [2]], ["filter", [2]], ["flat_map", [2]], ["from_index", [0, 1]], ["intersperse", [2]], ["interval", [1]], ["into", [2, 3]], ["iterate", [2]], ["map", [2]], ["map_every", [3]], ["reject", [2]], ["repeatedly", [1]], ["resource", [3]], ["run", [1]], ["scan", [2, 3]], ["take", [2]], ["take_every", [2]], ["take_while", [2]], ["timer", [1]], ["transform", [3, 4, 5]], ["unfold", [2]], ["uniq", [1]], ["uniq_by", [2]], ["with_index", [1, 2]], ["zip", [1, 2]], ["zip_with", [2, 3]]],
186
+ "String": [["at", [2]], ["bag_distance", [2]], ["byte_slice", [3]], ["capitalize", [1, 2]], ["chunk", [2]], ["codepoints", [1]], ["contains?", [2]], ["count", [2]], ["downcase", [1, 2]], ["duplicate", [2]], ["ends_with?", [2]], ["equivalent?", [2]], ["first", [1]], ["graphemes", [1]], ["jaro_distance", [2]], ["last", [1]], ["length", [1]], ["match?", [2]], ["myers_difference", [2]], ["next_codepoint", [1]], ["next_grapheme", [1]], ["next_grapheme_size", [1]], ["normalize", [2]], ["pad_leading", [2, 3]], ["pad_trailing", [2, 3]], ["printable?", [1, 2]], ["replace", [3, 4]], ["replace_invalid", [1, 2]], ["replace_leading", [3]], ["replace_prefix", [3]], ["replace_suffix", [3]], ["replace_trailing", [3]], ["reverse", [1]], ["slice", [2, 3]], ["split", [1, 2, 3]], ["split_at", [2]], ["splitter", [2, 3]], ["starts_with?", [2]], ["to_atom", [1]], ["to_charlist", [1]], ["to_existing_atom", [1]], ["to_float", [1]], ["to_integer", [1, 2]], ["trim", [1, 2]], ["trim_leading", [1, 2]], ["trim_trailing", [1, 2]], ["upcase", [1, 2]], ["valid?", [1, 2]]],
187
+ "Task": [["async", [1, 3]], ["async_stream", [2, 3, 4, 5]], ["await", [1, 2]], ["await_many", [1, 2]], ["child_spec", [1]], ["completed", [1]], ["ignore", [1]], ["shutdown", [1, 2]], ["start", [1, 3]], ["start_link", [1, 3]], ["yield", [1, 2]], ["yield_many", [1, 2]]],
188
+ "Time": [["add", [2, 3]], ["after?", [2]], ["before?", [2]], ["compare", [2]], ["convert", [2]], ["convert!", [2]], ["diff", [2, 3]], ["from_erl", [1, 2, 3]], ["from_erl!", [1, 2, 3]], ["from_iso8601", [1, 2]], ["from_iso8601!", [1, 2]], ["from_seconds_after_midnight", [1, 2, 3]], ["new", [3, 4, 5]], ["new!", [3, 4, 5]], ["shift", [2]], ["to_erl", [1]], ["to_iso8601", [1, 2]], ["to_seconds_after_midnight", [1]], ["to_string", [1]], ["truncate", [2]], ["utc_now", [0, 1, 2]]],
189
+ "Tuple": [["delete_at", [2]], ["duplicate", [2]], ["insert_at", [3]], ["product", [1]], ["sum", [1]], ["to_list", [1]]],
190
+ "URI": [["append_path", [2]], ["append_query", [2]], ["char_reserved?", [1]], ["char_unescaped?", [1]], ["char_unreserved?", [1]], ["decode", [1]], ["decode_query", [1, 2, 3]], ["decode_www_form", [1]], ["default_port", [1, 2]], ["encode", [1, 2]], ["encode_query", [1, 2]], ["encode_www_form", [1]], ["merge", [2]], ["new", [1]], ["new!", [1]], ["parse", [1]], ["query_decoder", [1, 2]], ["to_string", [1]]],
191
+ };
192
+
193
+ /**
194
+ Snippets for common Elixir constructs.
195
+ */
196
+ const snippets = [
197
+ autocomplete.snippetCompletion("defmodule ${Name} do\n\t${}\nend", {
198
+ label: "defmodule",
199
+ detail: "module",
200
+ type: "keyword",
201
+ }),
202
+ autocomplete.snippetCompletion("def ${name}(${args}) do\n\t${}\nend", {
203
+ label: "def",
204
+ detail: "function",
205
+ type: "keyword",
206
+ }),
207
+ autocomplete.snippetCompletion("def ${name}(${args}), do: ${}", {
208
+ label: "def",
209
+ detail: "one-line function",
210
+ type: "keyword",
211
+ }),
212
+ autocomplete.snippetCompletion("defp ${name}(${args}) do\n\t${}\nend", {
213
+ label: "defp",
214
+ detail: "private function",
215
+ type: "keyword",
216
+ }),
217
+ autocomplete.snippetCompletion("defstruct [${fields}]", {
218
+ label: "defstruct",
219
+ detail: "struct",
220
+ type: "keyword",
221
+ }),
222
+ autocomplete.snippetCompletion("if ${condition} do\n\t${}\nend", {
223
+ label: "if",
224
+ detail: "block",
225
+ type: "keyword",
226
+ }),
227
+ autocomplete.snippetCompletion("if ${condition} do\n\t${}\nelse\n\t${}\nend", {
228
+ label: "if",
229
+ detail: "/ else block",
230
+ type: "keyword",
231
+ }),
232
+ autocomplete.snippetCompletion("unless ${condition} do\n\t${}\nend", {
233
+ label: "unless",
234
+ detail: "block",
235
+ type: "keyword",
236
+ }),
237
+ autocomplete.snippetCompletion("case ${value} do\n\t${pattern} -> ${}\nend", {
238
+ label: "case",
239
+ detail: "block",
240
+ type: "keyword",
241
+ }),
242
+ autocomplete.snippetCompletion("cond do\n\t${condition} -> ${}\n\ttrue -> ${}\nend", {
243
+ label: "cond",
244
+ detail: "block",
245
+ type: "keyword",
246
+ }),
247
+ autocomplete.snippetCompletion("with ${pattern} <- ${value} do\n\t${}\nend", {
248
+ label: "with",
249
+ detail: "block",
250
+ type: "keyword",
251
+ }),
252
+ autocomplete.snippetCompletion("for ${item} <- ${enumerable} do\n\t${}\nend", {
253
+ label: "for",
254
+ detail: "comprehension",
255
+ type: "keyword",
256
+ }),
257
+ autocomplete.snippetCompletion("fn ${args} -> ${} end", {
258
+ label: "fn",
259
+ detail: "anonymous function",
260
+ type: "keyword",
261
+ }),
262
+ autocomplete.snippetCompletion("receive do\n\t${pattern} -> ${}\nend", {
263
+ label: "receive",
264
+ detail: "block",
265
+ type: "keyword",
266
+ }),
267
+ autocomplete.snippetCompletion("try do\n\t${}\nrescue\n\t${error} -> ${}\nend", {
268
+ label: "try",
269
+ detail: "/ rescue block",
270
+ type: "keyword",
271
+ }),
272
+ ];
273
+ const keywordNames = [
274
+ // Reserved words
275
+ "true false nil when and or not in fn do end catch rescue after else",
276
+ // Definitions
277
+ "def defp defmacro defmacrop defguard defguardp defdelegate defmodule",
278
+ "defstruct defprotocol defimpl defexception defoverridable",
279
+ // Other constructs
280
+ "case cond if unless for with try receive raise reraise throw",
281
+ "import alias require use quote unquote unquote_splicing super",
282
+ "__MODULE__ __DIR__ __ENV__ __CALLER__ __STACKTRACE__",
283
+ ]
284
+ .join(" ")
285
+ .split(" ");
286
+ const keywords = keywordNames.map((label) => ({
287
+ label,
288
+ type: "keyword",
289
+ }));
290
+ function arities(module, name, list) {
291
+ return list.map((a) => `${module}.${name}/${a}`).join(", ");
292
+ }
293
+ // Kernel is imported into every module, so its functions are offered
294
+ // without a prefix.
295
+ const kernelFunctions = stdlib.Kernel.filter(([name]) => !keywordNames.includes(name)).map(([label, list]) => ({
296
+ label,
297
+ type: "function",
298
+ detail: arities("Kernel", label, list).replace(/Kernel\./g, ""),
299
+ }));
300
+ const moduleNames = Object.keys(stdlib).map((label) => ({
301
+ label,
302
+ type: "namespace",
303
+ }));
304
+ const moduleFunctions = {};
305
+ for (const module of Object.keys(stdlib)) {
306
+ moduleFunctions[module] = stdlib[module].map(([label, list]) => ({
307
+ label,
308
+ type: "function",
309
+ detail: list.map((a) => `/${a}`).join(" "),
310
+ }));
311
+ }
312
+ // Attributes with a built-in meaning, offered after "@".
313
+ const builtinAttributes = [
314
+ "moduledoc doc typedoc spec type typep opaque callback macrocallback",
315
+ "impl behaviour derive enforce_keys compile deprecated dialyzer since",
316
+ ]
317
+ .join(" ")
318
+ .split(" ")
319
+ .map((label) => ({ label, type: "keyword" }));
320
+ const identifierBefore = /[A-Za-z_][\w]*[?!]?$/;
321
+ const validFor = /^[\w]*[?!]?$/;
322
+ // Inside these nodes there is nothing to complete, unless the cursor is
323
+ // in an interpolation.
324
+ const textNodes = new Set([
325
+ "String",
326
+ "Charlist",
327
+ "Sigil",
328
+ "StringSigil",
329
+ "Comment",
330
+ "QuotedAtom",
331
+ "QuotedKeyword",
332
+ "Atom",
333
+ "Keyword",
334
+ "Char",
335
+ "Integer",
336
+ "Float",
337
+ ]);
338
+ function inText(state, pos) {
339
+ for (let node = language.syntaxTree(state).resolveInner(pos, -1); node; node = node.parent) {
340
+ if (node.name == "Interpolation")
341
+ return false;
342
+ if (textNodes.has(node.name))
343
+ return true;
344
+ }
345
+ return false;
346
+ }
347
+ function wordAt(context) {
348
+ const match = context.matchBefore(identifierBefore);
349
+ const from = match ? match.from : context.pos;
350
+ return {
351
+ from,
352
+ text: match ? match.text : "",
353
+ before: context.state.sliceDoc(from - 1, from),
354
+ };
355
+ }
356
+ // The module before "Module.", for example "String" or "Foo.Bar".
357
+ function moduleBeforeDot(state, dotPos) {
358
+ const line = state.doc.lineAt(dotPos);
359
+ const text = line.text.slice(0, dotPos - line.from);
360
+ const match = /(?:^|[^\w.])([A-Z][\w]*(?:\.[A-Z][\w]*)*)$/.exec(text);
361
+ return match ? match[1] : null;
362
+ }
363
+ /**
364
+ Completes keywords, Kernel functions, snippets, standard library
365
+ modules, functions after a standard library module name (such as
366
+ `Enum.`) and built-in module attributes.
367
+ */
368
+ const elixirCompletionSource = (context) => {
369
+ const word = wordAt(context);
370
+ if (inText(context.state, context.pos))
371
+ return null;
372
+ if (word.before == ".") {
373
+ const module = moduleBeforeDot(context.state, word.from - 1);
374
+ const options = module && moduleFunctions[module];
375
+ if (!options || /^[A-Z]/.test(word.text))
376
+ return null;
377
+ return { from: word.from, options, validFor };
378
+ }
379
+ if (word.before == "@") {
380
+ return { from: word.from, options: builtinAttributes, validFor };
381
+ }
382
+ if (word.before == ":")
383
+ return null;
384
+ if (!word.text && !context.explicit)
385
+ return null;
386
+ if (/^[A-Z]/.test(word.text)) {
387
+ return { from: word.from, options: moduleNames, validFor };
388
+ }
389
+ return {
390
+ from: word.from,
391
+ options: [...snippets, ...keywords, ...kernelFunctions],
392
+ validFor,
393
+ };
394
+ };
395
+ const publicDefinitionCalls = new Set([
396
+ "def",
397
+ "defmacro",
398
+ "defguard",
399
+ "defdelegate",
400
+ ]);
401
+ const moduleCalls = new Set(["defmodule", "defprotocol"]);
402
+ const bindingCalls = new Set(["for", "with"]);
403
+ function isScope(node) {
404
+ switch (node.name) {
405
+ case "Source":
406
+ case "DoBlock":
407
+ case "AnonymousFunction":
408
+ case "StabClause":
409
+ case "FunctionDefinitionCall":
410
+ return true;
411
+ case "KernelCall":
412
+ return bindingCalls.has(callName(node));
413
+ default:
414
+ return false;
415
+ }
416
+ }
417
+ // The document being read. Each completion source sets it before it
418
+ // walks the tree, so the helpers below can read node text.
419
+ let currentDoc = state.Text.empty;
420
+ function text(node) {
421
+ return currentDoc.sliceString(node.from, node.to);
422
+ }
423
+ function callName(node) {
424
+ const id = node.getChild("Identifier");
425
+ return id ? text(id) : "";
426
+ }
427
+ function operator(node) {
428
+ const op = node.getChild("Operator");
429
+ return op ? text(op) : "";
430
+ }
431
+ // The `name(args)` part of `def name(args) when guard`, or `name` alone.
432
+ function definitionHead(node) {
433
+ var _a, _b;
434
+ let head = (_b = (_a = node.getChild("Arguments")) === null || _a === void 0 ? void 0 : _a.firstChild) !== null && _b !== void 0 ? _b : null;
435
+ if ((head === null || head === void 0 ? void 0 : head.name) == "WhenOperator")
436
+ head = head.firstChild;
437
+ return head;
438
+ }
439
+ function definitionName(node) {
440
+ const head = definitionHead(node);
441
+ if ((head === null || head === void 0 ? void 0 : head.name) == "Identifier")
442
+ return head;
443
+ if ((head === null || head === void 0 ? void 0 : head.name) == "Call") {
444
+ const id = head.firstChild;
445
+ if ((id === null || id === void 0 ? void 0 : id.name) == "Identifier")
446
+ return id;
447
+ }
448
+ return null;
449
+ }
450
+ // Adds every variable bound by a pattern, such as `{a, [b | c]}` or
451
+ // `%{key: value} = map`.
452
+ function bindPattern(node, out) {
453
+ if (!node)
454
+ return;
455
+ switch (node.name) {
456
+ case "Identifier":
457
+ out.push({ label: text(node), type: "variable", from: node.from });
458
+ return;
459
+ case "Arguments":
460
+ case "Tuple":
461
+ case "List":
462
+ case "Bitstring":
463
+ case "Map":
464
+ case "MapContent":
465
+ case "Struct":
466
+ case "Keywords":
467
+ case "Block":
468
+ for (let c = node.firstChild; c; c = c.nextSibling)
469
+ bindPattern(c, out);
470
+ return;
471
+ case "Pair":
472
+ bindPattern(node.lastChild, out);
473
+ return;
474
+ case "WhenOperator":
475
+ bindPattern(node.firstChild, out);
476
+ return;
477
+ case "BinaryOperator": {
478
+ const op = operator(node);
479
+ if (op == "::" || op == "\\\\") {
480
+ bindPattern(node.firstChild, out);
481
+ }
482
+ else {
483
+ bindPattern(node.firstChild, out);
484
+ bindPattern(node.lastChild, out);
485
+ }
486
+ return;
487
+ }
488
+ }
489
+ }
490
+ function aliasNames(node, out) {
491
+ var _a, _b, _c, _d, _e;
492
+ const args = node.getChild("Arguments");
493
+ const target = args === null || args === void 0 ? void 0 : args.firstChild;
494
+ if (!args || !target)
495
+ return;
496
+ const as = (_a = args.getChild("Keywords")) === null || _a === void 0 ? void 0 : _a.getChildren("Pair");
497
+ const asPair = as === null || as === void 0 ? void 0 : as.find((p) => text(p.firstChild).startsWith("as:"));
498
+ if (((_b = asPair === null || asPair === void 0 ? void 0 : asPair.lastChild) === null || _b === void 0 ? void 0 : _b.name) == "Alias") {
499
+ out.push({ label: text(asPair.lastChild), type: "namespace", from: node.from });
500
+ }
501
+ else if (target.name == "Alias") {
502
+ const name = text(target).split(".").pop();
503
+ out.push({ label: name, type: "namespace", from: node.from });
504
+ }
505
+ else if (target.name == "Dot") {
506
+ // alias Foo.{Bar, Baz}
507
+ const names = (_e = (_d = (_c = target.getChild("Right")) === null || _c === void 0 ? void 0 : _c.getChild("Tuple")) === null || _d === void 0 ? void 0 : _d.getChildren("Alias")) !== null && _e !== void 0 ? _e : [];
508
+ for (const a of names) {
509
+ out.push({ label: text(a).split(".").pop(), type: "namespace", from: node.from });
510
+ }
511
+ }
512
+ }
513
+ // Adds what a node inside a scope declares. Returns false when the walk
514
+ // shouldn't go into the node's children.
515
+ function gather(node, out) {
516
+ var _a;
517
+ switch (node.name) {
518
+ case "FunctionDefinitionCall": {
519
+ const name = definitionName(node);
520
+ if (name)
521
+ out.push({ label: text(name), type: "function", from: name.from });
522
+ return false;
523
+ }
524
+ case "KernelCall": {
525
+ const name = callName(node);
526
+ if (moduleCalls.has(name)) {
527
+ const alias = (_a = node.getChild("Arguments")) === null || _a === void 0 ? void 0 : _a.getChild("Alias");
528
+ if (alias)
529
+ out.push({ label: text(alias), type: "namespace", from: node.from });
530
+ return false;
531
+ }
532
+ if (name == "alias") {
533
+ aliasNames(node, out);
534
+ return false;
535
+ }
536
+ return !bindingCalls.has(name);
537
+ }
538
+ case "AtOperator": {
539
+ const call = node.getChild("Call");
540
+ const id = call === null || call === void 0 ? void 0 : call.getChild("Identifier");
541
+ if (id)
542
+ out.push({ label: text(id), type: "constant", from: id.from, attribute: true });
543
+ return false;
544
+ }
545
+ case "BinaryOperator":
546
+ if (operator(node) == "=")
547
+ bindPattern(node.firstChild, out);
548
+ return true;
549
+ case "StabClause":
550
+ case "DoBlock":
551
+ case "AnonymousFunction":
552
+ case "DocAtOperator":
553
+ case "Comment":
554
+ case "String":
555
+ case "Charlist":
556
+ case "Sigil":
557
+ case "StringSigil":
558
+ return false;
559
+ }
560
+ return true;
561
+ }
562
+ // Adds what a scope node declares for its own children, such as the
563
+ // parameters of a function or the patterns of a `case` clause.
564
+ function gatherOwn(node, out) {
565
+ switch (node.name) {
566
+ case "FunctionDefinitionCall": {
567
+ const head = definitionHead(node);
568
+ if ((head === null || head === void 0 ? void 0 : head.name) == "Call")
569
+ bindPattern(head.getChild("Arguments"), out);
570
+ break;
571
+ }
572
+ case "StabClause": {
573
+ let args = node.firstChild;
574
+ if ((args === null || args === void 0 ? void 0 : args.name) == "WhenOperator")
575
+ args = args.firstChild;
576
+ if ((args === null || args === void 0 ? void 0 : args.name) == "Arguments")
577
+ bindPattern(args, out);
578
+ break;
579
+ }
580
+ case "KernelCall": {
581
+ const args = node.getChild("Arguments");
582
+ for (let c = args === null || args === void 0 ? void 0 : args.firstChild; c; c = c.nextSibling) {
583
+ if (c.name == "BinaryOperator" && operator(c) == "<-")
584
+ bindPattern(c.firstChild, out);
585
+ }
586
+ break;
587
+ }
588
+ }
589
+ }
590
+ const cache = new common.NodeWeakMap();
591
+ function getScope(doc, scope) {
592
+ const cached = cache.get(scope);
593
+ if (cached)
594
+ return cached;
595
+ currentDoc = doc;
596
+ const out = [];
597
+ gatherOwn(scope, out);
598
+ const walk = (node) => {
599
+ for (let c = node.firstChild; c; c = c.nextSibling) {
600
+ if (gather(c, out))
601
+ walk(c);
602
+ }
603
+ };
604
+ walk(scope);
605
+ cache.set(scope, out);
606
+ return out;
607
+ }
608
+ function scopesAt(tree, pos) {
609
+ const scopes = [];
610
+ for (let node = tree.resolveInner(pos, -1); node; node = node.parent) {
611
+ if (isScope(node))
612
+ scopes.push(node);
613
+ }
614
+ return scopes;
615
+ }
616
+ /**
617
+ Completes names declared in the file: functions, parameters,
618
+ variables, modules, aliases and module attributes. It offers a
619
+ variable only after the place it is bound, and a name only where it
620
+ is in scope. After `Module.` it offers the public functions of a
621
+ module defined in the file.
622
+ */
623
+ const localCompletionSource = (context) => {
624
+ const { state } = context;
625
+ const word = wordAt(context);
626
+ if (inText(state, context.pos))
627
+ return null;
628
+ const tree = language.syntaxTree(state);
629
+ if (word.before == ".") {
630
+ const module = moduleBeforeDot(state, word.from - 1);
631
+ if (!module || /^[A-Z]/.test(word.text))
632
+ return null;
633
+ const options = moduleFunctionsInFile(state, tree, module);
634
+ return options.length ? { from: word.from, options, validFor } : null;
635
+ }
636
+ if (word.before == ":")
637
+ return null;
638
+ if (!word.text && !context.explicit && word.before != "@")
639
+ return null;
640
+ const attribute = word.before == "@";
641
+ const upper = /^[A-Z]/.test(word.text);
642
+ currentDoc = state.doc;
643
+ const seen = new Set();
644
+ const options = [];
645
+ for (const node of scopesAt(tree, context.pos)) {
646
+ for (const d of getScope(state.doc, node)) {
647
+ if (!!d.attribute != attribute)
648
+ continue;
649
+ if (d.from == word.from)
650
+ continue;
651
+ if (d.type == "variable" && d.from > word.from)
652
+ continue;
653
+ if (upper != (d.type == "namespace"))
654
+ continue;
655
+ if (seen.has(d.label))
656
+ continue;
657
+ seen.add(d.label);
658
+ options.push({ label: d.label, type: d.type });
659
+ }
660
+ }
661
+ return { from: word.from, options, validFor };
662
+ };
663
+ function moduleFunctionsInFile(state, tree, module) {
664
+ currentDoc = state.doc;
665
+ const options = [];
666
+ const seen = new Set();
667
+ const visit = (node, prefix) => {
668
+ var _a;
669
+ for (let c = node.firstChild; c; c = c.nextSibling) {
670
+ if (c.name == "KernelCall" && moduleCalls.has(callName(c))) {
671
+ const alias = (_a = c.getChild("Arguments")) === null || _a === void 0 ? void 0 : _a.getChild("Alias");
672
+ const body = c.getChild("DoBlock");
673
+ if (!alias || !body)
674
+ continue;
675
+ const name = prefix + text(alias);
676
+ if (name == module || name.endsWith("." + module)) {
677
+ for (let f = body.firstChild; f; f = f.nextSibling) {
678
+ if (f.name != "FunctionDefinitionCall")
679
+ continue;
680
+ if (!publicDefinitionCalls.has(callName(f)))
681
+ continue;
682
+ const id = definitionName(f);
683
+ if (id && !seen.has(text(id))) {
684
+ seen.add(text(id));
685
+ options.push({ label: text(id), type: "function" });
686
+ }
687
+ }
688
+ }
689
+ visit(body, name + ".");
690
+ }
691
+ }
692
+ };
693
+ visit(tree.topNode, "");
694
+ return options;
695
+ }
696
+
697
+ const elixirKeymap = [{ key: "Enter", run: insertNewlineCloseEnd }];
698
+ /**
699
+ Elixir language support, with completion of keywords, snippets,
700
+ standard library functions and names declared in the file.
701
+ */
702
+ function elixir() {
703
+ const support = [
704
+ state.Prec.high(view.keymap.of(elixirKeymap)),
705
+ elixirLanguage.data.of({ autocomplete: elixirCompletionSource }),
706
+ elixirLanguage.data.of({ autocomplete: localCompletionSource }),
707
+ ];
708
+ return new language.LanguageSupport(elixirLanguage, support);
709
+ }
710
+
711
+ exports.elixir = elixir;
712
+ exports.elixirCompletionSource = elixirCompletionSource;
713
+ exports.elixirLanguage = elixirLanguage;
714
+ exports.localCompletionSource = localCompletionSource;
715
+ exports.snippets = snippets;