@player-tools/json-language-service 0.2.2--canary.20.454

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,2519 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var jsoncParser = require('jsonc-parser');
6
+ var tapableTs = require('tapable-ts');
7
+ var vscodeLanguageserverTypes = require('vscode-languageserver-types');
8
+ var vscodeLanguageserverTextdocument = require('vscode-languageserver-textdocument');
9
+ var detectIndent = require('detect-indent');
10
+ var timm = require('timm');
11
+ var xlrSdk = require('@player-tools/xlr-sdk');
12
+ var xlrUtils = require('@player-tools/xlr-utils');
13
+
14
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
15
+
16
+ var detectIndent__default = /*#__PURE__*/_interopDefaultLegacy(detectIndent);
17
+
18
+ function isPropertyNode(node) {
19
+ return (node == null ? void 0 : node.type) === "property";
20
+ }
21
+ function isObjectNode(node) {
22
+ return (node == null ? void 0 : node.type) === "object";
23
+ }
24
+ function isKeyNode(node) {
25
+ return isPropertyNode(node == null ? void 0 : node.parent) && (node == null ? void 0 : node.parent.keyNode) === node;
26
+ }
27
+ function isValueNode(node) {
28
+ return isPropertyNode(node == null ? void 0 : node.parent) && (node == null ? void 0 : node.parent.valueNode) === node;
29
+ }
30
+ function isViewNode(node) {
31
+ return (node == null ? void 0 : node.type) === "view";
32
+ }
33
+ function isStateNode(node) {
34
+ return (node == null ? void 0 : node.type) === "state";
35
+ }
36
+ function isFlowNode(node) {
37
+ return (node == null ? void 0 : node.type) === "flow";
38
+ }
39
+ function isNavigationNode(node) {
40
+ return (node == null ? void 0 : node.type) === "navigation";
41
+ }
42
+ function getValForKey(node, key) {
43
+ var _a;
44
+ const prop = node.properties.find((k) => k.keyNode.value === key);
45
+ return (_a = prop == null ? void 0 : prop.valueNode) == null ? void 0 : _a.jsonNode.value;
46
+ }
47
+ function getPropForValNode(node) {
48
+ if (isValueNode(node) && isPropertyNode(node == null ? void 0 : node.parent)) {
49
+ return node == null ? void 0 : node.parent.keyNode.value;
50
+ }
51
+ }
52
+ function getViewNode(node) {
53
+ if (node.parent) {
54
+ if (node.parent.type === "view") {
55
+ return node.parent;
56
+ }
57
+ return getViewNode(node.parent);
58
+ }
59
+ if (node.type === "asset") {
60
+ return node;
61
+ }
62
+ }
63
+ function getContentNode(node) {
64
+ if (node.type === "content") {
65
+ return node;
66
+ }
67
+ return node.parent ? getContentNode(node.parent) : void 0;
68
+ }
69
+
70
+ class ASTNodeImpl {
71
+ constructor(jsonNode, parent) {
72
+ this.jsonNode = jsonNode;
73
+ this.parent = parent;
74
+ }
75
+ }
76
+ class StringASTNodeImpl extends ASTNodeImpl {
77
+ constructor(jsonNode, parent) {
78
+ super(jsonNode, parent);
79
+ this.type = "string";
80
+ this.value = jsonNode.value;
81
+ }
82
+ }
83
+ class NumberASTNodeImpl extends ASTNodeImpl {
84
+ constructor(jsonNode, parent) {
85
+ super(jsonNode, parent);
86
+ this.type = "number";
87
+ this.value = jsonNode.value;
88
+ }
89
+ }
90
+ class BooleanASTNodeImpl extends ASTNodeImpl {
91
+ constructor(jsonNode, parent) {
92
+ super(jsonNode, parent);
93
+ this.type = "boolean";
94
+ this.value = jsonNode.value;
95
+ }
96
+ }
97
+ class NullASTNodeImpl extends ASTNodeImpl {
98
+ constructor() {
99
+ super(...arguments);
100
+ this.type = "null";
101
+ this.value = null;
102
+ }
103
+ }
104
+ class PropertyASTNodeImpl extends ASTNodeImpl {
105
+ constructor(jsonNode, parent, keyNode) {
106
+ super(jsonNode, parent);
107
+ this.type = "property";
108
+ this.keyNode = keyNode;
109
+ }
110
+ get children() {
111
+ return this.valueNode ? [this.keyNode, this.valueNode] : [this.keyNode];
112
+ }
113
+ }
114
+ class ViewASTNodeImpl extends ASTNodeImpl {
115
+ constructor() {
116
+ super(...arguments);
117
+ this.type = "view";
118
+ this.properties = [];
119
+ this.id = void 0;
120
+ this.viewType = void 0;
121
+ }
122
+ get children() {
123
+ return this.properties;
124
+ }
125
+ }
126
+ class ContentASTNodeImpl extends ASTNodeImpl {
127
+ constructor() {
128
+ super(...arguments);
129
+ this.type = "content";
130
+ this.properties = [];
131
+ this.views = void 0;
132
+ this.navigation = void 0;
133
+ }
134
+ get children() {
135
+ return this.properties;
136
+ }
137
+ }
138
+ class NavigationASTNodeImpl extends ASTNodeImpl {
139
+ constructor() {
140
+ super(...arguments);
141
+ this.type = "navigation";
142
+ this.properties = [];
143
+ this.begin = void 0;
144
+ this.flows = [];
145
+ }
146
+ get children() {
147
+ return this.properties;
148
+ }
149
+ }
150
+ class FlowASTNodeImpl extends ASTNodeImpl {
151
+ constructor() {
152
+ super(...arguments);
153
+ this.type = "flow";
154
+ this.properties = [];
155
+ this.start = void 0;
156
+ this.states = [];
157
+ }
158
+ get children() {
159
+ return this.properties;
160
+ }
161
+ }
162
+ class FlowStateASTNodeImpl extends ASTNodeImpl {
163
+ constructor() {
164
+ super(...arguments);
165
+ this.type = "state";
166
+ this.properties = [];
167
+ this.stateType = void 0;
168
+ }
169
+ get children() {
170
+ return this.properties;
171
+ }
172
+ }
173
+ class AssetASTNodeImpl extends ASTNodeImpl {
174
+ constructor() {
175
+ super(...arguments);
176
+ this.type = "asset";
177
+ this.properties = [];
178
+ this.id = void 0;
179
+ this.assetType = void 0;
180
+ }
181
+ get children() {
182
+ return this.properties;
183
+ }
184
+ }
185
+ class ArrayASTNodeImpl extends ASTNodeImpl {
186
+ constructor() {
187
+ super(...arguments);
188
+ this.type = "array";
189
+ this.items = [];
190
+ }
191
+ get children() {
192
+ return this.items;
193
+ }
194
+ }
195
+ class ObjectASTNodeImpl extends ASTNodeImpl {
196
+ constructor() {
197
+ super(...arguments);
198
+ this.type = "object";
199
+ this.properties = [];
200
+ }
201
+ get children() {
202
+ return this.properties;
203
+ }
204
+ }
205
+
206
+ var ParseErrorCode;
207
+ (function(ParseErrorCode2) {
208
+ ParseErrorCode2[ParseErrorCode2["InvalidSymbol"] = 1] = "InvalidSymbol";
209
+ ParseErrorCode2[ParseErrorCode2["InvalidNumberFormat"] = 2] = "InvalidNumberFormat";
210
+ ParseErrorCode2[ParseErrorCode2["PropertyNameExpected"] = 3] = "PropertyNameExpected";
211
+ ParseErrorCode2[ParseErrorCode2["ValueExpected"] = 4] = "ValueExpected";
212
+ ParseErrorCode2[ParseErrorCode2["ColonExpected"] = 5] = "ColonExpected";
213
+ ParseErrorCode2[ParseErrorCode2["CommaExpected"] = 6] = "CommaExpected";
214
+ ParseErrorCode2[ParseErrorCode2["CloseBraceExpected"] = 7] = "CloseBraceExpected";
215
+ ParseErrorCode2[ParseErrorCode2["CloseBracketExpected"] = 8] = "CloseBracketExpected";
216
+ ParseErrorCode2[ParseErrorCode2["EndOfFileExpected"] = 9] = "EndOfFileExpected";
217
+ ParseErrorCode2[ParseErrorCode2["InvalidCommentToken"] = 10] = "InvalidCommentToken";
218
+ ParseErrorCode2[ParseErrorCode2["UnexpectedEndOfComment"] = 11] = "UnexpectedEndOfComment";
219
+ ParseErrorCode2[ParseErrorCode2["UnexpectedEndOfString"] = 12] = "UnexpectedEndOfString";
220
+ ParseErrorCode2[ParseErrorCode2["UnexpectedEndOfNumber"] = 13] = "UnexpectedEndOfNumber";
221
+ ParseErrorCode2[ParseErrorCode2["InvalidUnicode"] = 14] = "InvalidUnicode";
222
+ ParseErrorCode2[ParseErrorCode2["InvalidEscapeCharacter"] = 15] = "InvalidEscapeCharacter";
223
+ ParseErrorCode2[ParseErrorCode2["InvalidCharacter"] = 16] = "InvalidCharacter";
224
+ })(ParseErrorCode || (ParseErrorCode = {}));
225
+ function prettyPrintParseErrorCode(code) {
226
+ switch (code) {
227
+ case 6:
228
+ return `Expected comma`;
229
+ case 5:
230
+ return `Expected colon`;
231
+ case 3:
232
+ return `Expected property name`;
233
+ case 4:
234
+ return `Expected value`;
235
+ case 7:
236
+ return `Expected }`;
237
+ case 9:
238
+ return `Expected end of file`;
239
+ case 8:
240
+ return `Expected ]`;
241
+ case 12:
242
+ return `Expected "`;
243
+ default:
244
+ return jsoncParser.printParseErrorCode(code);
245
+ }
246
+ }
247
+ function convertErrorsToDiags(document, errors) {
248
+ return errors.map((parseError) => {
249
+ return vscodeLanguageserverTypes.Diagnostic.create(vscodeLanguageserverTypes.Range.create(document.positionAt(parseError.offset), document.positionAt(parseError.offset + parseError.length)), prettyPrintParseErrorCode(parseError.error), vscodeLanguageserverTypes.DiagnosticSeverity.Error);
250
+ });
251
+ }
252
+
253
+ function isStringProperty(node) {
254
+ var _a;
255
+ return ((_a = node.valueNode) == null ? void 0 : _a.type) === "string";
256
+ }
257
+ class PlayerContent {
258
+ constructor(root, errors, jsonToNodeMap) {
259
+ this.root = root;
260
+ this.jsonNodeToNode = jsonToNodeMap;
261
+ this.syntaxErrors = errors;
262
+ }
263
+ getNodeFromOffset(offset) {
264
+ const jsonNode = jsoncParser.findNodeAtOffset(this.root.jsonNode, offset);
265
+ if (!jsonNode) {
266
+ return;
267
+ }
268
+ if (this.jsonNodeToNode.has(jsonNode)) {
269
+ return this.jsonNodeToNode.get(jsonNode);
270
+ }
271
+ const parentNode = jsonNode.parent;
272
+ if (this.jsonNodeToNode.has(parentNode)) {
273
+ return this.jsonNodeToNode.get(parentNode);
274
+ }
275
+ const grandparentNode = parentNode.parent;
276
+ if (this.jsonNodeToNode.has(grandparentNode)) {
277
+ return this.jsonNodeToNode.get(grandparentNode);
278
+ }
279
+ const greatGrandparentNode = grandparentNode.parent;
280
+ if (this.jsonNodeToNode.has(greatGrandparentNode)) {
281
+ return this.jsonNodeToNode.get(greatGrandparentNode);
282
+ }
283
+ }
284
+ }
285
+ exports.ObjType = void 0;
286
+ (function(ObjType2) {
287
+ ObjType2[ObjType2["FLOW"] = 0] = "FLOW";
288
+ ObjType2[ObjType2["ASSET"] = 1] = "ASSET";
289
+ ObjType2[ObjType2["ASSET_WRAPPER"] = 2] = "ASSET_WRAPPER";
290
+ ObjType2[ObjType2["UNKNOWN"] = 3] = "UNKNOWN";
291
+ })(exports.ObjType || (exports.ObjType = {}));
292
+ function identify(node) {
293
+ var _a;
294
+ if (node === void 0 || node.type !== "object") {
295
+ return 3;
296
+ }
297
+ const knownProps = (_a = node.children) == null ? void 0 : _a.reduce((props, childNode) => {
298
+ if (childNode.type === "property" && childNode.children) {
299
+ const [key] = childNode.children;
300
+ props.add(key.value);
301
+ }
302
+ return props;
303
+ }, new Set());
304
+ if (knownProps == null ? void 0 : knownProps.has("type")) {
305
+ return 1;
306
+ }
307
+ return 0;
308
+ }
309
+ function parse(document) {
310
+ const errors = [];
311
+ const jsonToNode = new Map();
312
+ const root = jsoncParser.parseTree(document.getText(), errors, {
313
+ disallowComments: true
314
+ });
315
+ const diags = convertErrorsToDiags(document, errors);
316
+ function parseAsset(node, parent) {
317
+ var _a;
318
+ const assetNode = new AssetASTNodeImpl(node, parent);
319
+ (_a = node.children) == null ? void 0 : _a.forEach((prop) => {
320
+ var _a2;
321
+ if (prop.type === "property" && ((_a2 = prop.children) == null ? void 0 : _a2.length)) {
322
+ const [key, val] = prop.children;
323
+ const keyNode = new StringASTNodeImpl(key);
324
+ const property = new PropertyASTNodeImpl(prop, assetNode, keyNode);
325
+ property.keyNode = new StringASTNodeImpl(key, property);
326
+ property.valueNode = parseUnknownNode(val, property);
327
+ if (key.value === "id" && isStringProperty(property)) {
328
+ assetNode.id = property;
329
+ } else if (key.value === "type" && isStringProperty(property)) {
330
+ assetNode.assetType = property;
331
+ }
332
+ assetNode.properties.push(property);
333
+ jsonToNode.set(prop, property);
334
+ jsonToNode.set(key, property.keyNode);
335
+ if (property.valueNode) {
336
+ jsonToNode.set(val, property.valueNode);
337
+ }
338
+ }
339
+ });
340
+ return assetNode;
341
+ }
342
+ function parseUnknownNode(node, parent) {
343
+ var _a, _b;
344
+ switch (node == null ? void 0 : node.type) {
345
+ case "string": {
346
+ const newNode = new StringASTNodeImpl(node, parent);
347
+ jsonToNode.set(node, newNode);
348
+ return newNode;
349
+ }
350
+ case "null": {
351
+ const newNode = new NullASTNodeImpl(node, parent);
352
+ jsonToNode.set(node, newNode);
353
+ return newNode;
354
+ }
355
+ case "boolean": {
356
+ const newNode = new BooleanASTNodeImpl(node, parent);
357
+ jsonToNode.set(node, newNode);
358
+ return newNode;
359
+ }
360
+ case "number": {
361
+ const newNode = new NumberASTNodeImpl(node, parent);
362
+ jsonToNode.set(node, newNode);
363
+ return newNode;
364
+ }
365
+ case "array": {
366
+ const arr = new ArrayASTNodeImpl(node, parent);
367
+ (_a = node.children) == null ? void 0 : _a.forEach((arrChild) => {
368
+ const child = parseUnknownNode(arrChild, arr);
369
+ if (child) {
370
+ jsonToNode.set(arrChild, child);
371
+ arr.items.push(child);
372
+ }
373
+ });
374
+ jsonToNode.set(node, arr);
375
+ return arr;
376
+ }
377
+ case "object": {
378
+ const obj = new ObjectASTNodeImpl(node, parent);
379
+ jsonToNode.set(node, obj);
380
+ (_b = node.children) == null ? void 0 : _b.forEach((prop) => {
381
+ var _a2;
382
+ if (prop.type === "property" && ((_a2 = prop.children) == null ? void 0 : _a2.length)) {
383
+ const [key, val] = prop.children;
384
+ const keyNode = new StringASTNodeImpl(key);
385
+ const propNode = new PropertyASTNodeImpl(prop, obj, keyNode);
386
+ propNode.keyNode = new StringASTNodeImpl(key, propNode);
387
+ if (val) {
388
+ if (keyNode.value === "asset") {
389
+ propNode.valueNode = parseAsset(val, propNode);
390
+ } else {
391
+ propNode.valueNode = parseUnknownNode(val, propNode);
392
+ }
393
+ }
394
+ jsonToNode.set(prop, propNode);
395
+ jsonToNode.set(key, propNode.keyNode);
396
+ if (propNode.valueNode) {
397
+ jsonToNode.set(val, propNode.valueNode);
398
+ }
399
+ obj.properties.push(propNode);
400
+ }
401
+ });
402
+ return obj;
403
+ }
404
+ }
405
+ }
406
+ function parseView(node, parent) {
407
+ var _a;
408
+ const viewNode = new ViewASTNodeImpl(node, parent);
409
+ (_a = node.children) == null ? void 0 : _a.forEach((prop) => {
410
+ var _a2, _b, _c;
411
+ if (prop.type === "property" && ((_b = (_a2 = prop.children) == null ? void 0 : _a2.length) != null ? _b : 0) > 0) {
412
+ const [key, val] = (_c = prop.children) != null ? _c : [];
413
+ const keyNode = new StringASTNodeImpl(key);
414
+ const property = new PropertyASTNodeImpl(prop, viewNode, keyNode);
415
+ property.keyNode = new StringASTNodeImpl(key, property);
416
+ if (val) {
417
+ property.valueNode = parseUnknownNode(val, property);
418
+ if (key.value === "id" && isStringProperty(property)) {
419
+ viewNode.id = property;
420
+ } else if (key.value === "type" && isStringProperty(property)) {
421
+ viewNode.viewType = property;
422
+ }
423
+ }
424
+ jsonToNode.set(prop, property);
425
+ jsonToNode.set(key, property.keyNode);
426
+ if (property.valueNode) {
427
+ jsonToNode.set(val, property.valueNode);
428
+ }
429
+ viewNode.properties.push(property);
430
+ }
431
+ });
432
+ jsonToNode.set(node, viewNode);
433
+ return viewNode;
434
+ }
435
+ function parseFlowState(node, parent) {
436
+ var _a;
437
+ const state = new FlowStateASTNodeImpl(node, parent);
438
+ jsonToNode.set(node, state);
439
+ if (node.type === "object") {
440
+ (_a = node.children) == null ? void 0 : _a.forEach((prop) => {
441
+ var _a2;
442
+ if (prop.type === "property" && ((_a2 = prop.children) == null ? void 0 : _a2.length)) {
443
+ const [key, val] = prop.children;
444
+ const keyNode = new StringASTNodeImpl(key);
445
+ const property = new PropertyASTNodeImpl(prop, state, keyNode);
446
+ property.keyNode = new StringASTNodeImpl(key, property);
447
+ if (key.value === "state_type" && (val == null ? void 0 : val.type) === "string") {
448
+ property.valueNode = parseUnknownNode(val, property);
449
+ state.stateType = property;
450
+ } else {
451
+ property.valueNode = parseUnknownNode(val, property);
452
+ }
453
+ jsonToNode.set(prop, property);
454
+ jsonToNode.set(key, property.keyNode);
455
+ if (property.valueNode) {
456
+ jsonToNode.set(val, property.valueNode);
457
+ }
458
+ state.properties.push(property);
459
+ }
460
+ });
461
+ }
462
+ return state;
463
+ }
464
+ function parseFlow(node, parent) {
465
+ var _a;
466
+ const flow = new FlowASTNodeImpl(node, parent);
467
+ jsonToNode.set(node, flow);
468
+ if (node.type === "object") {
469
+ (_a = node.children) == null ? void 0 : _a.forEach((prop) => {
470
+ var _a2;
471
+ if (prop.type === "property" && ((_a2 = prop.children) == null ? void 0 : _a2.length)) {
472
+ const [key, val] = prop.children;
473
+ const keyNode = new StringASTNodeImpl(key);
474
+ const property = new PropertyASTNodeImpl(prop, flow, keyNode);
475
+ property.keyNode = new StringASTNodeImpl(key, property);
476
+ if (key.value === "startState" && (val == null ? void 0 : val.type) === "string") {
477
+ property.valueNode = parseUnknownNode(val, property);
478
+ flow.start = property;
479
+ } else if ((val == null ? void 0 : val.type) === "object" && property.keyNode.value !== "onStart" && property.keyNode.value !== "onEnd") {
480
+ property.valueNode = parseFlowState(val, property);
481
+ flow.states.push(property);
482
+ } else {
483
+ property.valueNode = parseUnknownNode(val, property);
484
+ }
485
+ jsonToNode.set(prop, property);
486
+ jsonToNode.set(key, property.keyNode);
487
+ if (property.valueNode) {
488
+ jsonToNode.set(val, property.valueNode);
489
+ }
490
+ flow.properties.push(property);
491
+ }
492
+ });
493
+ }
494
+ return flow;
495
+ }
496
+ function parseNavigation(node, parent) {
497
+ var _a;
498
+ const navNode = new NavigationASTNodeImpl(node, parent);
499
+ jsonToNode.set(node, navNode);
500
+ if (node.type === "object") {
501
+ (_a = node.children) == null ? void 0 : _a.forEach((prop) => {
502
+ var _a2;
503
+ if (prop.type === "property" && ((_a2 = prop.children) == null ? void 0 : _a2.length)) {
504
+ const [key, val] = prop.children;
505
+ const keyNode = new StringASTNodeImpl(key);
506
+ const property = new PropertyASTNodeImpl(prop, navNode, keyNode);
507
+ property.keyNode = new StringASTNodeImpl(key, property);
508
+ if (key.value === "BEGIN" && (val == null ? void 0 : val.type) === "string") {
509
+ property.valueNode = parseUnknownNode(val, property);
510
+ navNode.begin = property;
511
+ } else if ((val == null ? void 0 : val.type) === "object") {
512
+ property.valueNode = parseFlow(val, property);
513
+ navNode.flows.push(property);
514
+ }
515
+ jsonToNode.set(prop, property);
516
+ jsonToNode.set(key, property.keyNode);
517
+ if (property.valueNode) {
518
+ jsonToNode.set(val, property.valueNode);
519
+ }
520
+ navNode.properties.push(property);
521
+ }
522
+ });
523
+ }
524
+ return navNode;
525
+ }
526
+ function parseContent(node) {
527
+ var _a;
528
+ const contentNode = new ContentASTNodeImpl(node, void 0);
529
+ if (node.type === "object") {
530
+ (_a = node.children) == null ? void 0 : _a.forEach((childProp) => {
531
+ var _a2, _b;
532
+ if (childProp.type === "property" && ((_a2 = childProp.children) == null ? void 0 : _a2.length)) {
533
+ const [key, val] = childProp.children;
534
+ const keyNode = new StringASTNodeImpl(key);
535
+ const property = new PropertyASTNodeImpl(childProp, contentNode, keyNode);
536
+ property.keyNode = new StringASTNodeImpl(key, property);
537
+ if (key.value === "views" && (val == null ? void 0 : val.type) === "array") {
538
+ const views = new ArrayASTNodeImpl(val, property);
539
+ (_b = val.children) == null ? void 0 : _b.forEach((view) => {
540
+ const parsedV = parseView(view, views);
541
+ jsonToNode.set(view, parsedV);
542
+ views.items.push(parsedV);
543
+ });
544
+ property.valueNode = views;
545
+ contentNode.views = property;
546
+ } else if (key.value === "navigation" && val) {
547
+ const nav = parseNavigation(val, property);
548
+ contentNode.navigation = property;
549
+ property.valueNode = nav;
550
+ } else if (val) {
551
+ property.valueNode = parseUnknownNode(val, property);
552
+ }
553
+ jsonToNode.set(childProp, property);
554
+ jsonToNode.set(key, property.keyNode);
555
+ if (property.valueNode) {
556
+ jsonToNode.set(val, property.valueNode);
557
+ }
558
+ contentNode.properties.push(property);
559
+ }
560
+ });
561
+ }
562
+ jsonToNode.set(node, contentNode);
563
+ return contentNode;
564
+ }
565
+ let rootASTNode = {
566
+ type: "empty",
567
+ value: void 0,
568
+ jsonNode: root
569
+ };
570
+ const objType = identify(root);
571
+ switch (objType) {
572
+ case 1:
573
+ rootASTNode = parseAsset(root);
574
+ break;
575
+ case 0:
576
+ rootASTNode = parseContent(root);
577
+ break;
578
+ }
579
+ return new PlayerContent(rootASTNode, diags, jsonToNode);
580
+ }
581
+
582
+ function replaceString(node, newText) {
583
+ return {
584
+ type: "replace",
585
+ node,
586
+ value: newText
587
+ };
588
+ }
589
+ function toRange(document, node) {
590
+ return vscodeLanguageserverTypes.Range.create(document.positionAt(node.jsonNode.offset), document.positionAt(node.jsonNode.offset + node.jsonNode.length));
591
+ }
592
+ function toTextEdit(document, edit) {
593
+ switch (edit.type) {
594
+ case "replace":
595
+ return vscodeLanguageserverTypes.TextEdit.replace(toRange(document, edit.node), edit.value);
596
+ default:
597
+ throw new Error("Dont know how to convert this to a TextEdit");
598
+ }
599
+ }
600
+
601
+ var __async$9 = (__this, __arguments, generator) => {
602
+ return new Promise((resolve, reject) => {
603
+ var fulfilled = (value) => {
604
+ try {
605
+ step(generator.next(value));
606
+ } catch (e) {
607
+ reject(e);
608
+ }
609
+ };
610
+ var rejected = (value) => {
611
+ try {
612
+ step(generator.throw(value));
613
+ } catch (e) {
614
+ reject(e);
615
+ }
616
+ };
617
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
618
+ step((generator = generator.apply(__this, __arguments)).next());
619
+ });
620
+ };
621
+ function walk(node, visitor) {
622
+ return __async$9(this, null, function* () {
623
+ const queue = [node];
624
+ let stop = false;
625
+ while (queue.length > 0 && !stop) {
626
+ const nodeToVisit = queue.shift();
627
+ if (nodeToVisit == null ? void 0 : nodeToVisit.children) {
628
+ queue.push(...nodeToVisit.children);
629
+ }
630
+ stop = nodeToVisit ? yield visitor(nodeToVisit) : true;
631
+ }
632
+ });
633
+ }
634
+ function getNodeValue(node) {
635
+ return jsoncParser.getNodeValue(node.jsonNode);
636
+ }
637
+
638
+ const typeToVisitorMap = {
639
+ string: "StringNode",
640
+ number: "NumberNode",
641
+ boolean: "BooleanNode",
642
+ array: "ArrayNode",
643
+ null: "NullNode",
644
+ empty: "EmptyNode",
645
+ property: "PropertyNode",
646
+ object: "ObjectNode",
647
+ asset: "AssetNode",
648
+ view: "ViewNode",
649
+ content: "ContentNode",
650
+ navigation: "NavigationNode",
651
+ flow: "FlowNode",
652
+ state: "FlowStateNode"
653
+ };
654
+ function containsRange(source, range) {
655
+ const { start, end } = range;
656
+ const { start: srcStart, end: srcEnd } = source;
657
+ return start.line === srcStart.line && end.line === srcEnd.line && start.character >= srcStart.character && end.character <= srcEnd.character;
658
+ }
659
+ function toTextDocument(str) {
660
+ return vscodeLanguageserverTextdocument.TextDocument.create("foo", "json", 1, str);
661
+ }
662
+ function isKnownRootType(document) {
663
+ const { type } = document.root;
664
+ return type === "view" || type === "asset" || type === "content";
665
+ }
666
+ function isValueCompletion(node) {
667
+ var _a;
668
+ return ((_a = node.parent) == null ? void 0 : _a.type) === "property" && node.parent.valueNode === node;
669
+ }
670
+ function isPropertyCompletion(node) {
671
+ var _a;
672
+ return ((_a = node.parent) == null ? void 0 : _a.type) === "property" && node.parent.keyNode === node;
673
+ }
674
+ function getProperty(obj, name) {
675
+ if ("properties" in obj) {
676
+ return obj.properties.find((p) => p.keyNode.value === name);
677
+ }
678
+ }
679
+ function getLSLocationOfNode(document, node) {
680
+ const nodeRange = vscodeLanguageserverTypes.Range.create(document.positionAt(node.jsonNode.offset), document.positionAt(node.jsonNode.offset + node.jsonNode.length));
681
+ return vscodeLanguageserverTypes.Location.create(document.uri, nodeRange);
682
+ }
683
+ function getDepth(node) {
684
+ if (!node.parent) {
685
+ return 0;
686
+ }
687
+ if (node.type === "property") {
688
+ return getDepth(node.parent);
689
+ }
690
+ return 1 + getDepth(node.parent);
691
+ }
692
+ function formatLikeNode(document, originalNode, replacement) {
693
+ const { indent } = detectIndent__default["default"](document.getText());
694
+ const depth = getDepth(originalNode);
695
+ return JSON.stringify(replacement, null, indent).split("\n").map((l, index) => index === 0 ? l : `${indent.repeat(depth)}${l}`).join("\n");
696
+ }
697
+ function mapFlowStateToType(flowType) {
698
+ let flowXLR;
699
+ switch (flowType) {
700
+ case "VIEW":
701
+ flowXLR = "NavigationFlowViewState";
702
+ break;
703
+ case "END":
704
+ flowXLR = "NavigationFlowEndState";
705
+ break;
706
+ case "ACTION":
707
+ flowXLR = "NavigationFlowActionState";
708
+ break;
709
+ case "EXTERNAL":
710
+ flowXLR = "NavigationFlowExternalState";
711
+ break;
712
+ case "FLOW":
713
+ flowXLR = "NavigationFlowFlowState";
714
+ break;
715
+ }
716
+ return flowXLR;
717
+ }
718
+
719
+ var __async$8 = (__this, __arguments, generator) => {
720
+ return new Promise((resolve, reject) => {
721
+ var fulfilled = (value) => {
722
+ try {
723
+ step(generator.next(value));
724
+ } catch (e) {
725
+ reject(e);
726
+ }
727
+ };
728
+ var rejected = (value) => {
729
+ try {
730
+ step(generator.throw(value));
731
+ } catch (e) {
732
+ reject(e);
733
+ }
734
+ };
735
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
736
+ step((generator = generator.apply(__this, __arguments)).next());
737
+ });
738
+ };
739
+ const isInView = (node) => {
740
+ if (node.type === "view") {
741
+ return true;
742
+ }
743
+ if (!node.parent) {
744
+ return false;
745
+ }
746
+ return isInView(node.parent);
747
+ };
748
+ const checkTypesForAssetWrapper = (nodes) => {
749
+ for (let i = 0; i < nodes.length; i++) {
750
+ const node = nodes[i];
751
+ if (node.type === "ref" && node.ref.includes("AssetWrapper"))
752
+ return true;
753
+ if (node.type === "or")
754
+ return checkTypesForAssetWrapper(node.or);
755
+ if (node.type === "and")
756
+ return checkTypesForAssetWrapper(node.and);
757
+ }
758
+ return false;
759
+ };
760
+ const checkSwitchCase = (node) => {
761
+ return node.value === "staticSwitch" || node.value === "dynamicSwitch";
762
+ };
763
+ class AssetWrapperArrayPlugin {
764
+ constructor() {
765
+ this.name = "asset-wrapper-to-array";
766
+ }
767
+ apply(service) {
768
+ service.hooks.validate.tap(this.name, (documentInfo, validationContext) => __async$8(this, null, function* () {
769
+ validationContext.useASTVisitor({
770
+ ArrayNode: (arrayNode) => __async$8(this, null, function* () {
771
+ if (!isInView(arrayNode)) {
772
+ return;
773
+ }
774
+ const xlrInfo = service.XLRService.getTypeInfoAtPosition(arrayNode);
775
+ if (!xlrInfo)
776
+ return;
777
+ const isAssetWrapper = checkTypesForAssetWrapper(xlrInfo.nodes);
778
+ const parentNode = arrayNode.parent;
779
+ if ((parentNode == null ? void 0 : parentNode.type) !== "property") {
780
+ return;
781
+ }
782
+ const targetLabel = parentNode.keyNode;
783
+ const isSwitchCase = checkSwitchCase(targetLabel);
784
+ if (isAssetWrapper && !isSwitchCase) {
785
+ let newAsset = {
786
+ asset: {
787
+ id: "",
788
+ type: "collection",
789
+ values: getNodeValue(arrayNode)
790
+ }
791
+ };
792
+ if (arrayNode.children.length === 1) {
793
+ newAsset = getNodeValue(arrayNode.children[0]);
794
+ }
795
+ validationContext.addViolation({
796
+ node: targetLabel,
797
+ severity: vscodeLanguageserverTypes.DiagnosticSeverity.Error,
798
+ message: `Implicit Array -> "collection" assets is not supported.`,
799
+ fix: () => {
800
+ return {
801
+ name: `Convert to ${arrayNode.children.length > 0 ? "collection" : "asset"}`,
802
+ edit: {
803
+ type: "replace",
804
+ node: arrayNode,
805
+ value: formatLikeNode(documentInfo.document, arrayNode, newAsset)
806
+ }
807
+ };
808
+ }
809
+ });
810
+ }
811
+ })
812
+ });
813
+ }));
814
+ }
815
+ }
816
+
817
+ var __async$7 = (__this, __arguments, generator) => {
818
+ return new Promise((resolve, reject) => {
819
+ var fulfilled = (value) => {
820
+ try {
821
+ step(generator.next(value));
822
+ } catch (e) {
823
+ reject(e);
824
+ }
825
+ };
826
+ var rejected = (value) => {
827
+ try {
828
+ step(generator.throw(value));
829
+ } catch (e) {
830
+ reject(e);
831
+ }
832
+ };
833
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
834
+ step((generator = generator.apply(__this, __arguments)).next());
835
+ });
836
+ };
837
+ function getBindingInfo(ctx) {
838
+ var _a, _b, _c;
839
+ const info = {
840
+ bindingToSchemaType: new Map(),
841
+ typeToNode: new Map()
842
+ };
843
+ if (ctx.PlayerContent.root.type !== "content") {
844
+ return info;
845
+ }
846
+ const schemaRoot = (_a = ctx.PlayerContent.root.properties) == null ? void 0 : _a.find((child) => child.keyNode.value === "schema");
847
+ if (!schemaRoot || ((_b = schemaRoot.valueNode) == null ? void 0 : _b.type) !== "object") {
848
+ return info;
849
+ }
850
+ const schemaTypeQueue = [
851
+ {
852
+ currentPath: "",
853
+ typeToVisit: "ROOT",
854
+ visited: new Set()
855
+ }
856
+ ];
857
+ while (schemaTypeQueue.length > 0) {
858
+ const next = schemaTypeQueue.shift();
859
+ if (!next) {
860
+ break;
861
+ }
862
+ if (next.visited.has(next.typeToVisit)) {
863
+ continue;
864
+ }
865
+ const visited = new Set(...next.visited, next.typeToVisit);
866
+ const { currentPath, typeToVisit } = next;
867
+ const typeNode = schemaRoot.valueNode.properties.find((child) => child.keyNode.value === typeToVisit);
868
+ if (!typeNode || ((_c = typeNode.valueNode) == null ? void 0 : _c.type) !== "object") {
869
+ continue;
870
+ }
871
+ info.typeToNode.set(typeToVisit, { type: typeToVisit, typeNode });
872
+ typeNode.valueNode.properties.forEach((prop) => {
873
+ var _a2, _b2;
874
+ const nextPath = [currentPath, prop.keyNode.value].join(currentPath === "" ? "" : ".");
875
+ info.bindingToSchemaType.set(nextPath, {
876
+ binding: nextPath,
877
+ typeName: typeToVisit,
878
+ key: prop.keyNode.value
879
+ });
880
+ if (((_a2 = prop.valueNode) == null ? void 0 : _a2.type) === "object") {
881
+ const nestedTypeName = prop.valueNode.properties.find((c) => c.keyNode.value === "type");
882
+ if (nestedTypeName && ((_b2 = nestedTypeName.valueNode) == null ? void 0 : _b2.type) === "string") {
883
+ schemaTypeQueue.push({
884
+ currentPath: nextPath,
885
+ typeToVisit: nestedTypeName.valueNode.value,
886
+ visited
887
+ });
888
+ }
889
+ }
890
+ });
891
+ }
892
+ return info;
893
+ }
894
+ const checkTypesForBinding = (nodes) => {
895
+ for (let i = 0; i < nodes.length; i++) {
896
+ const node = nodes[i];
897
+ if (node.type === "string" && node.name === "Binding")
898
+ return true;
899
+ if (node.type === "or")
900
+ return checkTypesForBinding(node.or);
901
+ if (node.type === "and")
902
+ return checkTypesForBinding(node.and);
903
+ }
904
+ return false;
905
+ };
906
+ function isBindingPropertyAssignment(ctx) {
907
+ var _a, _b, _c;
908
+ if (ctx.node.type !== "string" || ((_a = ctx.node.parent) == null ? void 0 : _a.type) !== "property") {
909
+ return false;
910
+ }
911
+ if (checkTypesForBinding((_c = (_b = ctx.XLR) == null ? void 0 : _b.nodes) != null ? _c : [])) {
912
+ return true;
913
+ }
914
+ return false;
915
+ }
916
+ function getLocationForBindingTypeDefinition(ctx, schemaInfo) {
917
+ var _a;
918
+ if (!isBindingPropertyAssignment(ctx)) {
919
+ return;
920
+ }
921
+ const existingBindingValue = ctx.node.value;
922
+ const info = schemaInfo.bindingToSchemaType.get(existingBindingValue);
923
+ if (!info) {
924
+ return;
925
+ }
926
+ const nodeLocation = schemaInfo.typeToNode.get(info.typeName);
927
+ if (!nodeLocation || ((_a = nodeLocation.typeNode.valueNode) == null ? void 0 : _a.type) !== "object") {
928
+ return;
929
+ }
930
+ const prop = getProperty(nodeLocation.typeNode.valueNode, info.key);
931
+ if (!prop) {
932
+ return;
933
+ }
934
+ return getLSLocationOfNode(ctx.document, prop);
935
+ }
936
+ function getLocationForSchemaType(ctx, schemaInfo) {
937
+ var _a, _b, _c;
938
+ if (isValueCompletion(ctx.node)) {
939
+ if (((_a = ctx.node.parent) == null ? void 0 : _a.type) === "property" && ctx.node.type === "string" && ctx.node.parent.keyNode.value === "type") {
940
+ const typeName = ctx.node.value;
941
+ const node = schemaInfo.typeToNode.get(typeName);
942
+ if (!node) {
943
+ return;
944
+ }
945
+ const schemaPropNode = (_b = getContentNode(ctx.node)) == null ? void 0 : _b.properties.find((p) => p.keyNode.value === "schema");
946
+ if (((_c = schemaPropNode == null ? void 0 : schemaPropNode.valueNode) == null ? void 0 : _c.type) !== "object") {
947
+ return;
948
+ }
949
+ const schemaTypeNode = schemaPropNode.valueNode.properties.find((p) => p.keyNode.value === typeName);
950
+ if (schemaTypeNode !== node.typeNode) {
951
+ return;
952
+ }
953
+ return getLSLocationOfNode(ctx.document, node.typeNode);
954
+ }
955
+ }
956
+ }
957
+ class SchemaInfoPlugin {
958
+ constructor() {
959
+ this.name = "view-node";
960
+ }
961
+ apply(service) {
962
+ let schemaInfo;
963
+ service.hooks.onDocumentUpdate.tap(this.name, (ctx) => {
964
+ schemaInfo = getBindingInfo(ctx);
965
+ });
966
+ service.hooks.complete.tap(this.name, (ctx, completionCtx) => __async$7(this, null, function* () {
967
+ var _a;
968
+ if (!isBindingPropertyAssignment(ctx)) {
969
+ return;
970
+ }
971
+ const existingBindingValue = ctx.node.value;
972
+ const bindings = Array.from((_a = schemaInfo == null ? void 0 : schemaInfo.bindingToSchemaType.keys()) != null ? _a : []).filter((k) => k.startsWith(existingBindingValue));
973
+ bindings.forEach((b) => {
974
+ completionCtx.addCompletionItem({
975
+ kind: vscodeLanguageserverTypes.CompletionItemKind.Value,
976
+ label: b.substring(existingBindingValue.length)
977
+ });
978
+ });
979
+ }));
980
+ service.hooks.definition.tap(this.name, (ctx) => {
981
+ if (!schemaInfo) {
982
+ return;
983
+ }
984
+ return getLocationForSchemaType(ctx, schemaInfo) || getLocationForBindingTypeDefinition(ctx, schemaInfo);
985
+ });
986
+ }
987
+ }
988
+
989
+ var __async$6 = (__this, __arguments, generator) => {
990
+ return new Promise((resolve, reject) => {
991
+ var fulfilled = (value) => {
992
+ try {
993
+ step(generator.next(value));
994
+ } catch (e) {
995
+ reject(e);
996
+ }
997
+ };
998
+ var rejected = (value) => {
999
+ try {
1000
+ step(generator.throw(value));
1001
+ } catch (e) {
1002
+ reject(e);
1003
+ }
1004
+ };
1005
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
1006
+ step((generator = generator.apply(__this, __arguments)).next());
1007
+ });
1008
+ };
1009
+ const findErrorNode = (rootNode, nodeToFind) => {
1010
+ const children = [rootNode];
1011
+ while (children.length > 0) {
1012
+ const child = children.pop();
1013
+ if (child.jsonNode === nodeToFind) {
1014
+ return child;
1015
+ }
1016
+ if (child.children) {
1017
+ children.push(...child.children);
1018
+ }
1019
+ }
1020
+ return rootNode;
1021
+ };
1022
+ function createValidationVisitor$3(ctx, sdk) {
1023
+ const nodesWithErrors = new Set();
1024
+ return {
1025
+ AssetNode: (assetNode) => {
1026
+ var _a, _b, _c, _d;
1027
+ let expectedType = (_b = (_a = assetNode.assetType) == null ? void 0 : _a.valueNode) == null ? void 0 : _b.value;
1028
+ if (!sdk.hasType(expectedType)) {
1029
+ ctx.addViolation({
1030
+ node: assetNode,
1031
+ message: `Warning - Asset Type ${(_d = (_c = assetNode.assetType) == null ? void 0 : _c.valueNode) == null ? void 0 : _d.value} was not loaded into Validator definitions`,
1032
+ severity: vscodeLanguageserverTypes.DiagnosticSeverity.Warning
1033
+ });
1034
+ expectedType = "Asset";
1035
+ }
1036
+ const validationIssues = sdk.validateByName(expectedType, assetNode.jsonNode);
1037
+ validationIssues.forEach((issue) => {
1038
+ if (!nodesWithErrors.has(issue.node) || issue.type === "missing") {
1039
+ nodesWithErrors.add(issue.node);
1040
+ ctx.addViolation({
1041
+ node: findErrorNode(assetNode, issue.node),
1042
+ message: `Asset Validation Error - ${issue.type}: ${issue.message}`,
1043
+ severity: vscodeLanguageserverTypes.DiagnosticSeverity.Error
1044
+ });
1045
+ }
1046
+ });
1047
+ },
1048
+ ViewNode: (viewNode) => {
1049
+ var _a, _b, _c, _d;
1050
+ let expectedType = (_b = (_a = viewNode.viewType) == null ? void 0 : _a.valueNode) == null ? void 0 : _b.value;
1051
+ if (!sdk.hasType(expectedType)) {
1052
+ ctx.addViolation({
1053
+ node: viewNode,
1054
+ message: `Warning - View Type ${(_d = (_c = viewNode.viewType) == null ? void 0 : _c.valueNode) == null ? void 0 : _d.value} was not loaded into Validator definitions`,
1055
+ severity: vscodeLanguageserverTypes.DiagnosticSeverity.Warning
1056
+ });
1057
+ expectedType = "View";
1058
+ }
1059
+ const validationIssues = sdk.validateByName(expectedType, viewNode.jsonNode);
1060
+ validationIssues.forEach((issue) => {
1061
+ if (!nodesWithErrors.has(issue.node) || issue.type === "missing") {
1062
+ nodesWithErrors.add(issue.node);
1063
+ ctx.addViolation({
1064
+ node: findErrorNode(viewNode, issue.node),
1065
+ message: `View Validation Error - ${issue.type}: ${issue.message}`,
1066
+ severity: vscodeLanguageserverTypes.DiagnosticSeverity.Error
1067
+ });
1068
+ }
1069
+ });
1070
+ },
1071
+ ContentNode: (contentNode) => {
1072
+ var _a;
1073
+ const flowType = sdk.getType("Flow");
1074
+ if (!flowType) {
1075
+ throw new Error("Flow is not a registered type, can't validate content. Did you load a version of the base Player types?");
1076
+ }
1077
+ const assetType = sdk.getType("Asset");
1078
+ if (!assetType) {
1079
+ throw new Error("Asset is not a registered type, can't validate content. Did you load a version of the base Player types?");
1080
+ }
1081
+ if (flowType.type === "object" && ((_a = flowType.properties.views) == null ? void 0 : _a.node.type) === "array") {
1082
+ flowType.properties.views.node.elementType = assetType;
1083
+ }
1084
+ const validationIssues = sdk.validateByType(flowType, contentNode.jsonNode);
1085
+ validationIssues.forEach((issue) => {
1086
+ if (!nodesWithErrors.has(issue.node) || issue.type === "missing") {
1087
+ nodesWithErrors.add(issue.node);
1088
+ ctx.addViolation({
1089
+ node: findErrorNode(contentNode, issue.node),
1090
+ message: `Content Validation Error - ${issue.type}: ${issue.message}`,
1091
+ severity: vscodeLanguageserverTypes.DiagnosticSeverity.Error
1092
+ });
1093
+ }
1094
+ });
1095
+ },
1096
+ NavigationNode: (navigationNode) => {
1097
+ const expectedType = "Navigation";
1098
+ const validationIssues = sdk.validateByName(expectedType, navigationNode.jsonNode);
1099
+ validationIssues.forEach((issue) => {
1100
+ if (!nodesWithErrors.has(issue.node) || issue.type === "missing") {
1101
+ nodesWithErrors.add(issue.node);
1102
+ ctx.addViolation({
1103
+ node: findErrorNode(navigationNode, issue.node),
1104
+ message: `Navigation Validation Error - ${issue.type}: ${issue.message}`,
1105
+ severity: vscodeLanguageserverTypes.DiagnosticSeverity.Error
1106
+ });
1107
+ }
1108
+ });
1109
+ },
1110
+ FlowNode: (flowNode) => {
1111
+ const expectedType = "NavigationFlow";
1112
+ const validationIssues = sdk.validateByName(expectedType, flowNode.jsonNode);
1113
+ validationIssues.forEach((issue) => {
1114
+ if (!nodesWithErrors.has(issue.node) || issue.type === "missing") {
1115
+ nodesWithErrors.add(issue.node);
1116
+ ctx.addViolation({
1117
+ node: findErrorNode(flowNode, issue.node),
1118
+ message: `Navigation Flow Validation Error - ${issue.type}: ${issue.message}`,
1119
+ severity: vscodeLanguageserverTypes.DiagnosticSeverity.Error
1120
+ });
1121
+ }
1122
+ });
1123
+ },
1124
+ FlowStateNode: (flowStateNode) => {
1125
+ var _a, _b;
1126
+ const flowxlr = mapFlowStateToType((_b = (_a = flowStateNode.stateType) == null ? void 0 : _a.valueNode) == null ? void 0 : _b.value);
1127
+ if (flowxlr) {
1128
+ const validationIssues = sdk.validateByName(flowxlr, flowStateNode.jsonNode);
1129
+ validationIssues.forEach((issue) => {
1130
+ if (!nodesWithErrors.has(issue.node) || issue.type === "missing") {
1131
+ nodesWithErrors.add(issue.node);
1132
+ ctx.addViolation({
1133
+ node: findErrorNode(flowStateNode, issue.node),
1134
+ message: `Navigation Node Validation Error - ${issue.type}: ${issue.message}`,
1135
+ severity: vscodeLanguageserverTypes.DiagnosticSeverity.Error
1136
+ });
1137
+ }
1138
+ });
1139
+ } else {
1140
+ ctx.addViolation({
1141
+ node: flowStateNode,
1142
+ message: "Unknown Flow Type, valid options are: VIEW, END, ACTION, EXTERNAL, FLOW",
1143
+ severity: vscodeLanguageserverTypes.DiagnosticSeverity.Error
1144
+ });
1145
+ }
1146
+ }
1147
+ };
1148
+ }
1149
+ function getObjectCompletions(authoredNode, potentialTypes) {
1150
+ const completions = [];
1151
+ const presentKeys = new Set();
1152
+ if (authoredNode.properties) {
1153
+ authoredNode.properties.forEach((propertyNode) => presentKeys.add(propertyNode.keyNode.value));
1154
+ }
1155
+ potentialTypes.forEach((node) => {
1156
+ if (node.type === "object") {
1157
+ Object.keys(node.properties).forEach((prop) => {
1158
+ var _a;
1159
+ if (!presentKeys.has(prop)) {
1160
+ completions.push({
1161
+ label: prop,
1162
+ documentation: (_a = node.properties[prop].node.description) != null ? _a : node.properties[prop].node.title,
1163
+ kind: vscodeLanguageserverTypes.CompletionItemKind.Property
1164
+ });
1165
+ }
1166
+ });
1167
+ } else if (node.type === "and") {
1168
+ completions.push(...getObjectCompletions(authoredNode, node.and));
1169
+ } else if (node.type === "or") {
1170
+ completions.push(...getObjectCompletions(authoredNode, node.or));
1171
+ }
1172
+ });
1173
+ return completions;
1174
+ }
1175
+ function getPropertyCompletions(propertyName, potentialTypes) {
1176
+ const completions = [];
1177
+ potentialTypes.forEach((nodeType) => {
1178
+ var _a;
1179
+ if (nodeType.type === "object") {
1180
+ const propertyNode = (_a = nodeType.properties[propertyName]) == null ? void 0 : _a.node;
1181
+ if (propertyNode && propertyNode.type === "string" && propertyNode.const) {
1182
+ completions.push({
1183
+ label: propertyNode.const,
1184
+ kind: vscodeLanguageserverTypes.CompletionItemKind.Value
1185
+ });
1186
+ }
1187
+ }
1188
+ });
1189
+ return completions;
1190
+ }
1191
+ function complete(ctx) {
1192
+ var _a, _b, _c;
1193
+ if ((_a = ctx.XLR) == null ? void 0 : _a.nearestObjects) {
1194
+ if (ctx.node.type === "string" && ((_c = (_b = ctx.node) == null ? void 0 : _b.parent) == null ? void 0 : _c.type) === "property") {
1195
+ return getPropertyCompletions(ctx.node.parent.keyNode.value, ctx.XLR.nearestObjects);
1196
+ }
1197
+ return getObjectCompletions(ctx.node, ctx.XLR.nearestObjects);
1198
+ }
1199
+ return [];
1200
+ }
1201
+ function hover(ctx) {
1202
+ var _a;
1203
+ if (ctx.XLR && ctx.node.type === "string") {
1204
+ const docStrings = [];
1205
+ const prop = ctx.node.value;
1206
+ ctx.XLR.nearestObjects.forEach((typeNode) => {
1207
+ var _a2, _b, _c, _d, _e, _f;
1208
+ const docString = (_f = (_e = (_b = (_a2 = typeNode.properties[prop]) == null ? void 0 : _a2.node) == null ? void 0 : _b.description) != null ? _e : (_d = (_c = typeNode.properties[prop]) == null ? void 0 : _c.node) == null ? void 0 : _d.title) != null ? _f : void 0;
1209
+ if (docString) {
1210
+ docStrings.push(docString);
1211
+ }
1212
+ });
1213
+ if (docStrings.length > 1) {
1214
+ return {
1215
+ contents: {
1216
+ kind: vscodeLanguageserverTypes.MarkupKind.PlainText,
1217
+ value: "Docs unavailable - More than one type could exist at this location"
1218
+ }
1219
+ };
1220
+ }
1221
+ return {
1222
+ contents: {
1223
+ kind: vscodeLanguageserverTypes.MarkupKind.PlainText,
1224
+ value: (_a = docStrings[0]) != null ? _a : "Error getting docs"
1225
+ }
1226
+ };
1227
+ }
1228
+ }
1229
+ class XLRPlugin {
1230
+ constructor() {
1231
+ this.name = "xlr-plugin";
1232
+ }
1233
+ apply(service) {
1234
+ service.hooks.validate.tap(this.name, (ctx, validation) => __async$6(this, null, function* () {
1235
+ validation.useASTVisitor(createValidationVisitor$3(validation, service.XLRService.XLRSDK));
1236
+ }));
1237
+ service.hooks.complete.tap(this.name, (ctx, completion) => __async$6(this, null, function* () {
1238
+ complete(ctx).map((i) => completion.addCompletionItem(i));
1239
+ }));
1240
+ service.hooks.hover.tap(this.name, (ctx) => {
1241
+ return hover(ctx);
1242
+ });
1243
+ }
1244
+ }
1245
+
1246
+ var __async$5 = (__this, __arguments, generator) => {
1247
+ return new Promise((resolve, reject) => {
1248
+ var fulfilled = (value) => {
1249
+ try {
1250
+ step(generator.next(value));
1251
+ } catch (e) {
1252
+ reject(e);
1253
+ }
1254
+ };
1255
+ var rejected = (value) => {
1256
+ try {
1257
+ step(generator.throw(value));
1258
+ } catch (e) {
1259
+ reject(e);
1260
+ }
1261
+ };
1262
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
1263
+ step((generator = generator.apply(__this, __arguments)).next());
1264
+ });
1265
+ };
1266
+ const generateID = (node) => {
1267
+ var _a, _b, _c;
1268
+ if (!node || node.type === "view") {
1269
+ return "";
1270
+ }
1271
+ const prefix = generateID(node.parent);
1272
+ let current = "";
1273
+ if (node.type === "property") {
1274
+ current = node.keyNode.value;
1275
+ } else if (node.type === "asset" && ((_b = (_a = node.assetType) == null ? void 0 : _a.valueNode) == null ? void 0 : _b.value)) {
1276
+ current = (_c = node.assetType.valueNode) == null ? void 0 : _c.value;
1277
+ }
1278
+ return [prefix, current].filter(Boolean).join("-");
1279
+ };
1280
+ const createViolation = (node) => {
1281
+ var _a, _b, _c;
1282
+ const valueNode = (_a = node.id) == null ? void 0 : _a.valueNode;
1283
+ if (!valueNode) {
1284
+ return;
1285
+ }
1286
+ return {
1287
+ node: valueNode,
1288
+ severity: vscodeLanguageserverTypes.DiagnosticSeverity.Error,
1289
+ message: `The id '${(_c = (_b = node.id) == null ? void 0 : _b.valueNode) == null ? void 0 : _c.value}' is already in use in this view.`,
1290
+ fix: () => {
1291
+ return {
1292
+ name: "Generate new ID",
1293
+ edit: replaceString(valueNode, `"${generateID(node)}"`)
1294
+ };
1295
+ }
1296
+ };
1297
+ };
1298
+ const createValidationVisitor$2 = (ctx) => {
1299
+ const viewInfo = new Map();
1300
+ return {
1301
+ AssetNode: (assetNode) => {
1302
+ var _a, _b;
1303
+ const view = getViewNode(assetNode);
1304
+ if (!view) {
1305
+ throw new Error("Asset found but not within a view. Something is wrong");
1306
+ }
1307
+ const assetID = assetNode.id;
1308
+ if (!assetID || !((_a = assetID.valueNode) == null ? void 0 : _a.value)) {
1309
+ return;
1310
+ }
1311
+ const id = (_b = assetID.valueNode) == null ? void 0 : _b.value;
1312
+ if (!viewInfo.has(view)) {
1313
+ viewInfo.set(view, new Map());
1314
+ }
1315
+ const assetIDMap = viewInfo.get(view);
1316
+ const idInfo = assetIDMap == null ? void 0 : assetIDMap.get(id);
1317
+ if (idInfo) {
1318
+ if (!idInfo.handled) {
1319
+ const origViolation = createViolation(idInfo.original);
1320
+ if (origViolation) {
1321
+ ctx.addViolation(origViolation);
1322
+ }
1323
+ idInfo.handled = true;
1324
+ }
1325
+ const assetViolation = createViolation(assetNode);
1326
+ if (assetViolation) {
1327
+ ctx.addViolation(assetViolation);
1328
+ }
1329
+ } else {
1330
+ assetIDMap == null ? void 0 : assetIDMap.set(id, { original: assetNode, handled: false });
1331
+ }
1332
+ }
1333
+ };
1334
+ };
1335
+ class DuplicateIDPlugin {
1336
+ constructor() {
1337
+ this.name = "duplicate-id";
1338
+ }
1339
+ apply(service) {
1340
+ service.hooks.validate.tap(this.name, (ctx, validation) => __async$5(this, null, function* () {
1341
+ validation.useASTVisitor(createValidationVisitor$2(validation));
1342
+ }));
1343
+ }
1344
+ }
1345
+
1346
+ var __defProp$3 = Object.defineProperty;
1347
+ var __getOwnPropSymbols$3 = Object.getOwnPropertySymbols;
1348
+ var __hasOwnProp$3 = Object.prototype.hasOwnProperty;
1349
+ var __propIsEnum$3 = Object.prototype.propertyIsEnumerable;
1350
+ var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1351
+ var __spreadValues$3 = (a, b) => {
1352
+ for (var prop in b || (b = {}))
1353
+ if (__hasOwnProp$3.call(b, prop))
1354
+ __defNormalProp$3(a, prop, b[prop]);
1355
+ if (__getOwnPropSymbols$3)
1356
+ for (var prop of __getOwnPropSymbols$3(b)) {
1357
+ if (__propIsEnum$3.call(b, prop))
1358
+ __defNormalProp$3(a, prop, b[prop]);
1359
+ }
1360
+ return a;
1361
+ };
1362
+ var __async$4 = (__this, __arguments, generator) => {
1363
+ return new Promise((resolve, reject) => {
1364
+ var fulfilled = (value) => {
1365
+ try {
1366
+ step(generator.next(value));
1367
+ } catch (e) {
1368
+ reject(e);
1369
+ }
1370
+ };
1371
+ var rejected = (value) => {
1372
+ try {
1373
+ step(generator.throw(value));
1374
+ } catch (e) {
1375
+ reject(e);
1376
+ }
1377
+ };
1378
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
1379
+ step((generator = generator.apply(__this, __arguments)).next());
1380
+ });
1381
+ };
1382
+ function createRuleVisitor$1(context) {
1383
+ const checkForLegacyAction = (node) => {
1384
+ if (node.type === "asset") {
1385
+ return;
1386
+ }
1387
+ if (node.type === "object" && !node.properties.some((p) => p.keyNode.value === "asset" || p.keyNode.value === "dynamicSwitch" || p.keyNode.value === "staticSwitch")) {
1388
+ context.addViolation({
1389
+ message: "Migrate to an action-asset",
1390
+ node,
1391
+ severity: vscodeLanguageserverTypes.DiagnosticSeverity.Warning,
1392
+ fix: () => {
1393
+ const newActionAsset = {
1394
+ asset: __spreadValues$3({
1395
+ type: "action"
1396
+ }, getNodeValue(node))
1397
+ };
1398
+ return {
1399
+ name: "Convert to Asset",
1400
+ edit: {
1401
+ type: "replace",
1402
+ node,
1403
+ value: JSON.stringify(newActionAsset, null, 2)
1404
+ }
1405
+ };
1406
+ }
1407
+ });
1408
+ }
1409
+ };
1410
+ return {
1411
+ ViewNode: (viewNode) => {
1412
+ var _a;
1413
+ const actionsProp = viewNode.properties.find((p) => p.keyNode.value === "actions");
1414
+ if (!actionsProp || ((_a = actionsProp.valueNode) == null ? void 0 : _a.type) !== "array") {
1415
+ return;
1416
+ }
1417
+ actionsProp.valueNode.children.forEach((action) => {
1418
+ checkForLegacyAction(action);
1419
+ });
1420
+ }
1421
+ };
1422
+ }
1423
+ class LegacyActionPlugin {
1424
+ constructor() {
1425
+ this.name = "legacy-action";
1426
+ }
1427
+ apply(service) {
1428
+ service.hooks.validate.tap(this.name, (ctx, validationContext) => __async$4(this, null, function* () {
1429
+ validationContext.useASTVisitor(createRuleVisitor$1(validationContext));
1430
+ }));
1431
+ }
1432
+ }
1433
+
1434
+ var __defProp$2 = Object.defineProperty;
1435
+ var __defProps$2 = Object.defineProperties;
1436
+ var __getOwnPropDescs$2 = Object.getOwnPropertyDescriptors;
1437
+ var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
1438
+ var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
1439
+ var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
1440
+ var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1441
+ var __spreadValues$2 = (a, b) => {
1442
+ for (var prop in b || (b = {}))
1443
+ if (__hasOwnProp$2.call(b, prop))
1444
+ __defNormalProp$2(a, prop, b[prop]);
1445
+ if (__getOwnPropSymbols$2)
1446
+ for (var prop of __getOwnPropSymbols$2(b)) {
1447
+ if (__propIsEnum$2.call(b, prop))
1448
+ __defNormalProp$2(a, prop, b[prop]);
1449
+ }
1450
+ return a;
1451
+ };
1452
+ var __spreadProps$2 = (a, b) => __defProps$2(a, __getOwnPropDescs$2(b));
1453
+ var __async$3 = (__this, __arguments, generator) => {
1454
+ return new Promise((resolve, reject) => {
1455
+ var fulfilled = (value) => {
1456
+ try {
1457
+ step(generator.next(value));
1458
+ } catch (e) {
1459
+ reject(e);
1460
+ }
1461
+ };
1462
+ var rejected = (value) => {
1463
+ try {
1464
+ step(generator.throw(value));
1465
+ } catch (e) {
1466
+ reject(e);
1467
+ }
1468
+ };
1469
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
1470
+ step((generator = generator.apply(__this, __arguments)).next());
1471
+ });
1472
+ };
1473
+ function createRuleVisitor(context, docInfo) {
1474
+ const checkForLegacyTemplate = (node) => {
1475
+ var _a;
1476
+ const templateDataProp = node.properties.find((p) => p.keyNode.value === "templateData");
1477
+ const templateValueProp = node.properties.find((p) => p.keyNode.value === "template");
1478
+ const templateOutputProp = node.properties.find((p) => p.keyNode.value === "templateOutput");
1479
+ if (!templateDataProp && !templateOutputProp && (!templateValueProp || ((_a = templateValueProp.valueNode) == null ? void 0 : _a.type) === "array")) {
1480
+ return;
1481
+ }
1482
+ const templateViolation = {
1483
+ severity: vscodeLanguageserverTypes.DiagnosticSeverity.Error,
1484
+ message: `Migrate to the template[] syntax.`,
1485
+ fix: () => {
1486
+ var _a2, _b, _c, _d, _e, _f, _g, _h;
1487
+ const path = [
1488
+ "template",
1489
+ ((_a2 = templateValueProp == null ? void 0 : templateValueProp.valueNode) == null ? void 0 : _a2.type) === "array" ? templateValueProp.valueNode.children.length : 0
1490
+ ];
1491
+ const newTemplateObj = {
1492
+ value: ((_b = templateValueProp == null ? void 0 : templateValueProp.valueNode) == null ? void 0 : _b.type) !== "array" && (templateValueProp == null ? void 0 : templateValueProp.valueNode) ? getNodeValue(templateValueProp == null ? void 0 : templateValueProp.valueNode) : {},
1493
+ output: (_d = (_c = templateOutputProp == null ? void 0 : templateOutputProp.valueNode) == null ? void 0 : _c.jsonNode.value) != null ? _d : "",
1494
+ data: (_f = (_e = templateDataProp == null ? void 0 : templateDataProp.valueNode) == null ? void 0 : _e.jsonNode.value) != null ? _f : ""
1495
+ };
1496
+ const oldValue = getNodeValue(node);
1497
+ let newValue = timm.omit(oldValue, "templateData");
1498
+ newValue = timm.omit(newValue, "templateOutput");
1499
+ if (((_g = templateValueProp == null ? void 0 : templateValueProp.valueNode) == null ? void 0 : _g.type) !== "array") {
1500
+ newValue = timm.omit(newValue, "template");
1501
+ }
1502
+ newValue = timm.set(newValue, "template", timm.addLast((_h = newValue.template) != null ? _h : [], newTemplateObj));
1503
+ return {
1504
+ edit: {
1505
+ type: "replace",
1506
+ path,
1507
+ node,
1508
+ value: formatLikeNode(docInfo.document, node, newValue)
1509
+ },
1510
+ name: "Convert to template[]"
1511
+ };
1512
+ }
1513
+ };
1514
+ if (templateDataProp) {
1515
+ context.addViolation(__spreadProps$2(__spreadValues$2({}, templateViolation), {
1516
+ node: templateDataProp
1517
+ }));
1518
+ }
1519
+ if (templateOutputProp) {
1520
+ context.addViolation(__spreadProps$2(__spreadValues$2({}, templateViolation), {
1521
+ node: templateOutputProp
1522
+ }));
1523
+ }
1524
+ };
1525
+ return {
1526
+ ViewNode: checkForLegacyTemplate,
1527
+ AssetNode: checkForLegacyTemplate,
1528
+ ObjectNode: checkForLegacyTemplate
1529
+ };
1530
+ }
1531
+ class LegacyTemplatePlugin {
1532
+ constructor() {
1533
+ this.name = "legacy-template";
1534
+ }
1535
+ apply(service) {
1536
+ service.hooks.validate.tap(this.name, (ctx, validationContext) => __async$3(this, null, function* () {
1537
+ validationContext.useASTVisitor(createRuleVisitor(validationContext, ctx));
1538
+ }));
1539
+ }
1540
+ }
1541
+
1542
+ const getObjectTarget = (node) => {
1543
+ if (isObjectNode(node)) {
1544
+ return node;
1545
+ }
1546
+ if (isKeyNode(node) && isPropertyNode(node.parent) && isObjectNode(node.parent.valueNode)) {
1547
+ return node.parent.valueNode;
1548
+ }
1549
+ };
1550
+ class MissingAssetWrapperPlugin {
1551
+ constructor() {
1552
+ this.name = "missing-asset-wrapper";
1553
+ }
1554
+ apply(languageService) {
1555
+ languageService.hooks.onValidateEnd.tap(this.name, (diagnostics, { addFixableViolation, documentContext }) => {
1556
+ let filteredDiags = diagnostics;
1557
+ const expectedAssetDiags = diagnostics.filter((d) => d.message.includes("Does not match any of the expected types for type: 'AssetWrapperOrSwitch'") || d.message.startsWith("Expected property: asset"));
1558
+ expectedAssetDiags.forEach((d) => {
1559
+ var _a;
1560
+ const originalNode = documentContext.PlayerContent.getNodeFromOffset(documentContext.document.offsetAt(d.range.start));
1561
+ const objectNode = getObjectTarget(originalNode);
1562
+ if (objectNode && originalNode) {
1563
+ const associatedDiags = filteredDiags.filter((nestedDiag) => {
1564
+ const diagNode = documentContext.PlayerContent.getNodeFromOffset(documentContext.document.offsetAt(nestedDiag.range.start));
1565
+ return objectNode.properties.some((p) => p.keyNode === diagNode);
1566
+ });
1567
+ addFixableViolation(d, {
1568
+ node: originalNode,
1569
+ message: d.message,
1570
+ severity: (_a = d.severity) != null ? _a : vscodeLanguageserverTypes.DiagnosticSeverity.Error,
1571
+ fix: () => ({
1572
+ name: `Wrap in "asset"`,
1573
+ edit: {
1574
+ type: "replace",
1575
+ node: objectNode,
1576
+ value: formatLikeNode(documentContext.document, objectNode, {
1577
+ asset: getNodeValue(objectNode)
1578
+ })
1579
+ }
1580
+ })
1581
+ });
1582
+ filteredDiags = filteredDiags.filter((filteredD) => !associatedDiags.includes(filteredD));
1583
+ }
1584
+ });
1585
+ return filteredDiags;
1586
+ });
1587
+ }
1588
+ }
1589
+
1590
+ var __async$2 = (__this, __arguments, generator) => {
1591
+ return new Promise((resolve, reject) => {
1592
+ var fulfilled = (value) => {
1593
+ try {
1594
+ step(generator.next(value));
1595
+ } catch (e) {
1596
+ reject(e);
1597
+ }
1598
+ };
1599
+ var rejected = (value) => {
1600
+ try {
1601
+ step(generator.throw(value));
1602
+ } catch (e) {
1603
+ reject(e);
1604
+ }
1605
+ };
1606
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
1607
+ step((generator = generator.apply(__this, __arguments)).next());
1608
+ });
1609
+ };
1610
+ const createValidationVisitor$1 = (ctx) => {
1611
+ const validTransitions = new Map();
1612
+ return {
1613
+ FlowNode: (flowNode) => {
1614
+ var _a;
1615
+ const flowNodeId = (_a = flowNode.parent) == null ? void 0 : _a.keyNode.value;
1616
+ flowNode.states.forEach((p) => {
1617
+ var _a2;
1618
+ if (!validTransitions.has(flowNodeId)) {
1619
+ validTransitions.set(flowNodeId, new Set());
1620
+ }
1621
+ (_a2 = validTransitions.get(flowNodeId)) == null ? void 0 : _a2.add(p.keyNode.value);
1622
+ });
1623
+ },
1624
+ FlowStateNode: (flowState) => {
1625
+ var _a, _b, _c;
1626
+ const transitions = flowState.properties.find((p) => p.keyNode.value === "transitions");
1627
+ let flowNode = flowState.parent;
1628
+ while (flowNode && !isFlowNode(flowNode)) {
1629
+ flowNode = flowNode == null ? void 0 : flowNode.parent;
1630
+ }
1631
+ const flowNodeId = (_a = flowNode == null ? void 0 : flowNode.parent) == null ? void 0 : _a.keyNode.value;
1632
+ (_c = (_b = transitions == null ? void 0 : transitions.valueNode) == null ? void 0 : _b.children) == null ? void 0 : _c.forEach((transitionObjects) => {
1633
+ var _a2, _b2;
1634
+ if (transitionObjects.type === "property" && ((_a2 = transitionObjects.valueNode) == null ? void 0 : _a2.type) === "string") {
1635
+ if (!((_b2 = validTransitions.get(flowNodeId)) == null ? void 0 : _b2.has(transitionObjects.valueNode.value))) {
1636
+ ctx.addViolation({
1637
+ node: transitionObjects.valueNode,
1638
+ severity: vscodeLanguageserverTypes.DiagnosticSeverity.Error,
1639
+ message: `Node '${transitionObjects.valueNode.value}' not found`
1640
+ });
1641
+ }
1642
+ }
1643
+ });
1644
+ }
1645
+ };
1646
+ };
1647
+ const isTransitionValue = (node) => {
1648
+ var _a;
1649
+ return node.type === "string" && isPropertyNode(node.parent) && ((_a = node.parent.parent) == null ? void 0 : _a.type) === "object" && isPropertyNode(node.parent.parent.parent) && node.parent.parent.parent.keyNode.value === "transitions" && isStateNode(node.parent.parent.parent.parent) && isPropertyNode(node.parent.parent.parent.parent.parent) && isFlowNode(node.parent.parent.parent.parent.parent.parent);
1650
+ };
1651
+ const getFlowNode = (node) => {
1652
+ if (isFlowNode(node)) {
1653
+ return node;
1654
+ }
1655
+ if (node.parent) {
1656
+ return getFlowNode(node.parent);
1657
+ }
1658
+ };
1659
+ class NavStatePlugin {
1660
+ constructor() {
1661
+ this.name = "nav-state";
1662
+ }
1663
+ apply(service) {
1664
+ service.hooks.validate.tap(this.name, (ctx, validation) => __async$2(this, null, function* () {
1665
+ validation.useASTVisitor(createValidationVisitor$1(validation));
1666
+ }));
1667
+ service.hooks.complete.tap(this.name, (ctx, completionCtx) => __async$2(this, null, function* () {
1668
+ if (!isValueCompletion(ctx.node)) {
1669
+ return;
1670
+ }
1671
+ if (isTransitionValue(ctx.node)) {
1672
+ const flowNode = getFlowNode(ctx.node);
1673
+ flowNode == null ? void 0 : flowNode.states.forEach((p) => {
1674
+ completionCtx.addCompletionItem({
1675
+ kind: vscodeLanguageserverTypes.CompletionItemKind.Value,
1676
+ label: p.keyNode.value
1677
+ });
1678
+ });
1679
+ }
1680
+ }));
1681
+ service.hooks.definition.tap(this.name, (ctx) => {
1682
+ if (!isValueCompletion(ctx.node)) {
1683
+ return;
1684
+ }
1685
+ if (ctx.node.type === "string" && isTransitionValue(ctx.node)) {
1686
+ const flowNode = getFlowNode(ctx.node);
1687
+ const { value } = ctx.node;
1688
+ const flowProp = flowNode == null ? void 0 : flowNode.states.find((n) => n.keyNode.value === value);
1689
+ if (flowProp) {
1690
+ return getLSLocationOfNode(ctx.document, flowProp.keyNode);
1691
+ }
1692
+ }
1693
+ });
1694
+ }
1695
+ }
1696
+
1697
+ var __async$1 = (__this, __arguments, generator) => {
1698
+ return new Promise((resolve, reject) => {
1699
+ var fulfilled = (value) => {
1700
+ try {
1701
+ step(generator.next(value));
1702
+ } catch (e) {
1703
+ reject(e);
1704
+ }
1705
+ };
1706
+ var rejected = (value) => {
1707
+ try {
1708
+ step(generator.throw(value));
1709
+ } catch (e) {
1710
+ reject(e);
1711
+ }
1712
+ };
1713
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
1714
+ step((generator = generator.apply(__this, __arguments)).next());
1715
+ });
1716
+ };
1717
+ const createValidationVisitor = (ctx, viewInfo) => {
1718
+ return {
1719
+ FlowStateNode: (flowState) => {
1720
+ var _a, _b, _c;
1721
+ if (((_b = (_a = flowState.stateType) == null ? void 0 : _a.valueNode) == null ? void 0 : _b.value) === "VIEW") {
1722
+ const refNode = getProperty(flowState, "ref");
1723
+ if (!refNode || ((_c = refNode.valueNode) == null ? void 0 : _c.type) !== "string") {
1724
+ return;
1725
+ }
1726
+ const refID = refNode.valueNode.value;
1727
+ if (viewInfo.views.has(refID)) {
1728
+ return;
1729
+ }
1730
+ ctx.addViolation({
1731
+ node: refNode.valueNode,
1732
+ message: `View with id: ${refID} does not exist.`,
1733
+ severity: vscodeLanguageserverTypes.DiagnosticSeverity.Error
1734
+ });
1735
+ }
1736
+ },
1737
+ ViewNode: (viewNode) => {
1738
+ var _a, _b;
1739
+ if (viewNode.id && ((_a = viewNode.id.valueNode) == null ? void 0 : _a.value) !== void 0) {
1740
+ if (!viewInfo.nodes.get((_b = viewNode.id.valueNode) == null ? void 0 : _b.value)) {
1741
+ ctx.addViolation({
1742
+ node: viewNode.id.valueNode,
1743
+ message: `View is not reachable`,
1744
+ severity: vscodeLanguageserverTypes.DiagnosticSeverity.Warning
1745
+ });
1746
+ }
1747
+ }
1748
+ }
1749
+ };
1750
+ };
1751
+ const getViewInfo = (ctx) => {
1752
+ var _a, _b, _c, _d;
1753
+ const views = new Map();
1754
+ const nodes = new Map();
1755
+ const { root } = ctx.PlayerContent;
1756
+ if (root.type === "content") {
1757
+ (_b = (_a = root.views) == null ? void 0 : _a.valueNode) == null ? void 0 : _b.children.forEach((c) => {
1758
+ var _a2;
1759
+ if (isViewNode(c) && ((_a2 = c.id) == null ? void 0 : _a2.valueNode)) {
1760
+ views.set(c.id.valueNode.value, {
1761
+ id: c.id.valueNode.value,
1762
+ idProp: c.id
1763
+ });
1764
+ }
1765
+ });
1766
+ (_d = (_c = root.navigation) == null ? void 0 : _c.valueNode) == null ? void 0 : _d.flows.forEach((flow) => {
1767
+ var _a2, _b2;
1768
+ (_b2 = (_a2 = flow.valueNode) == null ? void 0 : _a2.states) == null ? void 0 : _b2.forEach((state) => {
1769
+ var _a3, _b3, _c2, _d2;
1770
+ if (((_c2 = (_b3 = (_a3 = state.valueNode) == null ? void 0 : _a3.stateType) == null ? void 0 : _b3.valueNode) == null ? void 0 : _c2.value) === "VIEW") {
1771
+ const ref = state.valueNode.properties.find((p) => p.keyNode.value === "ref");
1772
+ if (((_d2 = ref == null ? void 0 : ref.valueNode) == null ? void 0 : _d2.type) === "string") {
1773
+ const refVal = ref.valueNode.value;
1774
+ nodes.set(refVal, {
1775
+ id: refVal,
1776
+ refProp: ref
1777
+ });
1778
+ }
1779
+ }
1780
+ });
1781
+ });
1782
+ }
1783
+ return {
1784
+ views,
1785
+ nodes
1786
+ };
1787
+ };
1788
+ class ViewNodePlugin {
1789
+ constructor() {
1790
+ this.name = "view-node";
1791
+ }
1792
+ apply(service) {
1793
+ let viewInfo;
1794
+ service.hooks.validate.tap(this.name, (ctx, validation) => __async$1(this, null, function* () {
1795
+ if (!viewInfo) {
1796
+ return;
1797
+ }
1798
+ validation.useASTVisitor(createValidationVisitor(validation, viewInfo));
1799
+ }));
1800
+ service.hooks.onDocumentUpdate.tap(this.name, (ctx) => {
1801
+ viewInfo = getViewInfo(ctx);
1802
+ });
1803
+ service.hooks.complete.tap(this.name, (ctx, completionCtx) => __async$1(this, null, function* () {
1804
+ var _a, _b;
1805
+ if (!isValueCompletion(ctx.node)) {
1806
+ return;
1807
+ }
1808
+ if (ctx.node.type === "string" && isPropertyNode(ctx.node.parent) && isStateNode(ctx.node.parent.parent) && ctx.node.parent.keyNode.value === "ref") {
1809
+ Array.from((_a = viewInfo == null ? void 0 : viewInfo.views.keys()) != null ? _a : []).forEach((vID) => {
1810
+ completionCtx.addCompletionItem({
1811
+ kind: vscodeLanguageserverTypes.CompletionItemKind.Value,
1812
+ label: vID
1813
+ });
1814
+ });
1815
+ } else if (ctx.node.type === "string" && isPropertyNode(ctx.node.parent) && isViewNode(ctx.node.parent.parent) && ctx.node.parent.keyNode.value === "id") {
1816
+ Array.from((_b = viewInfo == null ? void 0 : viewInfo.nodes.keys()) != null ? _b : []).forEach((vID) => {
1817
+ completionCtx.addCompletionItem({
1818
+ kind: vscodeLanguageserverTypes.CompletionItemKind.Value,
1819
+ label: vID
1820
+ });
1821
+ });
1822
+ }
1823
+ }));
1824
+ service.hooks.definition.tap(this.name, (ctx) => {
1825
+ if (!isValueCompletion(ctx.node)) {
1826
+ return;
1827
+ }
1828
+ if (ctx.node.type === "string" && isPropertyNode(ctx.node.parent)) {
1829
+ if (isViewNode(ctx.node.parent.parent) && ctx.node.parent.keyNode.value === "id") {
1830
+ const { value } = ctx.node;
1831
+ const stateNode = viewInfo == null ? void 0 : viewInfo.nodes.get(value);
1832
+ if (stateNode) {
1833
+ return getLSLocationOfNode(ctx.document, stateNode.refProp);
1834
+ }
1835
+ } else if (isStateNode(ctx.node.parent.parent) && ctx.node.parent.keyNode.value === "ref") {
1836
+ const { value } = ctx.node;
1837
+ const viewNode = viewInfo == null ? void 0 : viewInfo.views.get(value);
1838
+ if (viewNode) {
1839
+ return getLSLocationOfNode(ctx.document, viewNode.idProp);
1840
+ }
1841
+ }
1842
+ }
1843
+ });
1844
+ }
1845
+ }
1846
+
1847
+ var __defProp$1 = Object.defineProperty;
1848
+ var __defProps$1 = Object.defineProperties;
1849
+ var __getOwnPropDescs$1 = Object.getOwnPropertyDescriptors;
1850
+ var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
1851
+ var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
1852
+ var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
1853
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1854
+ var __spreadValues$1 = (a, b) => {
1855
+ for (var prop in b || (b = {}))
1856
+ if (__hasOwnProp$1.call(b, prop))
1857
+ __defNormalProp$1(a, prop, b[prop]);
1858
+ if (__getOwnPropSymbols$1)
1859
+ for (var prop of __getOwnPropSymbols$1(b)) {
1860
+ if (__propIsEnum$1.call(b, prop))
1861
+ __defNormalProp$1(a, prop, b[prop]);
1862
+ }
1863
+ return a;
1864
+ };
1865
+ var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b));
1866
+ const applyCommonProps = (inputNode, capability) => {
1867
+ const outputNode = __spreadValues$1({}, inputNode);
1868
+ if (capability === "Assets") {
1869
+ if (outputNode.type === "object") {
1870
+ if (!outputNode.properties.applicability) {
1871
+ outputNode.properties.applicability = {
1872
+ required: false,
1873
+ node: {
1874
+ type: "or",
1875
+ name: "Applicability",
1876
+ description: "Evaluate the given expression (or boolean) and if falsy, remove this node from the tree. This is re-computed for each change in the data-model",
1877
+ or: [
1878
+ {
1879
+ type: "boolean"
1880
+ },
1881
+ {
1882
+ type: "ref",
1883
+ ref: "Expression"
1884
+ }
1885
+ ]
1886
+ }
1887
+ };
1888
+ }
1889
+ }
1890
+ }
1891
+ if (outputNode.type === "object" && !outputNode.properties._comment) {
1892
+ outputNode.properties._comment = {
1893
+ required: false,
1894
+ node: {
1895
+ description: "Adds a comment for the given node",
1896
+ type: "string"
1897
+ }
1898
+ };
1899
+ } else if (outputNode.type === "and") {
1900
+ outputNode.and.map((n) => applyCommonProps(n, capability));
1901
+ } else if (outputNode.type === "or") {
1902
+ outputNode.or.map((n) => applyCommonProps(n, capability));
1903
+ }
1904
+ return outputNode;
1905
+ };
1906
+ const applyAssetWrapperOrSwitch = (node, capability) => {
1907
+ return xlrSdk.simpleTransformGenerator("ref", "Assets", (xlrNode) => {
1908
+ if (xlrNode.ref.includes("AssetWrapper")) {
1909
+ return __spreadProps$1(__spreadValues$1({}, xlrNode), {
1910
+ ref: xlrNode.ref.replace("AssetWrapper", "AssetWrapperOrSwitch")
1911
+ });
1912
+ }
1913
+ return xlrNode;
1914
+ })(node, capability);
1915
+ };
1916
+ const applyValueRefs = (node, capability) => {
1917
+ return xlrSdk.simpleTransformGenerator("object", "Assets", (inputNode) => {
1918
+ const xlrNode = __spreadValues$1({}, inputNode);
1919
+ for (const key in xlrNode.properties) {
1920
+ if (key === "id" || key === "type") {
1921
+ continue;
1922
+ }
1923
+ const value = xlrNode.properties[key];
1924
+ if (value.node.type === "or") {
1925
+ value.node.or.push({
1926
+ type: "ref",
1927
+ ref: "ExpressionRef"
1928
+ });
1929
+ value.node.or.push({
1930
+ type: "ref",
1931
+ ref: "BindingRef"
1932
+ });
1933
+ } else if (xlrUtils.isPrimitiveTypeNode(value.node)) {
1934
+ const newUnionType = {
1935
+ type: "or",
1936
+ description: value.node.description,
1937
+ or: [
1938
+ value.node,
1939
+ {
1940
+ type: "ref",
1941
+ ref: "ExpressionRef"
1942
+ },
1943
+ {
1944
+ type: "ref",
1945
+ ref: "BindingRef"
1946
+ }
1947
+ ]
1948
+ };
1949
+ value.node = newUnionType;
1950
+ }
1951
+ }
1952
+ return xlrNode;
1953
+ })(node, capability);
1954
+ };
1955
+ const applyTemplateProperty = (node, capability) => {
1956
+ const templateTypes = [];
1957
+ return xlrSdk.simpleTransformGenerator("object", "Assets", (inputNode) => {
1958
+ const xlrNode = __spreadValues$1({}, inputNode);
1959
+ for (const key in xlrNode.properties) {
1960
+ const value = xlrNode.properties[key];
1961
+ if (value.node.type === "array") {
1962
+ value.required = false;
1963
+ templateTypes.push({
1964
+ type: "ref",
1965
+ ref: `Template<${value.node.elementType.type === "ref" ? value.node.elementType.ref : value.node.elementType.name}, "${key}">`
1966
+ });
1967
+ }
1968
+ }
1969
+ if (templateTypes.length > 0) {
1970
+ const templateType = {
1971
+ type: "array",
1972
+ elementType: templateTypes.length > 1 ? { type: "or", or: templateTypes } : templateTypes[0],
1973
+ description: "A list of templates to process for this node"
1974
+ };
1975
+ xlrNode.properties.template = {
1976
+ required: false,
1977
+ node: templateType
1978
+ };
1979
+ }
1980
+ return xlrNode;
1981
+ })(node, capability);
1982
+ };
1983
+
1984
+ const PLUGINS = [
1985
+ new DuplicateIDPlugin(),
1986
+ new ViewNodePlugin(),
1987
+ new SchemaInfoPlugin(),
1988
+ new LegacyTemplatePlugin(),
1989
+ new LegacyActionPlugin(),
1990
+ new AssetWrapperArrayPlugin(),
1991
+ new NavStatePlugin(),
1992
+ new MissingAssetWrapperPlugin(),
1993
+ new XLRPlugin()
1994
+ ];
1995
+ const DEFAULT_FILTERS = {
1996
+ typeFilter: "Transformed"
1997
+ };
1998
+ const TRANSFORM_FUNCTIONS = [
1999
+ applyAssetWrapperOrSwitch,
2000
+ applyValueRefs,
2001
+ applyCommonProps,
2002
+ applyTemplateProperty
2003
+ ];
2004
+
2005
+ class PlayerXLRRegistry extends xlrSdk.BasicXLRRegistry {
2006
+ constructor() {
2007
+ super();
2008
+ this.registrationMap = new Map();
2009
+ }
2010
+ get(id) {
2011
+ const realNames = this.registrationMap.get(id);
2012
+ if (realNames === void 0) {
2013
+ return void 0;
2014
+ }
2015
+ if (Array.isArray(realNames)) {
2016
+ return {
2017
+ name: `${id}PartialMatchType`,
2018
+ source: "registry.ts",
2019
+ type: "or",
2020
+ or: realNames.map((partialMatchID) => super.get(partialMatchID))
2021
+ };
2022
+ }
2023
+ return super.get(realNames);
2024
+ }
2025
+ add(type, plugin, capability) {
2026
+ var _a, _b, _c, _d, _e, _f;
2027
+ let registeredName = type.name;
2028
+ if ((capability === "Assets" || capability === "Views") && type.type === "object" && ((_c = (_b = (_a = type.extends) == null ? void 0 : _a.genericArguments) == null ? void 0 : _b[0]) == null ? void 0 : _c.type) === "string" && ((_f = (_e = (_d = type.extends) == null ? void 0 : _d.genericArguments) == null ? void 0 : _e[0]) == null ? void 0 : _f.const)) {
2029
+ this.registrationMap.set(registeredName, type.name);
2030
+ registeredName = type.extends.genericArguments[0].const;
2031
+ }
2032
+ if (this.registrationMap.has(registeredName)) {
2033
+ const current = this.registrationMap.get(registeredName);
2034
+ if (Array.isArray(current)) {
2035
+ current.push(registeredName);
2036
+ } else {
2037
+ this.registrationMap.set(registeredName, [current, type.name]);
2038
+ }
2039
+ } else {
2040
+ this.registrationMap.set(registeredName, type.name);
2041
+ }
2042
+ super.add(type, plugin, capability);
2043
+ }
2044
+ has(id) {
2045
+ return this.registrationMap.has(id);
2046
+ }
2047
+ list(filterArgs) {
2048
+ return super.list(filterArgs);
2049
+ }
2050
+ info(id) {
2051
+ const realNames = this.registrationMap.get(id);
2052
+ if (realNames === void 0) {
2053
+ return void 0;
2054
+ }
2055
+ if (Array.isArray(realNames)) {
2056
+ const metaDataArrays = realNames.map((name) => super.info(name));
2057
+ const firstElement = metaDataArrays[0];
2058
+ const equal = metaDataArrays.every((metaData) => (metaData == null ? void 0 : metaData.plugin) === (firstElement == null ? void 0 : firstElement.plugin) && (metaData == null ? void 0 : metaData.capability) === (firstElement == null ? void 0 : firstElement.capability));
2059
+ if (equal) {
2060
+ return firstElement;
2061
+ }
2062
+ throw Error(`Error: can't determine accurate info for type ${id} as it is provided by multiple plugins/capabilities`);
2063
+ }
2064
+ return super.info(realNames);
2065
+ }
2066
+ }
2067
+
2068
+ class XLRService {
2069
+ constructor() {
2070
+ this.baseTypes = [
2071
+ "asset",
2072
+ "view",
2073
+ "flow",
2074
+ "content",
2075
+ "navigation",
2076
+ "state"
2077
+ ];
2078
+ this.XLRSDK = new xlrSdk.XLRSDK(new PlayerXLRRegistry());
2079
+ }
2080
+ walker(n, path) {
2081
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
2082
+ if (this.baseTypes.indexOf(n.type) > -1) {
2083
+ if (n.type === "asset") {
2084
+ const name = this.XLRSDK.hasType((_c = (_b = (_a = n.assetType) == null ? void 0 : _a.valueNode) == null ? void 0 : _b.value) != null ? _c : "") ? (_e = (_d = n.assetType) == null ? void 0 : _d.valueNode) == null ? void 0 : _e.value : "Asset";
2085
+ return {
2086
+ name,
2087
+ path
2088
+ };
2089
+ }
2090
+ if (n.type === "view") {
2091
+ const name = this.XLRSDK.hasType((_h = (_g = (_f = n.viewType) == null ? void 0 : _f.valueNode) == null ? void 0 : _g.value) != null ? _h : "") ? (_j = (_i = n.viewType) == null ? void 0 : _i.valueNode) == null ? void 0 : _j.value : "View";
2092
+ return { name, path };
2093
+ }
2094
+ if (n.type === "state") {
2095
+ if ((_l = (_k = n.stateType) == null ? void 0 : _k.valueNode) == null ? void 0 : _l.value) {
2096
+ const flowStateType = mapFlowStateToType((_n = (_m = n.stateType) == null ? void 0 : _m.valueNode) == null ? void 0 : _n.value);
2097
+ if (flowStateType) {
2098
+ return { name: flowStateType, path };
2099
+ }
2100
+ }
2101
+ }
2102
+ if (n.type === "content") {
2103
+ return { name: "Flow", path };
2104
+ }
2105
+ if (n.type === "flow") {
2106
+ return { name: "NavigationFlow", path };
2107
+ }
2108
+ if (n.type === "navigation") {
2109
+ return { name: "Navigation", path };
2110
+ }
2111
+ }
2112
+ if (n.parent) {
2113
+ path.push(n.parent);
2114
+ return this.walker(n.parent, path);
2115
+ }
2116
+ return void 0;
2117
+ }
2118
+ getTypeInfoAtPosition(node) {
2119
+ var _a;
2120
+ if (!node)
2121
+ return;
2122
+ const pointer = node;
2123
+ const xlrInfo = this.walker(pointer, []);
2124
+ if (!xlrInfo)
2125
+ return;
2126
+ const activeNode = this.XLRSDK.getType(xlrInfo.name);
2127
+ if (!activeNode)
2128
+ return;
2129
+ let nearestObjectTypes = [activeNode];
2130
+ let pointers = [];
2131
+ if (activeNode.type === "and") {
2132
+ pointers = activeNode.and;
2133
+ } else if (activeNode.type === "or") {
2134
+ pointers = activeNode.or;
2135
+ } else {
2136
+ pointers = [activeNode];
2137
+ }
2138
+ for (const pathSegment of xlrInfo.path.reverse()) {
2139
+ const newPointers = [];
2140
+ for (let nodePointer of pointers) {
2141
+ let newNode;
2142
+ if (nodePointer.type === "ref") {
2143
+ if (this.XLRSDK.hasType(nodePointer.ref)) {
2144
+ nodePointer = this.XLRSDK.getType(nodePointer.ref);
2145
+ } else {
2146
+ continue;
2147
+ }
2148
+ }
2149
+ if (pathSegment.type === "property" && nodePointer.type === "object") {
2150
+ if (nodePointer == null ? void 0 : nodePointer.properties[pathSegment.keyNode.value]) {
2151
+ newNode = (_a = nodePointer == null ? void 0 : nodePointer.properties[pathSegment.keyNode.value]) == null ? void 0 : _a.node;
2152
+ } else if (nodePointer == null ? void 0 : nodePointer.additionalProperties) {
2153
+ const adNode = nodePointer == null ? void 0 : nodePointer.additionalProperties;
2154
+ if (typeof adNode !== "boolean") {
2155
+ newNode = adNode;
2156
+ }
2157
+ }
2158
+ } else if (pathSegment.type === "object" || this.baseTypes.indexOf(pathSegment.type) !== -1) {
2159
+ newNode = nodePointer;
2160
+ } else if (pathSegment.type === "array") {
2161
+ newNode = nodePointer.elementType;
2162
+ }
2163
+ if (!newNode) {
2164
+ continue;
2165
+ } else if (newNode.type === "or") {
2166
+ newPointers.push(...newNode.or);
2167
+ } else if (newNode.type === "and") {
2168
+ newPointers.push(...newNode.and);
2169
+ } else if (newNode.type === "ref" && this.XLRSDK.hasType(newNode.ref)) {
2170
+ newPointers.push(this.XLRSDK.getType(newNode.ref));
2171
+ } else {
2172
+ newPointers.push(newNode);
2173
+ }
2174
+ }
2175
+ if (newPointers.filter((n) => n).length === 0) {
2176
+ break;
2177
+ }
2178
+ const newObjectTypes = newPointers.filter((n) => n.type === "object");
2179
+ if (newObjectTypes.length > 0) {
2180
+ nearestObjectTypes = newObjectTypes;
2181
+ }
2182
+ pointers = newPointers;
2183
+ }
2184
+ return {
2185
+ name: xlrInfo.name,
2186
+ nodes: pointers,
2187
+ nearestObjects: nearestObjectTypes
2188
+ };
2189
+ }
2190
+ }
2191
+
2192
+ const LOG_TYPES = ["debug", "info", "warn", "error"];
2193
+
2194
+ var __defProp = Object.defineProperty;
2195
+ var __defProps = Object.defineProperties;
2196
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
2197
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
2198
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
2199
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
2200
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
2201
+ var __spreadValues = (a, b) => {
2202
+ for (var prop in b || (b = {}))
2203
+ if (__hasOwnProp.call(b, prop))
2204
+ __defNormalProp(a, prop, b[prop]);
2205
+ if (__getOwnPropSymbols)
2206
+ for (var prop of __getOwnPropSymbols(b)) {
2207
+ if (__propIsEnum.call(b, prop))
2208
+ __defNormalProp(a, prop, b[prop]);
2209
+ }
2210
+ return a;
2211
+ };
2212
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
2213
+ var __async = (__this, __arguments, generator) => {
2214
+ return new Promise((resolve, reject) => {
2215
+ var fulfilled = (value) => {
2216
+ try {
2217
+ step(generator.next(value));
2218
+ } catch (e) {
2219
+ reject(e);
2220
+ }
2221
+ };
2222
+ var rejected = (value) => {
2223
+ try {
2224
+ step(generator.throw(value));
2225
+ } catch (e) {
2226
+ reject(e);
2227
+ }
2228
+ };
2229
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
2230
+ step((generator = generator.apply(__this, __arguments)).next());
2231
+ });
2232
+ };
2233
+ class PlayerLanguageService {
2234
+ constructor(config) {
2235
+ this.parseCache = new Map();
2236
+ this.fixableViolationsForDocument = new Map();
2237
+ this.hooks = {
2238
+ onDocumentUpdate: new tapableTs.SyncHook(),
2239
+ validate: new tapableTs.AsyncParallelHook(),
2240
+ onValidateEnd: new tapableTs.SyncWaterfallHook(),
2241
+ complete: new tapableTs.AsyncParallelHook(),
2242
+ hover: new tapableTs.SyncBailHook(),
2243
+ definition: new tapableTs.SyncBailHook()
2244
+ };
2245
+ var _a;
2246
+ this.XLRService = new XLRService();
2247
+ PLUGINS.forEach((p) => p.apply(this));
2248
+ (_a = config == null ? void 0 : config.plugins) == null ? void 0 : _a.forEach((p) => p.apply(this));
2249
+ }
2250
+ parseTextDocument(document) {
2251
+ if (!this.parseCache.has(document.uri)) {
2252
+ const parsed = parse(document);
2253
+ this.parseCache.set(document.uri, { version: document.version, parsed });
2254
+ return parsed;
2255
+ }
2256
+ const cached = this.parseCache.get(document.uri);
2257
+ if (!cached || cached.version < document.version) {
2258
+ this.parseCache.delete(document.uri);
2259
+ return this.parseTextDocument(document);
2260
+ }
2261
+ return cached.parsed;
2262
+ }
2263
+ updateSource(document) {
2264
+ return __async(this, null, function* () {
2265
+ const parsed = this.parseTextDocument(document);
2266
+ const documentContext = {
2267
+ log: {
2268
+ debug: console.log,
2269
+ info: console.log,
2270
+ warn: console.warn,
2271
+ error: console.error
2272
+ },
2273
+ document,
2274
+ PlayerContent: parsed
2275
+ };
2276
+ const ctx = __spreadValues({}, documentContext);
2277
+ this.hooks.onDocumentUpdate.call(ctx);
2278
+ return ctx;
2279
+ });
2280
+ }
2281
+ getJSONPositionInfo(ctx, position) {
2282
+ return __async(this, null, function* () {
2283
+ const { document, PlayerContent } = ctx;
2284
+ const node = PlayerContent.getNodeFromOffset(document.offsetAt(position));
2285
+ return {
2286
+ node
2287
+ };
2288
+ });
2289
+ }
2290
+ updateSourceWithPosition(document, position) {
2291
+ return __async(this, null, function* () {
2292
+ const ctx = yield this.updateSource(document);
2293
+ const { node } = yield this.getJSONPositionInfo(ctx, position);
2294
+ const XLRInfo = this.XLRService.getTypeInfoAtPosition(node);
2295
+ if (!node || !XLRInfo) {
2296
+ return void 0;
2297
+ }
2298
+ return __spreadProps(__spreadValues({}, ctx), {
2299
+ node,
2300
+ position,
2301
+ XLR: XLRInfo
2302
+ });
2303
+ });
2304
+ }
2305
+ onClose(document) {
2306
+ this.fixableViolationsForDocument.delete(document.uri);
2307
+ this.parseCache.delete(document.uri);
2308
+ }
2309
+ formatTextDocument(document, options, range) {
2310
+ return __async(this, null, function* () {
2311
+ const formattingOptions = {
2312
+ tabSize: options.tabSize,
2313
+ insertSpaces: options.insertSpaces
2314
+ };
2315
+ let formatRange;
2316
+ if (range) {
2317
+ const startOffset = document.offsetAt(range.start);
2318
+ formatRange = {
2319
+ offset: startOffset,
2320
+ length: document.offsetAt(range.end) - startOffset
2321
+ };
2322
+ }
2323
+ return jsoncParser.format(document.getText(), formatRange, formattingOptions).map((edit) => {
2324
+ return vscodeLanguageserverTypes.TextEdit.replace(vscodeLanguageserverTypes.Range.create(document.positionAt(edit.offset), document.positionAt(edit.offset + edit.length)), edit.content);
2325
+ });
2326
+ });
2327
+ }
2328
+ validateTextDocument(document) {
2329
+ return __async(this, null, function* () {
2330
+ const ctx = yield this.updateSource(document);
2331
+ this.fixableViolationsForDocument.delete(document.uri);
2332
+ if (!isKnownRootType(ctx.PlayerContent)) {
2333
+ return;
2334
+ }
2335
+ const diagnostics = [...ctx.PlayerContent.syntaxErrors];
2336
+ const astVisitors = [];
2337
+ const addFixableViolation = (diagnostic, violation) => {
2338
+ if (!this.fixableViolationsForDocument.has(document.uri)) {
2339
+ this.fixableViolationsForDocument.set(document.uri, new Map());
2340
+ }
2341
+ const fixableDiags = this.fixableViolationsForDocument.get(document.uri);
2342
+ fixableDiags == null ? void 0 : fixableDiags.set(diagnostic, violation);
2343
+ };
2344
+ if (ctx.PlayerContent.root) {
2345
+ const validationContext = {
2346
+ addViolation: (violation) => {
2347
+ const { message, node, severity, fix } = violation;
2348
+ const range = toRange(document, node);
2349
+ const diagnostic = {
2350
+ message,
2351
+ severity,
2352
+ range
2353
+ };
2354
+ if (fix) {
2355
+ addFixableViolation(diagnostic, violation);
2356
+ }
2357
+ diagnostics.push(diagnostic);
2358
+ },
2359
+ useASTVisitor: (visitor) => {
2360
+ astVisitors.push(visitor);
2361
+ },
2362
+ addDiagnostic(d) {
2363
+ diagnostics.push(d);
2364
+ }
2365
+ };
2366
+ yield this.hooks.validate.call(ctx, validationContext);
2367
+ yield walk(ctx.PlayerContent.root, (node) => __async(this, null, function* () {
2368
+ const visitorProp = typeToVisitorMap[node.type];
2369
+ astVisitors.forEach((visitor) => __async(this, null, function* () {
2370
+ var _a, _b;
2371
+ try {
2372
+ yield (_a = visitor[visitorProp]) == null ? void 0 : _a.call(visitor, node);
2373
+ } catch (e) {
2374
+ (_b = ctx.log) == null ? void 0 : _b.error(`Error running rules for ${visitorProp}: ${e.message}, Stack ${e.stack}`);
2375
+ }
2376
+ }));
2377
+ return false;
2378
+ }));
2379
+ }
2380
+ return this.hooks.onValidateEnd.call(diagnostics, {
2381
+ documentContext: ctx,
2382
+ addFixableViolation
2383
+ });
2384
+ });
2385
+ }
2386
+ getCompletionsAtPosition(document, position) {
2387
+ return __async(this, null, function* () {
2388
+ const ctxWithPos = yield this.updateSourceWithPosition(document, position);
2389
+ if (!ctxWithPos) {
2390
+ return vscodeLanguageserverTypes.CompletionList.create();
2391
+ }
2392
+ const completionItems = [];
2393
+ const completionContext = {
2394
+ addCompletionItem: (i) => {
2395
+ completionItems.push(i);
2396
+ }
2397
+ };
2398
+ yield this.hooks.complete.call(ctxWithPos, completionContext);
2399
+ return vscodeLanguageserverTypes.CompletionList.create(completionItems);
2400
+ });
2401
+ }
2402
+ resolveCompletionItem(completionItem) {
2403
+ return __async(this, null, function* () {
2404
+ return completionItem;
2405
+ });
2406
+ }
2407
+ getHoverInfoAtPosition(document, position) {
2408
+ return __async(this, null, function* () {
2409
+ const context = yield this.updateSourceWithPosition(document, position);
2410
+ if (!context) {
2411
+ return void 0;
2412
+ }
2413
+ return this.hooks.hover.call(context);
2414
+ });
2415
+ }
2416
+ getCodeActionsInRange(document, context) {
2417
+ return __async(this, null, function* () {
2418
+ const diagsForDocument = this.fixableViolationsForDocument.get(document.uri);
2419
+ if (!diagsForDocument || diagsForDocument.size === 0 || context.diagnostics.length === 0) {
2420
+ return [];
2421
+ }
2422
+ const actions = [];
2423
+ diagsForDocument.forEach((violation, diagnostic) => {
2424
+ var _a;
2425
+ const matching = context.diagnostics.find((diag) => containsRange(diagnostic.range, diag.range));
2426
+ const fixedAction = (_a = violation.fix) == null ? void 0 : _a.call(violation);
2427
+ if (!matching || !fixedAction) {
2428
+ return;
2429
+ }
2430
+ actions.push({
2431
+ title: fixedAction.name,
2432
+ kind: vscodeLanguageserverTypes.CodeActionKind.QuickFix,
2433
+ edit: {
2434
+ changes: {
2435
+ [document.uri]: [toTextEdit(document, fixedAction.edit)]
2436
+ }
2437
+ }
2438
+ });
2439
+ });
2440
+ return actions;
2441
+ });
2442
+ }
2443
+ getDefinitionAtPosition(document, position) {
2444
+ return __async(this, null, function* () {
2445
+ const context = yield this.updateSourceWithPosition(document, position);
2446
+ if (!context) {
2447
+ return void 0;
2448
+ }
2449
+ return this.hooks.definition.call(context);
2450
+ });
2451
+ }
2452
+ setAssetTypes(typeFiles) {
2453
+ return __async(this, null, function* () {
2454
+ typeFiles.forEach((file) => {
2455
+ if (file.includes("types")) {
2456
+ this.XLRService.XLRSDK.loadDefinitionsFromDisk(file, {});
2457
+ } else {
2458
+ this.XLRService.XLRSDK.loadDefinitionsFromDisk(file, DEFAULT_FILTERS, TRANSFORM_FUNCTIONS);
2459
+ }
2460
+ });
2461
+ });
2462
+ }
2463
+ }
2464
+
2465
+ exports.ASTNodeImpl = ASTNodeImpl;
2466
+ exports.ArrayASTNodeImpl = ArrayASTNodeImpl;
2467
+ exports.AssetASTNodeImpl = AssetASTNodeImpl;
2468
+ exports.BooleanASTNodeImpl = BooleanASTNodeImpl;
2469
+ exports.ContentASTNodeImpl = ContentASTNodeImpl;
2470
+ exports.DEFAULT_FILTERS = DEFAULT_FILTERS;
2471
+ exports.FlowASTNodeImpl = FlowASTNodeImpl;
2472
+ exports.FlowStateASTNodeImpl = FlowStateASTNodeImpl;
2473
+ exports.LOG_TYPES = LOG_TYPES;
2474
+ exports.NavigationASTNodeImpl = NavigationASTNodeImpl;
2475
+ exports.NullASTNodeImpl = NullASTNodeImpl;
2476
+ exports.NumberASTNodeImpl = NumberASTNodeImpl;
2477
+ exports.ObjectASTNodeImpl = ObjectASTNodeImpl;
2478
+ exports.PLUGINS = PLUGINS;
2479
+ exports.PlayerContent = PlayerContent;
2480
+ exports.PlayerLanguageService = PlayerLanguageService;
2481
+ exports.PlayerXLRRegistry = PlayerXLRRegistry;
2482
+ exports.PropertyASTNodeImpl = PropertyASTNodeImpl;
2483
+ exports.StringASTNodeImpl = StringASTNodeImpl;
2484
+ exports.TRANSFORM_FUNCTIONS = TRANSFORM_FUNCTIONS;
2485
+ exports.ViewASTNodeImpl = ViewASTNodeImpl;
2486
+ exports.XLRService = XLRService;
2487
+ exports.applyAssetWrapperOrSwitch = applyAssetWrapperOrSwitch;
2488
+ exports.applyCommonProps = applyCommonProps;
2489
+ exports.applyTemplateProperty = applyTemplateProperty;
2490
+ exports.applyValueRefs = applyValueRefs;
2491
+ exports.containsRange = containsRange;
2492
+ exports.formatLikeNode = formatLikeNode;
2493
+ exports.getContentNode = getContentNode;
2494
+ exports.getLSLocationOfNode = getLSLocationOfNode;
2495
+ exports.getNodeValue = getNodeValue;
2496
+ exports.getPropForValNode = getPropForValNode;
2497
+ exports.getProperty = getProperty;
2498
+ exports.getValForKey = getValForKey;
2499
+ exports.getViewNode = getViewNode;
2500
+ exports.isFlowNode = isFlowNode;
2501
+ exports.isKeyNode = isKeyNode;
2502
+ exports.isKnownRootType = isKnownRootType;
2503
+ exports.isNavigationNode = isNavigationNode;
2504
+ exports.isObjectNode = isObjectNode;
2505
+ exports.isPropertyCompletion = isPropertyCompletion;
2506
+ exports.isPropertyNode = isPropertyNode;
2507
+ exports.isStateNode = isStateNode;
2508
+ exports.isValueCompletion = isValueCompletion;
2509
+ exports.isValueNode = isValueNode;
2510
+ exports.isViewNode = isViewNode;
2511
+ exports.mapFlowStateToType = mapFlowStateToType;
2512
+ exports.parse = parse;
2513
+ exports.replaceString = replaceString;
2514
+ exports.toRange = toRange;
2515
+ exports.toTextDocument = toTextDocument;
2516
+ exports.toTextEdit = toTextEdit;
2517
+ exports.typeToVisitorMap = typeToVisitorMap;
2518
+ exports.walk = walk;
2519
+ //# sourceMappingURL=index.cjs.js.map