@bpmn-io/feel-editor 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.es.js CHANGED
@@ -633,6 +633,17 @@ function codePointAt(str, pos) {
633
633
  return ((code0 - 0xd800) << 10) + (code1 - 0xdc00) + 0x10000;
634
634
  }
635
635
  /**
636
+ Given a Unicode codepoint, return the JavaScript string that
637
+ respresents it (like
638
+ [`String.fromCodePoint`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint)).
639
+ */
640
+ function fromCodePoint(code) {
641
+ if (code <= 0xffff)
642
+ return String.fromCharCode(code);
643
+ code -= 0x10000;
644
+ return String.fromCharCode((code >> 10) + 0xd800, (code & 1023) + 0xdc00);
645
+ }
646
+ /**
636
647
  The amount of positions a character takes up a JavaScript string.
637
648
  */
638
649
  function codePointSize(code) { return code < 0x10000 ? 1 : 2; }
@@ -2449,9 +2460,9 @@ function extendTransaction(tr) {
2449
2460
  }
2450
2461
  return spec == tr ? tr : Transaction.create(state, tr.changes, tr.selection, spec.effects, spec.annotations, spec.scrollIntoView);
2451
2462
  }
2452
- const none = [];
2463
+ const none$1 = [];
2453
2464
  function asArray(value) {
2454
- return value == null ? none : Array.isArray(value) ? value : [value];
2465
+ return value == null ? none$1 : Array.isArray(value) ? value : [value];
2455
2466
  }
2456
2467
 
2457
2468
  /**
@@ -5844,7 +5855,7 @@ class ScrollTarget {
5844
5855
  return changes.empty ? this : new ScrollTarget(this.range.map(changes), this.y, this.x, this.yMargin, this.xMargin);
5845
5856
  }
5846
5857
  }
5847
- const scrollIntoView = /*@__PURE__*/StateEffect.define({ map: (t, ch) => t.map(ch) });
5858
+ const scrollIntoView$1 = /*@__PURE__*/StateEffect.define({ map: (t, ch) => t.map(ch) });
5848
5859
  /**
5849
5860
  Log or report an unhandled exception in client code. Should
5850
5861
  probably only be used by extension code that allows client code to
@@ -10211,7 +10222,7 @@ class EditorView {
10211
10222
  scrollTarget = new ScrollTarget(main.empty ? main : EditorSelection.cursor(main.head, main.head > main.anchor ? -1 : 1));
10212
10223
  }
10213
10224
  for (let e of tr.effects)
10214
- if (e.is(scrollIntoView))
10225
+ if (e.is(scrollIntoView$1))
10215
10226
  scrollTarget = e.value;
10216
10227
  }
10217
10228
  this.viewState.update(update, scrollTarget);
@@ -10732,7 +10743,7 @@ class EditorView {
10732
10743
  cause it to scroll the given position or range into view.
10733
10744
  */
10734
10745
  static scrollIntoView(pos, options = {}) {
10735
- return scrollIntoView.of(new ScrollTarget(typeof pos == "number" ? EditorSelection.cursor(pos) : pos, options.y, options.x, options.yMargin, options.xMargin));
10746
+ return scrollIntoView$1.of(new ScrollTarget(typeof pos == "number" ? EditorSelection.cursor(pos) : pos, options.y, options.x, options.yMargin, options.xMargin));
10736
10747
  }
10737
10748
  /**
10738
10749
  Returns an extension that can be used to add DOM event handlers.
@@ -11359,7 +11370,7 @@ const tooltipPlugin = /*@__PURE__*/ViewPlugin.fromClass(class {
11359
11370
  scroll() { this.maybeMeasure(); }
11360
11371
  }
11361
11372
  });
11362
- const baseTheme$2 = /*@__PURE__*/EditorView.baseTheme({
11373
+ const baseTheme$3 = /*@__PURE__*/EditorView.baseTheme({
11363
11374
  ".cm-tooltip": {
11364
11375
  zIndex: 100
11365
11376
  },
@@ -11425,7 +11436,7 @@ const noOffset = { x: 0, y: 0 };
11425
11436
  Facet to which an extension can add a value to show a tooltip.
11426
11437
  */
11427
11438
  const showTooltip = /*@__PURE__*/Facet.define({
11428
- enables: [tooltipPlugin, baseTheme$2]
11439
+ enables: [tooltipPlugin, baseTheme$3]
11429
11440
  });
11430
11441
  const showHoverTooltip = /*@__PURE__*/Facet.define();
11431
11442
  class HoverTooltipHost {
@@ -11635,6 +11646,16 @@ function hoverTooltip(source, options = {}) {
11635
11646
  showHoverTooltipHost
11636
11647
  ];
11637
11648
  }
11649
+ /**
11650
+ Get the active tooltip view for a given tooltip, if available.
11651
+ */
11652
+ function getTooltip(view, tooltip) {
11653
+ let plugin = view.plugin(tooltipPlugin);
11654
+ if (!plugin)
11655
+ return null;
11656
+ let found = plugin.manager.tooltips.indexOf(tooltip);
11657
+ return found < 0 ? null : plugin.manager.tooltipViews[found];
11658
+ }
11638
11659
  const closeHoverTooltipEffect = /*@__PURE__*/StateEffect.define();
11639
11660
 
11640
11661
  const panelConfig = /*@__PURE__*/Facet.define({
@@ -18885,7 +18906,7 @@ function maybeEnableLint(state, effects) {
18885
18906
  ]);
18886
18907
  }),
18887
18908
  hoverTooltip(lintTooltip, { hideOn: hideTooltip }),
18888
- baseTheme$1
18909
+ baseTheme$2
18889
18910
  ]));
18890
18911
  }
