@coze-editor/lang-javascript 0.1.0-alpha.6b15c1
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 +7 -0
- package/dist/esm/index.js +553 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/index.d.mts +29 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +573 -0
- package/dist/index.js.map +1 -0
- package/package.json +43 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import * as _codemirror_state from '@codemirror/state';
|
|
2
|
+
import { LRLanguage, LanguageSupport } from '@codemirror/language';
|
|
3
|
+
import { Completion, CompletionContext, CompletionResult, CompletionSource } from '@codemirror/autocomplete';
|
|
4
|
+
import { EditorView } from '@codemirror/view';
|
|
5
|
+
import { Diagnostic } from '@codemirror/lint';
|
|
6
|
+
|
|
7
|
+
declare const javascriptLanguage: LRLanguage;
|
|
8
|
+
declare const typescriptLanguage: LRLanguage;
|
|
9
|
+
declare const jsxLanguage: LRLanguage;
|
|
10
|
+
declare const tsxLanguage: LRLanguage;
|
|
11
|
+
declare function javascript(config?: {
|
|
12
|
+
jsx?: boolean;
|
|
13
|
+
typescript?: boolean;
|
|
14
|
+
}): LanguageSupport;
|
|
15
|
+
declare const autoCloseTags: _codemirror_state.Extension;
|
|
16
|
+
|
|
17
|
+
declare const snippets: readonly Completion[];
|
|
18
|
+
declare const typescriptSnippets: Completion[];
|
|
19
|
+
|
|
20
|
+
declare function esLint(eslint: any, config?: any): (view: EditorView) => Diagnostic[];
|
|
21
|
+
|
|
22
|
+
declare function localCompletionSource(context: CompletionContext): CompletionResult | null;
|
|
23
|
+
declare function completionPath(context: CompletionContext): {
|
|
24
|
+
path: readonly string[];
|
|
25
|
+
name: string;
|
|
26
|
+
} | null;
|
|
27
|
+
declare function scopeCompletionSource(scope: any): CompletionSource;
|
|
28
|
+
|
|
29
|
+
export { autoCloseTags, completionPath, esLint, javascript, javascriptLanguage, jsxLanguage, localCompletionSource, scopeCompletionSource, snippets, tsxLanguage, typescriptLanguage, typescriptSnippets };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import * as _codemirror_state from '@codemirror/state';
|
|
2
|
+
import { LRLanguage, LanguageSupport } from '@codemirror/language';
|
|
3
|
+
import { Completion, CompletionContext, CompletionResult, CompletionSource } from '@codemirror/autocomplete';
|
|
4
|
+
import { EditorView } from '@codemirror/view';
|
|
5
|
+
import { Diagnostic } from '@codemirror/lint';
|
|
6
|
+
|
|
7
|
+
declare const javascriptLanguage: LRLanguage;
|
|
8
|
+
declare const typescriptLanguage: LRLanguage;
|
|
9
|
+
declare const jsxLanguage: LRLanguage;
|
|
10
|
+
declare const tsxLanguage: LRLanguage;
|
|
11
|
+
declare function javascript(config?: {
|
|
12
|
+
jsx?: boolean;
|
|
13
|
+
typescript?: boolean;
|
|
14
|
+
}): LanguageSupport;
|
|
15
|
+
declare const autoCloseTags: _codemirror_state.Extension;
|
|
16
|
+
|
|
17
|
+
declare const snippets: readonly Completion[];
|
|
18
|
+
declare const typescriptSnippets: Completion[];
|
|
19
|
+
|
|
20
|
+
declare function esLint(eslint: any, config?: any): (view: EditorView) => Diagnostic[];
|
|
21
|
+
|
|
22
|
+
declare function localCompletionSource(context: CompletionContext): CompletionResult | null;
|
|
23
|
+
declare function completionPath(context: CompletionContext): {
|
|
24
|
+
path: readonly string[];
|
|
25
|
+
name: string;
|
|
26
|
+
} | null;
|
|
27
|
+
declare function scopeCompletionSource(scope: any): CompletionSource;
|
|
28
|
+
|
|
29
|
+
export { autoCloseTags, completionPath, esLint, javascript, javascriptLanguage, jsxLanguage, localCompletionSource, scopeCompletionSource, snippets, tsxLanguage, typescriptLanguage, typescriptSnippets };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,573 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/index.ts
|
|
20
|
+
var index_exports = {};
|
|
21
|
+
__export(index_exports, {
|
|
22
|
+
autoCloseTags: () => autoCloseTags,
|
|
23
|
+
completionPath: () => completionPath,
|
|
24
|
+
esLint: () => esLint,
|
|
25
|
+
javascript: () => javascript,
|
|
26
|
+
javascriptLanguage: () => javascriptLanguage,
|
|
27
|
+
jsxLanguage: () => jsxLanguage,
|
|
28
|
+
localCompletionSource: () => localCompletionSource,
|
|
29
|
+
scopeCompletionSource: () => scopeCompletionSource,
|
|
30
|
+
snippets: () => snippets,
|
|
31
|
+
tsxLanguage: () => tsxLanguage,
|
|
32
|
+
typescriptLanguage: () => typescriptLanguage,
|
|
33
|
+
typescriptSnippets: () => typescriptSnippets
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(index_exports);
|
|
36
|
+
|
|
37
|
+
// src/javascript.ts
|
|
38
|
+
var import_javascript = require("@lezer/javascript");
|
|
39
|
+
var import_view = require("@codemirror/view");
|
|
40
|
+
var import_state = require("@codemirror/state");
|
|
41
|
+
var import_language2 = require("@codemirror/language");
|
|
42
|
+
var import_autocomplete2 = require("@codemirror/autocomplete");
|
|
43
|
+
|
|
44
|
+
// src/snippets.ts
|
|
45
|
+
var import_autocomplete = require("@codemirror/autocomplete");
|
|
46
|
+
var snippets = [
|
|
47
|
+
(0, import_autocomplete.snippetCompletion)("function ${name}(${params}) {\n ${}\n}", {
|
|
48
|
+
label: "function",
|
|
49
|
+
detail: "definition",
|
|
50
|
+
type: "keyword"
|
|
51
|
+
}),
|
|
52
|
+
(0, import_autocomplete.snippetCompletion)("for (let ${index} = 0; ${index} < ${bound}; ${index}++) {\n ${}\n}", {
|
|
53
|
+
label: "for",
|
|
54
|
+
detail: "loop",
|
|
55
|
+
type: "keyword"
|
|
56
|
+
}),
|
|
57
|
+
(0, import_autocomplete.snippetCompletion)("for (let ${name} of ${collection}) {\n ${}\n}", {
|
|
58
|
+
label: "for",
|
|
59
|
+
detail: "of loop",
|
|
60
|
+
type: "keyword"
|
|
61
|
+
}),
|
|
62
|
+
(0, import_autocomplete.snippetCompletion)("do {\n ${}\n} while (${})", {
|
|
63
|
+
label: "do",
|
|
64
|
+
detail: "loop",
|
|
65
|
+
type: "keyword"
|
|
66
|
+
}),
|
|
67
|
+
(0, import_autocomplete.snippetCompletion)("while (${}) {\n ${}\n}", {
|
|
68
|
+
label: "while",
|
|
69
|
+
detail: "loop",
|
|
70
|
+
type: "keyword"
|
|
71
|
+
}),
|
|
72
|
+
(0, import_autocomplete.snippetCompletion)("try {\n ${}\n} catch (${error}) {\n ${}\n}", {
|
|
73
|
+
label: "try",
|
|
74
|
+
detail: "/ catch block",
|
|
75
|
+
type: "keyword"
|
|
76
|
+
}),
|
|
77
|
+
(0, import_autocomplete.snippetCompletion)("if (${}) {\n ${}\n}", {
|
|
78
|
+
label: "if",
|
|
79
|
+
detail: "block",
|
|
80
|
+
type: "keyword"
|
|
81
|
+
}),
|
|
82
|
+
(0, import_autocomplete.snippetCompletion)("if (${}) {\n ${}\n} else {\n ${}\n}", {
|
|
83
|
+
label: "if",
|
|
84
|
+
detail: "/ else block",
|
|
85
|
+
type: "keyword"
|
|
86
|
+
}),
|
|
87
|
+
(0, import_autocomplete.snippetCompletion)("class ${name} {\n constructor(${params}) {\n ${}\n }\n}", {
|
|
88
|
+
label: "class",
|
|
89
|
+
detail: "definition",
|
|
90
|
+
type: "keyword"
|
|
91
|
+
}),
|
|
92
|
+
(0, import_autocomplete.snippetCompletion)('import {${names}} from "${module}"\n${}', {
|
|
93
|
+
label: "import",
|
|
94
|
+
detail: "named",
|
|
95
|
+
type: "keyword"
|
|
96
|
+
}),
|
|
97
|
+
(0, import_autocomplete.snippetCompletion)('import ${name} from "${module}"\n${}', {
|
|
98
|
+
label: "import",
|
|
99
|
+
detail: "default",
|
|
100
|
+
type: "keyword"
|
|
101
|
+
})
|
|
102
|
+
];
|
|
103
|
+
var typescriptSnippets = snippets.concat([
|
|
104
|
+
(0, import_autocomplete.snippetCompletion)("interface ${name} {\n ${}\n}", {
|
|
105
|
+
label: "interface",
|
|
106
|
+
detail: "definition",
|
|
107
|
+
type: "keyword"
|
|
108
|
+
}),
|
|
109
|
+
(0, import_autocomplete.snippetCompletion)("type ${name} = ${type}", {
|
|
110
|
+
label: "type",
|
|
111
|
+
detail: "definition",
|
|
112
|
+
type: "keyword"
|
|
113
|
+
}),
|
|
114
|
+
(0, import_autocomplete.snippetCompletion)("enum ${name} {\n ${}\n}", {
|
|
115
|
+
label: "enum",
|
|
116
|
+
detail: "definition",
|
|
117
|
+
type: "keyword"
|
|
118
|
+
})
|
|
119
|
+
]);
|
|
120
|
+
|
|
121
|
+
// src/complete.ts
|
|
122
|
+
var import_common = require("@lezer/common");
|
|
123
|
+
var import_language = require("@codemirror/language");
|
|
124
|
+
var cache = new import_common.NodeWeakMap();
|
|
125
|
+
var ScopeNodes = /* @__PURE__ */ new Set([
|
|
126
|
+
"Script",
|
|
127
|
+
"Block",
|
|
128
|
+
"FunctionExpression",
|
|
129
|
+
"FunctionDeclaration",
|
|
130
|
+
"ArrowFunction",
|
|
131
|
+
"MethodDeclaration",
|
|
132
|
+
"ForStatement"
|
|
133
|
+
]);
|
|
134
|
+
function defID(type) {
|
|
135
|
+
return (node, def) => {
|
|
136
|
+
const id = node.node.getChild("VariableDefinition");
|
|
137
|
+
if (id) {
|
|
138
|
+
def(id, type);
|
|
139
|
+
}
|
|
140
|
+
return true;
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
var functionContext = ["FunctionDeclaration"];
|
|
144
|
+
var gatherCompletions = {
|
|
145
|
+
FunctionDeclaration: defID("function"),
|
|
146
|
+
ClassDeclaration: defID("class"),
|
|
147
|
+
ClassExpression: () => true,
|
|
148
|
+
EnumDeclaration: defID("constant"),
|
|
149
|
+
TypeAliasDeclaration: defID("type"),
|
|
150
|
+
NamespaceDeclaration: defID("namespace"),
|
|
151
|
+
VariableDefinition(node, def) {
|
|
152
|
+
if (!node.matchContext(functionContext)) {
|
|
153
|
+
def(node, "variable");
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
TypeDefinition(node, def) {
|
|
157
|
+
def(node, "type");
|
|
158
|
+
},
|
|
159
|
+
__proto__: null
|
|
160
|
+
};
|
|
161
|
+
function getScope(doc, node) {
|
|
162
|
+
const cached = cache.get(node);
|
|
163
|
+
if (cached) {
|
|
164
|
+
return cached;
|
|
165
|
+
}
|
|
166
|
+
const completions = [];
|
|
167
|
+
let top = true;
|
|
168
|
+
function def(node2, type) {
|
|
169
|
+
const name = doc.sliceString(node2.from, node2.to);
|
|
170
|
+
completions.push({ label: name, type });
|
|
171
|
+
}
|
|
172
|
+
node.cursor(import_common.IterMode.IncludeAnonymous).iterate((node2) => {
|
|
173
|
+
if (top) {
|
|
174
|
+
top = false;
|
|
175
|
+
} else if (node2.name) {
|
|
176
|
+
const gather = gatherCompletions[node2.name];
|
|
177
|
+
if (gather && gather(node2, def) || ScopeNodes.has(node2.name)) {
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
} else if (node2.to - node2.from > 8192) {
|
|
181
|
+
for (const c of getScope(doc, node2.node)) {
|
|
182
|
+
completions.push(c);
|
|
183
|
+
}
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
cache.set(node, completions);
|
|
188
|
+
return completions;
|
|
189
|
+
}
|
|
190
|
+
var Identifier = /^[\w$\xa1-\uffff][\w$\d\xa1-\uffff]*$/;
|
|
191
|
+
var dontComplete = [
|
|
192
|
+
"TemplateString",
|
|
193
|
+
"String",
|
|
194
|
+
"RegExp",
|
|
195
|
+
"LineComment",
|
|
196
|
+
"BlockComment",
|
|
197
|
+
"VariableDefinition",
|
|
198
|
+
"TypeDefinition",
|
|
199
|
+
"Label",
|
|
200
|
+
"PropertyDefinition",
|
|
201
|
+
"PropertyName",
|
|
202
|
+
"PrivatePropertyDefinition",
|
|
203
|
+
"PrivatePropertyName",
|
|
204
|
+
"JSXText",
|
|
205
|
+
"JSXAttributeValue",
|
|
206
|
+
"JSXOpenTag",
|
|
207
|
+
"JSXCloseTag",
|
|
208
|
+
"JSXSelfClosingTag",
|
|
209
|
+
".",
|
|
210
|
+
"?."
|
|
211
|
+
];
|
|
212
|
+
function localCompletionSource(context) {
|
|
213
|
+
const inner = (0, import_language.syntaxTree)(context.state).resolveInner(context.pos, -1);
|
|
214
|
+
if (dontComplete.indexOf(inner.name) > -1) {
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
const isWord = inner.name == "VariableName" || inner.to - inner.from < 20 && Identifier.test(context.state.sliceDoc(inner.from, inner.to));
|
|
218
|
+
if (!isWord && !context.explicit) {
|
|
219
|
+
return null;
|
|
220
|
+
}
|
|
221
|
+
let options = [];
|
|
222
|
+
for (let pos = inner; pos; pos = pos.parent) {
|
|
223
|
+
if (ScopeNodes.has(pos.name)) {
|
|
224
|
+
options = options.concat(getScope(context.state.doc, pos));
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return {
|
|
228
|
+
options,
|
|
229
|
+
from: isWord ? inner.from : context.pos,
|
|
230
|
+
validFor: Identifier
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
function pathFor(read, member, name) {
|
|
234
|
+
var _a;
|
|
235
|
+
const path = [];
|
|
236
|
+
for (; ; ) {
|
|
237
|
+
const obj = member.firstChild;
|
|
238
|
+
let prop;
|
|
239
|
+
if ((obj == null ? void 0 : obj.name) == "VariableName") {
|
|
240
|
+
path.push(read(obj));
|
|
241
|
+
return { path: path.reverse(), name };
|
|
242
|
+
} else if ((obj == null ? void 0 : obj.name) == "MemberExpression" && ((_a = prop = obj.lastChild) == null ? void 0 : _a.name) == "PropertyName") {
|
|
243
|
+
path.push(read(prop));
|
|
244
|
+
member = obj;
|
|
245
|
+
} else {
|
|
246
|
+
return null;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
function completionPath(context) {
|
|
251
|
+
const read = (node) => context.state.doc.sliceString(node.from, node.to);
|
|
252
|
+
const inner = (0, import_language.syntaxTree)(context.state).resolveInner(context.pos, -1);
|
|
253
|
+
if (inner.name == "PropertyName") {
|
|
254
|
+
return pathFor(read, inner.parent, read(inner));
|
|
255
|
+
} else if ((inner.name == "." || inner.name == "?.") && inner.parent.name == "MemberExpression") {
|
|
256
|
+
return pathFor(read, inner.parent, "");
|
|
257
|
+
} else if (dontComplete.indexOf(inner.name) > -1) {
|
|
258
|
+
return null;
|
|
259
|
+
} else if (inner.name == "VariableName" || inner.to - inner.from < 20 && Identifier.test(read(inner))) {
|
|
260
|
+
return { path: [], name: read(inner) };
|
|
261
|
+
} else if (inner.name == "MemberExpression") {
|
|
262
|
+
return pathFor(read, inner, "");
|
|
263
|
+
} else {
|
|
264
|
+
return context.explicit ? { path: [], name: "" } : null;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
function enumeratePropertyCompletions(obj, top) {
|
|
268
|
+
const options = [], seen = /* @__PURE__ */ new Set();
|
|
269
|
+
for (let depth = 0; ; depth++) {
|
|
270
|
+
for (const name of (Object.getOwnPropertyNames || Object.keys)(obj)) {
|
|
271
|
+
if (!/^[a-zA-Z_$\xaa-\uffdc][\w$\xaa-\uffdc]*$/.test(name) || seen.has(name)) {
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
seen.add(name);
|
|
275
|
+
let value;
|
|
276
|
+
try {
|
|
277
|
+
value = obj[name];
|
|
278
|
+
} catch (_) {
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
options.push({
|
|
282
|
+
label: name,
|
|
283
|
+
type: typeof value === "function" ? /^[A-Z]/.test(name) ? "class" : top ? "function" : "method" : top ? "variable" : "property",
|
|
284
|
+
boost: -depth
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
const next = Object.getPrototypeOf(obj);
|
|
288
|
+
if (!next) {
|
|
289
|
+
return options;
|
|
290
|
+
}
|
|
291
|
+
obj = next;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
function scopeCompletionSource(scope) {
|
|
295
|
+
const cache2 = /* @__PURE__ */ new Map();
|
|
296
|
+
return (context) => {
|
|
297
|
+
const path = completionPath(context);
|
|
298
|
+
if (!path) {
|
|
299
|
+
return null;
|
|
300
|
+
}
|
|
301
|
+
let target = scope;
|
|
302
|
+
for (const step of path.path) {
|
|
303
|
+
target = target[step];
|
|
304
|
+
if (!target) {
|
|
305
|
+
return null;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
let options = cache2.get(target);
|
|
309
|
+
if (!options) {
|
|
310
|
+
cache2.set(
|
|
311
|
+
target,
|
|
312
|
+
options = enumeratePropertyCompletions(target, !path.path.length)
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
return {
|
|
316
|
+
from: context.pos - path.name.length,
|
|
317
|
+
options,
|
|
318
|
+
validFor: Identifier
|
|
319
|
+
};
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// src/javascript.ts
|
|
324
|
+
var javascriptLanguage = import_language2.LRLanguage.define({
|
|
325
|
+
name: "javascript",
|
|
326
|
+
parser: import_javascript.parser.configure({
|
|
327
|
+
props: [
|
|
328
|
+
import_language2.indentNodeProp.add({
|
|
329
|
+
IfStatement: (0, import_language2.continuedIndent)({ except: /^\s*({|else\b)/ }),
|
|
330
|
+
TryStatement: (0, import_language2.continuedIndent)({ except: /^\s*({|catch\b|finally\b)/ }),
|
|
331
|
+
LabeledStatement: import_language2.flatIndent,
|
|
332
|
+
SwitchBody: (context) => {
|
|
333
|
+
const after = context.textAfter, closed = /^\s*\}/.test(after), isCase = /^\s*(case|default)\b/.test(after);
|
|
334
|
+
return context.baseIndent + (closed ? 0 : isCase ? 1 : 2) * context.unit;
|
|
335
|
+
},
|
|
336
|
+
Block: (0, import_language2.delimitedIndent)({ closing: "}" }),
|
|
337
|
+
ArrowFunction: (cx) => cx.baseIndent + cx.unit,
|
|
338
|
+
"TemplateString BlockComment": () => null,
|
|
339
|
+
"Statement Property": (0, import_language2.continuedIndent)({ except: /^\s*{/ }),
|
|
340
|
+
JSXElement(context) {
|
|
341
|
+
const closed = /^\s*<\//.test(context.textAfter);
|
|
342
|
+
return context.lineIndent(context.node.from) + (closed ? 0 : context.unit);
|
|
343
|
+
},
|
|
344
|
+
JSXEscape(context) {
|
|
345
|
+
const closed = /\s*\}/.test(context.textAfter);
|
|
346
|
+
return context.lineIndent(context.node.from) + (closed ? 0 : context.unit);
|
|
347
|
+
},
|
|
348
|
+
"JSXOpenTag JSXSelfClosingTag"(context) {
|
|
349
|
+
return context.column(context.node.from) + context.unit;
|
|
350
|
+
}
|
|
351
|
+
}),
|
|
352
|
+
import_language2.foldNodeProp.add({
|
|
353
|
+
"Block ClassBody SwitchBody EnumBody ObjectExpression ArrayExpression ObjectType": import_language2.foldInside,
|
|
354
|
+
BlockComment(tree) {
|
|
355
|
+
return { from: tree.from + 2, to: tree.to - 2 };
|
|
356
|
+
},
|
|
357
|
+
JSXElement(node) {
|
|
358
|
+
var _a;
|
|
359
|
+
const first = node.firstChild;
|
|
360
|
+
const start = (first == null ? void 0 : first.firstChild) ? first.firstChild.nextSibling : null;
|
|
361
|
+
let end = null;
|
|
362
|
+
if (!first) {
|
|
363
|
+
return null;
|
|
364
|
+
}
|
|
365
|
+
if (first.name === "JSXSelfClosingTag") {
|
|
366
|
+
end = first.lastChild;
|
|
367
|
+
}
|
|
368
|
+
if (["JSXOpenTag", "JSXFragmentTag"].includes(first.name)) {
|
|
369
|
+
end = ((_a = node.lastChild) == null ? void 0 : _a.name) === "JSXCloseTag" ? node.lastChild : null;
|
|
370
|
+
}
|
|
371
|
+
if (!start || !end) {
|
|
372
|
+
return null;
|
|
373
|
+
}
|
|
374
|
+
return start && start.to < end.from ? { from: start.to, to: end.type.isError ? node.to : end.from } : null;
|
|
375
|
+
}
|
|
376
|
+
})
|
|
377
|
+
]
|
|
378
|
+
}),
|
|
379
|
+
languageData: {
|
|
380
|
+
closeBrackets: { brackets: ["(", "[", "{", "'", '"', "`"] },
|
|
381
|
+
commentTokens: { line: "//", block: { open: "/*", close: "*/" } },
|
|
382
|
+
indentOnInput: /^\s*(?:case |default:|\{|\}|<\/)$/,
|
|
383
|
+
wordChars: "$"
|
|
384
|
+
}
|
|
385
|
+
});
|
|
386
|
+
var jsxSublanguage = {
|
|
387
|
+
test: (node) => /^JSX/.test(node.name),
|
|
388
|
+
facet: (0, import_language2.defineLanguageFacet)({
|
|
389
|
+
commentTokens: { block: { open: "{/*", close: "*/}" } }
|
|
390
|
+
})
|
|
391
|
+
};
|
|
392
|
+
var typescriptLanguage = javascriptLanguage.configure(
|
|
393
|
+
{ dialect: "ts" },
|
|
394
|
+
"typescript"
|
|
395
|
+
);
|
|
396
|
+
var jsxLanguage = javascriptLanguage.configure({
|
|
397
|
+
dialect: "jsx",
|
|
398
|
+
props: [import_language2.sublanguageProp.add((n) => n.isTop ? [jsxSublanguage] : void 0)]
|
|
399
|
+
});
|
|
400
|
+
var tsxLanguage = javascriptLanguage.configure(
|
|
401
|
+
{
|
|
402
|
+
dialect: "jsx ts",
|
|
403
|
+
props: [import_language2.sublanguageProp.add((n) => n.isTop ? [jsxSublanguage] : void 0)]
|
|
404
|
+
},
|
|
405
|
+
"typescript"
|
|
406
|
+
);
|
|
407
|
+
var kwCompletion = (name) => ({ label: name, type: "keyword" });
|
|
408
|
+
var keywords = "break case const continue default delete export extends false finally in instanceof let new return static super switch this throw true typeof var yield".split(" ").map(kwCompletion);
|
|
409
|
+
var typescriptKeywords = keywords.concat(
|
|
410
|
+
["declare", "implements", "private", "protected", "public"].map(kwCompletion)
|
|
411
|
+
);
|
|
412
|
+
function javascript(config = {}) {
|
|
413
|
+
const lang = config.jsx ? config.typescript ? tsxLanguage : jsxLanguage : config.typescript ? typescriptLanguage : javascriptLanguage;
|
|
414
|
+
const completions = config.typescript ? typescriptSnippets.concat(typescriptKeywords) : snippets.concat(keywords);
|
|
415
|
+
return new import_language2.LanguageSupport(lang, [
|
|
416
|
+
javascriptLanguage.data.of({
|
|
417
|
+
autocomplete: (0, import_autocomplete2.ifNotIn)(dontComplete, (0, import_autocomplete2.completeFromList)(completions))
|
|
418
|
+
}),
|
|
419
|
+
javascriptLanguage.data.of({
|
|
420
|
+
autocomplete: localCompletionSource
|
|
421
|
+
}),
|
|
422
|
+
config.jsx ? autoCloseTags : []
|
|
423
|
+
]);
|
|
424
|
+
}
|
|
425
|
+
function findOpenTag(node) {
|
|
426
|
+
for (; ; ) {
|
|
427
|
+
if (node.name == "JSXOpenTag" || node.name == "JSXSelfClosingTag" || node.name == "JSXFragmentTag") {
|
|
428
|
+
return node;
|
|
429
|
+
}
|
|
430
|
+
if (node.name == "JSXEscape" || !node.parent) {
|
|
431
|
+
return null;
|
|
432
|
+
}
|
|
433
|
+
node = node.parent;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
function elementName(doc, tree, max = doc.length) {
|
|
437
|
+
for (let ch = tree == null ? void 0 : tree.firstChild; ch; ch = ch.nextSibling) {
|
|
438
|
+
if (ch.name == "JSXIdentifier" || ch.name == "JSXBuiltin" || ch.name == "JSXNamespacedName" || ch.name == "JSXMemberExpression") {
|
|
439
|
+
return doc.sliceString(ch.from, Math.min(ch.to, max));
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
return "";
|
|
443
|
+
}
|
|
444
|
+
var android = typeof navigator === "object" && /Android\b/.test(navigator.userAgent);
|
|
445
|
+
var autoCloseTags = import_view.EditorView.inputHandler.of(
|
|
446
|
+
(view, from, to, text, defaultInsert) => {
|
|
447
|
+
if ((android ? view.composing : view.compositionStarted) || view.state.readOnly || from != to || text != ">" && text != "/" || !javascriptLanguage.isActiveAt(view.state, from, -1)) {
|
|
448
|
+
return false;
|
|
449
|
+
}
|
|
450
|
+
const base = defaultInsert(), { state } = base;
|
|
451
|
+
const closeTags = state.changeByRange((range) => {
|
|
452
|
+
var _a;
|
|
453
|
+
const { head } = range;
|
|
454
|
+
let around = (0, import_language2.syntaxTree)(state).resolveInner(head - 1, -1);
|
|
455
|
+
let name;
|
|
456
|
+
if (around.name == "JSXStartTag") {
|
|
457
|
+
around = around.parent;
|
|
458
|
+
}
|
|
459
|
+
if (state.doc.sliceString(head - 1, head) != text || around.name == "JSXAttributeValue" && around.to > head) {
|
|
460
|
+
} else if (text == ">" && around.name == "JSXFragmentTag") {
|
|
461
|
+
return { range, changes: { from: head, insert: "</>" } };
|
|
462
|
+
} else if (text == "/" && around.name == "JSXStartCloseTag") {
|
|
463
|
+
const empty = around.parent, base2 = empty.parent;
|
|
464
|
+
if (base2 && empty.from == head - 2 && ((name = elementName(state.doc, base2.firstChild, head)) || ((_a = base2.firstChild) == null ? void 0 : _a.name) == "JSXFragmentTag")) {
|
|
465
|
+
const insert = `${name}>`;
|
|
466
|
+
return {
|
|
467
|
+
range: import_state.EditorSelection.cursor(head + insert.length, -1),
|
|
468
|
+
changes: { from: head, insert }
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
} else if (text == ">") {
|
|
472
|
+
const openTag = findOpenTag(around);
|
|
473
|
+
if (openTag && openTag.name == "JSXOpenTag" && !/^\/?>|^<\//.test(state.doc.sliceString(head, head + 2)) && (name = elementName(state.doc, openTag, head))) {
|
|
474
|
+
return { range, changes: { from: head, insert: `</${name}>` } };
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
return { range };
|
|
478
|
+
});
|
|
479
|
+
if (closeTags.changes.empty) {
|
|
480
|
+
return false;
|
|
481
|
+
}
|
|
482
|
+
view.dispatch([
|
|
483
|
+
base,
|
|
484
|
+
state.update(closeTags, {
|
|
485
|
+
userEvent: "input.complete",
|
|
486
|
+
scrollIntoView: true
|
|
487
|
+
})
|
|
488
|
+
]);
|
|
489
|
+
return true;
|
|
490
|
+
}
|
|
491
|
+
);
|
|
492
|
+
|
|
493
|
+
// src/eslint.ts
|
|
494
|
+
function esLint(eslint, config) {
|
|
495
|
+
if (!config) {
|
|
496
|
+
config = {
|
|
497
|
+
parserOptions: { ecmaVersion: 2019, sourceType: "module" },
|
|
498
|
+
env: {
|
|
499
|
+
browser: true,
|
|
500
|
+
node: true,
|
|
501
|
+
es6: true,
|
|
502
|
+
es2015: true,
|
|
503
|
+
es2017: true,
|
|
504
|
+
es2020: true
|
|
505
|
+
},
|
|
506
|
+
rules: {}
|
|
507
|
+
};
|
|
508
|
+
eslint.getRules().forEach((desc, name) => {
|
|
509
|
+
var _a;
|
|
510
|
+
if ((_a = desc.meta.docs) == null ? void 0 : _a.recommended) {
|
|
511
|
+
config.rules[name] = 2;
|
|
512
|
+
}
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
return (view) => {
|
|
516
|
+
const { state } = view, found = [];
|
|
517
|
+
for (const { from, to } of javascriptLanguage.findRegions(state)) {
|
|
518
|
+
const fromLine = state.doc.lineAt(from), offset = {
|
|
519
|
+
line: fromLine.number - 1,
|
|
520
|
+
col: from - fromLine.from,
|
|
521
|
+
pos: from
|
|
522
|
+
};
|
|
523
|
+
for (const d of eslint.verify(state.sliceDoc(from, to), config)) {
|
|
524
|
+
found.push(translateDiagnostic(d, state.doc, offset));
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
return found;
|
|
528
|
+
};
|
|
529
|
+
}
|
|
530
|
+
function mapPos(line, col, doc, offset) {
|
|
531
|
+
return doc.line(line + offset.line).from + col + (line == 1 ? offset.col - 1 : -1);
|
|
532
|
+
}
|
|
533
|
+
function translateDiagnostic(input, doc, offset) {
|
|
534
|
+
const start = mapPos(input.line, input.column, doc, offset);
|
|
535
|
+
const result = {
|
|
536
|
+
from: start,
|
|
537
|
+
to: input.endLine != null && input.endColumn != 1 ? mapPos(input.endLine, input.endColumn, doc, offset) : start,
|
|
538
|
+
message: input.message,
|
|
539
|
+
source: input.ruleId ? `eslint:${input.ruleId}` : "eslint",
|
|
540
|
+
severity: input.severity == 1 ? "warning" : "error"
|
|
541
|
+
};
|
|
542
|
+
if (input.fix) {
|
|
543
|
+
const { range, text } = input.fix, from = range[0] + offset.pos - start, to = range[1] + offset.pos - start;
|
|
544
|
+
result.actions = [
|
|
545
|
+
{
|
|
546
|
+
name: "fix",
|
|
547
|
+
apply(view, start2) {
|
|
548
|
+
view.dispatch({
|
|
549
|
+
changes: { from: start2 + from, to: start2 + to, insert: text },
|
|
550
|
+
scrollIntoView: true
|
|
551
|
+
});
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
];
|
|
555
|
+
}
|
|
556
|
+
return result;
|
|
557
|
+
}
|
|
558
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
559
|
+
0 && (module.exports = {
|
|
560
|
+
autoCloseTags,
|
|
561
|
+
completionPath,
|
|
562
|
+
esLint,
|
|
563
|
+
javascript,
|
|
564
|
+
javascriptLanguage,
|
|
565
|
+
jsxLanguage,
|
|
566
|
+
localCompletionSource,
|
|
567
|
+
scopeCompletionSource,
|
|
568
|
+
snippets,
|
|
569
|
+
tsxLanguage,
|
|
570
|
+
typescriptLanguage,
|
|
571
|
+
typescriptSnippets
|
|
572
|
+
});
|
|
573
|
+
//# sourceMappingURL=index.js.map
|