import{ViewPlugin as ne,gutter as se,EditorView as R,getPanel as oe,hoverTooltip as le,showPanel as re,Decoration as y,showTooltip as ae,GutterMarker as ce,WidgetType as fe,logException as de}from"@codemirror/view";import{StateEffect as C,StateField as A,RangeSet as M,Facet as G,combineConfig as z,RangeSetBuilder as ue}from"@codemirror/state";function p(){var t=arguments[0];typeof t=="string"&&(t=document.createElement(t));var e=1,n=arguments[1];if(n&&typeof n=="object"&&n.nodeType==null&&!Array.isArray(n)){for(var i in n)if(Object.prototype.hasOwnProperty.call(n,i)){var s=n[i];typeof s=="string"?t.setAttribute(i,s):s!=null&&(t[i]=s)}e++}for(;e<arguments.length;e++)H(t,arguments[e]);return t}function H(t,e){if(typeof e=="string")t.appendChild(document.createTextNode(e));else if(e!=null)if(e.nodeType!=null)t.appendChild(e);else if(Array.isArray(e))for(var n=0;n<e.length;n++)H(t,e[n]);else throw new RangeError("Unsupported child node: "+e)}class ${constructor(e,n,i){this.from=e,this.to=n,this.diagnostic=i}}class k{constructor(e,n,i){this.diagnostics=e,this.panel=n,this.selected=i}static init(e,n,i){let s=e,o=i.facet(b).markerFilter;o&&(s=o(s,i));let l=e.slice().sort((u,d)=>u.from-d.from||u.to-d.to),a=new ue,r=[],c=0;for(let u=0;;){let d=u==l.length?null:l[u];if(!d&&!r.length)break;let m,w;for(r.length?(m=c,w=r.reduce((f,x)=>Math.min(f,x.to),d&&d.from>m?d.from:1e8)):(m=d.from,w=d.to,r.push(d),u++);u<l.length;){let f=l[u];if(f.from==m&&(f.to>f.from||f.to==m))r.push(f),u++,w=Math.min(f.to,w);else{w=Math.min(f.from,w);break}}let B=Q(r);if(r.some(f=>f.from==f.to||f.from==f.to-1&&i.doc.lineAt(f.from).to==f.from))a.add(m,m,y.widget({widget:new ye(B),diagnostics:r.slice()}));else{let f=r.reduce((x,j)=>j.markClass?x+" "+j.markClass:x,"");a.add(m,w,y.mark({class:"cm-lintRange cm-lintRange-"+B+f,diagnostics:r.slice(),inclusiveEnd:r.some(x=>x.to>w)}))}c=w;for(let f=0;f<r.length;f++)r[f].to<=c&&r.splice(f--,1)}let h=a.finish();return new k(h,n,v(h))}}function v(t,e=null,n=0){let i=null;return t.between(n,1e9,(s,o,{spec:l})=>{if(!(e&&l.diagnostics.indexOf(e)<0))if(!i)i=new $(s,o,e||l.diagnostics[0]);else{if(l.diagnostics.indexOf(i.diagnostic)<0)return!1;i=new $(i.from,o,i.diagnostic)}}),i}function N(t,e){let n=e.pos,i=e.end||n,s=t.state.facet(b).hideOn(t,n,i);if(s!=null)return s;let o=t.startState.doc.lineAt(e.pos);return!!(t.effects.some(l=>l.is(T))||t.changes.touchesRange(o.from,Math.max(o.to,i)))}function V(t,e){return t.field(g,!1)?e:e.concat(C.appendConfig.of(ie))}function Y(t,e){return{effects:V(t,[T.of(e)])}}const T=C.define(),I=C.define(),_=C.define(),g=A.define({create(){return new k(y.none,null,null)},update(t,e){if(e.docChanged&&t.diagnostics.size){let n=t.diagnostics.map(e.changes),i=null,s=t.panel;if(t.selected){let o=e.changes.mapPos(t.selected.from,1);i=v(n,t.selected.diagnostic,o)||v(n,null,o)}!n.size&&s&&e.state.facet(b).autoPanel&&(s=null),t=new k(n,s,i)}for(let n of e.effects)if(n.is(T)){let i=e.state.facet(b).autoPanel?n.value.length?S.open:null:t.panel;t=k.init(n.value,i,e.state)}else n.is(I)?t=new k(t.diagnostics,n.value?S.open:null,t.selected):n.is(_)&&(t=new k(t.diagnostics,t.panel,n.value));return t},provide:t=>[re.from(t,e=>e.panel),R.decorations.from(t,e=>e.diagnostics)]});function me(t){let e=t.field(g,!1);return e?e.diagnostics.size:0}const he=y.mark({class:"cm-lintRange cm-lintRange-active"});function ge(t,e,n){let{diagnostics:i}=t.state.field(g),s,o=-1,l=-1;i.between(e-(n<0?1:0),e+(n>0?1:0),(r,c,{spec:h})=>{if(e>=r&&e<=c&&(r==c||(e>r||n>0)&&(e<c||n<0)))return s=h.diagnostics,o=r,l=c,!1});let a=t.state.facet(b).tooltipFilter;return s&&a&&(s=a(s,t.state)),s?{pos:o,end:l,above:t.state.doc.lineAt(o).to<l,create(){return{dom:q(t,s)}}}:null}function q(t,e){return p("ul",{class:"cm-tooltip-lint"},e.map(n=>X(t,n,!1)))}const U=t=>{let e=t.state.field(g,!1);(!e||!e.panel)&&t.dispatch({effects:V(t.state,[I.of(!0)])});let n=oe(t,S.open);return n&&n.dom.querySelector(".cm-panel-lint ul").focus(),!0},F=t=>{let e=t.state.field(g,!1);return!e||!e.panel?!1:(t.dispatch({effects:I.of(!1)}),!0)},W=t=>{let e=t.state.field(g,!1);if(!e)return!1;let n=t.state.selection.main,i=e.diagnostics.iter(n.to+1);return!i.value&&(i=e.diagnostics.iter(0),!i.value||i.from==n.from&&i.to==n.to)?!1:(t.dispatch({selection:{anchor:i.from,head:i.to},scrollIntoView:!0}),!0)},pe=t=>{let{state:e}=t,n=e.field(g,!1);if(!n)return!1;let i=e.selection.main,s,o,l,a;return n.diagnostics.between(0,e.doc.length,(r,c)=>{c<i.to&&(s==null||s<r)&&(s=r,o=c),(l==null||r>l)&&(l=r,a=c)}),l==null||s==null&&l==i.from?!1:(t.dispatch({selection:{anchor:s??l,head:o??a},scrollIntoView:!0}),!0)},be=[{key:"Mod-Shift-m",run:U,preventDefault:!0},{key:"F8",run:W}],Z=ne.fromClass(class{constructor(t){this.view=t,this.timeout=-1,this.set=!0;let{delay:e}=t.state.facet(b);this.lintTime=Date.now()+e,this.run=this.run.bind(this),this.timeout=setTimeout(this.run,e)}run(){clearTimeout(this.timeout);let t=Date.now();if(t<this.lintTime-10)this.timeout=setTimeout(this.run,this.lintTime-t);else{this.set=!1;let{state:e}=this.view,{sources:n}=e.facet(b);n.length&&we(n.map(i=>Promise.resolve(i(this.view))),i=>{this.view.state.doc==e.doc&&this.view.dispatch(Y(this.view.state,i.reduce((s,o)=>s.concat(o))))},i=>{de(this.view.state,i)})}}update(t){let e=t.state.facet(b);(t.docChanged||e!=t.startState.facet(b)||e.needsRefresh&&e.needsRefresh(t))&&(this.lintTime=Date.now()+e.delay,this.set||(this.set=!0,this.timeout=setTimeout(this.run,e.delay)))}force(){this.set&&(this.lintTime=Date.now(),this.run())}destroy(){clearTimeout(this.timeout)}});function we(t,e,n){let i=[],s=-1;for(let o of t)o.then(l=>{i.push(l),clearTimeout(s),i.length==t.length?e(i):s=setTimeout(()=>e(i),200)},n)}const b=G.define({combine(t){return Object.assign({sources:t.map(e=>e.source).filter(e=>e!=null)},z(t.map(e=>e.config),{delay:750,markerFilter:null,tooltipFilter:null,needsRefresh:null,hideOn:()=>null},{needsRefresh:(e,n)=>e?n?i=>e(i)||n(i):e:n}))}});function ke(t,e={}){return[b.of({source:t,config:e}),Z,ie]}function xe(t){let e=t.plugin(Z);e&&e.force()}function K(t){let e=[];if(t)e:for(let{name:n}of t){for(let i=0;i<n.length;i++){let s=n[i];if(/[a-zA-Z]/.test(s)&&!e.some(o=>o.toLowerCase()==s.toLowerCase())){e.push(s);continue e}}e.push("")}return e}function X(t,e,n){var i;let s=n?K(e.actions):[];return p("li",{class:"cm-diagnostic cm-diagnostic-"+e.severity},p("span",{class:"cm-diagnosticText"},e.renderMessage?e.renderMessage(t):e.message),(i=e.actions)===null||i===void 0?void 0:i.map((o,l)=>{let a=!1,r=d=>{if(d.preventDefault(),a)return;a=!0;let m=v(t.state.field(g).diagnostics,e);m&&o.apply(t,m.from,m.to)},{name:c}=o,h=s[l]?c.indexOf(s[l]):-1,u=h<0?c:[c.slice(0,h),p("u",c.slice(h,h+1)),c.slice(h+1)];return p("button",{type:"button",class:"cm-diagnosticAction",onclick:r,onmousedown:r,"aria-label":` Action: ${c}${h<0?"":` (access key "${s[l]})"`}.`},u)}),e.source&&p("div",{class:"cm-diagnosticSource"},e.source))}class ye extends fe{constructor(e){super(),this.sev=e}eq(e){return e.sev==this.sev}toDOM(){return p("span",{class:"cm-lintPoint cm-lintPoint-"+this.sev})}}class J{constructor(e,n){this.diagnostic=n,this.id="item_"+Math.floor(Math.random()*4294967295).toString(16),this.dom=X(e,n,!0),this.dom.id=this.id,this.dom.setAttribute("role","option")}}class S{constructor(e){this.view=e,this.items=[];let n=s=>{if(s.keyCode==27)F(this.view),this.view.focus();else if(s.keyCode==38||s.keyCode==33)this.moveSelection((this.selectedIndex-1+this.items.length)%this.items.length);else if(s.keyCode==40||s.keyCode==34)this.moveSelection((this.selectedIndex+1)%this.items.length);else if(s.keyCode==36)this.moveSelection(0);else if(s.keyCode==35)this.moveSelection(this.items.length-1);else if(s.keyCode==13)this.view.focus();else if(s.keyCode>=65&&s.keyCode<=90&&this.selectedIndex>=0){let{diagnostic:o}=this.items[this.selectedIndex],l=K(o.actions);for(let a=0;a<l.length;a++)if(l[a].toUpperCase().charCodeAt(0)==s.keyCode){let r=v(this.view.state.field(g).diagnostics,o);r&&o.actions[a].apply(e,r.from,r.to)}}else return;s.preventDefault()},i=s=>{for(let o=0;o<this.items.length;o++)this.items[o].dom.contains(s.target)&&this.moveSelection(o)};this.list=p("ul",{tabIndex:0,role:"listbox","aria-label":this.view.state.phrase("Diagnostics"),onkeydown:n,onclick:i}),this.dom=p("div",{class:"cm-panel-lint"},this.list,p("button",{type:"button",name:"close","aria-label":this.view.state.phrase("close"),onclick:()=>F(this.view)},"\xD7")),this.update()}get selectedIndex(){let e=this.view.state.field(g).selected;if(!e)return-1;for(let n=0;n<this.items.length;n++)if(this.items[n].diagnostic==e.diagnostic)return n;return-1}update(){let{diagnostics:e,selected:n}=this.view.state.field(g),i=0,s=!1,o=null,l=new Set;for(e.between(0,this.view.state.doc.length,(a,r,{spec:c})=>{for(let h of c.diagnostics){if(l.has(h))continue;l.add(h);let u=-1,d;for(let m=i;m<this.items.length;m++)if(this.items[m].diagnostic==h){u=m;break}u<0?(d=new J(this.view,h),this.items.splice(i,0,d),s=!0):(d=this.items[u],u>i&&(this.items.splice(i,u-i),s=!0)),n&&d.diagnostic==n.diagnostic?d.dom.hasAttribute("aria-selected")||(d.dom.setAttribute("aria-selected","true"),o=d):d.dom.hasAttribute("aria-selected")&&d.dom.removeAttribute("aria-selected"),i++}});i<this.items.length&&!(this.items.length==1&&this.items[0].diagnostic.from<0);)s=!0,this.items.pop();this.items.length==0&&(this.items.push(new J(this.view,{from:-1,to:-1,severity:"info",message:this.view.state.phrase("No diagnostics")})),s=!0),o?(this.list.setAttribute("aria-activedescendant",o.id),this.view.requestMeasure({key:this,read:()=>({sel:o.dom.getBoundingClientRect(),panel:this.list.getBoundingClientRect()}),write:({sel:a,panel:r})=>{let c=r.height/this.list.offsetHeight;a.top<r.top?this.list.scrollTop-=(r.top-a.top)/c:a.bottom>r.bottom&&(this.list.scrollTop+=(a.bottom-r.bottom)/c)}})):this.selectedIndex<0&&this.list.removeAttribute("aria-activedescendant"),s&&this.sync()}sync(){let e=this.list.firstChild;function n(){let i=e;e=i.nextSibling,i.remove()}for(let i of this.items)if(i.dom.parentNode==this.list){for(;e!=i.dom;)n();e=i.dom.nextSibling}else this.list.insertBefore(i.dom,e);for(;e;)n()}moveSelection(e){if(this.selectedIndex<0)return;let n=this.view.state.field(g),i=v(n.diagnostics,this.items[e].diagnostic);i&&this.view.dispatch({selection:{anchor:i.from,head:i.to},scrollIntoView:!0,effects:_.of(i)})}static open(e){return new S(e)}}function P(t,e='viewBox="0 0 40 40"'){return`url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" ${e}>${encodeURIComponent(t)}</svg>')`}function D(t){return P(`<path d="m0 2.5 l2 -1.5 l1 0 l2 1.5 l1 0" stroke="${t}" fill="none" stroke-width=".7"/>`,'width="6" height="3"')}const ve=R.baseTheme({".cm-diagnostic":{padding:"3px 6px 3px 8px",marginLeft:"-1px",display:"block",whiteSpace:"pre-wrap"},".cm-diagnostic-error":{borderLeft:"5px solid #d11"},".cm-diagnostic-warning":{borderLeft:"5px solid orange"},".cm-diagnostic-info":{borderLeft:"5px solid #999"},".cm-diagnostic-hint":{borderLeft:"5px solid #66d"},".cm-diagnosticAction":{font:"inherit",border:"none",padding:"2px 4px",backgroundColor:"#444",color:"white",borderRadius:"3px",marginLeft:"8px",cursor:"pointer"},".cm-diagnosticSource":{fontSize:"70%",opacity:.7},".cm-lintRange":{backgroundPosition:"left bottom",backgroundRepeat:"repeat-x",paddingBottom:"0.7px"},".cm-lintRange-error":{backgroundImage:D("#d11")},".cm-lintRange-warning":{backgroundImage:D("orange")},".cm-lintRange-info":{backgroundImage:D("#999")},".cm-lintRange-hint":{backgroundImage:D("#66d")},".cm-lintRange-active":{backgroundColor:"#ffdd9980"},".cm-tooltip-lint":{padding:0,margin:0},".cm-lintPoint":{position:"relative","&:after":{content:'""',position:"absolute",bottom:0,left:"-2px",borderLeft:"3px solid transparent",borderRight:"3px solid transparent",borderBottom:"4px solid #d11"}},".cm-lintPoint-warning":{"&:after":{borderBottomColor:"orange"}},".cm-lintPoint-info":{"&:after":{borderBottomColor:"#999"}},".cm-lintPoint-hint":{"&:after":{borderBottomColor:"#66d"}},".cm-panel.cm-panel-lint":{position:"relative","& ul":{maxHeight:"100px",overflowY:"auto","& [aria-selected]":{backgroundColor:"#ddd","& u":{textDecoration:"underline"}},"&:focus [aria-selected]":{background_fallback:"#bdf",backgroundColor:"Highlight",color_fallback:"white",color:"HighlightText"},"& u":{textDecoration:"none"},padding:0,margin:0},"& [name=close]":{position:"absolute",top:"0",right:"2px",background:"inherit",border:"none",font:"inherit",padding:0,margin:0}}});function Ce(t){return t=="error"?4:t=="warning"?3:t=="info"?2:1}function Q(t){let e="hint",n=1;for(let i of t){let s=Ce(i.severity);s>n&&(n=s,e=i.severity)}return e}class ee extends ce{constructor(e){super(),this.diagnostics=e,this.severity=Q(e)}toDOM(e){let n=document.createElement("div");n.className="cm-lint-marker cm-lint-marker-"+this.severity;let i=this.diagnostics,s=e.state.facet(L).tooltipFilter;return s&&(i=s(i,e.state)),i.length&&(n.onmouseover=()=>Se(e,n,i)),n}}function Te(t,e){let n=i=>{let s=e.getBoundingClientRect();if(!(i.clientX>s.left-10&&i.clientX<s.right+10&&i.clientY>s.top-10&&i.clientY<s.bottom+10)){for(let o=i.target;o;o=o.parentNode)if(o.nodeType==1&&o.classList.contains("cm-tooltip-lint"))return;window.removeEventListener("mousemove",n),t.state.field(te)&&t.dispatch({effects:E.of(null)})}};window.addEventListener("mousemove",n)}function Se(t,e,n){function i(){let l=t.elementAtHeight(e.getBoundingClientRect().top+5-t.documentTop);t.coordsAtPos(l.from)&&t.dispatch({effects:E.of({pos:l.from,above:!1,clip:!1,create(){return{dom:q(t,n),getCoords:()=>e.getBoundingClientRect()}}})}),e.onmouseout=e.onmousemove=null,Te(t,e)}let{hoverTime:s}=t.state.facet(L),o=setTimeout(i,s);e.onmouseout=()=>{clearTimeout(o),e.onmouseout=e.onmousemove=null},e.onmousemove=()=>{clearTimeout(o),o=setTimeout(i,s)}}function Re(t,e){let n=Object.create(null);for(let s of e){let o=t.lineAt(s.from);(n[o.from]||(n[o.from]=[])).push(s)}let i=[];for(let s in n)i.push(new ee(n[s]).range(+s));return M.of(i,!0)}const Pe=se({class:"cm-gutter-lint",markers:t=>t.state.field(O),widgetMarker:(t,e,n)=>{let i=[];return t.state.field(O).between(n.from,n.to,(s,o,l)=>{s>n.from&&s<n.to&&i.push(...l.diagnostics)}),i.length?new ee(i):null}}),O=A.define({create(){return M.empty},update(t,e){t=t.map(e.changes);let n=e.state.facet(L).markerFilter;for(let i of e.effects)if(i.is(T)){let s=i.value;n&&(s=n(s||[],e.state)),t=Re(e.state.doc,s.slice(0))}return t}}),E=C.define(),te=A.define({create(){return null},update(t,e){return t&&e.docChanged&&(t=N(e,t)?null:Object.assign(Object.assign({},t),{pos:e.changes.mapPos(t.pos)})),e.effects.reduce((n,i)=>i.is(E)?i.value:n,t)},provide:t=>ae.from(t)}),De=R.baseTheme({".cm-gutter-lint":{width:"1.4em","& .cm-gutterElement":{padding:".2em"}},".cm-lint-marker":{width:"1em",height:"1em"},".cm-lint-marker-info":{content:P('<path fill="#aaf" stroke="#77e" stroke-width="6" stroke-linejoin="round" d="M5 5L35 5L35 35L5 35Z"/>')},".cm-lint-marker-warning":{content:P('<path fill="#fe8" stroke="#fd7" stroke-width="6" stroke-linejoin="round" d="M20 6L37 35L3 35Z"/>')},".cm-lint-marker-error":{content:P('<circle cx="20" cy="20" r="15" fill="#f87" stroke="#f43" stroke-width="6"/>')}}),ie=[g,R.decorations.compute([g],t=>{let{selected:e,panel:n}=t.field(g);return!e||!n||e.from==e.to?y.none:y.set([he.range(e.from,e.to)])}),le(ge,{hideOn:N}),ve],L=G.define({combine(t){return z(t,{hoverTime:300,markerFilter:null,tooltipFilter:null})}});function Le(t={}){return[L.of(t),O,Pe,De,te]}function Ae(t,e){let n=t.field(g,!1);if(n&&n.diagnostics.size){let i=[],s=[],o=-1;for(let l=M.iter([n.diagnostics]);;l.next()){for(let a=0;a<i.length;a++)(!l.value||l.value.spec.diagnostics.indexOf(i[a])<0)&&(e(i[a],s[a],o),i.splice(a,1),s.splice(a--,1));if(!l.value)break;for(let a of l.value.spec.diagnostics)i.indexOf(a)<0&&(i.push(a),s.push(l.from));o=l.to}}}export{F as closeLintPanel,me as diagnosticCount,Ae as forEachDiagnostic,xe as forceLinting,Le as lintGutter,be as lintKeymap,ke as linter,W as nextDiagnostic,U as openLintPanel,pe as previousDiagnostic,Y as setDiagnostics,T as setDiagnosticsEffect};