UNPKG

@yuebai008/cli

Version:

Command line interface for rapid qg-minigame development

1 lines 6.34 kB
import*as i18n from"../../core/i18n/i18n.js";import*as CodeMirror from"../../third_party/codemirror.next/codemirror.next.js";import*as QuickOpen from"../../ui/legacy/components/quick_open/quick_open.js";import*as UI from"../../ui/legacy/legacy.js";import{SourcesView}from"./SourcesView.js";const UIStrings={noFileSelected:"No file selected.",openAJavascriptOrCssFileToSee:"Open a JavaScript or CSS file to see symbols",noResultsFound:"No results found"},str_=i18n.i18n.registerUIStrings("panels/sources/OutlineQuickOpen.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export function outline(e){function t(t){t=Math.max(0,Math.min(t,e.doc.length));const i=e.doc.lineAt(t);return{lineNumber:i.number-1,columnNumber:t-i.from}}function i(){for(;"ParamList"!==r.name&&r.nextSibling(););let t="";if("ParamList"===r.name&&r.firstChild())do{switch(r.name){case"ArrayPattern":t+="[‥]";break;case"ObjectPattern":t+="{‥}";break;case"VariableDefinition":t+=e.sliceDoc(r.from,r.to);break;case"Spread":t+="...";break;case",":t+=", "}}while(r.nextSibling());return`(${t})`}const n=[],r=CodeMirror.syntaxTree(e).cursor();do{switch(r.name){case"RuleSet":for(r.firstChild();;r.nextSibling()){const i=e.sliceDoc(r.from,r.to),{lineNumber:o,columnNumber:s}=t(r.from);if(n.push({title:i,lineNumber:o,columnNumber:s}),r.nextSibling(),","!==r.name)break}break;case"FunctionDeclaration":case"MethodDeclaration":{let o="";r.firstChild();do{switch(r.name){case"abstract":case"async":case"get":case"set":case"static":o=`${o}${r.name} `;break;case"Star":o+="*";break;case"PropertyDefinition":case"VariableDefinition":{const s=o+e.sliceDoc(r.from,r.to),{lineNumber:l,columnNumber:c}=t(r.from),m=i();n.push({title:s,subtitle:m,lineNumber:l,columnNumber:c});break}}}while(r.nextSibling());break}case"Property":{let o="";r.firstChild();do{if("async"===r.name||"get"===r.name||"set"===r.name)o=`${o}${r.name} `;else{if("Star"!==r.name){if("PropertyDefinition"===r.name){let s=e.sliceDoc(r.from,r.to);const{lineNumber:l,columnNumber:c}=t(r.from);for(;r.nextSibling();){if("ClassExpression"===r.name){s=`class ${s}`,n.push({title:s,lineNumber:l,columnNumber:c});break}if("ArrowFunction"!==r.name&&"FunctionExpression"!==r.name||r.firstChild(),"async"===r.name)o=`async ${o}`;else if("Star"===r.name)o+="*";else if("ParamList"===r.name){s=o+s;const e=i();n.push({title:s,subtitle:e,lineNumber:l,columnNumber:c});break}}break}break}o+="*"}}while(r.nextSibling());break}case"PropertyName":case"VariableDefinition":if(r.matchContext(["ClassDeclaration"])){const i="class "+e.sliceDoc(r.from,r.to),{lineNumber:o,columnNumber:s}=t(r.from);n.push({title:i,lineNumber:o,columnNumber:s})}else if(r.matchContext(["AssignmentExpression","MemberExpression"])||r.matchContext(["VariableDeclaration"])){let o=e.sliceDoc(r.from,r.to);const{lineNumber:s,columnNumber:l}=t(r.from);for(;"Equals"!==r.name&&r.next(););if(!r.nextSibling())break;if("ArrowFunction"===r.name||"FunctionExpression"===r.name){r.firstChild();let e="";for(;"ParamList"!==r.name&&("async"===r.name?e=`async ${e}`:"Star"===r.name&&(e+="*"),r.nextSibling()););o=e+o;const t=i();n.push({title:o,subtitle:t,lineNumber:s,columnNumber:l})}else"ClassExpression"===r.name&&(o=`class ${o}`,n.push({title:o,lineNumber:s,columnNumber:l}))}break;case"App":if(r.firstChild()&&r.nextSibling()&&"module"===e.sliceDoc(r.from,r.to)){if(r.nextSibling()&&"Identifier"===r.name){const i=e.sliceDoc(r.from,r.to),{lineNumber:o,columnNumber:s}=t(r.from);n.push({title:i,lineNumber:o,columnNumber:s})}do{if("App"===r.name&&r.firstChild()){if(r.nextSibling()&&"func"===e.sliceDoc(r.from,r.to)&&r.nextSibling()&&"Identifier"===r.name){const i=e.sliceDoc(r.from,r.to),{lineNumber:o,columnNumber:s}=t(r.from),l=[];for(;r.nextSibling();)"App"===r.name&&r.firstChild()&&(r.nextSibling()&&"param"===e.sliceDoc(r.from,r.to)&&(r.nextSibling()&&"Identifier"===r.name?l.push(e.sliceDoc(r.from,r.to)):l.push(`$${l.length}`)),r.parent());const c=`(${l.join(", ")})`;n.push({title:i,subtitle:c,lineNumber:o,columnNumber:s})}r.parent()}}while(r.nextSibling())}break;case"FieldIdentifier":case"Identifier":if(r.matchContext(["FunctionDeclarator"])){const i=e.sliceDoc(r.from,r.to),{lineNumber:o,columnNumber:s}=t(r.from);n.push({title:i,lineNumber:o,columnNumber:s})}break;case"TypeIdentifier":if(r.matchContext(["ClassSpecifier"])){const i=`class ${e.sliceDoc(r.from,r.to)}`,{lineNumber:o,columnNumber:s}=t(r.from);n.push({title:i,lineNumber:o,columnNumber:s})}else if(r.matchContext(["StructSpecifier"])){const i=`struct ${e.sliceDoc(r.from,r.to)}`,{lineNumber:o,columnNumber:s}=t(r.from);n.push({title:i,lineNumber:o,columnNumber:s})}}}while(r.next());return n}export class OutlineQuickOpen extends QuickOpen.FilteredListWidget.Provider{items=[];active=!1;attach(){const e=this.currentSourceFrame();e?(this.active=!0,this.items=outline(e.textEditor.state).map((({title:t,subtitle:i,lineNumber:n,columnNumber:r})=>(({lineNumber:n,columnNumber:r}=e.editorLocationToUILocation(n,r)),{title:t,subtitle:i,lineNumber:n,columnNumber:r})))):(this.active=!1,this.items=[])}detach(){this.active=!1,this.items=[]}itemCount(){return this.items.length}itemKeyAt(e){const t=this.items[e];return t.title+(t.subtitle?t.subtitle:"")}itemScoreAt(e,t){const i=this.items[e];return t.split("(")[0].toLowerCase()===i.title.toLowerCase()?1/(1+i.lineNumber):-i.lineNumber-1}renderItem(e,t,i,n){const r=this.items[e];i.textContent=r.title+(r.subtitle?r.subtitle:""),QuickOpen.FilteredListWidget.FilteredListWidget.highlightRanges(i,t);const o=this.currentSourceFrame();if(!o)return;const s=i.parentElement?.parentElement?.createChild("span","tag");if(!s)return;const l=o.wasmDisassembly;if(l){const e=l.lineNumberToBytecodeOffset(l.lineNumbers-1).toString(16).length;s.textContent=`:0x${r.columnNumber.toString(16).padStart(e,"0")}`}else s.textContent=`:${r.lineNumber+1}`}selectItem(e,t){if(null===e)return;const i=this.currentSourceFrame();if(!i)return;const n=this.items[e];i.revealPosition({lineNumber:n.lineNumber,columnNumber:n.columnNumber},!0)}currentSourceFrame(){const e=UI.Context.Context.instance().flavor(SourcesView);return e&&e.currentSourceFrame()}notFoundText(){return this.currentSourceFrame()?this.active?i18nString(UIStrings.noResultsFound):i18nString(UIStrings.openAJavascriptOrCssFileToSee):i18nString(UIStrings.noFileSelected)}}