import{parser as M}from"@lezer/javascript";import{syntaxTree as S,LRLanguage as _,indentNodeProp as R,flatIndent as W,continuedIndent as $,delimitedIndent as Z,foldNodeProp as z,foldInside as H,sublanguageProp as T,LanguageSupport as K,defineLanguageFacet 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"})],J=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,I=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 x(e,t){let r=X.get(t);if(r)return r;let n=[],o=!0;function i(a,l){let c=e.sliceString(a.from,a.to);n.push({label:c,type:l})}return t.cursor(te.IncludeAnonymous).iterate(a=>{if(o)o=!1;else if(a.name){let l=re[a.name];if(l&&l(a,i)||I.has(a.name))return!1}else if(a.to-a.from>8192){for(let l of x(e,a.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","JSXText","JSXAttributeValue","JSXOpenTag","JSXCloseTag","JSXSelfClosingTag",".","?."];function D(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 o=t;o;o=o.parent)I.has(o.name)&&(n=n.concat(x(e.state.doc,o)));return{options:n,from:r?t.from:e.pos,validFor:h}}function v(e,t,r){var n;let o=[];for(;;){let i=t.firstChild,a;if(i?.name=="VariableName")return o.push(e(i)),{path:o.reverse(),name:r};if(i?.name=="MemberExpression"&&((n=a=i.lastChild)===null||n===void 0?void 0:n.name)=="PropertyName")o.push(e(a)),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 o=0;;o++){for(let a of(Object.getOwnPropertyNames||Object.keys)(e)){if(!/^[a-zA-Z_$\xaa-\uffdc][\w$\xaa-\uffdc]*$/.test(a)||n.has(a))continue;n.add(a);let l;try{l=e[a]}catch{continue}r.push({label:a,type:typeof l=="function"?/^[A-Z]/.test(a)?"class":t?"function":"method":t?"variable":"property",boost:-o})}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 o=e;for(let a of n.path)if(o=o[a],!o)return null;let i=t.get(o);return i||t.set(o,i=oe(o,!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:/^\s*{/}),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:"$"}}),A={test:e=>/^JSX/.test(e.name),facet:q({commentTokens:{block:{open:"{/*",close:"*/}"}}})},N=m.configure({dialect:"ts"},"typescript"),O=m.configure({dialect:"jsx",props:[T.add(e=>e.isTop?[A]:void 0)]}),P=m.configure({dialect:"jsx ts",props:[T.add(e=>e.isTop?[A]:void 0)]},"typescript");let L=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(L),ie=F.concat(["declare","implements","private","protected","public"].map(L));function le(e={}){let t=e.jsx?e.typescript?P:O:e.typescript?N:m,r=e.typescript?J.concat(ie):w.concat(F);return new K(t,[m.data.of({autocomplete:U(k,Y(r))}),m.data.of({autocomplete:D}),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,o)=>{if((pe?e.composing:e.compositionStarted)||e.state.readOnly||t!=r||n!=">"&&n!="/"||!m.isActiveAt(e.state,t,-1))return!1;let i=o(),{state:a}=i,l=a.changeByRange(c=>{var u;let{head:s}=c,f=S(a).resolveInner(s-1,-1),g;if(f.name=="JSXStartTag"&&(f=f.parent),!(a.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(a.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(a.doc.sliceString(s,s+2))&&(g=j(a.doc,d,s)))return{range:c,changes:{from:s,insert:`</${g}>`}}}}return{range:c}});return l.changes.empty?!1:(e.dispatch([i,a.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)=>{var o;!((o=r.meta.docs)===null||o===void 0)&&o.recommended&&(t.rules[n]=2)})),r=>{let{state:n}=r,o=[];for(let{from:i,to:a}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,a),t))o.push(fe(u,n.doc,c))}return o}}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),o={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:a}=e.fix,l=i[0]+r.pos-n,c=i[1]+r.pos-n;o.actions=[{name:"fix",apply(u,s){u.dispatch({changes:{from:s+l,to:s+c,insert:a},scrollIntoView:!0})}}]}return o}export{B as autoCloseTags,E as completionPath,ce as esLint,le as javascript,m as javascriptLanguage,O as jsxLanguage,D as localCompletionSource,ae as scopeCompletionSource,w as snippets,P as tsxLanguage,N as typescriptLanguage,J as typescriptSnippets};
