@uiw/react-codemirror 4.21.3 → 4.21.4

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.
@@ -1652,7 +1652,7 @@ class FuzzyMatcher {
1652
1652
  // is. See `Penalty` above.
1653
1653
  match(word) {
1654
1654
  if (this.pattern.length == 0)
1655
- return [-100 /* Penalty.NotFull */];
1655
+ return [-100 /* NotFull */];
1656
1656
  if (word.length < this.pattern.length)
1657
1657
  return null;
1658
1658
  let { chars, folded, any, precise, byWord } = this;
@@ -1660,17 +1660,17 @@ class FuzzyMatcher {
1660
1660
  // at the start
1661
1661
  if (chars.length == 1) {
1662
1662
  let first = (0,state_.codePointAt)(word, 0), firstSize = (0,state_.codePointSize)(first);
1663
- let score = firstSize == word.length ? 0 : -100 /* Penalty.NotFull */;
1663
+ let score = firstSize == word.length ? 0 : -100 /* NotFull */;
1664
1664
  if (first == chars[0]) ;
1665
1665
  else if (first == folded[0])
1666
- score += -200 /* Penalty.CaseFold */;
1666
+ score += -200 /* CaseFold */;
1667
1667
  else
1668
1668
  return null;
1669
1669
  return [score, 0, firstSize];
1670
1670
  }
1671
1671
  let direct = word.indexOf(this.pattern);
1672
1672
  if (direct == 0)
1673
- return [word.length == this.pattern.length ? 0 : -100 /* Penalty.NotFull */, 0, this.pattern.length];
1673
+ return [word.length == this.pattern.length ? 0 : -100 /* NotFull */, 0, this.pattern.length];
1674
1674
  let len = chars.length, anyTo = 0;
1675
1675
  if (direct < 0) {
1676
1676
  for (let i = 0, e = Math.min(word.length, 200); i < e && anyTo < len;) {
@@ -1694,7 +1694,7 @@ class FuzzyMatcher {
1694
1694
  let adjacentTo = 0, adjacentStart = -1, adjacentEnd = -1;
1695
1695
  let hasLower = /[a-z]/.test(word), wordAdjacent = true;
1696
1696
  // Go over the option's text, scanning for the various kinds of matches
1697
- for (let i = 0, e = Math.min(word.length, 200), prevType = 0 /* Tp.NonWord */; i < e && byWordTo < len;) {
1697
+ for (let i = 0, e = Math.min(word.length, 200), prevType = 0 /* NonWord */; i < e && byWordTo < len;) {
1698
1698
  let next = (0,state_.codePointAt)(word, i);
1699
1699
  if (direct < 0) {
1700
1700
  if (preciseTo < len && next == chars[preciseTo])
@@ -1712,9 +1712,9 @@ class FuzzyMatcher {
1712
1712
  }
1713
1713
  }
1714
1714
  let ch, type = next < 0xff
1715
- ? (next >= 48 && next <= 57 || next >= 97 && next <= 122 ? 2 /* Tp.Lower */ : next >= 65 && next <= 90 ? 1 /* Tp.Upper */ : 0 /* Tp.NonWord */)
1716
- : ((ch = (0,state_.fromCodePoint)(next)) != ch.toLowerCase() ? 1 /* Tp.Upper */ : ch != ch.toUpperCase() ? 2 /* Tp.Lower */ : 0 /* Tp.NonWord */);
1717
- if (!i || type == 1 /* Tp.Upper */ && hasLower || prevType == 0 /* Tp.NonWord */ && type != 0 /* Tp.NonWord */) {
1715
+ ? (next >= 48 && next <= 57 || next >= 97 && next <= 122 ? 2 /* Lower */ : next >= 65 && next <= 90 ? 1 /* Upper */ : 0 /* NonWord */)
1716
+ : ((ch = (0,state_.fromCodePoint)(next)) != ch.toLowerCase() ? 1 /* Upper */ : ch != ch.toUpperCase() ? 2 /* Lower */ : 0 /* NonWord */);
1717
+ if (!i || type == 1 /* Upper */ && hasLower || prevType == 0 /* NonWord */ && type != 0 /* NonWord */) {
1718
1718
  if (chars[byWordTo] == next || (folded[byWordTo] == next && (byWordFolded = true)))
1719
1719
  byWord[byWordTo++] = i;
1720
1720
  else if (byWord.length)
@@ -1724,17 +1724,17 @@ class FuzzyMatcher {
1724
1724
  i += (0,state_.codePointSize)(next);
1725
1725
  }
1726
1726
  if (byWordTo == len && byWord[0] == 0 && wordAdjacent)
1727
- return this.result(-100 /* Penalty.ByWord */ + (byWordFolded ? -200 /* Penalty.CaseFold */ : 0), byWord, word);
1727
+ return this.result(-100 /* ByWord */ + (byWordFolded ? -200 /* CaseFold */ : 0), byWord, word);
1728
1728
  if (adjacentTo == len && adjacentStart == 0)
1729
- return [-200 /* Penalty.CaseFold */ - word.length + (adjacentEnd == word.length ? 0 : -100 /* Penalty.NotFull */), 0, adjacentEnd];
1729
+ return [-200 /* CaseFold */ - word.length + (adjacentEnd == word.length ? 0 : -100 /* NotFull */), 0, adjacentEnd];
1730
1730
  if (direct > -1)
1731
- return [-700 /* Penalty.NotStart */ - word.length, direct, direct + this.pattern.length];
1731
+ return [-700 /* NotStart */ - word.length, direct, direct + this.pattern.length];
1732
1732
  if (adjacentTo == len)
1733
- return [-200 /* Penalty.CaseFold */ + -700 /* Penalty.NotStart */ - word.length, adjacentStart, adjacentEnd];
1733
+ return [-200 /* CaseFold */ + -700 /* NotStart */ - word.length, adjacentStart, adjacentEnd];
1734
1734
  if (byWordTo == len)
1735
- return this.result(-100 /* Penalty.ByWord */ + (byWordFolded ? -200 /* Penalty.CaseFold */ : 0) + -700 /* Penalty.NotStart */ +
1736
- (wordAdjacent ? 0 : -1100 /* Penalty.Gap */), byWord, word);
1737
- return chars.length == 2 ? null : this.result((any[0] ? -700 /* Penalty.NotStart */ : 0) + -200 /* Penalty.CaseFold */ + -1100 /* Penalty.Gap */, any, word);
1735
+ return this.result(-100 /* ByWord */ + (byWordFolded ? -200 /* CaseFold */ : 0) + -700 /* NotStart */ +
1736
+ (wordAdjacent ? 0 : -1100 /* Gap */), byWord, word);
1737
+ return chars.length == 2 ? null : this.result((any[0] ? -700 /* NotStart */ : 0) + -200 /* CaseFold */ + -1100 /* Gap */, any, word);
1738
1738
  }
1739
1739
  result(score, positions, word) {
1740
1740
  let result = [score - word.length], i = 1;
@@ -1792,11 +1792,11 @@ function defaultPositionInfo(view, list, option, info, space) {
1792
1792
  left = true;
1793
1793
  if (infoWidth <= (left ? spaceLeft : spaceRight)) {
1794
1794
  offset = Math.max(space.top, Math.min(option.top, space.bottom - infoHeight)) - list.top;
1795
- maxWidth = Math.min(400 /* Info.Width */, left ? spaceLeft : spaceRight);
1795
+ maxWidth = Math.min(400 /* Width */, left ? spaceLeft : spaceRight);
1796
1796
  }
1797
1797
  else {
1798
1798
  narrow = true;
1799
- maxWidth = Math.min(400 /* Info.Width */, (rtl ? list.right : space.right - list.left) - 30 /* Info.Margin */);
1799
+ maxWidth = Math.min(400 /* Width */, (rtl ? list.right : space.right - list.left) - 30 /* Margin */);
1800
1800
  let spaceBelow = space.bottom - list.bottom;
1801
1801
  if (spaceBelow >= infoHeight || spaceBelow > list.top) { // Below the completion
1802
1802
  offset = option.bottom - list.top;
@@ -1812,232 +1812,6 @@ function defaultPositionInfo(view, list, option, info, space) {
1812
1812
  };
1813
1813
  }
1814
1814
 
1815
- /**
1816
- Returns a command that moves the completion selection forward or
1817
- backward by the given amount.
1818
- */
1819
- function moveCompletionSelection(forward, by = "option") {
1820
- return (view) => {
1821
- let cState = view.state.field(completionState, false);
1822
- if (!cState || !cState.open || cState.open.disabled ||
1823
- Date.now() - cState.open.timestamp < view.state.facet(completionConfig).interactionDelay)
1824
- return false;
1825
- let step = 1, tooltip;
1826
- if (by == "page" && (tooltip = (0,view_.getTooltip)(view, cState.open.tooltip)))
1827
- step = Math.max(2, Math.floor(tooltip.dom.offsetHeight /
1828
- tooltip.dom.querySelector("li").offsetHeight) - 1);
1829
- let { length } = cState.open.options;
1830
- let selected = cState.open.selected > -1 ? cState.open.selected + step * (forward ? 1 : -1) : forward ? 0 : length - 1;
1831
- if (selected < 0)
1832
- selected = by == "page" ? 0 : length - 1;
1833
- else if (selected >= length)
1834
- selected = by == "page" ? length - 1 : 0;
1835
- view.dispatch({ effects: setSelectedEffect.of(selected) });
1836
- return true;
1837
- };
1838
- }
1839
- /**
1840
- Accept the current completion.
1841
- */
1842
- const acceptCompletion = (view) => {
1843
- let cState = view.state.field(completionState, false);
1844
- if (view.state.readOnly || !cState || !cState.open || cState.open.selected < 0 ||
1845
- Date.now() - cState.open.timestamp < view.state.facet(completionConfig).interactionDelay)
1846
- return false;
1847
- if (!cState.open.disabled)
1848
- return applyCompletion(view, cState.open.options[cState.open.selected]);
1849
- return true;
1850
- };
1851
- /**
1852
- Explicitly start autocompletion.
1853
- */
1854
- const startCompletion = (view) => {
1855
- let cState = view.state.field(completionState, false);
1856
- if (!cState)
1857
- return false;
1858
- view.dispatch({ effects: startCompletionEffect.of(true) });
1859
- return true;
1860
- };
1861
- /**
1862
- Close the currently active completion.
1863
- */
1864
- const closeCompletion = (view) => {
1865
- let cState = view.state.field(completionState, false);
1866
- if (!cState || !cState.active.some(a => a.state != 0 /* State.Inactive */))
1867
- return false;
1868
- view.dispatch({ effects: closeCompletionEffect.of(null) });
1869
- return true;
1870
- };
1871
- class RunningQuery {
1872
- constructor(active, context) {
1873
- this.active = active;
1874
- this.context = context;
1875
- this.time = Date.now();
1876
- this.updates = [];
1877
- // Note that 'undefined' means 'not done yet', whereas 'null' means
1878
- // 'query returned null'.
1879
- this.done = undefined;
1880
- }
1881
- }
1882
- const DebounceTime = 50, MaxUpdateCount = 50, MinAbortTime = 1000;
1883
- const completionPlugin = /*@__PURE__*/view_.ViewPlugin.fromClass(class {
1884
- constructor(view) {
1885
- this.view = view;
1886
- this.debounceUpdate = -1;
1887
- this.running = [];
1888
- this.debounceAccept = -1;
1889
- this.composing = 0 /* CompositionState.None */;
1890
- for (let active of view.state.field(completionState).active)
1891
- if (active.state == 1 /* State.Pending */)
1892
- this.startQuery(active);
1893
- }
1894
- update(update) {
1895
- let cState = update.state.field(completionState);
1896
- if (!update.selectionSet && !update.docChanged && update.startState.field(completionState) == cState)
1897
- return;
1898
- let doesReset = update.transactions.some(tr => {
1899
- return (tr.selection || tr.docChanged) && !getUserEvent(tr);
1900
- });
1901
- for (let i = 0; i < this.running.length; i++) {
1902
- let query = this.running[i];
1903
- if (doesReset ||
1904
- query.updates.length + update.transactions.length > MaxUpdateCount && Date.now() - query.time > MinAbortTime) {
1905
- for (let handler of query.context.abortListeners) {
1906
- try {
1907
- handler();
1908
- }
1909
- catch (e) {
1910
- (0,view_.logException)(this.view.state, e);
1911
- }
1912
- }
1913
- query.context.abortListeners = null;
1914
- this.running.splice(i--, 1);
1915
- }
1916
- else {
1917
- query.updates.push(...update.transactions);
1918
- }
1919
- }
1920
- if (this.debounceUpdate > -1)
1921
- clearTimeout(this.debounceUpdate);
1922
- this.debounceUpdate = cState.active.some(a => a.state == 1 /* State.Pending */ && !this.running.some(q => q.active.source == a.source))
1923
- ? setTimeout(() => this.startUpdate(), DebounceTime) : -1;
1924
- if (this.composing != 0 /* CompositionState.None */)
1925
- for (let tr of update.transactions) {
1926
- if (getUserEvent(tr) == "input")
1927
- this.composing = 2 /* CompositionState.Changed */;
1928
- else if (this.composing == 2 /* CompositionState.Changed */ && tr.selection)
1929
- this.composing = 3 /* CompositionState.ChangedAndMoved */;
1930
- }
1931
- }
1932
- startUpdate() {
1933
- this.debounceUpdate = -1;
1934
- let { state } = this.view, cState = state.field(completionState);
1935
- for (let active of cState.active) {
1936
- if (active.state == 1 /* State.Pending */ && !this.running.some(r => r.active.source == active.source))
1937
- this.startQuery(active);
1938
- }
1939
- }
1940
- startQuery(active) {
1941
- let { state } = this.view, pos = cur(state);
1942
- let context = new CompletionContext(state, pos, active.explicitPos == pos);
1943
- let pending = new RunningQuery(active, context);
1944
- this.running.push(pending);
1945
- Promise.resolve(active.source(context)).then(result => {
1946
- if (!pending.context.aborted) {
1947
- pending.done = result || null;
1948
- this.scheduleAccept();
1949
- }
1950
- }, err => {
1951
- this.view.dispatch({ effects: closeCompletionEffect.of(null) });
1952
- (0,view_.logException)(this.view.state, err);
1953
- });
1954
- }
1955
- scheduleAccept() {
1956
- if (this.running.every(q => q.done !== undefined))
1957
- this.accept();
1958
- else if (this.debounceAccept < 0)
1959
- this.debounceAccept = setTimeout(() => this.accept(), DebounceTime);
1960
- }
1961
- // For each finished query in this.running, try to create a result
1962
- // or, if appropriate, restart the query.
1963
- accept() {
1964
- var _a;
1965
- if (this.debounceAccept > -1)
1966
- clearTimeout(this.debounceAccept);
1967
- this.debounceAccept = -1;
1968
- let updated = [];
1969
- let conf = this.view.state.facet(completionConfig);
1970
- for (let i = 0; i < this.running.length; i++) {
1971
- let query = this.running[i];
1972
- if (query.done === undefined)
1973
- continue;
1974
- this.running.splice(i--, 1);
1975
- if (query.done) {
1976
- 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));
1977
- // Replay the transactions that happened since the start of
1978
- // the request and see if that preserves the result
1979
- for (let tr of query.updates)
1980
- active = active.update(tr, conf);
1981
- if (active.hasResult()) {
1982
- updated.push(active);
1983
- continue;
1984
- }
1985
- }
1986
- let current = this.view.state.field(completionState).active.find(a => a.source == query.active.source);
1987
- if (current && current.state == 1 /* State.Pending */) {
1988
- if (query.done == null) {
1989
- // Explicitly failed. Should clear the pending status if it
1990
- // hasn't been re-set in the meantime.
1991
- let active = new ActiveSource(query.active.source, 0 /* State.Inactive */);
1992
- for (let tr of query.updates)
1993
- active = active.update(tr, conf);
1994
- if (active.state != 1 /* State.Pending */)
1995
- updated.push(active);
1996
- }
1997
- else {
1998
- // Cleared by subsequent transactions. Restart.
1999
- this.startQuery(current);
2000
- }
2001
- }
2002
- }
2003
- if (updated.length)
2004
- this.view.dispatch({ effects: setActiveEffect.of(updated) });
2005
- }
2006
- }, {
2007
- eventHandlers: {
2008
- blur(event) {
2009
- let state = this.view.state.field(completionState, false);
2010
- if (state && state.tooltip && this.view.state.facet(completionConfig).closeOnBlur) {
2011
- let dialog = state.open && (0,view_.getTooltip)(this.view, state.open.tooltip);
2012
- if (!dialog || !dialog.dom.contains(event.relatedTarget))
2013
- this.view.dispatch({ effects: closeCompletionEffect.of(null) });
2014
- }
2015
- },
2016
- compositionstart() {
2017
- this.composing = 1 /* CompositionState.Started */;
2018
- },
2019
- compositionend() {
2020
- if (this.composing == 3 /* CompositionState.ChangedAndMoved */) {
2021
- // Safari fires compositionend events synchronously, possibly
2022
- // from inside an update, so dispatch asynchronously to avoid reentrancy
2023
- setTimeout(() => this.view.dispatch({ effects: startCompletionEffect.of(false) }), 20);
2024
- }
2025
- this.composing = 0 /* CompositionState.None */;
2026
- }
2027
- }
2028
- });
2029
- function applyCompletion(view, option) {
2030
- const apply = option.completion.apply || option.completion.label;
2031
- let result = view.state.field(completionState).active.find(a => a.source == option.source);
2032
- if (!(result instanceof ActiveResult))
2033
- return false;
2034
- if (typeof apply == "string")
2035
- view.dispatch(Object.assign(Object.assign({}, insertCompletionText(view.state, apply, result.from, result.to)), { annotations: pickedCompletion.of(option.completion) }));
2036
- else
2037
- apply(view, option.completion, result.from, result.to);
2038
- return true;
2039
- }
2040
-
2041
1815
  function optionContent(config) {
2042
1816
  let content = config.addToOptions.slice();
2043
1817
  if (config.icons)
@@ -2097,10 +1871,12 @@ function rangeAroundSelected(total, selected, max) {
2097
1871
  return { from: total - (off + 1) * max, to: total - off * max };
2098
1872
  }
2099
1873
  class CompletionTooltip {
2100
- constructor(view, stateField) {
1874
+ constructor(view, stateField, applyCompletion) {
2101
1875
  this.view = view;
2102
1876
  this.stateField = stateField;
1877
+ this.applyCompletion = applyCompletion;
2103
1878
  this.info = null;
1879
+ this.infoDestroy = null;
2104
1880
  this.placeInfoReq = {
2105
1881
  read: () => this.measureInfo(),
2106
1882
  write: (pos) => this.placeInfo(pos),
@@ -2121,7 +1897,7 @@ class CompletionTooltip {
2121
1897
  this.dom.addEventListener("mousedown", (e) => {
2122
1898
  for (let dom = e.target, match; dom && dom != this.dom; dom = dom.parentNode) {
2123
1899
  if (dom.nodeName == "LI" && (match = /-(\d+)$/.exec(dom.id)) && +match[1] < options.length) {
2124
- applyCompletion(view, options[+match[1]]);
1900
+ this.applyCompletion(view, options[+match[1]]);
2125
1901
  e.preventDefault();
2126
1902
  return;
2127
1903
  }
@@ -2180,33 +1956,39 @@ class CompletionTooltip {
2180
1956
  });
2181
1957
  }
2182
1958
  if (this.updateSelectedOption(open.selected)) {
2183
- if (this.info) {
2184
- this.info.remove();
2185
- this.info = null;
2186
- }
1959
+ this.destroyInfo();
2187
1960
  let { completion } = open.options[open.selected];
2188
1961
  let { info } = completion;
2189
1962
  if (!info)
2190
1963
  return;
2191
- let infoResult = typeof info === 'string' ? document.createTextNode(info) : info(completion);
1964
+ let infoResult = typeof info === "string" ? document.createTextNode(info) : info(completion);
2192
1965
  if (!infoResult)
2193
1966
  return;
2194
- if ('then' in infoResult) {
2195
- infoResult.then(node => {
2196
- if (node && this.view.state.field(this.stateField, false) == cState)
2197
- this.addInfoPane(node);
1967
+ if ("then" in infoResult) {
1968
+ infoResult.then(obj => {
1969
+ if (obj && this.view.state.field(this.stateField, false) == cState)
1970
+ this.addInfoPane(obj, completion);
2198
1971
  }).catch(e => (0,view_.logException)(this.view.state, e, "completion info"));
2199
1972
  }
2200
1973
  else {
2201
- this.addInfoPane(infoResult);
1974
+ this.addInfoPane(infoResult, completion);
2202
1975
  }
2203
1976
  }
2204
1977
  }
2205
- addInfoPane(content) {
2206
- let dom = this.info = document.createElement("div");
2207
- dom.className = "cm-tooltip cm-completionInfo";
2208
- dom.appendChild(content);
2209
- this.dom.appendChild(dom);
1978
+ addInfoPane(content, completion) {
1979
+ this.destroyInfo();
1980
+ let wrap = this.info = document.createElement("div");
1981
+ wrap.className = "cm-tooltip cm-completionInfo";
1982
+ if (content.nodeType != null) {
1983
+ wrap.appendChild(content);
1984
+ this.infoDestroy = null;
1985
+ }
1986
+ else {
1987
+ let { dom, destroy } = content;
1988
+ wrap.appendChild(dom);
1989
+ this.infoDestroy = destroy || null;
1990
+ }
1991
+ this.dom.appendChild(wrap);
2210
1992
  this.view.requestMeasure(this.placeInfoReq);
2211
1993
  }
2212
1994
  updateSelectedOption(selected) {
@@ -2299,11 +2081,22 @@ class CompletionTooltip {
2299
2081
  ul.classList.add("cm-completionListIncompleteBottom");
2300
2082
  return ul;
2301
2083
  }
2084
+ destroyInfo() {
2085
+ if (this.info) {
2086
+ if (this.infoDestroy)
2087
+ this.infoDestroy();
2088
+ this.info.remove();
2089
+ this.info = null;
2090
+ }
2091
+ }
2092
+ destroy() {
2093
+ this.destroyInfo();
2094
+ }
2302
2095
  }
2303
2096
  // We allocate a new function instance every time the completion
2304
2097
  // changes to force redrawing/repositioning of the tooltip
2305
- function completionTooltip(stateField) {
2306
- return (view) => new CompletionTooltip(view, stateField);
2098
+ function completionTooltip(stateField, applyCompletion) {
2099
+ return (view) => new CompletionTooltip(view, stateField, applyCompletion);
2307
2100
  }
2308
2101
  function scrollIntoView(container, element) {
2309
2102
  let parent = container.getBoundingClientRect();
@@ -2396,7 +2189,7 @@ class CompletionDialog {
2396
2189
  static build(active, state, id, prev, conf) {
2397
2190
  let options = sortOptions(active, state);
2398
2191
  if (!options.length) {
2399
- return prev && active.some(a => a.state == 1 /* State.Pending */) ?
2192
+ return prev && active.some(a => a.state == 1 /* Pending */) ?
2400
2193
  new CompletionDialog(prev.options, prev.attrs, prev.tooltip, prev.timestamp, prev.selected, true) : null;
2401
2194
  }
2402
2195
  let selected = state.facet(completionConfig).selectOnOpen ? 0 : -1;
@@ -2410,7 +2203,7 @@ class CompletionDialog {
2410
2203
  }
2411
2204
  return new CompletionDialog(options, makeAttrs(id, selected), {
2412
2205
  pos: active.reduce((a, b) => b.hasResult() ? Math.min(a, b.from) : a, 1e8),
2413
- create: completionTooltip(completionState),
2206
+ create: completionTooltip(completionState, applyCompletion),
2414
2207
  above: conf.aboveCursor,
2415
2208
  }, prev ? prev.timestamp : Date.now(), selected, false);
2416
2209
  }
@@ -2433,7 +2226,7 @@ class CompletionState {
2433
2226
  state.languageDataAt("autocomplete", cur(state)).map(asSource);
2434
2227
  let active = sources.map(source => {
2435
2228
  let value = this.active.find(s => s.source == source) ||
2436
- new ActiveSource(source, this.active.some(a => a.state != 0 /* State.Inactive */) ? 1 /* State.Pending */ : 0 /* State.Inactive */);
2229
+ new ActiveSource(source, this.active.some(a => a.state != 0 /* Inactive */) ? 1 /* Pending */ : 0 /* Inactive */);
2437
2230
  return value.update(tr, conf);
2438
2231
  });
2439
2232
  if (active.length == this.active.length && active.every((a, i) => a == this.active[i]))
@@ -2444,10 +2237,10 @@ class CompletionState {
2444
2237
  if (tr.selection || active.some(a => a.hasResult() && tr.changes.touchesRange(a.from, a.to)) ||
2445
2238
  !sameResults(active, this.active))
2446
2239
  open = CompletionDialog.build(active, state, this.id, open, conf);
2447
- else if (open && open.disabled && !active.some(a => a.state == 1 /* State.Pending */))
2240
+ else if (open && open.disabled && !active.some(a => a.state == 1 /* Pending */))
2448
2241
  open = null;
2449
- if (!open && active.every(a => a.state != 1 /* State.Pending */) && active.some(a => a.hasResult()))
2450
- active = active.map(a => a.hasResult() ? new ActiveSource(a.source, 0 /* State.Inactive */) : a);
2242
+ if (!open && active.every(a => a.state != 1 /* Pending */) && active.some(a => a.hasResult()))
2243
+ active = active.map(a => a.hasResult() ? new ActiveSource(a.source, 0 /* Inactive */) : a);
2451
2244
  for (let effect of tr.effects)
2452
2245
  if (effect.is(setSelectedEffect))
2453
2246
  open = open && open.setSelected(effect.value, this.id);
@@ -2501,13 +2294,13 @@ class ActiveSource {
2501
2294
  value = value.handleUserEvent(tr, event, conf);
2502
2295
  else if (tr.docChanged)
2503
2296
  value = value.handleChange(tr);
2504
- else if (tr.selection && value.state != 0 /* State.Inactive */)
2505
- value = new ActiveSource(value.source, 0 /* State.Inactive */);
2297
+ else if (tr.selection && value.state != 0 /* Inactive */)
2298
+ value = new ActiveSource(value.source, 0 /* Inactive */);
2506
2299
  for (let effect of tr.effects) {
2507
2300
  if (effect.is(startCompletionEffect))
2508
- value = new ActiveSource(value.source, 1 /* State.Pending */, effect.value ? cur(tr.state) : -1);
2301
+ value = new ActiveSource(value.source, 1 /* Pending */, effect.value ? cur(tr.state) : -1);
2509
2302
  else if (effect.is(closeCompletionEffect))
2510
- value = new ActiveSource(value.source, 0 /* State.Inactive */);
2303
+ value = new ActiveSource(value.source, 0 /* Inactive */);
2511
2304
  else if (effect.is(setActiveEffect))
2512
2305
  for (let active of effect.value)
2513
2306
  if (active.source == value.source)
@@ -2516,10 +2309,10 @@ class ActiveSource {
2516
2309
  return value;
2517
2310
  }
2518
2311
  handleUserEvent(tr, type, conf) {
2519
- return type == "delete" || !conf.activateOnTyping ? this.map(tr.changes) : new ActiveSource(this.source, 1 /* State.Pending */);
2312
+ return type == "delete" || !conf.activateOnTyping ? this.map(tr.changes) : new ActiveSource(this.source, 1 /* Pending */);
2520
2313
  }
2521
2314
  handleChange(tr) {
2522
- return tr.changes.touchesRange(cur(tr.startState)) ? new ActiveSource(this.source, 0 /* State.Inactive */) : this.map(tr.changes);
2315
+ return tr.changes.touchesRange(cur(tr.startState)) ? new ActiveSource(this.source, 0 /* Inactive */) : this.map(tr.changes);
2523
2316
  }
2524
2317
  map(changes) {
2525
2318
  return changes.empty || this.explicitPos < 0 ? this : new ActiveSource(this.source, this.state, changes.mapPos(this.explicitPos));
@@ -2527,7 +2320,7 @@ class ActiveSource {
2527
2320
  }
2528
2321
  class ActiveResult extends ActiveSource {
2529
2322
  constructor(source, explicitPos, result, from, to) {
2530
- super(source, 2 /* State.Result */, explicitPos);
2323
+ super(source, 2 /* Result */, explicitPos);
2531
2324
  this.result = result;
2532
2325
  this.from = from;
2533
2326
  this.to = to;
@@ -2540,17 +2333,17 @@ class ActiveResult extends ActiveSource {
2540
2333
  if ((this.explicitPos < 0 ? pos <= from : pos < this.from) ||
2541
2334
  pos > to ||
2542
2335
  type == "delete" && cur(tr.startState) == this.from)
2543
- return new ActiveSource(this.source, type == "input" && conf.activateOnTyping ? 1 /* State.Pending */ : 0 /* State.Inactive */);
2336
+ return new ActiveSource(this.source, type == "input" && conf.activateOnTyping ? 1 /* Pending */ : 0 /* Inactive */);
2544
2337
  let explicitPos = this.explicitPos < 0 ? -1 : tr.changes.mapPos(this.explicitPos), updated;
2545
2338
  if (checkValid(this.result.validFor, tr.state, from, to))
2546
2339
  return new ActiveResult(this.source, explicitPos, this.result, from, to);
2547
2340
  if (this.result.update &&
2548
2341
  (updated = this.result.update(this.result, from, to, new CompletionContext(tr.state, pos, explicitPos >= 0))))
2549
2342
  return new ActiveResult(this.source, explicitPos, updated, updated.from, (_a = updated.to) !== null && _a !== void 0 ? _a : cur(tr.state));
2550
- return new ActiveSource(this.source, 1 /* State.Pending */, explicitPos);
2343
+ return new ActiveSource(this.source, 1 /* Pending */, explicitPos);
2551
2344
  }
2552
2345
  handleChange(tr) {
2553
- return tr.changes.touchesRange(this.from, this.to) ? new ActiveSource(this.source, 0 /* State.Inactive */) : this.map(tr.changes);
2346
+ return tr.changes.touchesRange(this.from, this.to) ? new ActiveSource(this.source, 0 /* Inactive */) : this.map(tr.changes);
2554
2347
  }
2555
2348
  map(mapping) {
2556
2349
  return mapping.empty ? this :
@@ -2575,6 +2368,232 @@ const completionState = /*@__PURE__*/state_.StateField.define({
2575
2368
  view_.EditorView.contentAttributes.from(f, state => state.attrs)
2576
2369
  ]
2577
2370
  });
2371
+ function applyCompletion(view, option) {
2372
+ const apply = option.completion.apply || option.completion.label;
2373
+ let result = view.state.field(completionState).active.find(a => a.source == option.source);
2374
+ if (!(result instanceof ActiveResult))
2375
+ return false;
2376
+ if (typeof apply == "string")
2377
+ view.dispatch(Object.assign(Object.assign({}, insertCompletionText(view.state, apply, result.from, result.to)), { annotations: pickedCompletion.of(option.completion) }));
2378
+ else
2379
+ apply(view, option.completion, result.from, result.to);
2380
+ return true;
2381
+ }
2382
+
2383
+ /**
2384
+ Returns a command that moves the completion selection forward or
2385
+ backward by the given amount.
2386
+ */
2387
+ function moveCompletionSelection(forward, by = "option") {
2388
+ return (view) => {
2389
+ let cState = view.state.field(completionState, false);
2390
+ if (!cState || !cState.open || cState.open.disabled ||
2391
+ Date.now() - cState.open.timestamp < view.state.facet(completionConfig).interactionDelay)
2392
+ return false;
2393
+ let step = 1, tooltip;
2394
+ if (by == "page" && (tooltip = (0,view_.getTooltip)(view, cState.open.tooltip)))
2395
+ step = Math.max(2, Math.floor(tooltip.dom.offsetHeight /
2396
+ tooltip.dom.querySelector("li").offsetHeight) - 1);
2397
+ let { length } = cState.open.options;
2398
+ let selected = cState.open.selected > -1 ? cState.open.selected + step * (forward ? 1 : -1) : forward ? 0 : length - 1;
2399
+ if (selected < 0)
2400
+ selected = by == "page" ? 0 : length - 1;
2401
+ else if (selected >= length)
2402
+ selected = by == "page" ? length - 1 : 0;
2403
+ view.dispatch({ effects: setSelectedEffect.of(selected) });
2404
+ return true;
2405
+ };
2406
+ }
2407
+ /**
2408
+ Accept the current completion.
2409
+ */
2410
+ const acceptCompletion = (view) => {
2411
+ let cState = view.state.field(completionState, false);
2412
+ if (view.state.readOnly || !cState || !cState.open || cState.open.selected < 0 ||
2413
+ Date.now() - cState.open.timestamp < view.state.facet(completionConfig).interactionDelay)
2414
+ return false;
2415
+ if (!cState.open.disabled)
2416
+ return applyCompletion(view, cState.open.options[cState.open.selected]);
2417
+ return true;
2418
+ };
2419
+ /**
2420
+ Explicitly start autocompletion.
2421
+ */
2422
+ const startCompletion = (view) => {
2423
+ let cState = view.state.field(completionState, false);
2424
+ if (!cState)
2425
+ return false;
2426
+ view.dispatch({ effects: startCompletionEffect.of(true) });
2427
+ return true;
2428
+ };
2429
+ /**
2430
+ Close the currently active completion.
2431
+ */
2432
+ const closeCompletion = (view) => {
2433
+ let cState = view.state.field(completionState, false);
2434
+ if (!cState || !cState.active.some(a => a.state != 0 /* Inactive */))
2435
+ return false;
2436
+ view.dispatch({ effects: closeCompletionEffect.of(null) });
2437
+ return true;
2438
+ };
2439
+ class RunningQuery {
2440
+ constructor(active, context) {
2441
+ this.active = active;
2442
+ this.context = context;
2443
+ this.time = Date.now();
2444
+ this.updates = [];
2445
+ // Note that 'undefined' means 'not done yet', whereas 'null' means
2446
+ // 'query returned null'.
2447
+ this.done = undefined;
2448
+ }
2449
+ }
2450
+ const DebounceTime = 50, MaxUpdateCount = 50, MinAbortTime = 1000;
2451
+ const completionPlugin = /*@__PURE__*/view_.ViewPlugin.fromClass(class {
2452
+ constructor(view) {
2453
+ this.view = view;
2454
+ this.debounceUpdate = -1;
2455
+ this.running = [];
2456
+ this.debounceAccept = -1;
2457
+ this.composing = 0 /* None */;
2458
+ for (let active of view.state.field(completionState).active)
2459
+ if (active.state == 1 /* Pending */)
2460
+ this.startQuery(active);
2461
+ }
2462
+ update(update) {
2463
+ let cState = update.state.field(completionState);
2464
+ if (!update.selectionSet && !update.docChanged && update.startState.field(completionState) == cState)
2465
+ return;
2466
+ let doesReset = update.transactions.some(tr => {
2467
+ return (tr.selection || tr.docChanged) && !getUserEvent(tr);
2468
+ });
2469
+ for (let i = 0; i < this.running.length; i++) {
2470
+ let query = this.running[i];
2471
+ if (doesReset ||
2472
+ query.updates.length + update.transactions.length > MaxUpdateCount && Date.now() - query.time > MinAbortTime) {
2473
+ for (let handler of query.context.abortListeners) {
2474
+ try {
2475
+ handler();
2476
+ }
2477
+ catch (e) {
2478
+ (0,view_.logException)(this.view.state, e);
2479
+ }
2480
+ }
2481
+ query.context.abortListeners = null;
2482
+ this.running.splice(i--, 1);
2483
+ }
2484
+ else {
2485
+ query.updates.push(...update.transactions);
2486
+ }
2487
+ }
2488
+ if (this.debounceUpdate > -1)
2489
+ clearTimeout(this.debounceUpdate);
2490
+ this.debounceUpdate = cState.active.some(a => a.state == 1 /* Pending */ && !this.running.some(q => q.active.source == a.source))
2491
+ ? setTimeout(() => this.startUpdate(), DebounceTime) : -1;
2492
+ if (this.composing != 0 /* None */)
2493
+ for (let tr of update.transactions) {
2494
+ if (getUserEvent(tr) == "input")
2495
+ this.composing = 2 /* Changed */;
2496
+ else if (this.composing == 2 /* Changed */ && tr.selection)
2497
+ this.composing = 3 /* ChangedAndMoved */;
2498
+ }
2499
+ }
2500
+ startUpdate() {
2501
+ this.debounceUpdate = -1;
2502
+ let { state } = this.view, cState = state.field(completionState);
2503
+ for (let active of cState.active) {
2504
+ if (active.state == 1 /* Pending */ && !this.running.some(r => r.active.source == active.source))
2505
+ this.startQuery(active);
2506
+ }
2507
+ }
2508
+ startQuery(active) {
2509
+ let { state } = this.view, pos = cur(state);
2510
+ let context = new CompletionContext(state, pos, active.explicitPos == pos);
2511
+ let pending = new RunningQuery(active, context);
2512
+ this.running.push(pending);
2513
+ Promise.resolve(active.source(context)).then(result => {
2514
+ if (!pending.context.aborted) {
2515
+ pending.done = result || null;
2516
+ this.scheduleAccept();
2517
+ }
2518
+ }, err => {
2519
+ this.view.dispatch({ effects: closeCompletionEffect.of(null) });
2520
+ (0,view_.logException)(this.view.state, err);
2521
+ });
2522
+ }
2523
+ scheduleAccept() {
2524
+ if (this.running.every(q => q.done !== undefined))
2525
+ this.accept();
2526
+ else if (this.debounceAccept < 0)
2527
+ this.debounceAccept = setTimeout(() => this.accept(), DebounceTime);
2528
+ }
2529
+ // For each finished query in this.running, try to create a result
2530
+ // or, if appropriate, restart the query.
2531
+ accept() {
2532
+ var _a;
2533
+ if (this.debounceAccept > -1)
2534
+ clearTimeout(this.debounceAccept);
2535
+ this.debounceAccept = -1;
2536
+ let updated = [];
2537
+ let conf = this.view.state.facet(completionConfig);
2538
+ for (let i = 0; i < this.running.length; i++) {
2539
+ let query = this.running[i];
2540
+ if (query.done === undefined)
2541
+ continue;
2542
+ this.running.splice(i--, 1);
2543
+ if (query.done) {
2544
+ 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));
2545
+ // Replay the transactions that happened since the start of
2546
+ // the request and see if that preserves the result
2547
+ for (let tr of query.updates)
2548
+ active = active.update(tr, conf);
2549
+ if (active.hasResult()) {
2550
+ updated.push(active);
2551
+ continue;
2552
+ }
2553
+ }
2554
+ let current = this.view.state.field(completionState).active.find(a => a.source == query.active.source);
2555
+ if (current && current.state == 1 /* Pending */) {
2556
+ if (query.done == null) {
2557
+ // Explicitly failed. Should clear the pending status if it
2558
+ // hasn't been re-set in the meantime.
2559
+ let active = new ActiveSource(query.active.source, 0 /* Inactive */);
2560
+ for (let tr of query.updates)
2561
+ active = active.update(tr, conf);
2562
+ if (active.state != 1 /* Pending */)
2563
+ updated.push(active);
2564
+ }
2565
+ else {
2566
+ // Cleared by subsequent transactions. Restart.
2567
+ this.startQuery(current);
2568
+ }
2569
+ }
2570
+ }
2571
+ if (updated.length)
2572
+ this.view.dispatch({ effects: setActiveEffect.of(updated) });
2573
+ }
2574
+ }, {
2575
+ eventHandlers: {
2576
+ blur(event) {
2577
+ let state = this.view.state.field(completionState, false);
2578
+ if (state && state.tooltip && this.view.state.facet(completionConfig).closeOnBlur) {
2579
+ let dialog = state.open && (0,view_.getTooltip)(this.view, state.open.tooltip);
2580
+ if (!dialog || !dialog.dom.contains(event.relatedTarget))
2581
+ this.view.dispatch({ effects: closeCompletionEffect.of(null) });
2582
+ }
2583
+ },
2584
+ compositionstart() {
2585
+ this.composing = 1 /* Started */;
2586
+ },
2587
+ compositionend() {
2588
+ if (this.composing == 3 /* ChangedAndMoved */) {
2589
+ // Safari fires compositionend events synchronously, possibly
2590
+ // from inside an update, so dispatch asynchronously to avoid reentrancy
2591
+ setTimeout(() => this.view.dispatch({ effects: startCompletionEffect.of(false) }), 20);
2592
+ }
2593
+ this.composing = 0 /* None */;
2594
+ }
2595
+ }
2596
+ });
2578
2597
 
2579
2598
  const dist_baseTheme = /*@__PURE__*/view_.EditorView.baseTheme({
2580
2599
  ".cm-tooltip.cm-tooltip-autocomplete": {
@@ -2631,13 +2650,13 @@ const dist_baseTheme = /*@__PURE__*/view_.EditorView.baseTheme({
2631
2650
  position: "absolute",
2632
2651
  padding: "3px 9px",
2633
2652
  width: "max-content",
2634
- maxWidth: `${400 /* Info.Width */}px`,
2653
+ maxWidth: `${400 /* Width */}px`,
2635
2654
  boxSizing: "border-box"
2636
2655
  },
2637
2656
  ".cm-completionInfo.cm-completionInfo-left": { right: "100%" },
2638
2657
  ".cm-completionInfo.cm-completionInfo-right": { left: "100%" },
2639
- ".cm-completionInfo.cm-completionInfo-left-narrow": { right: `${30 /* Info.Margin */}px` },
2640
- ".cm-completionInfo.cm-completionInfo-right-narrow": { left: `${30 /* Info.Margin */}px` },
2658
+ ".cm-completionInfo.cm-completionInfo-left-narrow": { right: `${30 /* Margin */}px` },
2659
+ ".cm-completionInfo.cm-completionInfo-right-narrow": { left: `${30 /* Margin */}px` },
2641
2660
  "&light .cm-snippetField": { backgroundColor: "#00000022" },
2642
2661
  "&dark .cm-snippetField": { backgroundColor: "#ffffff22" },
2643
2662
  ".cm-snippetFieldPosition": {
@@ -2990,7 +3009,7 @@ function storeWords(doc, wordRE, result, seen, ignoreAt) {
2990
3009
  if (!seen[m[0]] && pos + m.index != ignoreAt) {
2991
3010
  result.push({ type: "text", label: m[0] });
2992
3011
  seen[m[0]] = true;
2993
- if (result.length >= 2000 /* C.MaxList */)
3012
+ if (result.length >= 2000 /* MaxList */)
2994
3013
  return;
2995
3014
  }
2996
3015
  }
@@ -2998,7 +3017,7 @@ function storeWords(doc, wordRE, result, seen, ignoreAt) {
2998
3017
  }
2999
3018
  }
3000
3019
  function collectWords(doc, cache, wordRE, to, ignoreAt) {
3001
- let big = doc.length >= 1000 /* C.MinCacheLen */;
3020
+ let big = doc.length >= 1000 /* MinCacheLen */;
3002
3021
  let cached = big && cache.get(doc);
3003
3022
  if (cached)
3004
3023
  return cached;
@@ -3006,7 +3025,7 @@ function collectWords(doc, cache, wordRE, to, ignoreAt) {
3006
3025
  if (doc.children) {
3007
3026
  let pos = 0;
3008
3027
  for (let ch of doc.children) {
3009
- if (ch.length >= 1000 /* C.MinCacheLen */) {
3028
+ if (ch.length >= 1000 /* MinCacheLen */) {
3010
3029
  for (let c of collectWords(ch, cache, wordRE, to - pos, ignoreAt - pos)) {
3011
3030
  if (!seen[c.label]) {
3012
3031
  seen[c.label] = true;
@@ -3023,7 +3042,7 @@ function collectWords(doc, cache, wordRE, to, ignoreAt) {
3023
3042
  else {
3024
3043
  storeWords(doc, wordRE, result, seen, ignoreAt);
3025
3044
  }
3026
- if (big && result.length < 2000 /* C.MaxList */)
3045
+ if (big && result.length < 2000 /* MaxList */)
3027
3046
  cache.set(doc, result);
3028
3047
  return result;
3029
3048
  }
@@ -3039,7 +3058,7 @@ const completeAnyWord = context => {
3039
3058
  if (!token && !context.explicit)
3040
3059
  return null;
3041
3060
  let from = token ? token.from : context.pos;
3042
- let options = collectWords(context.state.doc, wordCache(wordChars), re, 50000 /* C.Range */, from);
3061
+ let options = collectWords(context.state.doc, wordCache(wordChars), re, 50000 /* Range */, from);
3043
3062
  return { from, options, validFor: mapRE(re, s => "^" + s) };
3044
3063
  };
3045
3064
 
@@ -3331,8 +3350,8 @@ returns `null`.
3331
3350
  */
3332
3351
  function completionStatus(state) {
3333
3352
  let cState = state.field(completionState, false);
3334
- return cState && cState.active.some(a => a.state == 1 /* State.Pending */) ? "pending"
3335
- : cState && cState.active.some(a => a.state != 0 /* State.Inactive */) ? "active" : null;
3353
+ return cState && cState.active.some(a => a.state == 1 /* Pending */) ? "pending"
3354
+ : cState && cState.active.some(a => a.state != 0 /* Inactive */) ? "active" : null;
3336
3355
  }
3337
3356
  const completionArrayCache = /*@__PURE__*/new WeakMap;
3338
3357
  /**