@uiw/react-codemirror
Version:
CodeMirror component for React.
1 lines • 157 kB
JavaScript
!function(e,t){"object"===typeof exports&&"object"===typeof module?module.exports=t(require("react"),require("react/jsx-runtime"),require("@codemirror/state"),require("@codemirror/theme-one-dark"),require("@codemirror/view")):"function"===typeof define&&define.amd?define(["react","react/jsx-runtime",,,],t):"object"===typeof exports?exports["@uiw/codemirror"]=t(require("react"),require("react/jsx-runtime"),require("@codemirror/state"),require("@codemirror/theme-one-dark"),require("@codemirror/view")):e["@uiw/codemirror"]=t(e.React,e.ReactJSXRuntime,e.CM["@codemirror/state"],e.CM["@codemirror/theme-one-dark"],e.CM["@codemirror/view"])}(self,(e,t,n,i,r)=>(()=>{"use strict";var o={89(e,t,n){n.r(t),n.d(t,{defaultLightThemeOption:()=>a.c,getDefaultExtensions:()=>h});var i=n(720),r=n(687),o=n(730),s=n(708),l=n(60),a=n(806),c={};for(const u in s)["default","getDefaultExtensions"].indexOf(u)<0&&(c[u]=()=>s[u]);n.d(t,c);var h=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.indentWithTab,n=void 0===t||t,c=e.editable,h=void 0===c||c,u=e.readOnly,f=void 0!==u&&u,d=e.theme,p=void 0===d?"light":d,m=e.placeholder,g=void 0===m?"":m,y=e.basicSetup,v=void 0===y||y,b=[];switch(n&&b.unshift(o.keymap.of([i.Yc])),v&&("boolean"===typeof v?b.unshift((0,r.o)()):b.unshift((0,r.o)(v))),g&&b.unshift((0,o.placeholder)(g)),p){case"light":b.push(a.c);break;case"dark":b.push(s.oneDark);break;case"none":break;default:b.push(p)}return!1===h&&b.push(o.EditorView.editable.of(!1)),f&&b.push(l.EditorState.readOnly.of(!0)),[].concat(b)}},806(e,t,n){n.d(t,{c:()=>r});var i=n(730),r=i.EditorView.theme({"&":{backgroundColor:"#fff"}},{dark:!1})},341(e,t,n){function i(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,i=Array(t);n<t;n++)i[n]=e[n];return i}function r(e,t){if(e){if("string"==typeof e)return i(e,t);var n={}.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?i(e,t):void 0}}function o(e){return function(e){if(Array.isArray(e))return i(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||r(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function s(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=n){var i,r,o,s,l=[],a=!0,c=!1;try{if(o=(n=n.call(e)).next,0===t){if(Object(n)!==n)return;a=!1}else for(;!(a=(i=o.call(n)).done)&&(l.push(i.value),l.length!==t);a=!0);}catch(e){c=!0,r=e}finally{try{if(!a&&null!=n.return&&(s=n.return(),Object(s)!==s))return}finally{if(c)throw r}}return l}}(e,t)||r(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}n.d(t,{Q:()=>b,q:()=>x});var l=n(442),a=n(60),c=n(730),h=n(89),u=n(369);function f(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var d=n(236);function p(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,(0,d.A)(i.key),i)}}function m(e,t,n){return t&&p(e.prototype,t),n&&p(e,n),Object.defineProperty(e,"prototype",{writable:!1}),e}var g=function(){return m(function e(t,n){f(this,e),this.timeLeftMS=void 0,this.timeoutMS=void 0,this.isCancelled=!1,this.isTimeExhausted=!1,this.callbacks=[],this.timeLeftMS=n,this.timeoutMS=n,this.callbacks.push(t)},[{key:"tick",value:function(){if(!this.isCancelled&&!this.isTimeExhausted&&(this.timeLeftMS--,this.timeLeftMS<=0)){this.isTimeExhausted=!0;var e=this.callbacks.slice();this.callbacks.length=0,e.forEach(function(e){try{e()}catch(t){console.error("TimeoutLatch callback error:",t)}})}}},{key:"cancel",value:function(){this.isCancelled=!0,this.callbacks.length=0}},{key:"reset",value:function(){this.timeLeftMS=this.timeoutMS,this.isCancelled=!1,this.isTimeExhausted=!1}},{key:"isDone",get:function(){return this.isCancelled||this.isTimeExhausted}}])}(),y=function(){return m(function e(){f(this,e),this.interval=null,this.latches=new Set},[{key:"add",value:function(e){this.latches.add(e),this.start()}},{key:"remove",value:function(e){this.latches.delete(e),0===this.latches.size&&this.stop()}},{key:"start",value:function(){var e=this;null===this.interval&&(this.interval=setInterval(function(){e.latches.forEach(function(t){t.tick(),t.isDone&&e.remove(t)})},1))}},{key:"stop",value:function(){null!==this.interval&&(clearInterval(this.interval),this.interval=null)}}])}(),v=null,b=a.Annotation.define(),w=[];function x(e){var t=e.value,n=e.selection,i=e.onChange,r=e.onStatistics,f=e.onCreateEditor,d=e.onUpdate,p=e.extensions,m=void 0===p?w:p,x=e.autoFocus,k=e.theme,S=void 0===k?"light":k,C=e.height,A=void 0===C?null:C,E=e.minHeight,M=void 0===E?null:E,D=e.maxHeight,O=void 0===D?null:D,T=e.width,I=void 0===T?null:T,P=e.minWidth,B=void 0===P?null:P,R=e.maxWidth,L=void 0===R?null:R,N=e.placeholder,F=void 0===N?"":N,W=e.editable,j=void 0===W||W,V=e.readOnly,_=void 0!==V&&V,q=e.indentWithTab,z=void 0===q||q,U=e.basicSetup,H=void 0===U||U,$=e.root,K=e.initialState,J=s((0,l.useState)(),2),Y=J[0],G=J[1],Q=s((0,l.useState)(),2),Z=Q[0],X=Q[1],ee=s((0,l.useState)(),2),te=ee[0],ne=ee[1],ie=(0,l.useState)(function(){return{current:null}})[0],re=(0,l.useState)(function(){return{current:null}})[0],oe=c.EditorView.theme({"&":{height:A,minHeight:M,maxHeight:O,width:I,minWidth:B,maxWidth:L},"& .cm-scroller":{height:"100% !important"}}),se=c.EditorView.updateListener.of(function(e){if(e.docChanged&&"function"===typeof i&&!e.transactions.some(function(e){return e.annotation(b)})){ie.current?ie.current.reset():(ie.current=new g(function(){if(re.current){var e=re.current;re.current=null,e()}ie.current=null},200),("undefined"===typeof window?new y:(v||(v=new y),v)).add(ie.current));var t=e.state.doc.toString();i(t,e)}r&&r((0,u.m)(e))}),le=(0,h.getDefaultExtensions)({theme:S,editable:j,readOnly:_,placeholder:F,indentWithTab:z,basicSetup:H}),ae=[se,oe].concat(o(le));return d&&"function"===typeof d&&ae.push(c.EditorView.updateListener.of(d)),ae=ae.concat(m),(0,l.useLayoutEffect)(function(){if(Y&&!te){var e={doc:t,selection:n,extensions:ae},i=K?a.EditorState.fromJSON(K.json,e,K.fields):a.EditorState.create(e);if(ne(i),!Z){var r=new c.EditorView({state:i,parent:Y,root:$});X(r),f&&f(r,i)}}return function(){Z&&(ne(void 0),X(void 0))}},[Y,te]),(0,l.useEffect)(function(){e.container&&G(e.container)},[e.container]),(0,l.useEffect)(function(){return function(){Z&&(Z.destroy(),X(void 0)),ie.current&&(ie.current.cancel(),ie.current=null)}},[Z]),(0,l.useEffect)(function(){x&&Z&&Z.focus()},[x,Z]),(0,l.useEffect)(function(){Z&&Z.dispatch({effects:a.StateEffect.reconfigure.of(ae)})},[S,m,A,M,O,I,B,L,F,j,_,z,H,i,d]),(0,l.useEffect)(function(){if(void 0!==t){var e=Z?Z.state.doc.toString():"";if(Z&&t!==e){var n=function(){Z&&t!==Z.state.doc.toString()&&Z.dispatch({changes:{from:0,to:Z.state.doc.toString().length,insert:t||""},annotations:[b.of(!0)]})};ie.current&&!ie.current.isDone?re.current=n:n()}}},[t,Z]),{state:te,setState:ne,view:Z,setView:X,container:Y,setContainer:G}}},369(e,t,n){n.d(t,{m:()=>i});var i=function(e){return{line:e.state.doc.lineAt(e.state.selection.main.from),lineCount:e.state.doc.lines,lineBreak:e.state.lineBreak,length:e.state.doc.length,readOnly:e.state.readOnly,tabSize:e.state.tabSize,selection:e.state.selection,selectionAsSingle:e.state.selection.asSingle().main,ranges:e.state.selection.ranges,selectionCode:e.state.sliceDoc(e.state.selection.main.from,e.state.selection.main.to),selections:e.state.selection.ranges.map(function(t){return e.state.sliceDoc(t.from,t.to)}),selectedText:e.state.selection.ranges.some(function(e){return!e.empty})}}},442(t){t.exports=e},742(e){e.exports=t},60(e){e.exports=n},708(e){e.exports=i},730(e){e.exports=r},687(e,t,n){n.d(t,{o:()=>Vt,V:()=>_t});var i=n(730),r=n(60),o=n(720);function s(){var e=arguments[0];"string"==typeof e&&(e=document.createElement(e));var t=1,n=arguments[1];if(n&&"object"==typeof n&&null==n.nodeType&&!Array.isArray(n)){for(var i in n)if(Object.prototype.hasOwnProperty.call(n,i)){var r=n[i];"string"==typeof r?e.setAttribute(i,r):null!=r&&(e[i]=r)}t++}for(;t<arguments.length;t++)l(e,arguments[t]);return e}function l(e,t){if("string"==typeof t)e.appendChild(document.createTextNode(t));else if(null==t);else if(null!=t.nodeType)e.appendChild(t);else{if(!Array.isArray(t))throw new RangeError("Unsupported child node: "+t);for(var n=0;n<t.length;n++)l(e,t[n])}}const a="function"==typeof String.prototype.normalize?e=>e.normalize("NFKD"):e=>e;class c{constructor(e,t,n=0,i=e.length,r,o){this.test=o,this.value={from:0,to:0},this.done=!1,this.matches=[],this.buffer="",this.bufferPos=0,this.iter=e.iterRange(n,i),this.bufferStart=n,this.normalize=r?e=>r(a(e)):a,this.query=this.normalize(t)}peek(){if(this.bufferPos==this.buffer.length){if(this.bufferStart+=this.buffer.length,this.iter.next(),this.iter.done)return-1;this.bufferPos=0,this.buffer=this.iter.value}return(0,r.codePointAt)(this.buffer,this.bufferPos)}next(){for(;this.matches.length;)this.matches.pop();return this.nextOverlapping()}nextOverlapping(){for(;;){let e=this.peek();if(e<0)return this.done=!0,this;let t=(0,r.fromCodePoint)(e),n=this.bufferStart+this.bufferPos;this.bufferPos+=(0,r.codePointSize)(e);let i=this.normalize(t);if(i.length)for(let r=0,o=n;;r++){let e=i.charCodeAt(r),s=this.match(e,o,this.bufferPos+this.bufferStart);if(r==i.length-1){if(s)return this.value=s,this;break}o==n&&r<t.length&&t.charCodeAt(r)==e&&o++}}}match(e,t,n){let i=null;for(let r=0;r<this.matches.length;r+=2){let t=this.matches[r],o=!1;this.query.charCodeAt(t)==e&&(t==this.query.length-1?i={from:this.matches[r+1],to:n}:(this.matches[r]++,o=!0)),o||(this.matches.splice(r,2),r-=2)}return this.query.charCodeAt(0)==e&&(1==this.query.length?i={from:t,to:n}:this.matches.push(1,t)),i&&this.test&&!this.test(i.from,i.to,this.buffer,this.bufferStart)&&(i=null),i}}"undefined"!=typeof Symbol&&(c.prototype[Symbol.iterator]=function(){return this});const h={from:-1,to:-1,match:/.*/.exec("")},u="gm"+(null==/x/.unicode?"":"u");class f{constructor(e,t,n,i=0,r=e.length){if(this.text=e,this.to=r,this.curLine="",this.done=!1,this.value=h,/\\[sWDnr]|\n|\r|\[\^/.test(t))return new m(e,t,n,i,r);this.re=new RegExp(t,u+((null===n||void 0===n?void 0:n.ignoreCase)?"i":"")),this.test=null===n||void 0===n?void 0:n.test,this.iter=e.iter();let o=e.lineAt(i);this.curLineStart=o.from,this.matchPos=g(e,i),this.getLine(this.curLineStart)}getLine(e){this.iter.next(e),this.iter.lineBreak?this.curLine="":(this.curLine=this.iter.value,this.curLineStart+this.curLine.length>this.to&&(this.curLine=this.curLine.slice(0,this.to-this.curLineStart)),this.iter.next())}nextLine(){this.curLineStart=this.curLineStart+this.curLine.length+1,this.curLineStart>this.to?this.curLine="":this.getLine(0)}next(){for(let e=this.matchPos-this.curLineStart;;){this.re.lastIndex=e;let t=this.matchPos<=this.to&&this.re.exec(this.curLine);if(t){let n=this.curLineStart+t.index,i=n+t[0].length;if(this.matchPos=g(this.text,i+(n==i?1:0)),n==this.curLineStart+this.curLine.length&&this.nextLine(),(n<i||n>this.value.to)&&(!this.test||this.test(n,i,t)))return this.value={from:n,to:i,match:t},this;e=this.matchPos-this.curLineStart}else{if(!(this.curLineStart+this.curLine.length<this.to))return this.done=!0,this;this.nextLine(),e=0}}}}const d=new WeakMap;class p{constructor(e,t){this.from=e,this.text=t}get to(){return this.from+this.text.length}static get(e,t,n){let i=d.get(e);if(!i||i.from>=n||i.to<=t){let i=new p(t,e.sliceString(t,n));return d.set(e,i),i}if(i.from==t&&i.to==n)return i;let{text:r,from:o}=i;return o>t&&(r=e.sliceString(t,o)+r,o=t),i.to<n&&(r+=e.sliceString(i.to,n)),d.set(e,new p(o,r)),new p(t,r.slice(t-o,n-o))}}class m{constructor(e,t,n,i,r){this.text=e,this.to=r,this.done=!1,this.value=h,this.matchPos=g(e,i),this.re=new RegExp(t,u+((null===n||void 0===n?void 0:n.ignoreCase)?"i":"")),this.test=null===n||void 0===n?void 0:n.test,this.flat=p.get(e,i,this.chunkEnd(i+5e3))}chunkEnd(e){return e>=this.to?this.to:this.text.lineAt(e).to}next(){for(;;){let e=this.re.lastIndex=this.matchPos-this.flat.from,t=this.re.exec(this.flat.text);if(t&&!t[0]&&t.index==e&&(this.re.lastIndex=e+1,t=this.re.exec(this.flat.text)),t){let e=this.flat.from+t.index,n=e+t[0].length;if((this.flat.to>=this.to||t.index+t[0].length<=this.flat.text.length-10)&&(!this.test||this.test(e,n,t)))return this.value={from:e,to:n,match:t},this.matchPos=g(this.text,n+(e==n?1:0)),this}if(this.flat.to==this.to)return this.done=!0,this;this.flat=p.get(this.text,this.flat.from,this.chunkEnd(this.flat.from+2*this.flat.text.length))}}}function g(e,t){if(t>=e.length)return t;let n,i=e.lineAt(t);for(;t<i.to&&(n=i.text.charCodeAt(t-i.from))>=56320&&n<57344;)t++;return t}"undefined"!=typeof Symbol&&(f.prototype[Symbol.iterator]=m.prototype[Symbol.iterator]=function(){return this});const y={highlightWordAroundCursor:!1,minSelectionLength:1,maxMatches:100,wholeWords:!1},v=r.Facet.define({combine:e=>(0,r.combineConfig)(e,y,{highlightWordAroundCursor:(e,t)=>e||t,minSelectionLength:Math.min,maxMatches:Math.min})});const b=i.Decoration.mark({class:"cm-selectionMatch"}),w=i.Decoration.mark({class:"cm-selectionMatch cm-selectionMatch-main"});function x(e,t,n,i){return(0==n||e(t.sliceDoc(n-1,n))!=r.CharCategory.Word)&&(i==t.doc.length||e(t.sliceDoc(i,i+1))!=r.CharCategory.Word)}const k=i.ViewPlugin.fromClass(class{constructor(e){this.decorations=this.getDeco(e)}update(e){(e.selectionSet||e.docChanged||e.viewportChanged)&&(this.decorations=this.getDeco(e.view))}getDeco(e){let t=e.state.facet(v),{state:n}=e,o=n.selection;if(o.ranges.length>1)return i.Decoration.none;let s,l=o.main,a=null;if(l.empty){if(!t.highlightWordAroundCursor)return i.Decoration.none;let e=n.wordAt(l.head);if(!e)return i.Decoration.none;a=n.charCategorizer(l.head),s=n.sliceDoc(e.from,e.to)}else{let e=l.to-l.from;if(e<t.minSelectionLength||e>200)return i.Decoration.none;if(t.wholeWords){if(s=n.sliceDoc(l.from,l.to),a=n.charCategorizer(l.head),!x(a,n,l.from,l.to)||!function(e,t,n,i){return e(t.sliceDoc(n,n+1))==r.CharCategory.Word&&e(t.sliceDoc(i-1,i))==r.CharCategory.Word}(a,n,l.from,l.to))return i.Decoration.none}else if(s=n.sliceDoc(l.from,l.to),!s)return i.Decoration.none}let h=[];for(let r of e.visibleRanges){let e=new c(n.doc,s,r.from,r.to);for(;!e.next().done;){let{from:r,to:o}=e.value;if((!a||x(a,n,r,o))&&(l.empty&&r<=l.from&&o>=l.to?h.push(w.range(r,o)):(r>=l.to||o<=l.from)&&h.push(b.range(r,o)),h.length>t.maxMatches))return i.Decoration.none}}return i.Decoration.set(h)}},{decorations:e=>e.decorations}),S=i.EditorView.baseTheme({".cm-selectionMatch":{backgroundColor:"#99ff7780"},".cm-searchMatch .cm-selectionMatch":{backgroundColor:"transparent"}});const C=r.Facet.define({combine:e=>(0,r.combineConfig)(e,{top:!1,caseSensitive:!1,literal:!1,regexp:!1,wholeWord:!1,createPanel:e=>new X(e),scrollToMatch:e=>i.EditorView.scrollIntoView(e)})});class A{constructor(e){this.search=e.search,this.caseSensitive=!!e.caseSensitive,this.literal=!!e.literal,this.regexp=!!e.regexp,this.replace=e.replace||"",this.valid=!!this.search&&(!this.regexp||function(e){try{return new RegExp(e,u),!0}catch(t){return!1}}(this.search)),this.unquoted=this.unquote(this.search),this.wholeWord=!!e.wholeWord,this.test=e.test}unquote(e){return this.literal?e:e.replace(/\\([nrt\\])/g,(e,t)=>"n"==t?"\n":"r"==t?"\r":"t"==t?"\t":"\\")}eq(e){return this.search==e.search&&this.replace==e.replace&&this.caseSensitive==e.caseSensitive&&this.regexp==e.regexp&&this.wholeWord==e.wholeWord&&this.test==e.test}create(){return this.regexp?new P(this):new D(this)}getCursor(e,t=0,n){let i=e.doc?e:r.EditorState.create({doc:e});return null==n&&(n=i.doc.length),this.regexp?O(this,i,t,n):M(this,i,t,n)}}class E{constructor(e){this.spec=e}}function M(e,t,n,i){let o;var s,l;return e.wholeWord&&(s=t.doc,l=t.charCategorizer(t.selection.main.head),o=(e,t,n,i)=>((i>e||i+n.length<t)&&(i=Math.max(0,e-2),n=s.sliceString(i,Math.min(s.length,t+2))),(l(T(n,e-i))!=r.CharCategory.Word||l(I(n,e-i))!=r.CharCategory.Word)&&(l(I(n,t-i))!=r.CharCategory.Word||l(T(n,t-i))!=r.CharCategory.Word))),e.test&&(o=function(e,t,n){return(i,r,o,s)=>{if(n&&!n(i,r,o,s))return!1;let l=i>=s&&r<=s+o.length?o.slice(i-s,r-s):t.doc.sliceString(i,r);return e(l,t,i,r)}}(e.test,t,o)),new c(t.doc,e.unquoted,n,i,e.caseSensitive?void 0:e=>e.toLowerCase(),o)}class D extends E{constructor(e){super(e)}nextMatch(e,t,n){let i=M(this.spec,e,n,e.doc.length).nextOverlapping();if(i.done){let n=Math.min(e.doc.length,t+this.spec.unquoted.length);i=M(this.spec,e,0,n).nextOverlapping()}return i.done||i.value.from==t&&i.value.to==n?null:i.value}prevMatchInRange(e,t,n){for(let i=n;;){let n=Math.max(t,i-1e4-this.spec.unquoted.length),r=M(this.spec,e,n,i),o=null;for(;!r.nextOverlapping().done;)o=r.value;if(o)return o;if(n==t)return null;i-=1e4}}prevMatch(e,t,n){let i=this.prevMatchInRange(e,0,t);return i||(i=this.prevMatchInRange(e,Math.max(0,n-this.spec.unquoted.length),e.doc.length)),!i||i.from==t&&i.to==n?null:i}getReplacement(e){return this.spec.unquote(this.spec.replace)}matchAll(e,t){let n=M(this.spec,e,0,e.doc.length),i=[];for(;!n.next().done;){if(i.length>=t)return null;i.push(n.value)}return i}highlight(e,t,n,i){let r=M(this.spec,e,Math.max(0,t-this.spec.unquoted.length),Math.min(n+this.spec.unquoted.length,e.doc.length));for(;!r.next().done;)i(r.value.from,r.value.to)}}function O(e,t,n,i){let o;var s;return e.wholeWord&&(s=t.charCategorizer(t.selection.main.head),o=(e,t,n)=>!n[0].length||(s(T(n.input,n.index))!=r.CharCategory.Word||s(I(n.input,n.index))!=r.CharCategory.Word)&&(s(I(n.input,n.index+n[0].length))!=r.CharCategory.Word||s(T(n.input,n.index+n[0].length))!=r.CharCategory.Word)),e.test&&(o=function(e,t,n){return(i,r,o)=>(!n||n(i,r,o))&&e(o[0],t,i,r)}(e.test,t,o)),new f(t.doc,e.search,{ignoreCase:!e.caseSensitive,test:o},n,i)}function T(e,t){return e.slice((0,r.findClusterBreak)(e,t,!1),t)}function I(e,t){return e.slice(t,(0,r.findClusterBreak)(e,t))}class P extends E{nextMatch(e,t,n){let i=O(this.spec,e,n,e.doc.length).next();return i.done&&(i=O(this.spec,e,0,t).next()),i.done?null:i.value}prevMatchInRange(e,t,n){for(let i=1;;i++){let r=Math.max(t,n-1e4*i),o=O(this.spec,e,r,n),s=null;for(;!o.next().done;)s=o.value;if(s&&(r==t||s.from>r+10))return s;if(r==t)return null}}prevMatch(e,t,n){return this.prevMatchInRange(e,0,t)||this.prevMatchInRange(e,n,e.doc.length)}getReplacement(e){return this.spec.unquote(this.spec.replace).replace(/\$([$&]|\d+)/g,(t,n)=>{if("&"==n)return e.match[0];if("$"==n)return"$";for(let i=n.length;i>0;i--){let t=+n.slice(0,i);if(t>0&&t<e.match.length)return e.match[t]+n.slice(i)}return t})}matchAll(e,t){let n=O(this.spec,e,0,e.doc.length),i=[];for(;!n.next().done;){if(i.length>=t)return null;i.push(n.value)}return i}highlight(e,t,n,i){let r=O(this.spec,e,Math.max(0,t-250),Math.min(n+250,e.doc.length));for(;!r.next().done;)i(r.value.from,r.value.to)}}const B=r.StateEffect.define(),R=r.StateEffect.define(),L=r.StateField.define({create:e=>new N(K(e).create(),null),update(e,t){for(let n of t.effects)n.is(B)?e=new N(n.value.create(),e.panel):n.is(R)&&(e=new N(e.query,n.value?$:null));return e},provide:e=>i.showPanel.from(e,e=>e.panel)});class N{constructor(e,t){this.query=e,this.panel=t}}const F=i.Decoration.mark({class:"cm-searchMatch"}),W=i.Decoration.mark({class:"cm-searchMatch cm-searchMatch-selected"}),j=i.ViewPlugin.fromClass(class{constructor(e){this.view=e,this.decorations=this.highlight(e.state.field(L))}update(e){let t=e.state.field(L);(t!=e.startState.field(L)||e.docChanged||e.selectionSet||e.viewportChanged)&&(this.decorations=this.highlight(t))}highlight({query:e,panel:t}){if(!t||!e.spec.valid)return i.Decoration.none;let{view:n}=this,o=new r.RangeSetBuilder;for(let i=0,r=n.visibleRanges,s=r.length;i<s;i++){let{from:t,to:l}=r[i];for(;i<s-1&&l>r[i+1].from-500;)l=r[++i].to;e.highlight(n.state,t,l,(e,t)=>{let i=n.state.selection.ranges.some(n=>n.from==e&&n.to==t);o.add(e,t,i?W:F)})}return o.finish()}},{decorations:e=>e.decorations});function V(e){return t=>{let n=t.state.field(L,!1);return n&&n.query.spec.valid?e(t,n):G(t)}}const _=V((e,{query:t})=>{let{to:n}=e.state.selection.main,i=t.nextMatch(e.state,n,n);if(!i)return!1;let o=r.EditorSelection.single(i.from,i.to),s=e.state.facet(C);return e.dispatch({selection:o,effects:[ne(e,i),s.scrollToMatch(o.main,e)],userEvent:"select.search"}),Y(e),!0}),q=V((e,{query:t})=>{let{state:n}=e,{from:i}=n.selection.main,o=t.prevMatch(n,i,i);if(!o)return!1;let s=r.EditorSelection.single(o.from,o.to),l=e.state.facet(C);return e.dispatch({selection:s,effects:[ne(e,o),l.scrollToMatch(s.main,e)],userEvent:"select.search"}),Y(e),!0}),z=V((e,{query:t})=>{let n=t.matchAll(e.state,1e3);return!(!n||!n.length)&&(e.dispatch({selection:r.EditorSelection.create(n.map(e=>r.EditorSelection.range(e.from,e.to))),userEvent:"select.search.matches"}),!0)}),U=V((e,{query:t})=>{let{state:n}=e,{from:o,to:s}=n.selection.main;if(n.readOnly)return!1;let l=t.nextMatch(n,o,o);if(!l)return!1;let a,c,h=l,u=[],f=[];h.from==o&&h.to==s&&(c=n.toText(t.getReplacement(h)),u.push({from:h.from,to:h.to,insert:c}),h=t.nextMatch(n,h.from,h.to),f.push(i.EditorView.announce.of(n.phrase("replaced match on line $",n.doc.lineAt(o).number)+".")));let d=e.state.changes(u);return h&&(a=r.EditorSelection.single(h.from,h.to).map(d),f.push(ne(e,h)),f.push(n.facet(C).scrollToMatch(a.main,e))),e.dispatch({changes:d,selection:a,effects:f,userEvent:"input.replace"}),!0}),H=V((e,{query:t})=>{if(e.state.readOnly)return!1;let n=t.matchAll(e.state,1e9).map(e=>{let{from:n,to:i}=e;return{from:n,to:i,insert:t.getReplacement(e)}});if(!n.length)return!1;let r=e.state.phrase("replaced $ matches",n.length)+".";return e.dispatch({changes:n,effects:i.EditorView.announce.of(r),userEvent:"input.replace.all"}),!0});function $(e){return e.state.facet(C).createPanel(e)}function K(e,t){var n,i,r,o,s;let l=e.selection.main,a=l.empty||l.to>l.from+100?"":e.sliceDoc(l.from,l.to);if(t&&!a)return t;let c=e.facet(C);return new A({search:(null!==(n=null===t||void 0===t?void 0:t.literal)&&void 0!==n?n:c.literal)?a:a.replace(/\n/g,"\\n"),caseSensitive:null!==(i=null===t||void 0===t?void 0:t.caseSensitive)&&void 0!==i?i:c.caseSensitive,literal:null!==(r=null===t||void 0===t?void 0:t.literal)&&void 0!==r?r:c.literal,regexp:null!==(o=null===t||void 0===t?void 0:t.regexp)&&void 0!==o?o:c.regexp,wholeWord:null!==(s=null===t||void 0===t?void 0:t.wholeWord)&&void 0!==s?s:c.wholeWord})}function J(e){let t=(0,i.getPanel)(e,$);return t&&t.dom.querySelector("[main-field]")}function Y(e){let t=J(e);t&&t==e.root.activeElement&&t.select()}const G=e=>{let t=e.state.field(L,!1);if(t&&t.panel){let n=J(e);if(n&&n!=e.root.activeElement){let i=K(e.state,t.query.spec);i.valid&&e.dispatch({effects:B.of(i)}),n.focus(),n.select()}}else e.dispatch({effects:[R.of(!0),t?B.of(K(e.state,t.query.spec)):r.StateEffect.appendConfig.of(re)]});return!0},Q=e=>{let t=e.state.field(L,!1);if(!t||!t.panel)return!1;let n=(0,i.getPanel)(e,$);return n&&n.dom.contains(e.root.activeElement)&&e.focus(),e.dispatch({effects:R.of(!1)}),!0},Z=[{key:"Mod-f",run:G,scope:"editor search-panel"},{key:"F3",run:_,shift:q,scope:"editor search-panel",preventDefault:!0},{key:"Mod-g",run:_,shift:q,scope:"editor search-panel",preventDefault:!0},{key:"Escape",run:Q,scope:"editor search-panel"},{key:"Mod-Shift-l",run:({state:e,dispatch:t})=>{let n=e.selection;if(n.ranges.length>1||n.main.empty)return!1;let{from:i,to:o}=n.main,s=[],l=0;for(let a=new c(e.doc,e.sliceDoc(i,o));!a.next().done;){if(s.length>1e3)return!1;a.value.from==i&&(l=s.length),s.push(r.EditorSelection.range(a.value.from,a.value.to))}return t(e.update({selection:r.EditorSelection.create(s,l),userEvent:"select.search.matches"})),!0}},{key:"Mod-Alt-g",run:e=>{let{state:t}=e,n=String(t.doc.lineAt(e.state.selection.main.head).number),{close:o,result:s}=(0,i.showDialog)(e,{label:t.phrase("Go to line"),input:{type:"text",name:"line",value:n},focus:!0,submitLabel:t.phrase("go")});return s.then(n=>{let s=n&&/^([+-])?(\d+)?(:\d+)?(%)?$/.exec(n.elements.line.value);if(!s)return void e.dispatch({effects:o});let l=t.doc.lineAt(t.selection.main.head),[,a,c,h,u]=s,f=h?+h.slice(1):0,d=c?+c:l.number;if(c&&u){let e=d/100;a&&(e=e*("-"==a?-1:1)+l.number/t.doc.lines),d=Math.round(t.doc.lines*e)}else c&&a&&(d=d*("-"==a?-1:1)+l.number);let p=t.doc.line(Math.max(1,Math.min(t.doc.lines,d))),m=r.EditorSelection.cursor(p.from+Math.max(0,Math.min(f,p.length)));e.dispatch({effects:[o,i.EditorView.scrollIntoView(m.from,{y:"center"})],selection:m})}),!0}},{key:"Mod-d",run:({state:e,dispatch:t})=>{let{ranges:n}=e.selection;if(n.some(e=>e.from===e.to))return(({state:e,dispatch:t})=>{let{selection:n}=e,i=r.EditorSelection.create(n.ranges.map(t=>e.wordAt(t.head)||r.EditorSelection.cursor(t.head)),n.mainIndex);return!i.eq(n)&&(t(e.update({selection:i})),!0)})({state:e,dispatch:t});let o=e.sliceDoc(n[0].from,n[0].to);if(e.selection.ranges.some(t=>e.sliceDoc(t.from,t.to)!=o))return!1;let s=function(e,t){let{main:n,ranges:i}=e.selection,r=e.wordAt(n.head),o=r&&r.from==n.from&&r.to==n.to;for(let s=!1,l=new c(e.doc,t,i[i.length-1].to);;){if(l.next(),!l.done){if(s&&i.some(e=>e.from==l.value.from))continue;if(o){let t=e.wordAt(l.value.from);if(!t||t.from!=l.value.from||t.to!=l.value.to)continue}return l.value}if(s)return null;l=new c(e.doc,t,0,Math.max(0,i[i.length-1].from-1)),s=!0}}(e,o);return!!s&&(t(e.update({selection:e.selection.addRange(r.EditorSelection.range(s.from,s.to),!1),effects:i.EditorView.scrollIntoView(s.to)})),!0)},preventDefault:!0}];class X{constructor(e){this.view=e;let t=this.query=e.state.field(L).query.spec;function n(e,t,n){return s("button",{class:"cm-button",name:e,onclick:t,type:"button"},n)}this.commit=this.commit.bind(this),this.searchField=s("input",{value:t.search,placeholder:ee(e,"Find"),"aria-label":ee(e,"Find"),class:"cm-textfield",name:"search",form:"","main-field":"true",onchange:this.commit,onkeyup:this.commit}),this.replaceField=s("input",{value:t.replace,placeholder:ee(e,"Replace"),"aria-label":ee(e,"Replace"),class:"cm-textfield",name:"replace",form:"",onchange:this.commit,onkeyup:this.commit}),this.caseField=s("input",{type:"checkbox",name:"case",form:"",checked:t.caseSensitive,onchange:this.commit}),this.reField=s("input",{type:"checkbox",name:"re",form:"",checked:t.regexp,onchange:this.commit}),this.wordField=s("input",{type:"checkbox",name:"word",form:"",checked:t.wholeWord,onchange:this.commit}),this.dom=s("div",{onkeydown:e=>this.keydown(e),class:"cm-search"},[this.searchField,n("next",()=>_(e),[ee(e,"next")]),n("prev",()=>q(e),[ee(e,"previous")]),n("select",()=>z(e),[ee(e,"all")]),s("label",null,[this.caseField,ee(e,"match case")]),s("label",null,[this.reField,ee(e,"regexp")]),s("label",null,[this.wordField,ee(e,"by word")]),...e.state.readOnly?[]:[s("br"),this.replaceField,n("replace",()=>U(e),[ee(e,"replace")]),n("replaceAll",()=>H(e),[ee(e,"replace all")])],s("button",{name:"close",onclick:()=>Q(e),"aria-label":ee(e,"close"),type:"button"},["\xd7"])])}commit(){let e=new A({search:this.searchField.value,caseSensitive:this.caseField.checked,regexp:this.reField.checked,wholeWord:this.wordField.checked,replace:this.replaceField.value});e.eq(this.query)||(this.query=e,this.view.dispatch({effects:B.of(e)}))}keydown(e){(0,i.runScopeHandlers)(this.view,e,"search-panel")?e.preventDefault():13==e.keyCode&&e.target==this.searchField?(e.preventDefault(),(e.shiftKey?q:_)(this.view)):13==e.keyCode&&e.target==this.replaceField&&(e.preventDefault(),U(this.view))}update(e){for(let t of e.transactions)for(let e of t.effects)e.is(B)&&!e.value.eq(this.query)&&this.setQuery(e.value)}setQuery(e){this.query=e,this.searchField.value=e.search,this.replaceField.value=e.replace,this.caseField.checked=e.caseSensitive,this.reField.checked=e.regexp,this.wordField.checked=e.wholeWord}mount(){this.searchField.select()}get pos(){return 80}get top(){return this.view.state.facet(C).top}}function ee(e,t){return e.state.phrase(t)}const te=/[\s\.,:;?!]/;function ne(e,{from:t,to:n}){let r=e.state.doc.lineAt(t),o=e.state.doc.lineAt(n).to,s=Math.max(r.from,t-30),l=Math.min(o,n+30),a=e.state.sliceDoc(s,l);if(s!=r.from)for(let i=0;i<30;i++)if(!te.test(a[i+1])&&te.test(a[i])){a=a.slice(i);break}if(l!=o)for(let i=a.length-1;i>a.length-30;i--)if(!te.test(a[i-1])&&te.test(a[i])){a=a.slice(0,i);break}return i.EditorView.announce.of(`${e.state.phrase("current match")}. ${a} ${e.state.phrase("on line")} ${r.number}.`)}const ie=i.EditorView.baseTheme({".cm-panel.cm-search":{padding:"2px 6px 4px",position:"relative","& [name=close]":{position:"absolute",top:"0",right:"4px",backgroundColor:"inherit",border:"none",font:"inherit",padding:0,margin:0},"& input, & button, & label":{margin:".2em .6em .2em 0"},"& input[type=checkbox]":{marginRight:".2em"},"& label":{fontSize:"80%",whiteSpace:"pre"}},"&light .cm-searchMatch":{backgroundColor:"#ffff0054"},"&dark .cm-searchMatch":{backgroundColor:"#00ffff8a"},"&light .cm-searchMatch-selected":{backgroundColor:"#ff6a0054"},"&dark .cm-searchMatch-selected":{backgroundColor:"#ff00ff8a"}}),re=[L,r.Prec.low(j),ie];var oe=n(194);class se{constructor(e,t,n,i){this.state=e,this.pos=t,this.explicit=n,this.view=i,this.abortListeners=[],this.abortOnDocChange=!1}tokenBefore(e){let t=(0,oe.mv)(this.state).resolveInner(this.pos,-1);for(;t&&e.indexOf(t.name)<0;)t=t.parent;return t?{from:t.from,to:this.pos,text:this.state.sliceDoc(t.from,this.pos),type:t.type}:null}matchBefore(e){let t=this.state.doc.lineAt(this.pos),n=Math.max(t.from,this.pos-250),i=t.text.slice(n-t.from,this.pos-t.from),r=i.search(ue(e,!1));return r<0?null:{from:n+r,to:this.pos,text:i.slice(r)}}get aborted(){return null==this.abortListeners}addEventListener(e,t,n){"abort"==e&&this.abortListeners&&(this.abortListeners.push(t),n&&n.onDocChange&&(this.abortOnDocChange=!0))}}function le(e){let t=Object.keys(e).join(""),n=/\w/.test(t);return n&&(t=t.replace(/\w/g,"")),`[${n?"\\w":""}${t.replace(/[^\w\s]/g,"\\$&")}]`}function ae(e){let t=e.map(e=>"string"==typeof e?{label:e}:e),[n,i]=t.every(e=>/^\w+$/.test(e.label))?[/\w*$/,/\w+$/]:function(e){let t=Object.create(null),n=Object.create(null);for(let{label:r}of e){t[r[0]]=!0;for(let e=1;e<r.length;e++)n[r[e]]=!0}let i=le(t)+le(n)+"*$";return[new RegExp("^"+i),new RegExp(i)]}(t);return e=>{let r=e.matchBefore(i);return r||e.explicit?{from:r?r.from:e.pos,options:t,validFor:n}:null}}class ce{constructor(e,t,n,i){this.completion=e,this.source=t,this.match=n,this.score=i}}function he(e){return e.selection.main.from}function ue(e,t){var n;let{source:i}=e,r=t&&"^"!=i[0],o="$"!=i[i.length-1];return r||o?new RegExp(`${r?"^":""}(?:${i})${o?"$":""}`,null!==(n=e.flags)&&void 0!==n?n:e.ignoreCase?"i":""):e}const fe=r.Annotation.define();function de(e,t,n,i){let{main:o}=e.selection,s=n-o.from,l=i-o.from;return{...e.changeByRange(a=>{if(a!=o&&n!=i&&e.sliceDoc(a.from+s,a.from+l)!=e.sliceDoc(n,i))return{range:a};let c=e.toText(t);return{changes:{from:a.from+s,to:i==o.from?a.to:a.from+l,insert:c},range:r.EditorSelection.cursor(a.from+s+c.length)}}),scrollIntoView:!0,userEvent:"input.complete"}}const pe=new WeakMap;function me(e){if(!Array.isArray(e))return e;let t=pe.get(e);return t||pe.set(e,t=ae(e)),t}const ge=r.StateEffect.define(),ye=r.StateEffect.define();class ve{constructor(e){this.pattern=e,this.chars=[],this.folded=[],this.any=[],this.precise=[],this.byWord=[],this.score=0,this.matched=[];for(let t=0;t<e.length;){let n=(0,r.codePointAt)(e,t),i=(0,r.codePointSize)(n);this.chars.push(n);let o=e.slice(t,t+i),s=o.toUpperCase();this.folded.push((0,r.codePointAt)(s==o?o.toLowerCase():s,0)),t+=i}this.astral=e.length!=this.chars.length}ret(e,t){return this.score=e,this.matched=t,this}match(e){if(0==this.pattern.length)return this.ret(-100,[]);if(e.length<this.pattern.length)return null;let{chars:t,folded:n,any:i,precise:o,byWord:s}=this;if(1==t.length){let i=(0,r.codePointAt)(e,0),o=(0,r.codePointSize)(i),s=o==e.length?0:-100;if(i==t[0]);else{if(i!=n[0])return null;s+=-200}return this.ret(s,[0,o])}let l=e.indexOf(this.pattern);if(0==l)return this.ret(e.length==this.pattern.length?0:-100,[0,this.pattern.length]);let a=t.length,c=0;if(l<0){for(let o=0,s=Math.min(e.length,200);o<s&&c<a;){let s=(0,r.codePointAt)(e,o);s!=t[c]&&s!=n[c]||(i[c++]=o),o+=(0,r.codePointSize)(s)}if(c<a)return null}let h=0,u=0,f=!1,d=0,p=-1,m=-1,g=/[a-z]/.test(e),y=!0;for(let v=0,b=Math.min(e.length,200),w=0;v<b&&u<a;){let i=(0,r.codePointAt)(e,v);l<0&&(h<a&&i==t[h]&&(o[h++]=v),d<a&&(i==t[d]||i==n[d]?(0==d&&(p=v),m=v+1,d++):d=0));let c,b=i<255?i>=48&&i<=57||i>=97&&i<=122?2:i>=65&&i<=90?1:0:(c=(0,r.fromCodePoint)(i))!=c.toLowerCase()?1:c!=c.toUpperCase()?2:0;(!v||1==b&&g||0==w&&0!=b)&&(t[u]==i||n[u]==i&&(f=!0)?s[u++]=v:s.length&&(y=!1)),w=b,v+=(0,r.codePointSize)(i)}return u==a&&0==s[0]&&y?this.result((f?-200:0)-100,s,e):d==a&&0==p?this.ret(-200-e.length+(m==e.length?0:-100),[0,m]):l>-1?this.ret(-700-e.length,[l,l+this.pattern.length]):d==a?this.ret(-900-e.length,[p,m]):u==a?this.result((f?-200:0)-100-700+(y?0:-1100),s,e):2==t.length?null:this.result((i[0]?-700:0)-200-1100,i,e)}result(e,t,n){let i=[],o=0;for(let s of t){let e=s+(this.astral?(0,r.codePointSize)((0,r.codePointAt)(n,s)):1);o&&i[o-1]==s?i[o-1]=e:(i[o++]=s,i[o++]=e)}return this.ret(e-n.length,i)}}class be{constructor(e){this.pattern=e,this.matched=[],this.score=0,this.folded=e.toLowerCase()}match(e){if(e.length<this.pattern.length)return null;let t=e.slice(0,this.pattern.length),n=t==this.pattern?0:t.toLowerCase()==this.folded?-200:null;return null==n?null:(this.matched=[0,t.length],this.score=n+(e.length==this.pattern.length?0:-100),this)}}const we=r.Facet.define({combine:e=>(0,r.combineConfig)(e,{activateOnTyping:!0,activateOnCompletion:()=>!1,activateOnTypingDelay:100,selectOnOpen:!0,override:null,closeOnBlur:!0,maxRenderedOptions:100,defaultKeymap:!0,tooltipClass:()=>"",optionClass:()=>"",aboveCursor:!1,icons:!0,addToOptions:[],positionInfo:ke,filterStrict:!1,compareCompletions:(e,t)=>(e.sortText||e.label).localeCompare(t.sortText||t.label),interactionDelay:75,updateSyncTime:100},{defaultKeymap:(e,t)=>e&&t,closeOnBlur:(e,t)=>e&&t,icons:(e,t)=>e&&t,tooltipClass:(e,t)=>n=>xe(e(n),t(n)),optionClass:(e,t)=>n=>xe(e(n),t(n)),addToOptions:(e,t)=>e.concat(t),filterStrict:(e,t)=>e||t})});function xe(e,t){return e?t?e+" "+t:e:t}function ke(e,t,n,r,o,s){let l,a,c=e.textDirection==i.Direction.RTL,h=c,u=!1,f="top",d=t.left-o.left,p=o.right-t.right,m=r.right-r.left,g=r.bottom-r.top;if(h&&d<Math.min(m,p)?h=!1:!h&&p<Math.min(m,d)&&(h=!0),m<=(h?d:p))l=Math.max(o.top,Math.min(n.top,o.bottom-g))-t.top,a=Math.min(400,h?d:p);else{u=!0,a=Math.min(400,(c?t.right:o.right-t.left)-30);let e=o.bottom-t.bottom;e>=g||e>t.top?l=n.bottom-t.top:(f="bottom",l=t.bottom-n.top)}return{style:`${f}: ${l/((t.bottom-t.top)/s.offsetHeight)}px; max-width: ${a/((t.right-t.left)/s.offsetWidth)}px`,class:"cm-completionInfo-"+(u?c?"left-narrow":"right-narrow":h?"left":"right")}}const Se=r.StateEffect.define();function Ce(e,t,n){if(e<=n)return{from:0,to:e};if(t<0&&(t=0),t<=e>>1){let e=Math.floor(t/n);return{from:e*n,to:(e+1)*n}}let i=Math.floor((e-t)/n);return{from:e-(i+1)*n,to:e-i*n}}class Ae{constructor(e,t,n){this.view=e,this.stateField=t,this.applyCompletion=n,this.info=null,this.infoDestroy=null,this.placeInfoReq={read:()=>this.measureInfo(),write:e=>this.placeInfo(e),key:this},this.space=null,this.currentClass="";let i=e.state.field(t),{options:r,selected:o}=i.open,s=e.state.facet(we);this.optionContent=function(e){let t=e.addToOptions.slice();return e.icons&&t.push({render(e){let t=document.createElement("div");return t.classList.add("cm-completionIcon"),e.type&&t.classList.add(...e.type.split(/\s+/g).map(e=>"cm-completionIcon-"+e)),t.setAttribute("aria-hidden","true"),t},position:20}),t.push({render(e,t,n,i){let r=document.createElement("span");r.className="cm-completionLabel";let o=e.displayLabel||e.label,s=0;for(let l=0;l<i.length;){let e=i[l++],t=i[l++];e>s&&r.appendChild(document.createTextNode(o.slice(s,e)));let n=r.appendChild(document.createElement("span"));n.appendChild(document.createTextNode(o.slice(e,t))),n.className="cm-completionMatchedText",s=t}return s<o.length&&r.appendChild(document.createTextNode(o.slice(s))),r},position:50},{render(e){if(!e.detail)return null;let t=document.createElement("span");return t.className="cm-completionDetail",t.textContent=e.detail,t},position:80}),t.sort((e,t)=>e.position-t.position).map(e=>e.render)}(s),this.optionClass=s.optionClass,this.tooltipClass=s.tooltipClass,this.range=Ce(r.length,o,s.maxRenderedOptions),this.dom=document.createElement("div"),this.dom.className="cm-tooltip-autocomplete",this.updateTooltipClass(e.state),this.dom.addEventListener("mousedown",n=>{let{options:i}=e.state.field(t).open;for(let t,r=n.target;r&&r!=this.dom;r=r.parentNode)if("LI"==r.nodeName&&(t=/-(\d+)$/.exec(r.id))&&+t[1]<i.length)return this.applyCompletion(e,i[+t[1]]),void n.preventDefault();if(n.target==this.list){let t=this.list.classList.contains("cm-completionListIncompleteTop")&&n.clientY<this.list.firstChild.getBoundingClientRect().top?this.range.from-1:this.list.classList.contains("cm-completionListIncompleteBottom")&&n.clientY>this.list.lastChild.getBoundingClientRect().bottom?this.range.to:null;null!=t&&(e.dispatch({effects:Se.of(t)}),n.preventDefault())}}),this.dom.addEventListener("focusout",t=>{let n=e.state.field(this.stateField,!1);n&&n.tooltip&&e.state.facet(we).closeOnBlur&&t.relatedTarget!=e.contentDOM&&e.dispatch({effects:ye.of(null)})}),this.showOptions(r,i.id)}mount(){this.updateSel()}showOptions(e,t){this.list&&this.list.remove(),this.list=this.dom.appendChild(this.createListBox(e,t,this.range)),this.list.addEventListener("scroll",()=>{this.info&&this.view.requestMeasure(this.placeInfoReq)})}update(e){var t;let n=e.state.field(this.stateField),i=e.startState.field(this.stateField);if(this.updateTooltipClass(e.state),n!=i){let{options:r,selected:o,disabled:s}=n.open;i.open&&i.open.options==r||(this.range=Ce(r.length,o,e.state.facet(we).maxRenderedOptions),this.showOptions(r,n.id)),this.updateSel(),s!=(null===(t=i.open)||void 0===t?void 0:t.disabled)&&this.dom.classList.toggle("cm-tooltip-autocomplete-disabled",!!s)}}updateTooltipClass(e){let t=this.tooltipClass(e);if(t!=this.currentClass){for(let e of this.currentClass.split(" "))e&&this.dom.classList.remove(e);for(let e of t.split(" "))e&&this.dom.classList.add(e);this.currentClass=t}}positioned(e){this.space=e,this.info&&this.view.requestMeasure(this.placeInfoReq)}updateSel(){let e=this.view.state.field(this.stateField),t=e.open;(t.selected>-1&&t.selected<this.range.from||t.selected>=this.range.to)&&(this.range=Ce(t.options.length,t.selected,this.view.state.facet(we).maxRenderedOptions),this.showOptions(t.options,e.id));let n=this.updateSelectedOption(t.selected);if(n){this.destroyInfo();let{completion:r}=t.options[t.selected],{info:o}=r;if(!o)return;let s="string"===typeof o?document.createTextNode(o):o(r);if(!s)return;"then"in s?s.then(t=>{t&&this.view.state.field(this.stateField,!1)==e&&this.addInfoPane(t,r)}).catch(e=>(0,i.logException)(this.view.state,e,"completion info")):(this.addInfoPane(s,r),n.setAttribute("aria-describedby",this.info.id))}}addInfoPane(e,t){this.destroyInfo();let n=this.info=document.createElement("div");if(n.className="cm-tooltip cm-completionInfo",n.id="cm-completionInfo-"+Math.floor(65535*Math.random()).toString(16),null!=e.nodeType)n.appendChild(e),this.infoDestroy=null;else{let{dom:t,destroy:i}=e;n.appendChild(t),this.infoDestroy=i||null}this.dom.appendChild(n),this.view.requestMeasure(this.placeInfoReq)}updateSelectedOption(e){let t=null;for(let n=this.list.firstChild,i=this.range.from;n;n=n.nextSibling,i++)"LI"==n.nodeName&&n.id?i==e?n.hasAttribute("aria-selected")||(n.setAttribute("aria-selected","true"),t=n):n.hasAttribute("aria-selected")&&(n.removeAttribute("aria-selected"),n.removeAttribute("aria-describedby")):i--;return t&&function(e,t){let n=e.getBoundingClientRect(),i=t.getBoundingClientRect(),r=n.height/e.offsetHeight;i.top<n.top?e.scrollTop-=(n.top-i.top)/r:i.bottom>n.bottom&&(e.scrollTop+=(i.bottom-n.bottom)/r)}(this.list,t),t}measureInfo(){let e=this.dom.querySelector("[aria-selected]");if(!e||!this.info)return null;let t=this.dom.getBoundingClientRect(),n=this.info.getBoundingClientRect(),i=e.getBoundingClientRect(),r=this.space;if(!r){let e=this.dom.ownerDocument.documentElement;r={left:0,top:0,right:e.clientWidth,bottom:e.clientHeight}}return i.top>Math.min(r.bottom,t.bottom)-10||i.bottom<Math.max(r.top,t.top)+10?null:this.view.state.facet(we).positionInfo(this.view,t,i,n,r,this.dom)}placeInfo(e){this.info&&(e?(e.style&&(this.info.style.cssText=e.style),this.info.className="cm-tooltip cm-completionInfo "+(e.class||"")):this.info.style.cssText="top: -1e6px")}createListBox(e,t,n){const i=document.createElement("ul");i.id=t,i.setAttribute("role","listbox"),i.setAttribute("aria-expanded","true"),i.setAttribute("aria-label",this.view.state.phrase("Completions")),i.addEventListener("mousedown",e=>{e.target==i&&e.preventDefault()});let r=null;for(let o=n.from;o<n.to;o++){let{completion:s,match:l}=e[o],{section:a}=s;if(a){let e="string"==typeof a?a:a.name;if(e!=r&&(o>n.from||0==n.from))if(r=e,"string"!=typeof a&&a.header)i.appendChild(a.header(a));else{i.appendChild(document.createElement("completion-section")).textContent=e}}const c=i.appendChild(document.createElement("li"));c.id=t+"-"+o,c.setAttribute("role","option");let h=this.optionClass(s);h&&(c.className=h);for(let e of this.optionContent){let t=e(s,this.view.state,this.view,l);t&&c.appendChild(t)}}return n.from&&i.classList.add("cm-completionListIncompleteTop"),n.to<e.length&&i.classList.add("cm-completionListIncompleteBottom"),i}destroyInfo(){this.info&&(this.infoDestroy&&this.infoDestroy(),this.info.remove(),this.info=null)}destroy(){this.destroyInfo()}}function Ee(e,t){return n=>new Ae(n,e,t)}function Me(e){return 100*(e.boost||0)+(e.apply?10:0)+(e.info?5:0)+(e.type?1:0)}class De{constructor(e,t,n,i,r,o){this.options=e,this.attrs=t,this.tooltip=n,this.timestamp=i,this.selected=r,this.disabled=o}setSelected(e,t){return e==this.selected||e>=this.options.length?this:new De(this.options,Pe(t,e),this.tooltip,this.timestamp,e,this.disabled)}static build(e,t,n,i,r,o){if(i&&!o&&e.some(e=>e.isPending))return i.setDisabled();let s=function(e,t){let n=[],i=null,r=null,o=e=>{n.push(e);let{section:t}=e.completion;if(t){i||(i=[]);let e="string"==typeof t?t:t.name;i.some(t=>t.name==e)||i.push("string"==typeof t?{name:e}:t)}},s=t.facet(we);for(let h of e)if(h.hasResult()){let e=h.result.getMatch;if(!1===h.result.filter)for(let t of h.result.options)o(new ce(t,h.source,e?e(t):[],1e9-n.length));else{let n,i=t.sliceDoc(h.from,h.to),l=s.filterStrict?new be(i):new ve(i);for(let t of h.result.options)if(n=l.match(t.label)){let i=t.displayLabel?e?e(t,n.matched):[]:n.matched,s=n.score+(t.boost||0);if(o(new ce(t,h.source,i,s)),"object"==typeof t.section&&"dynamic"===t.section.rank){let{name:e}=t.section;r||(r=Object.create(null)),r[e]=Math.max(s,r[e]||-1e9)}}}}if(i){let e=Object.create(null),t=0,o=(e,t)=>("dynamic"===e.rank&&"dynamic"===t.rank?r[t.name]-r[e.name]:0)||("number"==typeof e.rank?e.rank:1e9)-("number"==typeof t.rank?t.rank:1e9)||(e.name<t.name?-1:1);for(let n of i.sort(o))t-=1e5,e[n.name]=t;for(let i of n){let{section:t}=i.completion;t&&(i.score+=e["string"==typeof t?t:t.name])}}let l=[],a=null,c=s.compareCompletions;for(let h of n.sort((e,t)=>t.score-e.score||c(e.completion,t.completion))){let e=h.completion;!a||a.label!=e.label||a.detail!=e.detail||null!=a.type&&null!=e.type&&a.type!=e.type||a.apply!=e.apply||a.boost!=e.boost?l.push(h):Me(h.completion)>Me(a)&&(l[l.length-1]=h),a=h.completion}return l}(e,t);if(!s.length)return i&&e.some(e=>e.isPending)?i.setDisabled():null;let l=t.facet(we).selectOnOpen?0:-1;if(i&&i.selected!=l&&-1!=i.selected){let e=i.options[i.selected].completion;for(let t=0;t<s.length;t++)if(s[t].completion==e){l=t;break}}return new De(s,Pe(n,l),{pos:e.reduce((e,t)=>t.hasResult()?Math.min(e,t.from):e,1e8),create:Ve,above:r.aboveCursor},i?i.timestamp:Date.now(),l,!1)}map(e){return new De(this.options,this.attrs,{...this.tooltip,pos:e.mapPos(this.tooltip.pos)},this.timestamp,this.selected,this.disabled)}setDisabled(){return new De(this.options,this.attrs,this.tooltip,this.timestamp,this.selected,!0)}}class Oe{constructor(e,t,n){this.active=e,this.id=t,this.open=n}static start(){return new Oe(Be,"cm-ac-"+Math.floor(2e6*Math.random()).toString(36),null)}update(e){let{state:t}=e,n=t.facet(we),i=(n.override||t.languageDataAt("autocomplete",he(t)).map(me)).map(t=>(this.active.find(e=>e.source==t)||new Le(t,this.active.some(e=>0!=e.state)?1:0)).update(e,n));i.length==this.active.length&&i.every((e,t)=>e==this.active[t])&&(i=this.active);let r=this.open,o=e.effects.some(e=>e.is(Fe));r&&e.docChanged&&(r=r.map(e.changes)),e.selection||i.some(t=>t.hasResult()&&e.changes.touchesRange(t.from,t.to))||!function(e,t){if(e==t)return!0;for(let n=0,i=0;;){for(;n<e.length&&!e[n].hasResult();)n++;for(;i<t.length&&!t[i].hasResult();)i++;let r=n==e.length,o=i==t.length;if(r||o)return r==o;if(e[n++].result!=t[i++].result)return!1}}(i,this.active)||o?r=De.build(i,t,this.id,r,n,o):r&&r.disabled&&!i.some(e=>e.isPending)&&(r=null),!r&&i.every(e=>!e.isPending)&&i.some(e=>e.hasResult())&&(i=i.map(e=>e.hasResult()?new Le(e.source,0):e));for(let s of e.effects)s.is(Se)&&(r=r&&r.setSelected(s.value,this.id));return i==this.active&&r==this.open?this:new Oe(i,this.id,r)}get tooltip(){return this.open?this.open.tooltip:null}get attrs(){return this.open?this.open.attrs:this.active.length?Te:Ie}}const Te={"aria-autocomplete":"list"},Ie={};function Pe(e,t){let n={"aria-autocomplete":"list","aria-haspopup":"listbox","aria-controls":e};return t>-1&&(n["aria-activedescendant"]=e+"-"+t),n}const Be=[];function Re(e,t){if(e.isUserEvent("input.complete")){let n=e.annotation(fe);if(n&&t.activateOnCompletion(n))return 12}let n=e.isUserEvent("input.type");return n&&t.activateOnTyping?5:n?1:e.isUserEvent("delete.backward")?2:e.selection?8:e.docChanged?16:0}class Le{constructor(e,t,n=!1){this.source=e,this.state=t,this.explicit=n}hasResult(){return!1}get isPending(){return 1==this.state}update(e,t){let n=Re(e,t),i=this;(8&n||16&n&&this.touches(e))&&(i=new Le(i.source,0)),4&n&&0==i.state&&(i=new Le(this.source,1)),i=i.updateFor(e,n);for(let r of e.effects)if(r.is(ge))i=new Le(i.source,1,r.value);else if(r.is(ye))i=new Le(i.source,0);else if(r.is(Fe))for(let e of r.value)e.source==i.source&&(i=e);return i}updateFor(e,t){return this.map(e.changes)}map(e){return this}touches(e){return e.changes.touchesRange(he(e.state))}}class Ne extends Le{constructor(e,t,n,i,r,o){super(e,3,t),this.limit=n,this.result=i,this.from=r,this.to=o}hasResult(){return!0}updateFor(e,t){var n;if(!(3&t))return this.map(e.changes);let i=this.result;i.map&&!e.changes.empty&&(i=i.map(i,e.changes));let r=e.changes.mapPos(this.from),o=e.changes.mapPos(this.to,1),s=he(e.state);if(s>o||!i||2&t&&(he(e.startState)==this.from||s<this.limit))return new Le(this.source,4&t?1:0);let l=e.changes.mapPos(this.limit);return function(e,t,n,i){if(!e)return!1;let r=t.sliceDoc(n,i);return"function"==typeof e?e(r,n,i,t):ue(e,!0).test(r)}(i.validFor,e.state,r,o)?new Ne(this.source,this.explicit,l,i,r,o):i.update&&(i=i.update(i,r,o,new se(e.state,s,!1)))?new Ne(this.source,this.explicit,l,i,i.from,null!==(n=i.to)&&void 0!==n?n:he(e.state)):new Le(this.source,1,this.explicit)}map(e){if(e.empty)return this;return(this.result.map?this.result.map(this.result,e):this.result)?new Ne(this.source,this.explicit,e.mapPos(this.limit),this.result,e.mapPos(this.from),e.mapPos(this.to,1)):new Le(this.source,0)}touches(e){return e.changes.touchesRange(this.from,this.to)}}const Fe=r.StateEffect.define({map:(e,t)=>e.map(e=>e.map(t))}),We=r.StateField.define({create:()=>Oe.start(),update:(e,t)=>e.update(t),provide:e=>[i.showTooltip.from(e,e=>e.tooltip),i.EditorView.contentAttributes.from(e,e=>e.attrs)]});function je(e,t){const n=t.completion.apply||t.completion.label;let i=e.state.field(We).active.find(e=>e.source==t.source);return i instanceof Ne&&("string"==typeof n?e.dispatch({...de(e.state,n,i.from,i.to),annotations:fe.of(t.completion)}):n(e,t.completion,i.from,i.to),!0)}const Ve=Ee(We,je);function _e(e,t="option"){return n=>{let r=n.state.field(We,!1);if(!r||!r.open||r.open.disabled||Date.now()-r.open.timestamp<n.state.facet(we).interactionDelay)return!1;let o,s=1;"page"==t&&(o=(0,i.getTooltip)(n,r.open.tooltip))&&(s=Math.max(2,Math.floor(o.dom.offsetHeight/o.dom.querySelector("li").offsetHeight)-1));let{length:l}=r.open.options,a=r.open.selected>-1?r.open.selected+s*(e?1:-1):e?0:l-1;return a<0?a="page"==t?0:l-1:a>=l&&(a="page"==t?l-1:0),n.dispatch({effects:Se.of(a)}),!0}}const qe=e=>!!e.state.field(We,!1)&&(e.dispatch({effects:ge.of(!0)}),!0);class ze{constructor(e,t){this.active=e,this.context=t,this.time=Date.now(),this.updates=[],this.done=void 0}}const Ue=i.ViewPlugin.fromClass(class{constructor(e){this.view=e,this.debounceUpdate=-1,this.running=[],this.debounceAccept=-1,this.pendingStart=!1,this.composing=0;for(let t of e.state.field(We).active)t.isPending&&this.startQuery(t)}update(e){let t=e.state.field(We),n=e.state.facet(we);if(!e.selectionSet&&!e.docChanged&&e.startState.field(We)==t)return;let r=e.transactions.some(e=>{let t=Re(e,n);return 8&t||(e.selection||e.docChanged)&&!(3&t)});for(let l=0;l<this.running.length;l++){let t=this.running[l];if(r||t.context.abortOnDocChange&&e.docChanged||t.updates