import{Decoration as h,showPanel as K,EditorView as y,ViewPlugin as U,logException as X,gutter as J,showTooltip as Q,hoverTooltip as ee,getPanel as te,WidgetType as ie,GutterMarker as ne}from"@codemirror/view";import{StateEffect as k,StateField as S,Facet as M,combineConfig as A,RangeSet as R}from"@codemirror/state";import u from"crelt";class se{constructor(e,n,i){this.from=e,this.to=n,this.diagnostic=i}}class g{constructor(e,n,i){this.diagnostics=e,this.panel=n,this.selected=i}static init(e,n,i){let s=e,o=i.facet(m).markerFilter;o&&(s=o(s,i));let l=h.set(s.map(r=>r.from==r.to||r.from==r.to-1&&i.doc.lineAt(r.from).to==r.from?h.widget({widget:new ue(r),diagnostic:r}).range(r.from):h.mark({attributes:{class:"cm-lintRange cm-lintRange-"+r.severity+(r.markClass?" "+r.markClass:"")},diagnostic:r}).range(r.from,r.to)),!0);return new g(l,n,b(l))}}function b(t,e=null,n=0){let i=null;return t.between(n,1e9,(s,o,{spec:l})=>{if(!(e&&l.diagnostic!=e))return i=new se(s,o,l.diagnostic),!1}),i}function F(t,e){let n=e.pos,i=e.end||n,s=t.state.facet(m).hideOn(t,n,i);if(s!=null)return s;let o=t.startState.doc.lineAt(e.pos);return!!(t.effects.some(l=>l.is(w))||t.changes.touchesRange(o.from,Math.max(o.to,i)))}function E(t,e){return t.field(f,!1)?e:e.concat(k.appendConfig.of(W))}function B(t,e){return{effects:E(t,[w.of(e)])}}const w=k.define(),D=k.define(),O=k.define(),f=S.define({create(){return new g(h.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=b(n,t.selected.diagnostic,o)||b(n,null,o)}!n.size&&s&&e.state.facet(m).autoPanel&&(s=null),t=new g(n,s,i)}for(let n of e.effects)if(n.is(w)){let i=e.state.facet(m).autoPanel?n.value.length?x.open:null:t.panel;t=g.init(n.value,i,e.state)}else n.is(D)?t=new g(t.diagnostics,n.value?x.open:null,t.selected):n.is(O)&&(t=new g(t.diagnostics,t.panel,n.value));return t},provide:t=>[K.from(t,e=>e.panel),y.decorations.from(t,e=>e.diagnostics)]});function oe(t){let e=t.field(f,!1);return e?e.diagnostics.size:0}const le=h.mark({class:"cm-lintRange cm-lintRange-active"});function re(t,e,n){let{diagnostics:i}=t.state.field(f),s=[],o=2e8,l=0;i.between(e-(n<0?1:0),e+(n>0?1:0),(a,c,{spec:d})=>{e>=a&&e<=c&&(a==c||(e>a||n>0)&&(eV(t,n,!1)))}const j=t=>{let e=t.state.field(f,!1);(!e||!e.panel)&&t.dispatch({effects:E(t.state,[D.of(!0)])});let n=te(t,x.open);return n&&n.dom.querySelector(".cm-panel-lint ul").focus(),!0},L=t=>{let e=t.state.field(f,!1);return!e||!e.panel?!1:(t.dispatch({effects:D.of(!1)}),!0)},z=t=>{let e=t.state.field(f,!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)},ae=t=>{let{state:e}=t,n=e.field(f,!1);if(!n)return!1;let i=e.selection.main,s,o,l,r;return n.diagnostics.between(0,e.doc.length,(a,c)=>{cl)&&(l=a,r=c)}),l==null||s==null&&l==i.from?!1:(t.dispatch({selection:{anchor:s??l,head:o??r},scrollIntoView:!0}),!0)},ce=[{key:"Mod-Shift-m",run:j,preventDefault:!0},{key:"F8",run:z}],H=U.fromClass(class{constructor(t){this.view=t,this.timeout=-1,this.set=!0;let{delay:e}=t.state.facet(m);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(tPromise.resolve(i(this.view)))).then(i=>{let s=i.reduce((o,l)=>o.concat(l));this.view.state.doc==e.doc&&this.view.dispatch(B(this.view.state,s))},i=>{X(this.view.state,i)})}}update(t){let e=t.state.facet(m);(t.docChanged||e!=t.startState.facet(m)||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)}}),m=M.define({combine(t){return Object.assign({sources:t.map(e=>e.source).filter(e=>e!=null)},A(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 de(t,e={}){return[m.of({source:t,config:e}),H,W]}function fe(t){let e=t.plugin(H);e&&e.force()}function $(t){let e=[];if(t)e:for(let{name:n}of t){for(let i=0;io.toLowerCase()==s.toLowerCase())){e.push(s);continue e}}e.push("")}return e}function V(t,e,n){var i;let s=n?$(e.actions):[];return u("li",{class:"cm-diagnostic cm-diagnostic-"+e.severity},u("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 r=!1,a=Z=>{if(Z.preventDefault(),r)return;r=!0;let P=b(t.state.field(f).diagnostics,e);P&&o.apply(t,P.from,P.to)},{name:c}=o,d=s[l]?c.indexOf(s[l]):-1,p=d<0?c:[c.slice(0,d),u("u",c.slice(d,d+1)),c.slice(d+1)];return u("button",{type:"button",class:"cm-diagnosticAction",onclick:a,onmousedown:a,"aria-label":` Action: ${c}${d<0?"":` (access key "${s[l]})"`}.`},p)}),e.source&&u("div",{class:"cm-diagnosticSource"},e.source))}class ue extends ie{constructor(e){super(),this.diagnostic=e}eq(e){return e.diagnostic==this.diagnostic}toDOM(){return u("span",{class:"cm-lintPoint cm-lintPoint-"+this.diagnostic.severity})}}class N{constructor(e,n){this.diagnostic=n,this.id="item_"+Math.floor(Math.random()*4294967295).toString(16),this.dom=V(e,n,!0),this.dom.id=this.id,this.dom.setAttribute("role","option")}}class x{constructor(e){this.view=e,this.items=[];let n=s=>{if(s.keyCode==27)L(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=$(o.actions);for(let r=0;r{for(let o=0;oL(this.view)},"\xD7")),this.update()}get selectedIndex(){let e=this.view.state.field(f).selected;if(!e)return-1;for(let n=0;n{let c=-1,d;for(let p=i;pi&&(this.items.splice(i,c-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({sel:o.dom.getBoundingClientRect(),panel:this.list.getBoundingClientRect()}),write:({sel:l,panel:r})=>{let a=r.height/this.list.offsetHeight;l.topr.bottom&&(this.list.scrollTop+=(l.bottom-r.bottom)/a)}})):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(f),i=b(n.diagnostics,this.items[e].diagnostic);i&&this.view.dispatch({selection:{anchor:i.from,head:i.to},scrollIntoView:!0,effects:O.of(i)})}static open(e){return new x(e)}}function v(t,e='viewBox="0 0 40 40"'){return`url('data:image/svg+xml,${encodeURIComponent(t)}')`}function C(t){return v(``,'width="6" height="3"')}const me=y.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:C("#d11")},".cm-lintRange-warning":{backgroundImage:C("orange")},".cm-lintRange-info":{backgroundImage:C("#999")},".cm-lintRange-hint":{backgroundImage:C("#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 Y(t){return t=="error"?4:t=="warning"?3:t=="info"?2:1}class he extends ne{constructor(e){super(),this.diagnostics=e,this.severity=e.reduce((n,i)=>Y(n)pe(e,n,i)),n}}function ge(t,e){let n=i=>{let s=e.getBoundingClientRect();if(!(i.clientX>s.left-10&&i.clientXs.top-10&&i.clientYe.getBoundingClientRect()}}})}),e.onmouseout=e.onmousemove=null,ge(t,e)}let{hoverTime:s}=t.state.facet(T),o=setTimeout(i,s);e.onmouseout=()=>{clearTimeout(o),e.onmouseout=e.onmousemove=null},e.onmousemove=()=>{clearTimeout(o),o=setTimeout(i,s)}}function be(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 he(n[s]).range(+s));return R.of(i,!0)}const ke=J({class:"cm-gutter-lint",markers:t=>t.state.field(_)}),_=S.define({create(){return R.empty},update(t,e){t=t.map(e.changes);let n=e.state.facet(T).markerFilter;for(let i of e.effects)if(i.is(w)){let s=i.value;n&&(s=n(s||[],e.state)),t=be(e.state.doc,s.slice(0))}return t}}),I=k.define(),q=S.define({create(){return null},update(t,e){return t&&e.docChanged&&(t=F(e,t)?null:Object.assign(Object.assign({},t),{pos:e.changes.mapPos(t.pos)})),e.effects.reduce((n,i)=>i.is(I)?i.value:n,t)},provide:t=>Q.from(t)}),we=y.baseTheme({".cm-gutter-lint":{width:"1.4em","& .cm-gutterElement":{padding:".2em"}},".cm-lint-marker":{width:"1em",height:"1em"},".cm-lint-marker-info":{content:v('')},".cm-lint-marker-warning":{content:v('')},".cm-lint-marker-error":{content:v('')}}),W=[f,y.decorations.compute([f],t=>{let{selected:e,panel:n}=t.field(f);return!e||!n||e.from==e.to?h.none:h.set([le.range(e.from,e.to)])}),ee(re,{hideOn:F}),me],T=M.define({combine(t){return A(t,{hoverTime:300,markerFilter:null,tooltipFilter:null})}});function xe(t={}){return[T.of(t),_,ke,we,q]}function ye(t,e){let n=t.field(f,!1);if(n&&n.diagnostics.size)for(let i=R.iter([n.diagnostics]);i.value;i.next())e(i.value.spec.diagnostic,i.from,i.to)}export{L as closeLintPanel,oe as diagnosticCount,ye as forEachDiagnostic,fe as forceLinting,xe as lintGutter,ce as lintKeymap,de as linter,z as nextDiagnostic,j as openLintPanel,ae as previousDiagnostic,B as setDiagnostics,w as setDiagnosticsEffect};