import{parser as M}from"@lezer/javascript";import{syntaxTree as S,LRLanguage as _,indentNodeProp as R,continuedIndent as $,flatIndent as W,delimitedIndent as Z,foldNodeProp as z,foldInside as H,defineLanguageFacet as K,sublanguageProp as T,LanguageSupport as q}from"@codemirror/language";import{EditorSelection as G}from"@codemirror/state";import{EditorView as Q}from"@codemirror/view";import{snippetCompletion as p,ifNotIn as U,completeFromList as Y}from"@codemirror/autocomplete";import{NodeWeakMap as ee,IterMode as te}from"@lezer/common";const w=[p("function ${name}(${params}) {\n	${}\n}",{label:"function",detail:"definition",type:"keyword"}),p("for (let ${index} = 0; ${index} < ${bound}; ${index}++) {\n	${}\n}",{label:"for",detail:"loop",type:"keyword"}),p("for (let ${name} of ${collection}) {\n	${}\n}",{label:"for",detail:"of loop",type:"keyword"}),p("do {\n	${}\n} while (${})",{label:"do",detail:"loop",type:"keyword"}),p("while (${}) {\n	${}\n}",{label:"while",detail:"loop",type:"keyword"}),p(`try {
	\${}
} catch (\${error}) {
	\${}
}`,{label:"try",detail:"/ catch block",type:"keyword"}),p("if (${}) {\n	${}\n}",{label:"if",detail:"block",type:"keyword"}),p(`if (\${}) {
	\${}
} else {
	\${}
}`,{label:"if",detail:"/ else block",type:"keyword"}),p(`class \${name} {
	constructor(\${params}) {
		\${}
	}
}`,{label:"class",detail:"definition",type:"keyword"}),p('import {${names}} from "${module}"\n${}',{label:"import",detail:"named",type:"keyword"}),p('import ${name} from "${module}"\n${}',{label:"import",detail:"default",type:"keyword"})],I=w.concat([p("interface ${name} {\n	${}\n}",{label:"interface",detail:"definition",type:"keyword"}),p("type ${name} = ${type}",{label:"type",detail:"definition",type:"keyword"}),p("enum ${name} {\n	${}\n}",{label:"enum",detail:"definition",type:"keyword"})]),x=new ee,D=new Set(["Script","Block","FunctionExpression","FunctionDeclaration","ArrowFunction","MethodDeclaration","ForStatement"]);function y(e){return(t,r)=>{let n=t.node.getChild("VariableDefinition");return n&&r(n,e),!0}}const ne=["FunctionDeclaration"],re={FunctionDeclaration:y("function"),ClassDeclaration:y("class"),ClassExpression:()=>!0,EnumDeclaration:y("constant"),TypeAliasDeclaration:y("type"),NamespaceDeclaration:y("namespace"),VariableDefinition(e,t){e.matchContext(ne)||t(e,"variable")},TypeDefinition(e,t){t(e,"type")},__proto__:null};function J(e,t){let r=x.get(t);if(r)return r;let n=[],a=!0;function i(o,l){let c=e.sliceString(o.from,o.to);n.push({label:c,type:l})}return t.cursor(te.IncludeAnonymous).iterate(o=>{if(a)a=!1;else if(o.name){let l=re[o.name];if(l&&l(o,i)||D.has(o.name))return!1}else if(o.to-o.from>8192){for(let l of J(e,o.node))n.push(l);return!1}}),x.set(t,n),n}const h=/^[\w$\xa1-\uffff][\w$\d\xa1-\uffff]*$/,k=["TemplateString","String","RegExp","LineComment","BlockComment","VariableDefinition","TypeDefinition","Label","PropertyDefinition","PropertyName","PrivatePropertyDefinition","PrivatePropertyName",".","?."];function X(e){let t=S(e.state).resolveInner(e.pos,-1);if(k.indexOf(t.name)>-1)return null;let r=t.name=="VariableName"||t.to-t.from<20&&h.test(e.state.sliceDoc(t.from,t.to));if(!r&&!e.explicit)return null;let n=[];for(let a=t;a;a=a.parent)D.has(a.name)&&(n=n.concat(J(e.state.doc,a)));return{options:n,from:r?t.from:e.pos,validFor:h}}function v(e,t,r){var n;let a=[];for(;;){let i=t.firstChild,o;if(i?.name=="VariableName")return a.push(e(i)),{path:a.reverse(),name:r};if(i?.name=="MemberExpression"&&((n=o=i.lastChild)===null||n===void 0?void 0:n.name)=="PropertyName")a.push(e(o)),t=i;else return null}}function E(e){let t=n=>e.state.doc.sliceString(n.from,n.to),r=S(e.state).resolveInner(e.pos,-1);return r.name=="PropertyName"?v(t,r.parent,t(r)):(r.name=="."||r.name=="?.")&&r.parent.name=="MemberExpression"?v(t,r.parent,""):k.indexOf(r.name)>-1?null:r.name=="VariableName"||r.to-r.from<20&&h.test(t(r))?{path:[],name:t(r)}:r.name=="MemberExpression"?v(t,r,""):e.explicit?{path:[],name:""}:null}function oe(e,t){let r=[],n=new Set;for(let a=0;;a++){for(let o of(Object.getOwnPropertyNames||Object.keys)(e)){if(!/^[a-zA-Z_$\xaa-\uffdc][\w$\xaa-\uffdc]*$/.test(o)||n.has(o))continue;n.add(o);let l;try{l=e[o]}catch{continue}r.push({label:o,type:typeof l=="function"?/^[A-Z]/.test(o)?"class":t?"function":"method":t?"variable":"property",boost:-a})}let i=Object.getPrototypeOf(e);if(!i)return r;e=i}}function ae(e){let t=new Map;return r=>{let n=E(r);if(!n)return null;let a=e;for(let o of n.path)if(a=a[o],!a)return null;let i=t.get(a);return i||t.set(a,i=oe(a,!n.path.length)),{from:r.pos-n.name.length,options:i,validFor:h}}}const m=_.define({name:"javascript",parser:M.configure({props:[R.add({IfStatement:$({except:/^\s*({|else\b)/}),TryStatement:$({except:/^\s*({|catch\b|finally\b)/}),LabeledStatement:W,SwitchBody:e=>{let t=e.textAfter,r=/^\s*\}/.test(t),n=/^\s*(case|default)\b/.test(t);return e.baseIndent+(r?0:n?1:2)*e.unit},Block:Z({closing:"}"}),ArrowFunction:e=>e.baseIndent+e.unit,"TemplateString BlockComment":()=>null,"Statement Property":$({except:/^{/}),JSXElement(e){let t=/^\s*<\//.test(e.textAfter);return e.lineIndent(e.node.from)+(t?0:e.unit)},JSXEscape(e){let t=/\s*\}/.test(e.textAfter);return e.lineIndent(e.node.from)+(t?0:e.unit)},"JSXOpenTag JSXSelfClosingTag"(e){return e.column(e.node.from)+e.unit}}),z.add({"Block ClassBody SwitchBody EnumBody ObjectExpression ArrayExpression ObjectType":H,BlockComment(e){return{from:e.from+2,to:e.to-2}}})]}),languageData:{closeBrackets:{brackets:["(","[","{","'",'"',"`"]},commentTokens:{line:"//",block:{open:"/*",close:"*/"}},indentOnInput:/^\s*(?:case |default:|\{|\}|<\/)$/,wordChars:"$"}}),N={test:e=>/^JSX/.test(e.name),facet:K({commentTokens:{block:{open:"{/*",close:"*/}"}}})},P=m.configure({dialect:"ts"},"typescript"),A=m.configure({dialect:"jsx",props:[T.add(e=>e.isTop?[N]:void 0)]}),L=m.configure({dialect:"jsx ts",props:[T.add(e=>e.isTop?[N]:void 0)]},"typescript");let O=e=>({label:e,type:"keyword"});const F="break case const continue default delete export extends false finally in instanceof let new return static super switch this throw true typeof var yield".split(" ").map(O),ie=F.concat(["declare","implements","private","protected","public"].map(O));function le(e={}){let t=e.jsx?e.typescript?L:A:e.typescript?P:m,r=e.typescript?I.concat(ie):w.concat(F);return new q(t,[m.data.of({autocomplete:U(k,Y(r))}),m.data.of({autocomplete:X}),e.jsx?B:[]])}function se(e){for(;;){if(e.name=="JSXOpenTag"||e.name=="JSXSelfClosingTag"||e.name=="JSXFragmentTag")return e;if(e.name=="JSXEscape"||!e.parent)return null;e=e.parent}}function j(e,t,r=e.length){for(let n=t?.firstChild;n;n=n.nextSibling)if(n.name=="JSXIdentifier"||n.name=="JSXBuiltin"||n.name=="JSXNamespacedName"||n.name=="JSXMemberExpression")return e.sliceString(n.from,Math.min(n.to,r));return""}const pe=typeof navigator=="object"&&/Android\b/.test(navigator.userAgent),B=Q.inputHandler.of((e,t,r,n,a)=>{if((pe?e.composing:e.compositionStarted)||e.state.readOnly||t!=r||n!=">"&&n!="/"||!m.isActiveAt(e.state,t,-1))return!1;let i=a(),{state:o}=i,l=o.changeByRange(c=>{var u;let{head:s}=c,f=S(o).resolveInner(s-1,-1),g;if(f.name=="JSXStartTag"&&(f=f.parent),!(o.doc.sliceString(s-1,s)!=n||f.name=="JSXAttributeValue"&&f.to>s)){if(n==">"&&f.name=="JSXFragmentTag")return{range:c,changes:{from:s,insert:"</>"}};if(n=="/"&&f.name=="JSXStartCloseTag"){let d=f.parent,b=d.parent;if(b&&d.from==s-2&&((g=j(o.doc,b.firstChild,s))||((u=b.firstChild)===null||u===void 0?void 0:u.name)=="JSXFragmentTag")){let C=`${g}>`;return{range:G.cursor(s+C.length,-1),changes:{from:s,insert:C}}}}else if(n==">"){let d=se(f);if(d&&d.name=="JSXOpenTag"&&!/^\/?>|^<\//.test(o.doc.sliceString(s,s+2))&&(g=j(o.doc,d,s)))return{range:c,changes:{from:s,insert:`</${g}>`}}}}return{range:c}});return l.changes.empty?!1:(e.dispatch([i,o.update(l,{userEvent:"input.complete",scrollIntoView:!0})]),!0)});function ce(e,t){return t||(t={parserOptions:{ecmaVersion:2019,sourceType:"module"},env:{browser:!0,node:!0,es6:!0,es2015:!0,es2017:!0,es2020:!0},rules:{}},e.getRules().forEach((r,n)=>{r.meta.docs.recommended&&(t.rules[n]=2)})),r=>{let{state:n}=r,a=[];for(let{from:i,to:o}of m.findRegions(n)){let l=n.doc.lineAt(i),c={line:l.number-1,col:i-l.from,pos:i};for(let u of e.verify(n.sliceDoc(i,o),t))a.push(fe(u,n.doc,c))}return a}}function V(e,t,r,n){return r.line(e+n.line).from+t+(e==1?n.col-1:-1)}function fe(e,t,r){let n=V(e.line,e.column,t,r),a={from:n,to:e.endLine!=null&&e.endColumn!=1?V(e.endLine,e.endColumn,t,r):n,message:e.message,source:e.ruleId?"eslint:"+e.ruleId:"eslint",severity:e.severity==1?"warning":"error"};if(e.fix){let{range:i,text:o}=e.fix,l=i[0]+r.pos-n,c=i[1]+r.pos-n;a.actions=[{name:"fix",apply(u,s){u.dispatch({changes:{from:s+l,to:s+c,insert:o},scrollIntoView:!0})}}]}return a}export{B as autoCloseTags,E as completionPath,ce as esLint,le as javascript,m as javascriptLanguage,A as jsxLanguage,X as localCompletionSource,ae as scopeCompletionSource,w as snippets,L as tsxLanguage,P as typescriptLanguage,I as typescriptSnippets};