18891
18912
  /**
@@ -19266,7 +19287,7 @@ function svg(content, attrs = `viewBox="0 0 40 40"`) {
19266
19287
  function underline(color) {
19267
19288
  return svg(`<path d="m0 2.5 l2 -1.5 l1 0 l2 1.5 l1 0" stroke="${color}" fill="none" stroke-width=".7"/>`, `width="6" height="3"`);
19268
19289
  }
19269
- const baseTheme$1 = /*@__PURE__*/EditorView.baseTheme({
19290
+ const baseTheme$2 = /*@__PURE__*/EditorView.baseTheme({
19270
19291
  ".cm-diagnostic": {
19271
19292
  padding: "3px 6px 3px 8px",
19272
19293
  marginLeft: "-1px",
@@ -19400,7 +19421,7 @@ var syntaxLinter = linter$1(FeelLinter);
19400
19421
 
19401
19422
  var linter = [ syntaxLinter ];
19402
19423
 
19403
- const baseTheme = EditorView.theme({
19424
+ const baseTheme$1 = EditorView.theme({
19404
19425
  '& .cm-content': {
19405
19426
  padding: '0px',
19406
19427
  },
@@ -19443,7 +19464,1182 @@ const syntaxClasses = syntaxHighlighting(
19443
19464
  ])
19444
19465
  );
19445
19466
 
19446
- var theme = [ baseTheme, highlightTheme, syntaxClasses ];
19467
+ var theme = [ baseTheme$1, highlightTheme, syntaxClasses ];
19468
+
19469
+ /**
19470
+ An instance of this is passed to completion source functions.
19471
+ */
19472
+ class CompletionContext {
19473
+ /**
19474
+ Create a new completion context. (Mostly useful for testing
19475
+ completion sources—in the editor, the extension will create
19476
+ these for you.)
19477
+ */
19478
+ constructor(
19479
+ /**
19480
+ The editor state that the completion happens in.
19481
+ */
19482
+ state,
19483
+ /**
19484
+ The position at which the completion is happening.
19485
+ */
19486
+ pos,
19487
+ /**
19488
+ Indicates whether completion was activated explicitly, or
19489
+ implicitly by typing. The usual way to respond to this is to
19490
+ only return completions when either there is part of a
19491
+ completable entity before the cursor, or `explicit` is true.
19492
+ */
19493
+ explicit) {
19494
+ this.state = state;
19495
+ this.pos = pos;
19496
+ this.explicit = explicit;
19497
+ /**
19498
+ @internal
19499
+ */
19500
+ this.abortListeners = [];
19501
+ }
19502
+ /**
19503
+ Get the extent, content, and (if there is a token) type of the
19504
+ token before `this.pos`.
19505
+ */
19506
+ tokenBefore(types) {
19507
+ let token = syntaxTree(this.state).resolveInner(this.pos, -1);
19508
+ while (token && types.indexOf(token.name) < 0)
19509
+ token = token.parent;
19510
+ return token ? { from: token.from, to: this.pos,
19511
+ text: this.state.sliceDoc(token.from, this.pos),
19512
+ type: token.type } : null;
19513
+ }
19514
+ /**
19515
+ Get the match of the given expression directly before the
19516
+ cursor.
19517
+ */
19518
+ matchBefore(expr) {
19519
+ let line = this.state.doc.lineAt(this.pos);
19520
+ let start = Math.max(line.from, this.pos - 250);
19521
+ let str = line.text.slice(start - line.from, this.pos - line.from);
19522
+ let found = str.search(ensureAnchor(expr, false));
19523
+ return found < 0 ? null : { from: start + found, to: this.pos, text: str.slice(found) };
19524
+ }
19525
+ /**
19526
+ Yields true when the query has been aborted. Can be useful in
19527
+ asynchronous queries to avoid doing work that will be ignored.
19528
+ */
19529
+ get aborted() { return this.abortListeners == null; }
19530
+ /**
19531
+ Allows you to register abort handlers, which will be called when
19532
+ the query is
19533
+ [aborted](https://codemirror.net/6/docs/ref/#autocomplete.CompletionContext.aborted).
19534
+ */
19535
+ addEventListener(type, listener) {
19536
+ if (type == "abort" && this.abortListeners)
19537
+ this.abortListeners.push(listener);
19538
+ }
19539
+ }
19540
+ function toSet(chars) {
19541
+ let flat = Object.keys(chars).join("");
19542
+ let words = /\w/.test(flat);
19543
+ if (words)
19544
+ flat = flat.replace(/\w/g, "");
19545
+ return `[${words ? "\\w" : ""}${flat.replace(/[^\w\s]/g, "\\$&")}]`;
19546
+ }
19547
+ function prefixMatch(options) {
19548
+ let first = Object.create(null), rest = Object.create(null);
19549
+ for (let { label } of options) {
19550
+ first[label[0]] = true;
19551
+ for (let i = 1; i < label.length; i++)
19552
+ rest[label[i]] = true;
19553
+ }
19554
+ let source = toSet(first) + toSet(rest) + "*$";
19555
+ return [new RegExp("^" + source), new RegExp(source)];
19556
+ }
19557
+ /**
19558
+ Given a a fixed array of options, return an autocompleter that
19559
+ completes them.
19560
+ */
19561
+ function completeFromList(list) {
19562
+ let options = list.map(o => typeof o == "string" ? { label: o } : o);
19563
+ let [validFor, match] = options.every(o => /^\w+$/.test(o.label)) ? [/\w*$/, /\w+$/] : prefixMatch(options);
19564
+ return (context) => {
19565
+ let token = context.matchBefore(match);
19566
+ return token || context.explicit ? { from: token ? token.from : context.pos, options, validFor } : null;
19567
+ };
19568
+ }
19569
+ class Option {
19570
+ constructor(completion, source, match) {
19571
+ this.completion = completion;
19572
+ this.source = source;
19573
+ this.match = match;
19574
+ }
19575
+ }
19576
+ function cur(state) { return state.selection.main.head; }
19577
+ // Make sure the given regexp has a $ at its end and, if `start` is
19578
+ // true, a ^ at its start.
19579
+ function ensureAnchor(expr, start) {
19580
+ var _a;
19581
+ let { source } = expr;
19582
+ let addStart = start && source[0] != "^", addEnd = source[source.length - 1] != "$";
19583
+ if (!addStart && !addEnd)
19584
+ return expr;
19585
+ return new RegExp(`${addStart ? "^" : ""}(?:${source})${addEnd ? "$" : ""}`, (_a = expr.flags) !== null && _a !== void 0 ? _a : (expr.ignoreCase ? "i" : ""));
19586
+ }
19587
+ /**
19588
+ Helper function that returns a transaction spec which inserts a
19589
+ completion's text in the main selection range, and any other
19590
+ selection range that has the same text in front of it.
19591
+ */
19592
+ function insertCompletionText(state, text, from, to) {
19593
+ return Object.assign(Object.assign({}, state.changeByRange(range => {
19594
+ if (range == state.selection.main)
19595
+ return {
19596
+ changes: { from: from, to: to, insert: text },
19597
+ range: EditorSelection.cursor(from + text.length)
19598
+ };
19599
+ let len = to - from;
19600
+ if (!range.empty ||
19601
+ len && state.sliceDoc(range.from - len, range.from) != state.sliceDoc(from, to))
19602
+ return { range };
19603
+ return {
19604
+ changes: { from: range.from - len, to: range.from, insert: text },
19605
+ range: EditorSelection.cursor(range.from - len + text.length)
19606
+ };
19607
+ })), { userEvent: "input.complete" });
19608
+ }
19609
+ function applyCompletion(view, option) {
19610
+ const apply = option.completion.apply || option.completion.label;
19611
+ let result = option.source;
19612
+ if (typeof apply == "string")
19613
+ view.dispatch(insertCompletionText(view.state, apply, result.from, result.to));
19614
+ else
19615
+ apply(view, option.completion, result.from, result.to);
19616
+ }
19617
+ const SourceCache = /*@__PURE__*/new WeakMap();
19618
+ function asSource(source) {
19619
+ if (!Array.isArray(source))
19620
+ return source;
19621
+ let known = SourceCache.get(source);
19622
+ if (!known)
19623
+ SourceCache.set(source, known = completeFromList(source));
19624
+ return known;
19625
+ }
19626
+
19627
+ // A pattern matcher for fuzzy completion matching. Create an instance
19628
+ // once for a pattern, and then use that to match any number of
19629
+ // completions.
19630
+ class FuzzyMatcher {
19631
+ constructor(pattern) {
19632
+ this.pattern = pattern;
19633
+ this.chars = [];
19634
+ this.folded = [];
19635
+ // Buffers reused by calls to `match` to track matched character
19636
+ // positions.
19637
+ this.any = [];
19638
+ this.precise = [];
19639
+ this.byWord = [];
19640
+ for (let p = 0; p < pattern.length;) {
19641
+ let char = codePointAt(pattern, p), size = codePointSize(char);
19642
+ this.chars.push(char);
19643
+ let part = pattern.slice(p, p + size), upper = part.toUpperCase();
19644
+ this.folded.push(codePointAt(upper == part ? part.toLowerCase() : upper, 0));
19645
+ p += size;
19646
+ }
19647
+ this.astral = pattern.length != this.chars.length;
19648
+ }
19649
+ // Matches a given word (completion) against the pattern (input).
19650
+ // Will return null for no match, and otherwise an array that starts
19651
+ // with the match score, followed by any number of `from, to` pairs
19652
+ // indicating the matched parts of `word`.
19653
+ //
19654
+ // The score is a number that is more negative the worse the match
19655
+ // is. See `Penalty` above.
19656
+ match(word) {
19657
+ if (this.pattern.length == 0)
19658
+ return [0];
19659
+ if (word.length < this.pattern.length)
19660
+ return null;
19661
+ let { chars, folded, any, precise, byWord } = this;
19662
+ // For single-character queries, only match when they occur right
19663
+ // at the start
19664
+ if (chars.length == 1) {
19665
+ let first = codePointAt(word, 0);
19666
+ return first == chars[0] ? [0, 0, codePointSize(first)]
19667
+ : first == folded[0] ? [-200 /* CaseFold */, 0, codePointSize(first)] : null;
19668
+ }
19669
+ let direct = word.indexOf(this.pattern);
19670
+ if (direct == 0)
19671
+ return [0, 0, this.pattern.length];
19672
+ let len = chars.length, anyTo = 0;
19673
+ if (direct < 0) {
19674
+ for (let i = 0, e = Math.min(word.length, 200); i < e && anyTo < len;) {
19675
+ let next = codePointAt(word, i);
19676
+ if (next == chars[anyTo] || next == folded[anyTo])
19677
+ any[anyTo++] = i;
19678
+ i += codePointSize(next);
19679
+ }
19680
+ // No match, exit immediately
19681
+ if (anyTo < len)
19682
+ return null;
19683
+ }
19684
+ // This tracks the extent of the precise (non-folded, not
19685
+ // necessarily adjacent) match
19686
+ let preciseTo = 0;
19687
+ // Tracks whether there is a match that hits only characters that
19688
+ // appear to be starting words. `byWordFolded` is set to true when
19689
+ // a case folded character is encountered in such a match
19690
+ let byWordTo = 0, byWordFolded = false;
19691
+ // If we've found a partial adjacent match, these track its state
19692
+ let adjacentTo = 0, adjacentStart = -1, adjacentEnd = -1;
19693
+ let hasLower = /[a-z]/.test(word), wordAdjacent = true;
19694
+ // Go over the option's text, scanning for the various kinds of matches
19695
+ for (let i = 0, e = Math.min(word.length, 200), prevType = 0 /* NonWord */; i < e && byWordTo < len;) {
19696
+ let next = codePointAt(word, i);
19697
+ if (direct < 0) {
19698
+ if (preciseTo < len && next == chars[preciseTo])
19699
+ precise[preciseTo++] = i;
19700
+ if (adjacentTo < len) {
19701
+ if (next == chars[adjacentTo] || next == folded[adjacentTo]) {
19702
+ if (adjacentTo == 0)
19703
+ adjacentStart = i;
19704
+ adjacentEnd = i + 1;
19705
+ adjacentTo++;
19706
+ }
19707
+ else {
19708
+ adjacentTo = 0;
19709
+ }
19710
+ }
19711
+ }
19712
+ let ch, type = next < 0xff
19713
+ ? (next >= 48 && next <= 57 || next >= 97 && next <= 122 ? 2 /* Lower */ : next >= 65 && next <= 90 ? 1 /* Upper */ : 0 /* NonWord */)
19714
+ : ((ch = fromCodePoint(next)) != ch.toLowerCase() ? 1 /* Upper */ : ch != ch.toUpperCase() ? 2 /* Lower */ : 0 /* NonWord */);
19715
+ if (!i || type == 1 /* Upper */ && hasLower || prevType == 0 /* NonWord */ && type != 0 /* NonWord */) {
19716
+ if (chars[byWordTo] == next || (folded[byWordTo] == next && (byWordFolded = true)))
19717
+ byWord[byWordTo++] = i;
19718
+ else if (byWord.length)
19719
+ wordAdjacent = false;
19720
+ }
19721
+ prevType = type;
19722
+ i += codePointSize(next);
19723
+ }
19724
+ if (byWordTo == len && byWord[0] == 0 && wordAdjacent)
19725
+ return this.result(-100 /* ByWord */ + (byWordFolded ? -200 /* CaseFold */ : 0), byWord, word);
19726
+ if (adjacentTo == len && adjacentStart == 0)
19727
+ return [-200 /* CaseFold */ - word.length, 0, adjacentEnd];
19728
+ if (direct > -1)
19729
+ return [-700 /* NotStart */ - word.length, direct, direct + this.pattern.length];
19730
+ if (adjacentTo == len)
19731
+ return [-200 /* CaseFold */ + -700 /* NotStart */ - word.length, adjacentStart, adjacentEnd];
19732
+ if (byWordTo == len)
19733
+ return this.result(-100 /* ByWord */ + (byWordFolded ? -200 /* CaseFold */ : 0) + -700 /* NotStart */ +
19734
+ (wordAdjacent ? 0 : -1100 /* Gap */), byWord, word);
19735
+ return chars.length == 2 ? null : this.result((any[0] ? -700 /* NotStart */ : 0) + -200 /* CaseFold */ + -1100 /* Gap */, any, word);
19736
+ }
19737
+ result(score, positions, word) {
19738
+ let result = [score - word.length], i = 1;
19739
+ for (let pos of positions) {
19740
+ let to = pos + (this.astral ? codePointSize(codePointAt(word, pos)) : 1);
19741
+ if (i > 1 && result[i - 1] == pos)
19742
+ result[i - 1] = to;
19743
+ else {
19744
+ result[i++] = pos;
19745
+ result[i++] = to;
19746
+ }
19747
+ }
19748
+ return result;
19749
+ }
19750
+ }
19751
+
19752
+ const completionConfig = /*@__PURE__*/Facet.define({
19753
+ combine(configs) {
19754
+ return combineConfig(configs, {
19755
+ activateOnTyping: true,
19756
+ override: null,
19757
+ closeOnBlur: true,
19758
+ maxRenderedOptions: 100,
19759
+ defaultKeymap: true,
19760
+ optionClass: () => "",
19761
+ aboveCursor: false,
19762
+ icons: true,
19763
+ addToOptions: []
19764
+ }, {
19765
+ defaultKeymap: (a, b) => a && b,
19766
+ closeOnBlur: (a, b) => a && b,
19767
+ icons: (a, b) => a && b,
19768
+ optionClass: (a, b) => c => joinClass(a(c), b(c)),
19769
+ addToOptions: (a, b) => a.concat(b)
19770
+ });
19771
+ }
19772
+ });
19773
+ function joinClass(a, b) {
19774
+ return a ? b ? a + " " + b : a : b;
19775
+ }
19776
+
19777
+ function optionContent(config) {
19778
+ let content = config.addToOptions.slice();
19779
+ if (config.icons)
19780
+ content.push({
19781
+ render(completion) {
19782
+ let icon = document.createElement("div");
19783
+ icon.classList.add("cm-completionIcon");
19784
+ if (completion.type)
19785
+ icon.classList.add(...completion.type.split(/\s+/g).map(cls => "cm-completionIcon-" + cls));
19786
+ icon.setAttribute("aria-hidden", "true");
19787
+ return icon;
19788
+ },
19789
+ position: 20
19790
+ });
19791
+ content.push({
19792
+ render(completion, _s, match) {
19793
+ let labelElt = document.createElement("span");
19794
+ labelElt.className = "cm-completionLabel";
19795
+ let { label } = completion, off = 0;
19796
+ for (let j = 1; j < match.length;) {
19797
+ let from = match[j++], to = match[j++];
19798
+ if (from > off)
19799
+ labelElt.appendChild(document.createTextNode(label.slice(off, from)));
19800
+ let span = labelElt.appendChild(document.createElement("span"));
19801
+ span.appendChild(document.createTextNode(label.slice(from, to)));
19802
+ span.className = "cm-completionMatchedText";
19803
+ off = to;
19804
+ }
19805
+ if (off < label.length)
19806
+ labelElt.appendChild(document.createTextNode(label.slice(off)));
19807
+ return labelElt;
19808
+ },
19809
+ position: 50
19810
+ }, {
19811
+ render(completion) {
19812
+ if (!completion.detail)
19813
+ return null;
19814
+ let detailElt = document.createElement("span");
19815
+ detailElt.className = "cm-completionDetail";
19816
+ detailElt.textContent = completion.detail;
19817
+ return detailElt;
19818
+ },
19819
+ position: 80
19820
+ });
19821
+ return content.sort((a, b) => a.position - b.position).map(a => a.render);
19822
+ }
19823
+ function rangeAroundSelected(total, selected, max) {
19824
+ if (total <= max)
19825
+ return { from: 0, to: total };
19826
+ if (selected <= (total >> 1)) {
19827
+ let off = Math.floor(selected / max);
19828
+ return { from: off * max, to: (off + 1) * max };
19829
+ }
19830
+ let off = Math.floor((total - selected) / max);
19831
+ return { from: total - (off + 1) * max, to: total - off * max };
19832
+ }
19833
+ class CompletionTooltip {
19834
+ constructor(view, stateField) {
19835
+ this.view = view;
19836
+ this.stateField = stateField;
19837
+ this.info = null;
19838
+ this.placeInfo = {
19839
+ read: () => this.measureInfo(),
19840
+ write: (pos) => this.positionInfo(pos),
19841
+ key: this
19842
+ };
19843
+ let cState = view.state.field(stateField);
19844
+ let { options, selected } = cState.open;
19845
+ let config = view.state.facet(completionConfig);
19846
+ this.optionContent = optionContent(config);
19847
+ this.optionClass = config.optionClass;
19848
+ this.range = rangeAroundSelected(options.length, selected, config.maxRenderedOptions);
19849
+ this.dom = document.createElement("div");
19850
+ this.dom.className = "cm-tooltip-autocomplete";
19851
+ this.dom.addEventListener("mousedown", (e) => {
19852
+ for (let dom = e.target, match; dom && dom != this.dom; dom = dom.parentNode) {
19853
+ if (dom.nodeName == "LI" && (match = /-(\d+)$/.exec(dom.id)) && +match[1] < options.length) {
19854
+ applyCompletion(view, options[+match[1]]);
19855
+ e.preventDefault();
19856
+ return;
19857
+ }
19858
+ }
19859
+ });
19860
+ this.list = this.dom.appendChild(this.createListBox(options, cState.id, this.range));
19861
+ this.list.addEventListener("scroll", () => {
19862
+ if (this.info)
19863
+ this.view.requestMeasure(this.placeInfo);
19864
+ });
19865
+ }
19866
+ mount() { this.updateSel(); }
19867
+ update(update) {
19868
+ if (update.state.field(this.stateField) != update.startState.field(this.stateField))
19869
+ this.updateSel();
19870
+ }
19871
+ positioned() {
19872
+ if (this.info)
19873
+ this.view.requestMeasure(this.placeInfo);
19874
+ }
19875
+ updateSel() {
19876
+ let cState = this.view.state.field(this.stateField), open = cState.open;
19877
+ if (open.selected < this.range.from || open.selected >= this.range.to) {
19878
+ this.range = rangeAroundSelected(open.options.length, open.selected, this.view.state.facet(completionConfig).maxRenderedOptions);
19879
+ this.list.remove();
19880
+ this.list = this.dom.appendChild(this.createListBox(open.options, cState.id, this.range));
19881
+ this.list.addEventListener("scroll", () => {
19882
+ if (this.info)
19883
+ this.view.requestMeasure(this.placeInfo);
19884
+ });
19885
+ }
19886
+ if (this.updateSelectedOption(open.selected)) {
19887
+ if (this.info) {
19888
+ this.info.remove();
19889
+ this.info = null;
19890
+ }
19891
+ let { completion } = open.options[open.selected];
19892
+ let { info } = completion;
19893
+ if (!info)
19894
+ return;
19895
+ let infoResult = typeof info === 'string' ? document.createTextNode(info) : info(completion);
19896
+ if (!infoResult)
19897
+ return;
19898
+ if ('then' in infoResult) {
19899
+ infoResult.then(node => {
19900
+ if (node && this.view.state.field(this.stateField, false) == cState)
19901
+ this.addInfoPane(node);
19902
+ }).catch(e => logException(this.view.state, e, "completion info"));
19903
+ }
19904
+ else {
19905
+ this.addInfoPane(infoResult);
19906
+ }
19907
+ }
19908
+ }
19909
+ addInfoPane(content) {
19910
+ let dom = this.info = document.createElement("div");
19911
+ dom.className = "cm-tooltip cm-completionInfo";
19912
+ dom.appendChild(content);
19913
+ this.dom.appendChild(dom);
19914
+ this.view.requestMeasure(this.placeInfo);
19915
+ }
19916
+ updateSelectedOption(selected) {
19917
+ let set = null;
19918
+ for (let opt = this.list.firstChild, i = this.range.from; opt; opt = opt.nextSibling, i++) {
19919
+ if (i == selected) {
19920
+ if (!opt.hasAttribute("aria-selected")) {
19921
+ opt.setAttribute("aria-selected", "true");
19922
+ set = opt;
19923
+ }
19924
+ }
19925
+ else {
19926
+ if (opt.hasAttribute("aria-selected"))
19927
+ opt.removeAttribute("aria-selected");
19928
+ }
19929
+ }
19930
+ if (set)
19931
+ scrollIntoView(this.list, set);
19932
+ return set;
19933
+ }
19934
+ measureInfo() {
19935
+ let sel = this.dom.querySelector("[aria-selected]");
19936
+ if (!sel || !this.info)
19937
+ return null;
19938
+ let listRect = this.dom.getBoundingClientRect();
19939
+ let infoRect = this.info.getBoundingClientRect();
19940
+ let selRect = sel.getBoundingClientRect();
19941
+ if (selRect.top > Math.min(innerHeight, listRect.bottom) - 10 || selRect.bottom < Math.max(0, listRect.top) + 10)
19942
+ return null;
19943
+ let top = Math.max(0, Math.min(selRect.top, innerHeight - infoRect.height)) - listRect.top;
19944
+ let left = this.view.textDirection == Direction.RTL;
19945
+ let spaceLeft = listRect.left, spaceRight = innerWidth - listRect.right;
19946
+ if (left && spaceLeft < Math.min(infoRect.width, spaceRight))
19947
+ left = false;
19948
+ else if (!left && spaceRight < Math.min(infoRect.width, spaceLeft))
19949
+ left = true;
19950
+ return { top, left };
19951
+ }
19952
+ positionInfo(pos) {
19953
+ if (this.info) {
19954
+ this.info.style.top = (pos ? pos.top : -1e6) + "px";
19955
+ if (pos) {
19956
+ this.info.classList.toggle("cm-completionInfo-left", pos.left);
19957
+ this.info.classList.toggle("cm-completionInfo-right", !pos.left);
19958
+ }
19959
+ }
19960
+ }
19961
+ createListBox(options, id, range) {
19962
+ const ul = document.createElement("ul");
19963
+ ul.id = id;
19964
+ ul.setAttribute("role", "listbox");
19965
+ ul.setAttribute("aria-expanded", "true");
19966
+ ul.setAttribute("aria-label", this.view.state.phrase("Completions"));
19967
+ for (let i = range.from; i < range.to; i++) {
19968
+ let { completion, match } = options[i];
19969
+ const li = ul.appendChild(document.createElement("li"));
19970
+ li.id = id + "-" + i;
19971
+ li.setAttribute("role", "option");
19972
+ let cls = this.optionClass(completion);
19973
+ if (cls)
19974
+ li.className = cls;
19975
+ for (let source of this.optionContent) {
19976
+ let node = source(completion, this.view.state, match);
19977
+ if (node)
19978
+ li.appendChild(node);
19979
+ }
19980
+ }
19981
+ if (range.from)
19982
+ ul.classList.add("cm-completionListIncompleteTop");
19983
+ if (range.to < options.length)
19984
+ ul.classList.add("cm-completionListIncompleteBottom");
19985
+ return ul;
19986
+ }
19987
+ }
19988
+ // We allocate a new function instance every time the completion
19989
+ // changes to force redrawing/repositioning of the tooltip
19990
+ function completionTooltip(stateField) {
19991
+ return (view) => new CompletionTooltip(view, stateField);
19992
+ }
19993
+ function scrollIntoView(container, element) {
19994
+ let parent = container.getBoundingClientRect();
19995
+ let self = element.getBoundingClientRect();
19996
+ if (self.top < parent.top)
19997
+ container.scrollTop -= parent.top - self.top;
19998
+ else if (self.bottom > parent.bottom)
19999
+ container.scrollTop += self.bottom - parent.bottom;
20000
+ }
20001
+
20002
+ // Used to pick a preferred option when two options with the same
20003
+ // label occur in the result.
20004
+ function score(option) {
20005
+ return (option.boost || 0) * 100 + (option.apply ? 10 : 0) + (option.info ? 5 : 0) +
20006
+ (option.type ? 1 : 0);
20007
+ }
20008
+ function sortOptions(active, state) {
20009
+ let options = [], i = 0;
20010
+ for (let a of active)
20011
+ if (a.hasResult()) {
20012
+ if (a.result.filter === false) {
20013
+ let getMatch = a.result.getMatch;
20014
+ for (let option of a.result.options) {
20015
+ let match = [1e9 - i++];
20016
+ if (getMatch)
20017
+ for (let n of getMatch(option))
20018
+ match.push(n);
20019
+ options.push(new Option(option, a, match));
20020
+ }
20021
+ }
20022
+ else {
20023
+ let matcher = new FuzzyMatcher(state.sliceDoc(a.from, a.to)), match;
20024
+ for (let option of a.result.options)
20025
+ if (match = matcher.match(option.label)) {
20026
+ if (option.boost != null)
20027
+ match[0] += option.boost;
20028
+ options.push(new Option(option, a, match));
20029
+ }
20030
+ }
20031
+ }
20032
+ let result = [], prev = null;
20033
+ for (let opt of options.sort(cmpOption)) {
20034
+ if (!prev || prev.label != opt.completion.label || prev.detail != opt.completion.detail ||
20035
+ (prev.type != null && opt.completion.type != null && prev.type != opt.completion.type) ||
20036
+ prev.apply != opt.completion.apply)
20037
+ result.push(opt);
20038
+ else if (score(opt.completion) > score(prev))
20039
+ result[result.length - 1] = opt;
20040
+ prev = opt.completion;
20041
+ }
20042
+ return result;
20043
+ }
20044
+ class CompletionDialog {
20045
+ constructor(options, attrs, tooltip, timestamp, selected) {
20046
+ this.options = options;
20047
+ this.attrs = attrs;
20048
+ this.tooltip = tooltip;
20049
+ this.timestamp = timestamp;
20050
+ this.selected = selected;
20051
+ }
20052
+ setSelected(selected, id) {
20053
+ return selected == this.selected || selected >= this.options.length ? this
20054
+ : new CompletionDialog(this.options, makeAttrs(id, selected), this.tooltip, this.timestamp, selected);
20055
+ }
20056
+ static build(active, state, id, prev, conf) {
20057
+ let options = sortOptions(active, state);
20058
+ if (!options.length)
20059
+ return null;
20060
+ let selected = 0;
20061
+ if (prev && prev.selected) {
20062
+ let selectedValue = prev.options[prev.selected].completion;
20063
+ for (let i = 0; i < options.length; i++)
20064
+ if (options[i].completion == selectedValue) {
20065
+ selected = i;
20066
+ break;
20067
+ }
20068
+ }
20069
+ return new CompletionDialog(options, makeAttrs(id, selected), {
20070
+ pos: active.reduce((a, b) => b.hasResult() ? Math.min(a, b.from) : a, 1e8),
20071
+ create: completionTooltip(completionState),
20072
+ above: conf.aboveCursor,
20073
+ }, prev ? prev.timestamp : Date.now(), selected);
20074
+ }
20075
+ map(changes) {
20076
+ return new CompletionDialog(this.options, this.attrs, Object.assign(Object.assign({}, this.tooltip), { pos: changes.mapPos(this.tooltip.pos) }), this.timestamp, this.selected);
20077
+ }
20078
+ }
20079
+ class CompletionState {
20080
+ constructor(active, id, open) {
20081
+ this.active = active;
20082
+ this.id = id;
20083
+ this.open = open;
20084
+ }
20085
+ static start() {
20086
+ return new CompletionState(none, "cm-ac-" + Math.floor(Math.random() * 2e6).toString(36), null);
20087
+ }
20088
+ update(tr) {
20089
+ let { state } = tr, conf = state.facet(completionConfig);
20090
+ let sources = conf.override ||
20091
+ state.languageDataAt("autocomplete", cur(state)).map(asSource);
20092
+ let active = sources.map(source => {
20093
+ let value = this.active.find(s => s.source == source) ||
20094
+ new ActiveSource(source, this.active.some(a => a.state != 0 /* Inactive */) ? 1 /* Pending */ : 0 /* Inactive */);
20095
+ return value.update(tr, conf);
20096
+ });
20097
+ if (active.length == this.active.length && active.every((a, i) => a == this.active[i]))
20098
+ active = this.active;
20099
+ let open = tr.selection || active.some(a => a.hasResult() && tr.changes.touchesRange(a.from, a.to)) ||
20100
+ !sameResults(active, this.active) ? CompletionDialog.build(active, state, this.id, this.open, conf)
20101
+ : this.open && tr.docChanged ? this.open.map(tr.changes) : this.open;
20102
+ if (!open && active.every(a => a.state != 1 /* Pending */) && active.some(a => a.hasResult()))
20103
+ active = active.map(a => a.hasResult() ? new ActiveSource(a.source, 0 /* Inactive */) : a);
20104
+ for (let effect of tr.effects)
20105
+ if (effect.is(setSelectedEffect))
20106
+ open = open && open.setSelected(effect.value, this.id);
20107
+ return active == this.active && open == this.open ? this : new CompletionState(active, this.id, open);
20108
+ }
20109
+ get tooltip() { return this.open ? this.open.tooltip : null; }
20110
+ get attrs() { return this.open ? this.open.attrs : baseAttrs; }
20111
+ }
20112
+ function sameResults(a, b) {
20113
+ if (a == b)
20114
+ return true;
20115
+ for (let iA = 0, iB = 0;;) {
20116
+ while (iA < a.length && !a[iA].hasResult)
20117
+ iA++;
20118
+ while (iB < b.length && !b[iB].hasResult)
20119
+ iB++;
20120
+ let endA = iA == a.length, endB = iB == b.length;
20121
+ if (endA || endB)
20122
+ return endA == endB;
20123
+ if (a[iA++].result != b[iB++].result)
20124
+ return false;
20125
+ }
20126
+ }
20127
+ const baseAttrs = {
20128
+ "aria-autocomplete": "list"
20129
+ };
20130
+ function makeAttrs(id, selected) {
20131
+ return {
20132
+ "aria-autocomplete": "list",
20133
+ "aria-haspopup": "listbox",
20134
+ "aria-activedescendant": id + "-" + selected,
20135
+ "aria-controls": id
20136
+ };
20137
+ }
20138
+ const none = [];
20139
+ function cmpOption(a, b) {
20140
+ let dScore = b.match[0] - a.match[0];
20141
+ if (dScore)
20142
+ return dScore;
20143
+ return a.completion.label.localeCompare(b.completion.label);
20144
+ }
20145
+ function getUserEvent(tr) {
20146
+ return tr.isUserEvent("input.type") ? "input" : tr.isUserEvent("delete.backward") ? "delete" : null;
20147
+ }
20148
+ class ActiveSource {
20149
+ constructor(source, state, explicitPos = -1) {
20150
+ this.source = source;
20151
+ this.state = state;
20152
+ this.explicitPos = explicitPos;
20153
+ }
20154
+ hasResult() { return false; }
20155
+ update(tr, conf) {
20156
+ let event = getUserEvent(tr), value = this;
20157
+ if (event)
20158
+ value = value.handleUserEvent(tr, event, conf);
20159
+ else if (tr.docChanged)
20160
+ value = value.handleChange(tr);
20161
+ else if (tr.selection && value.state != 0 /* Inactive */)
20162
+ value = new ActiveSource(value.source, 0 /* Inactive */);
20163
+ for (let effect of tr.effects) {
20164
+ if (effect.is(startCompletionEffect))
20165
+ value = new ActiveSource(value.source, 1 /* Pending */, effect.value ? cur(tr.state) : -1);
20166
+ else if (effect.is(closeCompletionEffect))
20167
+ value = new ActiveSource(value.source, 0 /* Inactive */);
20168
+ else if (effect.is(setActiveEffect))
20169
+ for (let active of effect.value)
20170
+ if (active.source == value.source)
20171
+ value = active;
20172
+ }
20173
+ return value;
20174
+ }
20175
+ handleUserEvent(tr, type, conf) {
20176
+ return type == "delete" || !conf.activateOnTyping ? this.map(tr.changes) : new ActiveSource(this.source, 1 /* Pending */);
20177
+ }
20178
+ handleChange(tr) {
20179
+ return tr.changes.touchesRange(cur(tr.startState)) ? new ActiveSource(this.source, 0 /* Inactive */) : this.map(tr.changes);
20180
+ }
20181
+ map(changes) {
20182
+ return changes.empty || this.explicitPos < 0 ? this : new ActiveSource(this.source, this.state, changes.mapPos(this.explicitPos));
20183
+ }
20184
+ }
20185
+ class ActiveResult extends ActiveSource {
20186
+ constructor(source, explicitPos, result, from, to) {
20187
+ super(source, 2 /* Result */, explicitPos);
20188
+ this.result = result;
20189
+ this.from = from;
20190
+ this.to = to;
20191
+ }
20192
+ hasResult() { return true; }
20193
+ handleUserEvent(tr, type, conf) {
20194
+ var _a;
20195
+ let from = tr.changes.mapPos(this.from), to = tr.changes.mapPos(this.to, 1);
20196
+ let pos = cur(tr.state);
20197
+ if ((this.explicitPos < 0 ? pos <= from : pos < this.from) ||
20198
+ pos > to ||
20199
+ type == "delete" && cur(tr.startState) == this.from)
20200
+ return new ActiveSource(this.source, type == "input" && conf.activateOnTyping ? 1 /* Pending */ : 0 /* Inactive */);
20201
+ let explicitPos = this.explicitPos < 0 ? -1 : tr.changes.mapPos(this.explicitPos), updated;
20202
+ if (checkValid(this.result.validFor, tr.state, from, to))
20203
+ return new ActiveResult(this.source, explicitPos, this.result, from, to);
20204
+ if (this.result.update &&
20205
+ (updated = this.result.update(this.result, from, to, new CompletionContext(tr.state, pos, explicitPos >= 0))))
20206
+ return new ActiveResult(this.source, explicitPos, updated, updated.from, (_a = updated.to) !== null && _a !== void 0 ? _a : cur(tr.state));
20207
+ return new ActiveSource(this.source, 1 /* Pending */, explicitPos);
20208
+ }
20209
+ handleChange(tr) {
20210
+ return tr.changes.touchesRange(this.from, this.to) ? new ActiveSource(this.source, 0 /* Inactive */) : this.map(tr.changes);
20211
+ }
20212
+ map(mapping) {
20213
+ return mapping.empty ? this :
20214
+ new ActiveResult(this.source, this.explicitPos < 0 ? -1 : mapping.mapPos(this.explicitPos), this.result, mapping.mapPos(this.from), mapping.mapPos(this.to, 1));
20215
+ }
20216
+ }
20217
+ function checkValid(validFor, state, from, to) {
20218
+ if (!validFor)
20219
+ return false;
20220
+ let text = state.sliceDoc(from, to);
20221
+ return typeof validFor == "function" ? validFor(text, from, to, state) : ensureAnchor(validFor, true).test(text);
20222
+ }
20223
+ const startCompletionEffect = /*@__PURE__*/StateEffect.define();
20224
+ const closeCompletionEffect = /*@__PURE__*/StateEffect.define();
20225
+ const setActiveEffect = /*@__PURE__*/StateEffect.define({
20226
+ map(sources, mapping) { return sources.map(s => s.map(mapping)); }
20227
+ });
20228
+ const setSelectedEffect = /*@__PURE__*/StateEffect.define();
20229
+ const completionState = /*@__PURE__*/StateField.define({
20230
+ create() { return CompletionState.start(); },
20231
+ update(value, tr) { return value.update(tr); },
20232
+ provide: f => [
20233
+ showTooltip.from(f, val => val.tooltip),
20234
+ EditorView.contentAttributes.from(f, state => state.attrs)
20235
+ ]
20236
+ });
20237
+
20238
+ const CompletionInteractMargin = 75;
20239
+ /**
20240
+ Returns a command that moves the completion selection forward or
20241
+ backward by the given amount.
20242
+ */
20243
+ function moveCompletionSelection(forward, by = "option") {
20244
+ return (view) => {
20245
+ let cState = view.state.field(completionState, false);
20246
+ if (!cState || !cState.open || Date.now() - cState.open.timestamp < CompletionInteractMargin)
20247
+ return false;
20248
+ let step = 1, tooltip;
20249
+ if (by == "page" && (tooltip = getTooltip(view, cState.open.tooltip)))
20250
+ step = Math.max(2, Math.floor(tooltip.dom.offsetHeight /
20251
+ tooltip.dom.querySelector("li").offsetHeight) - 1);
20252
+ let selected = cState.open.selected + step * (forward ? 1 : -1), { length } = cState.open.options;
20253
+ if (selected < 0)
20254
+ selected = by == "page" ? 0 : length - 1;
20255
+ else if (selected >= length)
20256
+ selected = by == "page" ? length - 1 : 0;
20257
+ view.dispatch({ effects: setSelectedEffect.of(selected) });
20258
+ return true;
20259
+ };
20260
+ }
20261
+ /**
20262
+ Accept the current completion.
20263
+ */
20264
+ const acceptCompletion = (view) => {
20265
+ let cState = view.state.field(completionState, false);
20266
+ if (view.state.readOnly || !cState || !cState.open || Date.now() - cState.open.timestamp < CompletionInteractMargin)
20267
+ return false;
20268
+ applyCompletion(view, cState.open.options[cState.open.selected]);
20269
+ return true;
20270
+ };
20271
+ /**
20272
+ Explicitly start autocompletion.
20273
+ */
20274
+ const startCompletion = (view) => {
20275
+ let cState = view.state.field(completionState, false);
20276
+ if (!cState)
20277
+ return false;
20278
+ view.dispatch({ effects: startCompletionEffect.of(true) });
20279
+ return true;
20280
+ };
20281
+ /**
20282
+ Close the currently active completion.
20283
+ */
20284
+ const closeCompletion = (view) => {
20285
+ let cState = view.state.field(completionState, false);
20286
+ if (!cState || !cState.active.some(a => a.state != 0 /* Inactive */))
20287
+ return false;
20288
+ view.dispatch({ effects: closeCompletionEffect.of(null) });
20289
+ return true;
20290
+ };
20291
+ class RunningQuery {
20292
+ constructor(active, context) {
20293
+ this.active = active;
20294
+ this.context = context;
20295
+ this.time = Date.now();
20296
+ this.updates = [];
20297
+ // Note that 'undefined' means 'not done yet', whereas 'null' means
20298
+ // 'query returned null'.
20299
+ this.done = undefined;
20300
+ }
20301
+ }
20302
+ const DebounceTime = 50, MaxUpdateCount = 50, MinAbortTime = 1000;
20303
+ const completionPlugin = /*@__PURE__*/ViewPlugin.fromClass(class {
20304
+ constructor(view) {
20305
+ this.view = view;
20306
+ this.debounceUpdate = -1;
20307
+ this.running = [];
20308
+ this.debounceAccept = -1;
20309
+ this.composing = 0 /* None */;
20310
+ for (let active of view.state.field(completionState).active)
20311
+ if (active.state == 1 /* Pending */)
20312
+ this.startQuery(active);
20313
+ }
20314
+ update(update) {
20315
+ let cState = update.state.field(completionState);
20316
+ if (!update.selectionSet && !update.docChanged && update.startState.field(completionState) == cState)
20317
+ return;
20318
+ let doesReset = update.transactions.some(tr => {
20319
+ return (tr.selection || tr.docChanged) && !getUserEvent(tr);
20320
+ });
20321
+ for (let i = 0; i < this.running.length; i++) {
20322
+ let query = this.running[i];
20323
+ if (doesReset ||
20324
+ query.updates.length + update.transactions.length > MaxUpdateCount && Date.now() - query.time > MinAbortTime) {
20325
+ for (let handler of query.context.abortListeners) {
20326
+ try {
20327
+ handler();
20328
+ }
20329
+ catch (e) {
20330
+ logException(this.view.state, e);
20331
+ }
20332
+ }
20333
+ query.context.abortListeners = null;
20334
+ this.running.splice(i--, 1);
20335
+ }
20336
+ else {
20337
+ query.updates.push(...update.transactions);
20338
+ }
20339
+ }
20340
+ if (this.debounceUpdate > -1)
20341
+ clearTimeout(this.debounceUpdate);
20342
+ this.debounceUpdate = cState.active.some(a => a.state == 1 /* Pending */ && !this.running.some(q => q.active.source == a.source))
20343
+ ? setTimeout(() => this.startUpdate(), DebounceTime) : -1;
20344
+ if (this.composing != 0 /* None */)
20345
+ for (let tr of update.transactions) {
20346
+ if (getUserEvent(tr) == "input")
20347
+ this.composing = 2 /* Changed */;
20348
+ else if (this.composing == 2 /* Changed */ && tr.selection)
20349
+ this.composing = 3 /* ChangedAndMoved */;
20350
+ }
20351
+ }
20352
+ startUpdate() {
20353
+ this.debounceUpdate = -1;
20354
+ let { state } = this.view, cState = state.field(completionState);
20355
+ for (let active of cState.active) {
20356
+ if (active.state == 1 /* Pending */ && !this.running.some(r => r.active.source == active.source))
20357
+ this.startQuery(active);
20358
+ }
20359
+ }
20360
+ startQuery(active) {
20361
+ let { state } = this.view, pos = cur(state);
20362
+ let context = new CompletionContext(state, pos, active.explicitPos == pos);
20363
+ let pending = new RunningQuery(active, context);
20364
+ this.running.push(pending);
20365
+ Promise.resolve(active.source(context)).then(result => {
20366
+ if (!pending.context.aborted) {
20367
+ pending.done = result || null;
20368
+ this.scheduleAccept();
20369
+ }
20370
+ }, err => {
20371
+ this.view.dispatch({ effects: closeCompletionEffect.of(null) });
20372
+ logException(this.view.state, err);
20373
+ });
20374
+ }
20375
+ scheduleAccept() {
20376
+ if (this.running.every(q => q.done !== undefined))
20377
+ this.accept();
20378
+ else if (this.debounceAccept < 0)
20379
+ this.debounceAccept = setTimeout(() => this.accept(), DebounceTime);
20380
+ }
20381
+ // For each finished query in this.running, try to create a result
20382
+ // or, if appropriate, restart the query.
20383
+ accept() {
20384
+ var _a;
20385
+ if (this.debounceAccept > -1)
20386
+ clearTimeout(this.debounceAccept);
20387
+ this.debounceAccept = -1;
20388
+ let updated = [];
20389
+ let conf = this.view.state.facet(completionConfig);
20390
+ for (let i = 0; i < this.running.length; i++) {
20391
+ let query = this.running[i];
20392
+ if (query.done === undefined)
20393
+ continue;
20394
+ this.running.splice(i--, 1);
20395
+ if (query.done) {
20396
+ let active = new ActiveResult(query.active.source, query.active.explicitPos, query.done, query.done.from, (_a = query.done.to) !== null && _a !== void 0 ? _a : cur(query.updates.length ? query.updates[0].startState : this.view.state));
20397
+ // Replay the transactions that happened since the start of
20398
+ // the request and see if that preserves the result
20399
+ for (let tr of query.updates)
20400
+ active = active.update(tr, conf);
20401
+ if (active.hasResult()) {
20402
+ updated.push(active);
20403
+ continue;
20404
+ }
20405
+ }
20406
+ let current = this.view.state.field(completionState).active.find(a => a.source == query.active.source);
20407
+ if (current && current.state == 1 /* Pending */) {
20408
+ if (query.done == null) {
20409
+ // Explicitly failed. Should clear the pending status if it
20410
+ // hasn't been re-set in the meantime.
20411
+ let active = new ActiveSource(query.active.source, 0 /* Inactive */);
20412
+ for (let tr of query.updates)
20413
+ active = active.update(tr, conf);
20414
+ if (active.state != 1 /* Pending */)
20415
+ updated.push(active);
20416
+ }
20417
+ else {
20418
+ // Cleared by subsequent transactions. Restart.
20419
+ this.startQuery(current);
20420
+ }
20421
+ }
20422
+ }
20423
+ if (updated.length)
20424
+ this.view.dispatch({ effects: setActiveEffect.of(updated) });
20425
+ }
20426
+ }, {
20427
+ eventHandlers: {
20428
+ blur() {
20429
+ let state = this.view.state.field(completionState, false);
20430
+ if (state && state.tooltip && this.view.state.facet(completionConfig).closeOnBlur)
20431
+ this.view.dispatch({ effects: closeCompletionEffect.of(null) });
20432
+ },
20433
+ compositionstart() {
20434
+ this.composing = 1 /* Started */;
20435
+ },
20436
+ compositionend() {
20437
+ if (this.composing == 3 /* ChangedAndMoved */) {
20438
+ // Safari fires compositionend events synchronously, possibly
20439
+ // from inside an update, so dispatch asynchronously to avoid reentrancy
20440
+ setTimeout(() => this.view.dispatch({ effects: startCompletionEffect.of(false) }), 20);
20441
+ }
20442
+ this.composing = 0 /* None */;
20443
+ }
20444
+ }
20445
+ });
20446
+
20447
+ const baseTheme = /*@__PURE__*/EditorView.baseTheme({
20448
+ ".cm-tooltip.cm-tooltip-autocomplete": {
20449
+ "& > ul": {
20450
+ fontFamily: "monospace",
20451
+ whiteSpace: "nowrap",
20452
+ overflow: "hidden auto",
20453
+ maxWidth_fallback: "700px",
20454
+ maxWidth: "min(700px, 95vw)",
20455
+ minWidth: "250px",
20456
+ maxHeight: "10em",
20457
+ listStyle: "none",
20458
+ margin: 0,
20459
+ padding: 0,
20460
+ "& > li": {
20461
+ overflowX: "hidden",
20462
+ textOverflow: "ellipsis",
20463
+ cursor: "pointer",
20464
+ padding: "1px 3px",
20465
+ lineHeight: 1.2
20466
+ },
20467
+ }
20468
+ },
20469
+ "&light .cm-tooltip-autocomplete ul li[aria-selected]": {
20470
+ background: "#17c",
20471
+ color: "white",
20472
+ },
20473
+ "&dark .cm-tooltip-autocomplete ul li[aria-selected]": {
20474
+ background: "#347",
20475
+ color: "white",
20476
+ },
20477
+ ".cm-completionListIncompleteTop:before, .cm-completionListIncompleteBottom:after": {
20478
+ content: '"···"',
20479
+ opacity: 0.5,
20480
+ display: "block",
20481
+ textAlign: "center"
20482
+ },
20483
+ ".cm-tooltip.cm-completionInfo": {
20484
+ position: "absolute",
20485
+ padding: "3px 9px",
20486
+ width: "max-content",
20487
+ maxWidth: "300px",
20488
+ },
20489
+ ".cm-completionInfo.cm-completionInfo-left": { right: "100%" },
20490
+ ".cm-completionInfo.cm-completionInfo-right": { left: "100%" },
20491
+ "&light .cm-snippetField": { backgroundColor: "#00000022" },
20492
+ "&dark .cm-snippetField": { backgroundColor: "#ffffff22" },
20493
+ ".cm-snippetFieldPosition": {
20494
+ verticalAlign: "text-top",
20495
+ width: 0,
20496
+ height: "1.15em",
20497
+ margin: "0 -0.7px -.7em",
20498
+ borderLeft: "1.4px dotted #888"
20499
+ },
20500
+ ".cm-completionMatchedText": {
20501
+ textDecoration: "underline"
20502
+ },
20503
+ ".cm-completionDetail": {
20504
+ marginLeft: "0.5em",
20505
+ fontStyle: "italic"
20506
+ },
20507
+ ".cm-completionIcon": {
20508
+ fontSize: "90%",
20509
+ width: ".8em",
20510
+ display: "inline-block",
20511
+ textAlign: "center",
20512
+ paddingRight: ".6em",
20513
+ opacity: "0.6"
20514
+ },
20515
+ ".cm-completionIcon-function, .cm-completionIcon-method": {
20516
+ "&:after": { content: "'ƒ'" }
20517
+ },
20518
+ ".cm-completionIcon-class": {
20519
+ "&:after": { content: "'○'" }
20520
+ },
20521
+ ".cm-completionIcon-interface": {
20522
+ "&:after": { content: "'◌'" }
20523
+ },
20524
+ ".cm-completionIcon-variable": {
20525
+ "&:after": { content: "'𝑥'" }
20526
+ },
20527
+ ".cm-completionIcon-constant": {
20528
+ "&:after": { content: "'𝐶'" }
20529
+ },
20530
+ ".cm-completionIcon-type": {
20531
+ "&:after": { content: "'𝑡'" }
20532
+ },
20533
+ ".cm-completionIcon-enum": {
20534
+ "&:after": { content: "'∪'" }
20535
+ },
20536
+ ".cm-completionIcon-property": {
20537
+ "&:after": { content: "'□'" }
20538
+ },
20539
+ ".cm-completionIcon-keyword": {
20540
+ "&:after": { content: "'🔑\uFE0E'" } // Disable emoji rendering
20541
+ },
20542
+ ".cm-completionIcon-namespace": {
20543
+ "&:after": { content: "'▢'" }
20544
+ },
20545
+ ".cm-completionIcon-text": {
20546
+ "&:after": { content: "'abc'", fontSize: "50%", verticalAlign: "middle" }
20547
+ }
20548
+ });
20549
+ const closedBracket = /*@__PURE__*/new class extends RangeValue {
20550
+ };
20551
+ closedBracket.startSide = 1;
20552
+ closedBracket.endSide = -1;
20553
+
20554
+ /**
20555
+ Returns an extension that enables autocompletion.
20556
+ */
20557
+ function autocompletion$1(config = {}) {
20558
+ return [
20559
+ completionState,
20560
+ completionConfig.of(config),
20561
+ completionPlugin,
20562
+ completionKeymapExt,
20563
+ baseTheme
20564
+ ];
20565
+ }
20566
+ /**
20567
+ Basic keybindings for autocompletion.
20568
+
20569
+ - Ctrl-Space: [`startCompletion`](https://codemirror.net/6/docs/ref/#autocomplete.startCompletion)
20570
+ - Escape: [`closeCompletion`](https://codemirror.net/6/docs/ref/#autocomplete.closeCompletion)
20571
+ - ArrowDown: [`moveCompletionSelection`](https://codemirror.net/6/docs/ref/#autocomplete.moveCompletionSelection)`(true)`
20572
+ - ArrowUp: [`moveCompletionSelection`](https://codemirror.net/6/docs/ref/#autocomplete.moveCompletionSelection)`(false)`
20573
+ - PageDown: [`moveCompletionSelection`](https://codemirror.net/6/docs/ref/#autocomplete.moveCompletionSelection)`(true, "page")`
20574
+ - PageDown: [`moveCompletionSelection`](https://codemirror.net/6/docs/ref/#autocomplete.moveCompletionSelection)`(true, "page")`
20575
+ - Enter: [`acceptCompletion`](https://codemirror.net/6/docs/ref/#autocomplete.acceptCompletion)
20576
+ */
20577
+ const completionKeymap = [
20578
+ { key: "Ctrl-Space", run: startCompletion },
20579
+ { key: "Escape", run: closeCompletion },
20580
+ { key: "ArrowDown", run: /*@__PURE__*/moveCompletionSelection(true) },
20581
+ { key: "ArrowUp", run: /*@__PURE__*/moveCompletionSelection(false) },
20582
+ { key: "PageDown", run: /*@__PURE__*/moveCompletionSelection(true, "page") },
20583
+ { key: "PageUp", run: /*@__PURE__*/moveCompletionSelection(false, "page") },
20584
+ { key: "Enter", run: acceptCompletion }
20585
+ ];
20586
+ const completionKeymapExt = /*@__PURE__*/Prec.highest(/*@__PURE__*/keymap.computeN([completionConfig], state => state.facet(completionConfig).defaultKeymap ? [completionKeymap] : []));
20587
+
20588
+ var variables = variables => context => {
20589
+ const options = variables.map(v => ({
20590
+ label: v.name,
20591
+ type: 'variable',
20592
+ info: v.info,
20593
+ detail: v.detail
20594
+ }));
20595
+
20596
+ // In most cases, use what is typed before the cursor
20597
+ let nodeBefore = syntaxTree(context.state).resolve(context.pos, -1);
20598
+
20599
+ // For the special case of empty nodes, we need to check the current node
20600
+ // as well. The previous node could be part of another token, e.g.
20601
+ // when typing functions "abs(".
20602
+ let nextNode = nodeBefore.nextSibling;
20603
+ const isInEmptyNode =
20604
+ isNodeEmpty(nodeBefore) ||
20605
+ nextNode && nextNode.from === context.pos && isNodeEmpty(nextNode);
20606
+
20607
+ if (context.explicit && isInEmptyNode) {
20608
+ return {
20609
+ from: context.pos,
20610
+ options: options
20611
+ };
20612
+ }
20613
+
20614
+ const result = {
20615
+ from: nodeBefore.from,
20616
+ options: options
20617
+ };
20618
+
20619
+ // Only auto-complete variables
20620
+ if (nodeBefore.name !== 'VariableName') {
20621
+ return null;
20622
+ }
20623
+
20624
+ return result;
20625
+ };
20626
+
20627
+
20628
+ // helpers ///////////////////////////////
20629
+
20630
+ function isNodeEmpty(node) {
20631
+ return node.from === node.to;
20632
+ }
20633
+
20634
+ function autocompletion(context) {
20635
+ return [
20636
+ autocompletion$1({
20637
+ override: [
20638
+ variables(context),
20639
+ ]
20640
+ })
20641
+ ];
20642
+ }
19447
20643
 
19448
20644
  /**
19449
20645
  * Creates a FEEL editor in the supplied container
@@ -19458,6 +20654,7 @@ var theme = [ baseTheme, highlightTheme, syntaxClasses ];
19458
20654
  */
19459
20655
  function FeelEditor({
19460
20656
  container,
20657
+ variables = [],
19461
20658
  onChange = () => {},
19462
20659
  onKeyDown = () => {},
19463
20660
  value = '',
@@ -19483,6 +20680,7 @@ function FeelEditor({
19483
20680
  changeHandler,
19484
20681
  keyHandler,
19485
20682
  language(),
20683
+ autocompletion(variables),
19486
20684
  theme,
19487
20685
  linter
19488
20686
  ];