Files
conservatorio-tomadini/media/vendor/codemirror/js/lezer-highlight.min.js
2024-12-17 17:34:10 +01:00

3 lines
7.0 KiB
JavaScript

import{NodeProp as $}from"@lezer/common";let P=0;class m{constructor(t,a,i,n){this.name=t,this.set=a,this.base=i,this.modified=n,this.id=P++}toString(){let{name:t}=this;for(let a of this.modified)a.name&&(t=`${a.name}(${t})`);return t}static define(t,a){let i=typeof t=="string"?t:"?";if(t instanceof m&&(a=t),a?.base)throw new Error("Can not derive from a modified tag");let n=new m(i,[],null,[]);if(n.set.push(n),a)for(let s of a.set)n.set.push(s);return n}static defineModifier(t){let a=new B(t);return i=>i.modified.indexOf(a)>-1?i:B.get(i.base||i,i.modified.concat(a).sort((n,s)=>n.id-s.id))}}let Q=0;class B{constructor(t){this.name=t,this.instances=[],this.id=Q++}static get(t,a){if(!a.length)return t;let i=a[0].instances.find(r=>r.base==t&&U(a,r.modified));if(i)return i;let n=[],s=new m(t.name,n,t,a);for(let r of a)r.instances.push(s);let c=W(a);for(let r of t.set)if(!r.modified.length)for(let d of c)n.push(B.get(r,d));return s}}function U(o,t){return o.length==t.length&&o.every((a,i)=>a==t[i])}function W(o){let t=[[]];for(let a=0;a<o.length;a++)for(let i=0,n=t.length;i<n;i++)t.push(t[i].concat(o[a]));return t.sort((a,i)=>i.length-a.length)}function X(o){let t=Object.create(null);for(let a in o){let i=o[a];Array.isArray(i)||(i=[i]);for(let n of a.split(" "))if(n){let s=[],c=2,r=n;for(let f=0;;){if(r=="..."&&f>0&&f+3==n.length){c=1;break}let h=/^"(?:[^"\\]|\\.)*?"|[^\/!]+/.exec(r);if(!h)throw new RangeError("Invalid path: "+n);if(s.push(h[0]=="*"?"":h[0][0]=='"'?JSON.parse(h[0]):h[0]),f+=h[0].length,f==n.length)break;let p=n[f++];if(f==n.length&&p=="!"){c=0;break}if(p!="/")throw new RangeError("Invalid path: "+n);r=n.slice(f)}let d=s.length-1,g=s[d];if(!g)throw new RangeError("Invalid path: "+n);let b=new T(i,c,d>0?s.slice(0,d):null);t[g]=b.sort(t[g])}}return F.add(t)}const F=new $;class T{constructor(t,a,i,n){this.tags=t,this.mode=a,this.context=i,this.next=n}get opaque(){return this.mode==0}get inherit(){return this.mode==1}sort(t){return!t||t.depth<this.depth?(this.next=t,this):(t.next=this.sort(t.next),t)}get depth(){return this.context?this.context.length:0}}T.empty=new T([],2,null);function J(o,t){let a=Object.create(null);for(let s of o)if(!Array.isArray(s.tag))a[s.tag.id]=s.class;else for(let c of s.tag)a[c.id]=s.class;let{scope:i,all:n=null}=t||{};return{style:s=>{let c=n;for(let r of s)for(let d of r.set){let g=a[d.id];if(g){c=c?c+" "+g:g;break}}return c},scope:i}}function Y(o,t){let a=null;for(let i of o){let n=i.style(t);n&&(a=a?a+" "+n:n)}return a}function V(o,t,a,i=0,n=o.length){let s=new _(i,Array.isArray(t)?t:[t],a);s.highlightRange(o.cursor(),i,n,"",s.highlighters),s.flush(n)}function Z(o,t,a,i,n,s=0,c=o.length){let r=s;function d(g,b){if(!(g<=r)){for(let f=o.slice(r,g),h=0;;){let p=f.indexOf(`
`,h),R=p<0?f.length:p;if(R>h&&i(f.slice(h,R),b),p<0)break;n(),h=p+1}r=g}}V(t,a,(g,b,f)=>{d(g,""),d(b,f)},s,c),d(c,"")}class _{constructor(t,a,i){this.at=t,this.highlighters=a,this.span=i,this.class=""}startSpan(t,a){a!=this.class&&(this.flush(t),t>this.at&&(this.at=t),this.class=a)}flush(t){t>this.at&&this.class&&this.span(this.at,t,this.class)}highlightRange(t,a,i,n,s){let{type:c,from:r,to:d}=t;if(r>=i||d<=a)return;c.isTop&&(s=this.highlighters.filter(p=>!p.scope||p.scope(c)));let g=n,b=z(t)||T.empty,f=Y(s,b.tags);if(f&&(g&&(g+=" "),g+=f,b.mode==1&&(n+=(n?" ":"")+f)),this.startSpan(Math.max(a,r),g),b.opaque)return;let h=t.tree&&t.tree.prop($.mounted);if(h&&h.overlay){let p=t.node.enter(h.overlay[0].from+r,1),R=this.highlighters.filter(v=>!v.scope||v.scope(h.tree.type)),j=t.firstChild();for(let v=0,M=r;;v++){let S=v<h.overlay.length?h.overlay[v]:null,E=S?S.from+r:d,D=Math.max(a,M),A=Math.min(i,E);if(D<A&&j)for(;t.from<A&&(this.highlightRange(t,D,A,n,s),this.startSpan(Math.min(A,t.to),g),!(t.to>=E||!t.nextSibling())););if(!S||E>i)break;M=S.to+r,M>a&&(this.highlightRange(p.cursor(),Math.max(a,S.from+r),Math.min(i,M),"",R),this.startSpan(Math.min(i,M),g))}j&&t.parent()}else if(t.firstChild()){h&&(n="");do if(!(t.to<=a)){if(t.from>=i)break;this.highlightRange(t,a,i,n,s),this.startSpan(Math.min(i,t.to),g)}while(t.nextSibling());t.parent()}}}function z(o){let t=o.type.prop(F);for(;t&&t.context&&!o.matchContext(t.context);)t=t.next;return t||null}const e=m.define,C=e(),N=e(),G=e(N),L=e(N),x=e(),I=e(x),H=e(x),u=e(),w=e(u),k=e(),y=e(),K=e(),O=e(K),q=e(),l={comment:C,lineComment:e(C),blockComment:e(C),docComment:e(C),name:N,variableName:e(N),typeName:G,tagName:e(G),propertyName:L,attributeName:e(L),className:e(N),labelName:e(N),namespace:e(N),macroName:e(N),literal:x,string:I,docString:e(I),character:e(I),attributeValue:e(I),number:H,integer:e(H),float:e(H),bool:e(x),regexp:e(x),escape:e(x),color:e(x),url:e(x),keyword:k,self:e(k),null:e(k),atom:e(k),unit:e(k),modifier:e(k),operatorKeyword:e(k),controlKeyword:e(k),definitionKeyword:e(k),moduleKeyword:e(k),operator:y,derefOperator:e(y),arithmeticOperator:e(y),logicOperator:e(y),bitwiseOperator:e(y),compareOperator:e(y),updateOperator:e(y),definitionOperator:e(y),typeOperator:e(y),controlOperator:e(y),punctuation:K,separator:e(K),bracket:O,angleBracket:e(O),squareBracket:e(O),paren:e(O),brace:e(O),content:u,heading:w,heading1:e(w),heading2:e(w),heading3:e(w),heading4:e(w),heading5:e(w),heading6:e(w),contentSeparator:e(u),list:e(u),quote:e(u),emphasis:e(u),strong:e(u),link:e(u),monospace:e(u),strikethrough:e(u),inserted:e(),deleted:e(),changed:e(),invalid:e(),meta:q,documentMeta:e(q),annotation:e(q),processingInstruction:e(q),definition:m.defineModifier("definition"),constant:m.defineModifier("constant"),function:m.defineModifier("function"),standard:m.defineModifier("standard"),local:m.defineModifier("local"),special:m.defineModifier("special")};for(let o in l){let t=l[o];t instanceof m&&(t.name=o)}const tt=J([{tag:l.link,class:"tok-link"},{tag:l.heading,class:"tok-heading"},{tag:l.emphasis,class:"tok-emphasis"},{tag:l.strong,class:"tok-strong"},{tag:l.keyword,class:"tok-keyword"},{tag:l.atom,class:"tok-atom"},{tag:l.bool,class:"tok-bool"},{tag:l.url,class:"tok-url"},{tag:l.labelName,class:"tok-labelName"},{tag:l.inserted,class:"tok-inserted"},{tag:l.deleted,class:"tok-deleted"},{tag:l.literal,class:"tok-literal"},{tag:l.string,class:"tok-string"},{tag:l.number,class:"tok-number"},{tag:[l.regexp,l.escape,l.special(l.string)],class:"tok-string2"},{tag:l.variableName,class:"tok-variableName"},{tag:l.local(l.variableName),class:"tok-variableName tok-local"},{tag:l.definition(l.variableName),class:"tok-variableName tok-definition"},{tag:l.special(l.variableName),class:"tok-variableName2"},{tag:l.definition(l.propertyName),class:"tok-propertyName tok-definition"},{tag:l.typeName,class:"tok-typeName"},{tag:l.namespace,class:"tok-namespace"},{tag:l.className,class:"tok-className"},{tag:l.macroName,class:"tok-macroName"},{tag:l.propertyName,class:"tok-propertyName"},{tag:l.operator,class:"tok-operator"},{tag:l.comment,class:"tok-comment"},{tag:l.meta,class:"tok-meta"},{tag:l.invalid,class:"tok-invalid"},{tag:l.punctuation,class:"tok-punctuation"}]);export{m as Tag,tt as classHighlighter,z as getStyleTags,Z as highlightCode,V as highlightTree,X as styleTags,J as tagHighlighter,l as tags};