import{ViewPlugin as ie,gutter as ne,EditorView as S,getPanel as se,hoverTooltip as oe,showPanel as le,Decoration as k,showTooltip as re,GutterMarker as ae,WidgetType as ce,logException as fe}from"@codemirror/view";import{StateEffect as v,StateField as A,RangeSet as D,Facet as j,combineConfig as G,RangeSetBuilder as de}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++)z(t,arguments[e]);return t}function z(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++)z(t,e[n]);else throw new RangeError("Unsupported child node: "+e)}class H{constructor(e,n,i){this.from=e,this.to=n,this.diagnostic=i}}class w{constructor(e,n,i){this.diagnostics=e,this.panel=n,this.selected=i}static init(e,n,i){let s=i.facet(b).markerFilter;s&&(e=s(e,i));let o=e.slice().sort((f,m)=>f.from-m.from||f.to-m.to),r=new de,l=[],a=0;for(let f=0;;){let m=f==o.length?null:o[f];if(!m&&!l.length)break;let u,h;for(l.length?(u=a,h=l.reduce((d,x)=>Math.min(d,x.to),m&&m.from>u?m.from:1e8)):(u=m.from,h=m.to,l.push(m),f++);f<o.length;){let d=o[f];if(d.from==u&&(d.to>d.from||d.to==u))l.push(d),f++,h=Math.min(d.to,h);else{h=Math.min(d.from,h);break}}let E=J(l);if(l.some(d=>d.from==d.to||d.from==d.to-1&&i.doc.lineAt(d.from).to==d.from))r.add(u,u,k.widget({widget:new ke(E),diagnostics:l.slice()}));else{let d=l.reduce((x,B)=>B.markClass?x+" "+B.markClass:x,"");r.add(u,h,k.mark({class:"cm-lintRange cm-lintRange-"+E+d,diagnostics:l.slice(),inclusiveEnd:l.some(x=>x.to>h)}))}a=h;for(let d=0;d<l.length;d++)l[d].to<=a&&l.splice(d--,1)}let c=r.finish();return new w(c,n,y(c))}}function y(t,e=null,n=0){let i=null;return t.between(n,1e9,(s,o,{spec:r})=>{if(!(e&&r.diagnostics.indexOf(e)<0))if(!i)i=new H(s,o,e||r.diagnostics[0]);else{if(r.diagnostics.indexOf(i.diagnostic)<0)return!1;i=new H(i.from,o,i.diagnostic)}}),i}function $(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(r=>r.is(C))||t.changes.touchesRange(o.from,Math.max(o.to,i)))}function N(t,e){return t.field(g,!1)?e:e.concat(v.appendConfig.of(te))}function V(t,e){return{effects:N(t,[C.of(e)])}}const C=v.define(),M=v.define(),Y=v.define(),g=A.define({create(){return new w(k.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=y(n,t.selected.diagnostic,o)||y(n,null,o)}!n.size&&s&&e.state.facet(b).autoPanel&&(s=null),t=new w(n,s,i)}for(let n of e.effects)if(n.is(C)){let i=e.state.facet(b).autoPanel?n.value.length?T.open:null:t.panel;t=w.init(n.value,i,e.state)}else n.is(M)?t=new w(t.diagnostics,n.value?T.open:null,t.selected):n.is(Y)&&(t=new w(t.diagnostics,t.panel,n.value));return t},provide:t=>[le.from(t,e=>e.panel),S.decorations.from(t,e=>e.diagnostics)]});function ue(t){let e=t.field(g,!1);return e?e.diagnostics.size:0}const me=k.mark({class:"cm-lintRange cm-lintRange-active"});function he(t,e,n){let{diagnostics:i}=t.state.field(g),s,o=-1,r=-1;i.between(e-(n<0?1:0),e+(n>0?1:0),(a,c,{spec:f})=>{if(e>=a&&e<=c&&(a==c||(e>a||n>0)&&(e<c||n<0)))return s=f.diagnostics,o=a,r=c,!1});let l=t.state.facet(b).tooltipFilter;return s&&l&&(s=l(s,t.state)),s?{pos:o,end:r,above:t.state.doc.lineAt(o).to<r,create(){return{dom:_(t,s)}}}:null}function _(t,e){return p("ul",{class:"cm-tooltip-lint"},e.map(n=>K(t,n,!1)))}const q=t=>{let e=t.state.field(g,!1);(!e||!e.panel)&&t.dispatch({effects:N(t.state,[M.of(!0)])});let n=se(t,T.open);return n&&n.dom.querySelector(".cm-panel-lint ul").focus(),!0},I=t=>{let e=t.state.field(g,!1);return!e||!e.panel?!1:(t.dispatch({effects:M.of(!1)}),!0)},U=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)},ge=t=>{let{state:e}=t,n=e.field(g,!1);if(!n)return!1;let i=e.selection.main,s,o,r,l;return n.diagnostics.between(0,e.doc.length,(a,c)=>{c<i.to&&(s==null||s<a)&&(s=a,o=c),(r==null||a>r)&&(r=a,l=c)}),r==null||s==null&&r==i.from?!1:(t.dispatch({selection:{anchor:s??r,head:o??l},scrollIntoView:!0}),!0)},pe=[{key:"Mod-Shift-m",run:q,preventDefault:!0},{key:"F8",run:U}],W=ie.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&&be(n.map(i=>Promise.resolve(i(this.view))),i=>{this.view.state.doc==e.doc&&this.view.dispatch(V(this.view.state,i.reduce((s,o)=>s.concat(o))))},i=>{fe(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 be(t,e,n){let i=[],s=-1;for(let o of t)o.then(r=>{i.push(r),clearTimeout(s),i.length==t.length?e(i):s=setTimeout(()=>e(i),200)},n)}const b=j.define({combine(t){return Object.assign({sources:t.map(e=>e.source).filter(e=>e!=null)},G(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 we(t,e={}){return[b.of({source:t,config:e}),W,te]}function xe(t){let e=t.plugin(W);e&&e.force()}function Z(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 K(t,e,n){var i;let s=n?Z(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,r)=>{let l=!1,a=u=>{if(u.preventDefault(),l)return;l=!0;let h=y(t.state.field(g).diagnostics,e);h&&o.apply(t,h.from,h.to)},{name:c}=o,f=s[r]?c.indexOf(s[r]):-1,m=f<0?c:[c.slice(0,f),p("u",c.slice(f,f+1)),c.slice(f+1)];return p("button",{type:"button",class:"cm-diagnosticAction",onclick:a,onmousedown:a,"aria-label":` Action: ${c}${f<0?"":` (access key "${s[r]})"`}.`},m)}),e.source&&p("div",{class:"cm-diagnosticSource"},e.source))}class ke extends ce{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 X{constructor(e,n){this.diagnostic=n,this.id="item_"+Math.floor(Math.random()*4294967295).toString(16),this.dom=K(e,n,!0),this.dom.id=this.id,this.dom.setAttribute("role","option")}}class T{constructor(e){this.view=e,this.items=[];let n=s=>{if(s.keyCode==27)I(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],r=Z(o.actions);for(let l=0;l<r.length;l++)if(r[l].toUpperCase().charCodeAt(0)==s.keyCode){let a=y(this.view.state.field(g).diagnostics,o);a&&o.actions[l].apply(e,a.from,a.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:()=>I(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,r=new Set;for(e.between(0,this.view.state.doc.length,(l,a,{spec:c})=>{for(let f of c.diagnostics){if(r.has(f))continue;r.add(f);let m=-1,u;for(let h=i;h<this.items.length;h++)if(this.items[h].diagnostic==f){m=h;break}m<0?(u=new X(this.view,f),this.items.splice(i,0,u),s=!0):(u=this.items[m],m>i&&(this.items.splice(i,m-i),s=!0)),n&&u.diagnostic==n.diagnostic?u.dom.hasAttribute("aria-selected")||(u.dom.setAttribute("aria-selected","true"),o=u):u.dom.hasAttribute("aria-selected")&&u.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 X(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:l,panel:a})=>{let c=a.height/this.list.offsetHeight;l.top<a.top?this.list.scrollTop-=(a.top-l.top)/c:l.bottom>a.bottom&&(this.list.scrollTop+=(l.bottom-a.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=y(n.diagnostics,this.items[e].diagnostic);i&&this.view.dispatch({selection:{anchor:i.from,head:i.to},scrollIntoView:!0,effects:Y.of(i)})}static open(e){return new T(e)}}function R(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 P(t){return R(`<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 ye=S.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:P("#d11")},".cm-lintRange-warning":{backgroundImage:P("orange")},".cm-lintRange-info":{backgroundImage:P("#999")},".cm-lintRange-hint":{backgroundImage:P("#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 ve(t){return t=="error"?4:t=="warning"?3:t=="info"?2:1}function J(t){let e="hint",n=1;for(let i of t){let s=ve(i.severity);s>n&&(n=s,e=i.severity)}return e}class Q extends ae{constructor(e){super(),this.diagnostics=e,this.severity=J(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=()=>Te(e,n,i)),n}}function Ce(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(ee)&&t.dispatch({effects:O.of(null)})}};window.addEventListener("mousemove",n)}function Te(t,e,n){function i(){let r=t.elementAtHeight(e.getBoundingClientRect().top+5-t.documentTop);t.coordsAtPos(r.from)&&t.dispatch({effects:O.of({pos:r.from,above:!1,clip:!1,create(){return{dom:_(t,n),getCoords:()=>e.getBoundingClientRect()}}})}),e.onmouseout=e.onmousemove=null,Ce(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 Se(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 Q(n[s]).range(+s));return D.of(i,!0)}const Re=ne({class:"cm-gutter-lint",markers:t=>t.state.field(F),widgetMarker:(t,e,n)=>{let i=[];return t.state.field(F).between(n.from,n.to,(s,o,r)=>{s>n.from&&s<n.to&&i.push(...r.diagnostics)}),i.length?new Q(i):null}}),F=A.define({create(){return D.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(C)){let s=i.value;n&&(s=n(s||[],e.state)),t=Se(e.state.doc,s.slice(0))}return t}}),O=v.define(),ee=A.define({create(){return null},update(t,e){return t&&e.docChanged&&(t=$(e,t)?null:Object.assign(Object.assign({},t),{pos:e.changes.mapPos(t.pos)})),e.effects.reduce((n,i)=>i.is(O)?i.value:n,t)},provide:t=>re.from(t)}),Pe=S.baseTheme({".cm-gutter-lint":{width:"1.4em","& .cm-gutterElement":{padding:".2em"}},".cm-lint-marker":{width:"1em",height:"1em"},".cm-lint-marker-info":{content:R('<path fill="#aaf" stroke="#77e" stroke-width="6" stroke-linejoin="round" d="M5 5L35 5L35 35L5 35Z"/>')},".cm-lint-marker-warning":{content:R('<path fill="#fe8" stroke="#fd7" stroke-width="6" stroke-linejoin="round" d="M20 6L37 35L3 35Z"/>')},".cm-lint-marker-error":{content:R('<circle cx="20" cy="20" r="15" fill="#f87" stroke="#f43" stroke-width="6"/>')}}),te=[g,S.decorations.compute([g],t=>{let{selected:e,panel:n}=t.field(g);return!e||!n||e.from==e.to?k.none:k.set([me.range(e.from,e.to)])}),oe(he,{hideOn:$}),ye],L=j.define({combine(t){return G(t,{hoverTime:300,markerFilter:null,tooltipFilter:null})}});function Le(t={}){return[L.of(t),F,Re,Pe,ee]}function Ae(t,e){let n=t.field(g,!1);if(n&&n.diagnostics.size){let i=[],s=[],o=-1;for(let r=D.iter([n.diagnostics]);;r.next()){for(let l=0;l<i.length;l++)(!r.value||r.value.spec.diagnostics.indexOf(i[l])<0)&&(e(i[l],s[l],o),i.splice(l,1),s.splice(l--,1));if(!r.value)break;for(let l of r.value.spec.diagnostics)i.indexOf(l)<0&&(i.push(l),s.push(r.from));o=r.to}}}export{I as closeLintPanel,ue as diagnosticCount,Ae as forEachDiagnostic,xe as forceLinting,Le as lintGutter,pe as lintKeymap,we as linter,U as nextDiagnostic,q as openLintPanel,ge as previousDiagnostic,V as setDiagnostics,C as setDiagnosticsEffect};