import{NodeType as U,NodeProp as v,IterMode as pt,Tree as y,TreeFragment as H,Parser as xe,NodeSet as gt}from"@lezer/common";import{Facet as x,EditorState as L,StateEffect as V,StateField as Q,countColumn as mt,RangeSet as Te,combineConfig as Ae,RangeSetBuilder as X,Prec as Ce}from"@codemirror/state";import{Decoration as m,Direction as $,ViewPlugin as _,logException as kt,EditorView as w,WidgetType as Pe,gutter as bt,GutterMarker as yt}from"@codemirror/view";import{tags as c,styleTags as wt,tagHighlighter as St,highlightTree as vt}from"@lezer/highlight";const Y="\u037C",Ie=typeof Symbol>"u"?"__"+Y:Symbol.for(Y),ee=typeof Symbol>"u"?"__styleSet"+Math.floor(Math.random()*1e8):Symbol("styleSet"),De=typeof globalThis<"u"?globalThis:typeof window<"u"?window:{};class Oe{constructor(e,t){this.rules=[];let{finish:r}=t||{};function i(o){return/^@/.test(o)?[o]:o.split(/,\s*/)}function s(o,l,a,h){let f=[],u=/^@(\w+)\b/.exec(o[0]),p=u&&u[1]=="keyframes";if(u&&l==null)return a.push(o[0]+";");for(let d in l){let g=l[d];if(/&/.test(d))s(d.split(/,\s*/).map(S=>o.map(N=>S.replace(/&/,N))).reduce((S,N)=>S.concat(N)),g,a);else if(g&&typeof g=="object"){if(!u)throw new RangeError("The value of a property ("+d+") should be a primitive value.");s(i(d),g,f,p)}else g!=null&&f.push(d.replace(/_.*/,"").replace(/[A-Z]/g,S=>"-"+S.toLowerCase())+": "+g+";")}(f.length||p)&&a.push((r&&!u&&!h?o.map(r):o).join(", ")+" {"+f.join(" ")+"}")}for(let o in e)s(i(o),e[o],this.rules)}getRules(){return this.rules.join(` `)}static newName(){let e=De[Ie]||1;return De[Ie]=e+1,Y+e.toString(36)}static mount(e,t,r){let i=e[ee],s=r&&r.nonce;i?s&&i.setNonce(s):i=new xt(e,s),i.mount(Array.isArray(t)?t:[t],e)}}let Me=new Map;class xt{constructor(e,t){let r=e.ownerDocument||e,i=r.defaultView;if(!e.head&&e.adoptedStyleSheets&&i.CSSStyleSheet){let s=Me.get(r);if(s)return e[ee]=s;this.sheet=new i.CSSStyleSheet,Me.set(r,this)}else this.styleTag=r.createElement("style"),t&&this.styleTag.setAttribute("nonce",t);this.modules=[],e[ee]=this}mount(e,t){let r=this.sheet,i=0,s=0;for(let o=0;o-1&&(this.modules.splice(a,1),s--,a=-1),a==-1){if(this.modules.splice(s++,0,l),r)for(let h=0;he.concat(n):void 0})}const Ne=new v;class k{constructor(e,t,r=[],i=""){this.data=e,this.name=i,L.prototype.hasOwnProperty("tree")||Object.defineProperty(L.prototype,"tree",{get(){return b(this)}}),this.parser=t,this.extension=[T.of(this),L.languageData.of((s,o,l)=>{let a=Le(s,o,l),h=a.type.prop(C);if(!h)return[];let f=s.facet(h),u=a.type.prop(Ne);if(u){let p=a.resolve(o-a.from,l);for(let d of u)if(d.test(p,s)){let g=s.facet(d.facet);return d.type=="replace"?g:g.concat(f)}}return f})].concat(r)}isActiveAt(e,t,r=-1){return Le(e,t,r).type.prop(C)==this.data}findRegions(e){let t=e.facet(T);if(t?.data==this.data)return[{from:0,to:e.doc.length}];if(!t||!t.allowsNesting)return[];let r=[],i=(s,o)=>{if(s.prop(C)==this.data){r.push({from:o,to:o+s.length});return}let l=s.prop(v.mounted);if(l){if(l.tree.prop(C)==this.data){if(l.overlay)for(let a of l.overlay)r.push({from:a.from+o,to:a.to+o});else r.push({from:o,to:o+s.length});return}else if(l.overlay){let a=r.length;if(i(l.tree,l.overlay[0].from+o),r.length>a)return}}for(let a=0;ar.isTop?t:void 0)]}),e.name)}configure(e,t){return new J(this.data,this.parser.configure(e),t||this.name)}get allowsNesting(){return this.parser.hasWrappers()}}function b(n){let e=n.field(k.state,!1);return e?e.tree:y.empty}function Be(n,e,t=50){var r;let i=(r=n.field(k.state,!1))===null||r===void 0?void 0:r.context;if(!i)return null;let s=i.viewport;i.updateViewport({from:0,to:e});let o=i.isDone(e)||i.work(t,e)?i.tree:null;return i.updateViewport(s),o}function Tt(n,e=n.doc.length){var t;return((t=n.field(k.state,!1))===null||t===void 0?void 0:t.context.isDone(e))||!1}function At(n,e=n.viewport.to,t=100){let r=Be(n.state,e,t);return r!=b(n.state)&&n.dispatch({}),!!r}function Ct(n){var e;return((e=n.plugin(je))===null||e===void 0?void 0:e.isWorking())||!1}class Re{constructor(e){this.doc=e,this.cursorPos=0,this.string="",this.cursor=e.iter()}get length(){return this.doc.length}syncTo(e){return this.string=this.cursor.next(e-this.cursorPos).value,this.cursorPos=e+this.string.length,this.cursorPos-this.string.length}chunk(e){return this.syncTo(e),this.string}get lineChunks(){return!0}read(e,t){let r=this.cursorPos-this.string.length;return e=this.cursorPos?this.doc.sliceString(e,t):this.string.slice(e-r,t-r)}}let B=null;class I{constructor(e,t,r=[],i,s,o,l,a){this.parser=e,this.state=t,this.fragments=r,this.tree=i,this.treeLen=s,this.viewport=o,this.skipped=l,this.scheduleOn=a,this.parse=null,this.tempSkipped=[]}static create(e,t,r){return new I(e,t,[],y.empty,0,r,[],null)}startParse(){return this.parser.startParse(new Re(this.state.doc),this.fragments)}work(e,t){return t!=null&&t>=this.state.doc.length&&(t=void 0),this.tree!=y.empty&&this.isDone(t??this.state.doc.length)?(this.takeTree(),!0):this.withContext(()=>{var r;if(typeof e=="number"){let i=Date.now()+e;e=()=>Date.now()>i}for(this.parse||(this.parse=this.startParse()),t!=null&&(this.parse.stoppedAt==null||this.parse.stoppedAt>t)&&t=this.treeLen&&((this.parse.stoppedAt==null||this.parse.stoppedAt>e)&&this.parse.stopAt(e),this.withContext(()=>{for(;!(t=this.parse.advance()););}),this.treeLen=e,this.tree=t,this.fragments=this.withoutTempSkipped(H.addTree(this.tree,this.fragments,!0)),this.parse=null)}withContext(e){let t=B;B=this;try{return e()}finally{B=t}}withoutTempSkipped(e){for(let t;t=this.tempSkipped.pop();)e=Ee(e,t.from,t.to);return e}changes(e,t){let{fragments:r,tree:i,treeLen:s,viewport:o,skipped:l}=this;if(this.takeTree(),!e.empty){let a=[];if(e.iterChangedRanges((h,f,u,p)=>a.push({fromA:h,toA:f,fromB:u,toB:p})),r=H.applyChanges(r,a),i=y.empty,s=0,o={from:e.mapPos(o.from,-1),to:e.mapPos(o.to,1)},this.skipped.length){l=[];for(let h of this.skipped){let f=e.mapPos(h.from,1),u=e.mapPos(h.to,-1);fe.from&&(this.fragments=Ee(this.fragments,i,s),this.skipped.splice(r--,1))}return this.skipped.length>=t?!1:(this.reset(),!0)}reset(){this.parse&&(this.takeTree(),this.parse=null)}skipUntilInView(e,t){this.skipped.push({from:e,to:t})}static getSkippingParser(e){return new class extends xe{createParse(t,r,i){let s=i[0].from,o=i[i.length-1].to;return{parsedPos:s,advance(){let a=B;if(a){for(let h of i)a.tempSkipped.push(h);e&&(a.scheduleOn=a.scheduleOn?Promise.all([a.scheduleOn,e]):e)}return this.parsedPos=o,new y(U.none,[],[],o-s)},stoppedAt:null,stopAt(){}}}}}isDone(e){e=Math.min(e,this.state.doc.length);let t=this.fragments;return this.treeLen>=e&&t.length&&t[0].from==0&&t[0].to>=e}static get(){return B}}function Ee(n,e,t){return H.applyChanges(n,[{fromA:e,toA:t,fromB:e,toB:t}])}class M{constructor(e){this.context=e,this.tree=e.tree}apply(e){if(!e.docChanged&&this.tree==this.context.tree)return this;let t=this.context.changes(e.changes,e.state),r=this.context.treeLen==e.startState.doc.length?void 0:Math.max(e.changes.mapPos(this.context.treeLen),t.viewport.to);return t.work(20,r)||t.takeTree(),new M(t)}static init(e){let t=Math.min(3e3,e.doc.length),r=I.create(e.facet(T).parser,e,{from:0,to:t});return r.work(20,t)||r.takeTree(),new M(r)}}k.state=Q.define({create:M.init,update(n,e){for(let t of e.effects)if(t.is(k.setState))return t.value;return e.startState.facet(T)!=e.state.facet(T)?M.init(e.state):n.apply(e)}});let Fe=n=>{let e=setTimeout(()=>n(),500);return()=>clearTimeout(e)};typeof requestIdleCallback<"u"&&(Fe=n=>{let e=-1,t=setTimeout(()=>{e=requestIdleCallback(n,{timeout:400})},100);return()=>e<0?clearTimeout(t):cancelIdleCallback(e)});const re=typeof navigator<"u"&&(!((te=navigator.scheduling)===null||te===void 0)&&te.isInputPending)?()=>navigator.scheduling.isInputPending():null,je=_.fromClass(class{constructor(e){this.view=e,this.working=null,this.workScheduled=0,this.chunkEnd=-1,this.chunkBudget=-1,this.work=this.work.bind(this),this.scheduleWork()}update(e){let t=this.view.state.field(k.state).context;(t.updateViewport(e.view.viewport)||this.view.viewport.to>t.treeLen)&&this.scheduleWork(),(e.docChanged||e.selectionSet)&&(this.view.hasFocus&&(this.chunkBudget+=50),this.scheduleWork()),this.checkAsyncSchedule(t)}scheduleWork(){if(this.working)return;let{state:e}=this.view,t=e.field(k.state);(t.tree!=t.context.tree||!t.context.isDone(e.doc.length))&&(this.working=Fe(this.work))}work(e){this.working=null;let t=Date.now();if(this.chunkEndi+1e3,a=s.context.work(()=>re&&re()||Date.now()>o,i+(l?0:1e5));this.chunkBudget-=Date.now()-t,(a||this.chunkBudget<=0)&&(s.context.takeTree(),this.view.dispatch({effects:k.setState.of(new M(s.context))})),this.chunkBudget>0&&!(a&&!l)&&this.scheduleWork(),this.checkAsyncSchedule(s.context)}checkAsyncSchedule(e){e.scheduleOn&&(this.workScheduled++,e.scheduleOn.then(()=>this.scheduleWork()).catch(t=>kt(this.view.state,t)).then(()=>this.workScheduled--),e.scheduleOn=null)}destroy(){this.working&&this.working()}isWorking(){return!!(this.working||this.workScheduled>0)}},{eventHandlers:{focus(){this.scheduleWork()}}}),T=x.define({combine(n){return n.length?n[0]:null},enables:n=>[k.state,je,w.contentAttributes.compute([n],e=>{let t=e.facet(n);return t&&t.name?{"data-language":t.name}:{}})]});class Pt{constructor(e,t=[]){this.language=e,this.support=t,this.extension=[e,t]}}class Se{constructor(e,t,r,i,s,o=void 0){this.name=e,this.alias=t,this.extensions=r,this.filename=i,this.loadFunc=s,this.support=o,this.loading=null}load(){return this.loading||(this.loading=this.loadFunc().then(e=>this.support=e,e=>{throw this.loading=null,e}))}static of(e){let{load:t,support:r}=e;if(!t){if(!r)throw new RangeError("Must pass either 'load' or 'support' to LanguageDescription.of");t=()=>Promise.resolve(r)}return new Se(e.name,(e.alias||[]).concat(e.name).map(i=>i.toLowerCase()),e.extensions||[],e.filename,t,r)}static matchFilename(e,t){for(let i of e)if(i.filename&&i.filename.test(t))return i;let r=/\.([^.]+)$/.exec(t);if(r){for(let i of e)if(i.extensions.indexOf(r[1])>-1)return i}return null}static matchLanguageName(e,t,r=!0){t=t.toLowerCase();for(let i of e)if(i.alias.some(s=>s==t))return i;if(r)for(let i of e)for(let s of i.alias){let o=t.indexOf(s);if(o>-1&&(s.length>2||!/\w/.test(t[o-1])&&!/\w/.test(t[o+s.length])))return i}return null}}const ie=x.define(),se=x.define({combine:n=>{if(!n.length)return" ";let e=n[0];if(!e||/\S/.test(e)||Array.from(e).some(t=>t!=e[0]))throw new Error("Invalid indent unit: "+JSON.stringify(n[0]));return e}});function R(n){let e=n.facet(se);return e.charCodeAt(0)==9?n.tabSize*e.length:e.length}function oe(n,e){let t="",r=n.tabSize,i=n.facet(se)[0];if(i==" "){for(;e>=r;)t+=" ",e-=r;i=" "}for(let s=0;s=e?Dt(n,t,e):null}function It(n,e,t){let r=Object.create(null),i=new q(n,{overrideIndentation:o=>{var l;return(l=r[o])!==null&&l!==void 0?l:-1}}),s=[];for(let o=e;o<=t;){let l=n.doc.lineAt(o);o=l.to+1;let a=le(i,l.from);if(a==null)continue;/\S/.test(l.text)||(a=0);let h=/^\s*/.exec(l.text)[0],f=oe(n,a);h!=f&&(r[l.from]=a,s.push({from:l.from,to:l.from+h.length,insert:f}))}return n.changes(s)}class q{constructor(e,t={}){this.state=e,this.options=t,this.unit=R(e)}lineAt(e,t=1){let r=this.state.doc.lineAt(e),{simulateBreak:i,simulateDoubleBreak:s}=this.options;return i!=null&&i>=r.from&&i<=r.to?s&&i==e?{text:"",from:e}:(t<0?i-1&&(s+=o-this.countColumn(r,r.search(/\S|$/))),s}countColumn(e,t=e.length){return mt(e,this.state.tabSize,t)}lineIndent(e,t=1){let{text:r,from:i}=this.lineAt(e,t),s=this.options.overrideIndentation;if(s){let o=s(i);if(o>-1)return o}return this.countColumn(r,r.search(/\S|$/))}get simulatedBreak(){return this.options.simulateBreak||null}}const We=new v;function Dt(n,e,t){let r=e.resolveStack(t),i=r.node.enterUnfinishedNodesBefore(t);if(i!=r.node){let s=[];for(let o=i;o!=r.node;o=o.parent)s.push(o);for(let o=s.length-1;o>=0;o--)r={node:s[o],next:r}}return Ue(r,n,t)}function Ue(n,e,t){for(let r=n;r;r=r.next){let i=Mt(r.node);if(i)return i(K.create(e,t,r))}return 0}function Ot(n){return n.pos==n.options.simulateBreak&&n.options.simulateDoubleBreak}function Mt(n){let e=n.type.prop(We);if(e)return e;let t=n.firstChild,r;if(t&&(r=t.type.prop(v.closedBy))){let i=n.lastChild,s=i&&r.indexOf(i.name)>-1;return o=>He(o,!0,1,void 0,s&&!Ot(o)?i.from:void 0)}return n.parent==null?Nt:null}function Nt(){return 0}class K extends q{constructor(e,t,r){super(e.state,e.options),this.base=e,this.pos=t,this.context=r}get node(){return this.context.node}static create(e,t,r){return new K(e,t,r)}get textAfter(){return this.textAfterPos(this.pos)}get baseIndent(){return this.baseIndentFor(this.node)}baseIndentFor(e){let t=this.state.doc.lineAt(e.from);for(;;){let r=e.resolve(t.from);for(;r.parent&&r.parent.from==r.from;)r=r.parent;if(Lt(r,e))break;t=this.state.doc.lineAt(r.from)}return this.lineIndent(t.from)}continue(){return Ue(this.context.next,this.base,this.pos)}}function Lt(n,e){for(let t=e;t;t=t.parent)if(n==t)return!0;return!1}function Bt(n){let e=n.node,t=e.childAfter(e.from),r=e.lastChild;if(!t)return null;let i=n.options.simulateBreak,s=n.state.doc.lineAt(t.from),o=i==null||i<=s.from?s.to:Math.min(s.to,i);for(let l=t.to;;){let a=e.childAfter(l);if(!a||a==r)return null;if(!a.type.isSkipped)return a.fromHe(r,e,t,n)}function He(n,e,t,r,i){let s=n.textAfter,o=s.match(/^\s*/)[0].length,l=r&&s.slice(o,o+r.length)==r||i==n.pos+o,a=e?Bt(n):null;return a?l?n.column(a.from):n.column(a.to):n.baseIndent+(l?0:n.unit*t)}const Et=n=>n.baseIndent;function Ft({except:n,units:e=1}={}){return t=>{let r=n&&n.test(t.textAfter);return t.baseIndent+(r?0:e*t.unit)}}const jt=200;function Wt(){return L.transactionFilter.of(n=>{if(!n.docChanged||!n.isUserEvent("input.type")&&!n.isUserEvent("input.complete"))return n;let e=n.startState.languageDataAt("indentOnInput",n.startState.selection.main.head);if(!e.length)return n;let t=n.newDoc,{head:r}=n.newSelection.main,i=t.lineAt(r);if(r>i.from+jt)return n;let s=t.sliceString(i.from,r);if(!e.some(h=>h.test(s)))return n;let{state:o}=n,l=-1,a=[];for(let{head:h}of o.selection.ranges){let f=o.doc.lineAt(h);if(f.from==l)continue;l=f.from;let u=le(o,f.from);if(u==null)continue;let p=/^\s*/.exec(f.text)[0],d=oe(o,u);p!=d&&a.push({from:f.from,to:f.from+p.length,insert:d})}return a.length?[n,{changes:a,sequential:!0}]:n})}const Ve=x.define(),$e=new v;function Ut(n){let e=n.firstChild,t=n.lastChild;return e&&e.tot)continue;if(s&&l.from=e&&h.to>t&&(s=h)}}return s}function Vt(n){let e=n.lastChild;return e&&e.to==n.to&&e.type.isError}function D(n,e,t){for(let r of n.facet(Ve)){let i=r(n,e,t);if(i)return i}return Ht(n,e,t)}function _e(n,e){let t=e.mapPos(n.from,1),r=e.mapPos(n.to,-1);return t>=r?void 0:{from:t,to:r}}const O=V.define({map:_e}),P=V.define({map:_e});function ae(n){let e=[];for(let{head:t}of n.state.selection.ranges)e.some(r=>r.from<=t&&r.to>=t)||e.push(n.lineBlockAt(t));return e}const A=Q.define({create(){return m.none},update(n,e){n=n.map(e.changes);for(let t of e.effects)if(t.is(O)&&!_t(n,t.value.from,t.value.to)){let{preparePlaceholder:r}=e.state.facet(fe),i=r?m.replace({widget:new Kt(r(e.state,t.value))}):Ze;n=n.update({add:[i.range(t.value.from,t.value.to)]})}else t.is(P)&&(n=n.update({filter:(r,i)=>t.value.from!=r||t.value.to!=i,filterFrom:t.value.from,filterTo:t.value.to}));if(e.selection){let t=!1,{head:r}=e.selection.main;n.between(r,r,(i,s)=>{ir&&(t=!0)}),t&&(n=n.update({filterFrom:r,filterTo:r,filter:(i,s)=>s<=r||i>=r}))}return n},provide:n=>w.decorations.from(n),toJSON(n,e){let t=[];return n.between(0,e.doc.length,(r,i)=>{t.push(r,i)}),t},fromJSON(n){if(!Array.isArray(n)||n.length%2)throw new RangeError("Invalid JSON for fold state");let e=[];for(let t=0;t{(!i||i.from>s)&&(i={from:s,to:o})}),i}function _t(n,e,t){let r=!1;return n.between(e,e,(i,s)=>{i==e&&s==t&&(r=!0)}),r}function he(n,e){return n.field(A,!1)?e:e.concat(V.appendConfig.of(ue()))}const qe=n=>{for(let e of ae(n)){let t=D(n.state,e.from,e.to);if(t)return n.dispatch({effects:he(n.state,[O.of(t),z(n,t)])}),!0}return!1},ze=n=>{if(!n.state.field(A,!1))return!1;let e=[];for(let t of ae(n)){let r=E(n.state,t.from,t.to);r&&e.push(P.of(r),z(n,r,!1))}return e.length&&n.dispatch({effects:e}),e.length>0};function z(n,e,t=!0){let r=n.state.doc.lineAt(e.from).number,i=n.state.doc.lineAt(e.to).number;return w.announce.of(`${n.state.phrase(t?"Folded lines":"Unfolded lines")} ${r} ${n.state.phrase("to")} ${i}.`)}const Ge=n=>{let{state:e}=n,t=[];for(let r=0;r{let e=n.state.field(A,!1);if(!e||!e.size)return!1;let t=[];return e.between(0,n.state.doc.length,(r,i)=>{t.push(P.of({from:r,to:i}))}),n.dispatch({effects:t}),!0};function qt(n,e){for(let t=e;;){let r=D(n.state,t.from,t.to);if(r&&r.to>e.from)return r;if(!t.from)return null;t=n.lineBlockAt(t.from-1)}}const zt=n=>{let e=[];for(let t of ae(n)){let r=E(n.state,t.from,t.to);if(r)e.push(P.of(r),z(n,r,!1));else{let i=qt(n,t);i&&e.push(O.of(i),z(n,i))}}return e.length>0&&n.dispatch({effects:he(n.state,e)}),!!e.length},Gt=[{key:"Ctrl-Shift-[",mac:"Cmd-Alt-[",run:qe},{key:"Ctrl-Shift-]",mac:"Cmd-Alt-]",run:ze},{key:"Ctrl-Alt-[",run:Ge},{key:"Ctrl-Alt-]",run:Je}],Jt={placeholderDOM:null,preparePlaceholder:null,placeholderText:"\u2026"},fe=x.define({combine(n){return Ae(n,Jt)}});function ue(n){let e=[A,Xt];return n&&e.push(fe.of(n)),e}function Ke(n,e){let{state:t}=n,r=t.facet(fe),i=o=>{let l=n.lineBlockAt(n.posAtDOM(o.target)),a=E(n.state,l.from,l.to);a&&n.dispatch({effects:P.of(a)}),o.preventDefault()};if(r.placeholderDOM)return r.placeholderDOM(n,i,e);let s=document.createElement("span");return s.textContent=r.placeholderText,s.setAttribute("aria-label",t.phrase("folded code")),s.title=t.phrase("unfold"),s.className="cm-foldPlaceholder",s.onclick=i,s}const Ze=m.replace({widget:new class extends Pe{toDOM(n){return Ke(n,null)}}});class Kt extends Pe{constructor(e){super(),this.value=e}eq(e){return this.value==e.value}toDOM(e){return Ke(e,this.value)}}const Zt={openText:"\u2304",closedText:"\u203A",markerDOM:null,domEventHandlers:{},foldingChanged:()=>!1};class ce extends yt{constructor(e,t){super(),this.config=e,this.open=t}eq(e){return this.config==e.config&&this.open==e.open}toDOM(e){if(this.config.markerDOM)return this.config.markerDOM(this.open);let t=document.createElement("span");return t.textContent=this.open?this.config.openText:this.config.closedText,t.title=e.state.phrase(this.open?"Fold line":"Unfold line"),t}}function Qt(n={}){let e=Object.assign(Object.assign({},Zt),n),t=new ce(e,!0),r=new ce(e,!1),i=_.fromClass(class{constructor(o){this.from=o.viewport.from,this.markers=this.buildMarkers(o)}update(o){(o.docChanged||o.viewportChanged||o.startState.facet(T)!=o.state.facet(T)||o.startState.field(A,!1)!=o.state.field(A,!1)||b(o.startState)!=b(o.state)||e.foldingChanged(o))&&(this.markers=this.buildMarkers(o.view))}buildMarkers(o){let l=new X;for(let a of o.viewportLineBlocks){let h=E(o.state,a.from,a.to)?r:D(o.state,a.from,a.to)?t:null;h&&l.add(a.from,a.from,h)}return l.finish()}}),{domEventHandlers:s}=e;return[i,bt({class:"cm-foldGutter",markers(o){var l;return((l=o.plugin(i))===null||l===void 0?void 0:l.markers)||Te.empty},initialSpacer(){return new ce(e,!1)},domEventHandlers:Object.assign(Object.assign({},s),{click:(o,l,a)=>{if(s.click&&s.click(o,l,a))return!0;let h=E(o.state,l.from,l.to);if(h)return o.dispatch({effects:P.of(h)}),!0;let f=D(o.state,l.from,l.to);return f?(o.dispatch({effects:O.of(f)}),!0):!1}})}),ue()]}const Xt=w.baseTheme({".cm-foldPlaceholder":{backgroundColor:"#eee",border:"1px solid #ddd",color:"#888",borderRadius:".2em",margin:"0 1px",padding:"0 1px",cursor:"pointer"},".cm-foldGutter span":{padding:"0 1px",cursor:"pointer"}});class W{constructor(e,t){this.specs=e;let r;function i(l){let a=Oe.newName();return(r||(r=Object.create(null)))["."+a]=l,a}const s=typeof t.all=="string"?t.all:t.all?i(t.all):void 0,o=t.scope;this.scope=o instanceof k?l=>l.prop(C)==o.data:o?l=>l==o:void 0,this.style=St(e.map(l=>({tag:l.tag,class:l.class||i(Object.assign({},l,{tag:null}))})),{all:s}).style,this.module=r?new Oe(r):null,this.themeType=t.themeType}static define(e,t){return new W(e,t||{})}}const de=x.define(),Qe=x.define({combine(n){return n.length?[n[0]]:null}});function G(n){let e=n.facet(de);return e.length?e:n.facet(Qe)}function Yt(n,e){let t=[nn],r;return n instanceof W&&(n.module&&t.push(w.styleModule.of(n.module)),r=n.themeType),e?.fallback?t.push(Qe.of(n)):r?t.push(de.computeN([w.darkTheme],i=>i.facet(w.darkTheme)==(r=="dark")?[n]:[])):t.push(de.of(n)),t}function en(n,e,t){let r=G(n),i=null;if(r){for(let s of r)if(!s.scope||t&&s.scope(t)){let o=s.style(e);o&&(i=i?i+" "+o:o)}}return i}class tn{constructor(e){this.markCache=Object.create(null),this.tree=b(e.state),this.decorations=this.buildDeco(e,G(e.state)),this.decoratedTo=e.viewport.to}update(e){let t=b(e.state),r=G(e.state),i=r!=G(e.startState),{viewport:s}=e.view,o=e.changes.mapPos(this.decoratedTo,1);t.length=s.to?(this.decorations=this.decorations.map(e.changes),this.decoratedTo=o):(t!=this.tree||e.viewportChanged||i)&&(this.tree=t,this.decorations=this.buildDeco(e.view,r),this.decoratedTo=s.to)}buildDeco(e,t){if(!t||!this.tree.length)return m.none;let r=new X;for(let{from:i,to:s}of e.visibleRanges)vt(this.tree,t,(o,l,a)=>{r.add(o,l,this.markCache[a]||(this.markCache[a]=m.mark({class:a})))},i,s);return r.finish()}}const nn=Ce.high(_.fromClass(tn,{decorations:n=>n.decorations})),rn=W.define([{tag:c.meta,color:"#404740"},{tag:c.link,textDecoration:"underline"},{tag:c.heading,textDecoration:"underline",fontWeight:"bold"},{tag:c.emphasis,fontStyle:"italic"},{tag:c.strong,fontWeight:"bold"},{tag:c.strikethrough,textDecoration:"line-through"},{tag:c.keyword,color:"#708"},{tag:[c.atom,c.bool,c.url,c.contentSeparator,c.labelName],color:"#219"},{tag:[c.literal,c.inserted],color:"#164"},{tag:[c.string,c.deleted],color:"#a11"},{tag:[c.regexp,c.escape,c.special(c.string)],color:"#e40"},{tag:c.definition(c.variableName),color:"#00f"},{tag:c.local(c.variableName),color:"#30a"},{tag:[c.typeName,c.namespace],color:"#085"},{tag:c.className,color:"#167"},{tag:[c.special(c.variableName),c.macroName],color:"#256"},{tag:c.definition(c.propertyName),color:"#00c"},{tag:c.comment,color:"#940"},{tag:c.invalid,color:"#f00"}]),sn=w.baseTheme({"&.cm-focused .cm-matchingBracket":{backgroundColor:"#328c8252"},"&.cm-focused .cm-nonmatchingBracket":{backgroundColor:"#bb555544"}}),Xe=1e4,Ye="()[]{}",et=x.define({combine(n){return Ae(n,{afterCursor:!0,brackets:Ye,maxScanDistance:Xe,renderMatch:an})}}),on=m.mark({class:"cm-matchingBracket"}),ln=m.mark({class:"cm-nonmatchingBracket"});function an(n){let e=[],t=n.matched?on:ln;return e.push(t.range(n.start.from,n.start.to)),n.end&&e.push(t.range(n.end.from,n.end.to)),e}const hn=Q.define({create(){return m.none},update(n,e){if(!e.docChanged&&!e.selection)return n;let t=[],r=e.state.facet(et);for(let i of e.state.selection.ranges){if(!i.empty)continue;let s=F(e.state,i.head,-1,r)||i.head>0&&F(e.state,i.head-1,1,r)||r.afterCursor&&(F(e.state,i.head,1,r)||i.headw.decorations.from(n)}),fn=[hn,sn];function un(n={}){return[et.of(n),fn]}const tt=new v;function pe(n,e,t){let r=n.prop(e<0?v.openedBy:v.closedBy);if(r)return r;if(n.name.length==1){let i=t.indexOf(n.name);if(i>-1&&i%2==(e<0?1:0))return[t[i+e]]}return null}function ge(n){let e=n.type.prop(tt);return e?e(n.node):n}function F(n,e,t,r={}){let i=r.maxScanDistance||Xe,s=r.brackets||Ye,o=b(n),l=o.resolveInner(e,t);for(let a=l;a;a=a.parent){let h=pe(a.type,t,s);if(h&&a.from0?e>=f.from&&ef.from&&e<=f.to))return cn(n,e,t,a,f,h,s)}}return dn(n,e,t,o,l.type,i,s)}function cn(n,e,t,r,i,s,o){let l=r.parent,a={from:i.from,to:i.to},h=0,f=l?.cursor();if(f&&(t<0?f.childBefore(r.from):f.childAfter(r.to)))do if(t<0?f.to<=r.from:f.from>=r.to){if(h==0&&s.indexOf(f.type.name)>-1&&f.from0)return null;let h={from:t<0?e-1:e,to:t>0?e+1:e},f=n.doc.iterRange(e,t>0?n.doc.length:0),u=0;for(let p=0;!f.next().done&&p<=s;){let d=f.value;t<0&&(p+=d.length);let g=e+p*t;for(let S=t>0?0:d.length-1,N=t>0?d.length:-1;S!=N;S+=t){let Z=o.indexOf(d[S]);if(!(Z<0||r.resolveInner(g+S,1).type!=i))if(Z%2==0==t>0)u++;else{if(u==1)return{start:h,end:{from:g+S,to:g+S+1},matched:Z>>1==a>>1};u--}}t>0&&(p+=d.length)}return f.done?{start:h,matched:!1}:null}function nt(n,e,t,r=0,i=0){e==null&&(e=n.search(/[^\s\u00a0]/),e==-1&&(e=n.length));let s=i;for(let o=r;o=this.string.length}sol(){return this.pos==0}peek(){return this.string.charAt(this.pos)||void 0}next(){if(this.post}eatSpace(){let e=this.pos;for(;/[\s\u00a0]/.test(this.string.charAt(this.pos));)++this.pos;return this.pos>e}skipToEnd(){this.pos=this.string.length}skipTo(e){let t=this.string.indexOf(e,this.pos);if(t>-1)return this.pos=t,!0}backUp(e){this.pos-=e}column(){return this.lastColumnPosr?o.toLowerCase():o,s=this.string.substr(this.pos,e.length);return i(s)==i(e)?(t!==!1&&(this.pos+=e.length),!0):null}else{let i=this.string.slice(this.pos).match(e);return i&&i.index>0?null:(i&&t!==!1&&(this.pos+=i[0].length),i)}}current(){return this.string.slice(this.start,this.pos)}}function pn(n){return{name:n.name||"",token:n.token,blankLine:n.blankLine||(()=>{}),startState:n.startState||(()=>!0),copyState:n.copyState||gn,indent:n.indent||(()=>null),languageData:n.languageData||{},tokenTable:n.tokenTable||be}}function gn(n){if(typeof n!="object")return n;let e={};for(let t in n){let r=n[t];e[t]=r instanceof Array?r.slice():r}return e}const rt=new WeakMap;class ve extends k{constructor(e){let t=ne(e.languageData),r=pn(e),i,s=new class extends xe{createParse(o,l,a){return new kn(i,o,l,a)}};super(t,s,[ie.of((o,l)=>this.getIndent(o,l))],e.name),this.topNode=wn(t),i=this,this.streamParser=r,this.stateAfter=new v({perNode:!0}),this.tokenTable=e.tokenTable?new ht(r.tokenTable):yn}static define(e){return new ve(e)}getIndent(e,t){let r=b(e.state),i=r.resolve(t);for(;i&&i.type!=this.topNode;)i=i.parent;if(!i)return null;let s,{overrideIndentation:o}=e.options;o&&(s=rt.get(e.state),s!=null&&s1e4)return null;for(;a=r&&t+e.length<=i&&e.prop(n.stateAfter);if(s)return{state:n.streamParser.copyState(s),pos:t+e.length};for(let o=e.children.length-1;o>=0;o--){let l=e.children[o],a=t+e.positions[o],h=l instanceof y&&a=e.length)return e;!i&&e.type==n.topNode&&(i=!0);for(let s=e.children.length-1;s>=0;s--){let o=e.positions[s],l=e.children[s],a;if(ot&&ke(n,i.tree,0-i.offset,t,o),a;if(l&&(a=it(n,i.tree,t+i.offset,l.pos+i.offset,!1)))return{state:l.state,tree:a}}return{state:n.streamParser.startState(r?R(r):4),tree:y.empty}}class kn{constructor(e,t,r,i){this.lang=e,this.input=t,this.fragments=r,this.ranges=i,this.stoppedAt=null,this.chunks=[],this.chunkPos=[],this.chunk=[],this.chunkReused=void 0,this.rangeIndex=0,this.to=i[i.length-1].to;let s=I.get(),o=i[0].from,{state:l,tree:a}=mn(e,r,o,s?.state);this.state=l,this.parsedPos=this.chunkStart=o+a.length;for(let h=0;h=t?this.finish():e&&this.parsedPos>=e.viewport.to?(e.skipUntilInView(this.parsedPos,t),this.finish()):null}stopAt(e){this.stoppedAt=e}lineAfter(e){let t=this.input.chunk(e);if(this.input.lineChunks)t==` `&&(t="");else{let r=t.indexOf(` `);r>-1&&(t=t.slice(0,r))}return e+t.length<=this.to?t:t.slice(0,this.to-e)}nextLine(){let e=this.parsedPos,t=this.lineAfter(e),r=e+t.length;for(let i=this.rangeIndex;;){let s=this.ranges[i].to;if(s>=r||(t=t.slice(0,s-(r-t.length)),i++,i==this.ranges.length))break;let o=this.ranges[i].from,l=this.lineAfter(o);t+=l,r=o+l.length}return{line:t,end:r}}skipGapsTo(e,t,r){for(;;){let i=this.ranges[this.rangeIndex].to,s=e+t;if(r>0?i>s:i>=s)break;let o=this.ranges[++this.rangeIndex].from;t+=o-i}return t}moveRangeIndex(){for(;this.ranges[this.rangeIndex].to1){s=this.skipGapsTo(t,s,1),t+=s;let o=this.chunk.length;s=this.skipGapsTo(r,s,-1),r+=s,i+=this.chunk.length-o}return this.chunk.push(e,t,r,i),s}parseLine(e){let{line:t,end:r}=this.nextLine(),i=0,{streamParser:s}=this.lang,o=new me(t,e?e.state.tabSize:4,e?R(e.state):2);if(o.eol())s.blankLine(this.state,o.indentUnit);else for(;!o.eol();){let l=st(s.token,o,this.state);if(l&&(i=this.emitToken(this.lang.tokenTable.resolve(l),this.parsedPos+o.start,this.parsedPos+o.pos,4,i)),o.start>1e4)break}this.parsedPos=r,this.moveRangeIndex(),this.parsedPose.start)return i}throw new Error("Stream parser failed to advance stream.")}const be=Object.create(null),j=[U.none],bn=new gt(j),ot=[],lt=Object.create(null),at=Object.create(null);for(let[n,e]of[["variable","variableName"],["variable-2","variableName.special"],["string-2","string.special"],["def","variableName.definition"],["tag","tagName"],["attribute","attributeName"],["type","typeName"],["builtin","variableName.standard"],["qualifier","modifier"],["error","invalid"],["header","heading"],["property","propertyName"]])at[n]=ft(be,e);class ht{constructor(e){this.extra=e,this.table=Object.assign(Object.create(null),at)}resolve(e){return e?this.table[e]||(this.table[e]=ft(this.extra,e)):0}}const yn=new ht(be);function ye(n,e){ot.indexOf(n)>-1||(ot.push(n),console.warn(e))}function ft(n,e){let t=[];for(let l of e.split(" ")){let a=[];for(let h of l.split(".")){let f=n[h]||c[h];f?typeof f=="function"?a.length?a=a.map(f):ye(h,`Modifier ${h} used at start of tag`):a.length?ye(h,`Tag ${h} used as modifier`):a=Array.isArray(f)?f:[f]:ye(h,`Unknown highlighting tag ${h}`)}for(let h of a)t.push(h)}if(!t.length)return 0;let r=e.replace(/ /g,"_"),i=r+" "+t.map(l=>l.id),s=lt[i];if(s)return s.id;let o=lt[i]=U.define({id:j.length,name:r,props:[wt({[r]:t})]});return j.push(o),o.id}function wn(n){let e=U.define({id:j.length,name:"Document",props:[C.add(()=>n)],top:!0});return j.push(e),e}function ut(n){return n.length<=4096&&/[\u0590-\u05f4\u0600-\u06ff\u0700-\u08ac\ufb50-\ufdff]/.test(n)}function ct(n){for(let e=n.iter();!e.next().done;)if(ut(e.value))return!0;return!1}function Sn(n){let e=!1;return n.iterChanges((t,r,i,s,o)=>{!e&&ct(o)&&(e=!0)}),e}const we=x.define({combine:n=>n.some(e=>e)});function vn(n={}){let e=[xn];return n.alwaysIsolate&&e.push(we.of(!0)),e}const xn=_.fromClass(class{constructor(n){this.always=n.state.facet(we)||n.textDirection!=$.LTR||n.state.facet(w.perLineTextDirection),this.hasRTL=!this.always&&ct(n.state.doc),this.tree=b(n.state),this.decorations=this.always||this.hasRTL?dt(n,this.tree,this.always):m.none}update(n){let e=n.state.facet(we)||n.view.textDirection!=$.LTR||n.state.facet(w.perLineTextDirection);if(!e&&!this.hasRTL&&Sn(n.changes)&&(this.hasRTL=!0),!e&&!this.hasRTL)return;let t=b(n.state);(e!=this.always||t!=this.tree||n.docChanged||n.viewportChanged)&&(this.tree=t,this.always=e,this.decorations=dt(n.view,t,e))}},{provide:n=>{function e(t){var r,i;return(i=(r=t.plugin(n))===null||r===void 0?void 0:r.decorations)!==null&&i!==void 0?i:m.none}return[w.outerDecorations.of(e),Ce.lowest(w.bidiIsolatedRanges.of(e))]}});function dt(n,e,t){let r=new X,i=n.visibleRanges;t||(i=Tn(i,n.state.doc));for(let{from:s,to:o}of i)e.iterate({enter:l=>{let a=l.type.prop(v.isolate);a&&r.add(l.from,l.to,An[a])},from:s,to:o});return r.finish()}function Tn(n,e){let t=e.iter(),r=0,i=[],s=null;for(let{from:o,to:l}of n)if(!(s&&s.to>o&&(o=s.to,o>=l)))for(r+t.value.lengtha-10?s.to=Math.min(l,h):i.push(s={from:a,to:Math.min(l,h)})),h>=l)break;r=h,t.next()}return i}const An={rtl:m.mark({class:"cm-iso",inclusive:!0,attributes:{dir:"rtl"},bidiIsolate:$.RTL}),ltr:m.mark({class:"cm-iso",inclusive:!0,attributes:{dir:"ltr"},bidiIsolate:$.LTR}),auto:m.mark({class:"cm-iso",inclusive:!0,attributes:{dir:"auto"},bidiIsolate:null})};export{Re as DocInput,W as HighlightStyle,q as IndentContext,J as LRLanguage,k as Language,Se as LanguageDescription,Pt as LanguageSupport,I as ParseContext,ve as StreamLanguage,me as StringStream,K as TreeIndentContext,vn as bidiIsolates,un as bracketMatching,tt as bracketMatchingHandle,ue as codeFolding,Ft as continuedIndent,rn as defaultHighlightStyle,ne as defineLanguageFacet,Rt as delimitedIndent,Be as ensureSyntaxTree,Et as flatIndent,Ge as foldAll,qe as foldCode,O as foldEffect,Qt as foldGutter,Ut as foldInside,Gt as foldKeymap,$e as foldNodeProp,Ve as foldService,A as foldState,D as foldable,$t as foldedRanges,At as forceParsing,R as getIndentUnit,le as getIndentation,en as highlightingFor,We as indentNodeProp,Wt as indentOnInput,It as indentRange,ie as indentService,oe as indentString,se as indentUnit,T as language,C as languageDataProp,F as matchBrackets,Ne as sublanguageProp,Yt as syntaxHighlighting,Ct as syntaxParserRunning,b as syntaxTree,Tt as syntaxTreeAvailable,zt as toggleFold,Je as unfoldAll,ze as unfoldCode,P as unfoldEffect};