UNPKG

@yuebai008/cli

Version:

Command line interface for rapid qg-minigame development

1 lines 5.49 kB
import*as SDK from"../../core/sdk/sdk.js";import*as TextUtils from"../text_utils/text_utils.js";import*as Workspace from"../workspace/workspace.js";import{DebuggerWorkspaceBinding}from"./DebuggerWorkspaceBinding.js";import{LiveLocationPool,LiveLocationWithPool}from"./LiveLocation.js";import{CSSWorkspaceBinding}from"./CSSWorkspaceBinding.js";export class PresentationSourceFrameMessageManager{#e=new WeakMap;constructor(){SDK.TargetManager.TargetManager.instance().observeModels(SDK.DebuggerModel.DebuggerModel,this),SDK.TargetManager.TargetManager.instance().observeModels(SDK.CSSModel.CSSModel,this)}modelAdded(e){const o=e.target(),s=this.#e.get(o)??new PresentationSourceFrameMessageHelper;e instanceof SDK.DebuggerModel.DebuggerModel?s.setDebuggerModel(e):s.setCSSModel(e),this.#e.set(o,s)}modelRemoved(e){const o=e.target(),s=this.#e.get(o);s?.clear()}addMessage(e,o,s){const t=this.#e.get(s);t?.addMessage(e,o)}clear(){for(const e of SDK.TargetManager.TargetManager.instance().targets()){const o=this.#e.get(e);o?.clear()}}}export class PresentationConsoleMessageManager{#o=new PresentationSourceFrameMessageManager;constructor(){SDK.TargetManager.TargetManager.instance().addModelListener(SDK.ConsoleModel.ConsoleModel,SDK.ConsoleModel.Events.MessageAdded,(e=>this.consoleMessageAdded(e.data))),SDK.ConsoleModel.ConsoleModel.allMessagesUnordered().forEach(this.consoleMessageAdded,this),SDK.TargetManager.TargetManager.instance().addModelListener(SDK.ConsoleModel.ConsoleModel,SDK.ConsoleModel.Events.ConsoleCleared,(()=>this.#o.clear()))}consoleMessageAdded(e){const o=e.runtimeModel();if(!e.isErrorOrWarning()||!e.runtimeModel()||"violation"===e.source||!o)return;const s="error"===e.level?Workspace.UISourceCode.Message.Level.Error:Workspace.UISourceCode.Message.Level.Warning;this.#o.addMessage(new Workspace.UISourceCode.Message(s,e.messageText),e,o.target())}}export class PresentationSourceFrameMessageHelper{#s;#t;#a=new Map;#r;constructor(){this.#r=new LiveLocationPool,Workspace.Workspace.WorkspaceImpl.instance().addEventListener(Workspace.Workspace.Events.UISourceCodeAdded,this.#i.bind(this))}setDebuggerModel(e){if(this.#s)throw new Error("Cannot set DebuggerModel twice");this.#s=e,e.addEventListener(SDK.DebuggerModel.Events.ParsedScriptSource,(e=>{queueMicrotask((()=>{this.#n(e)}))})),e.addEventListener(SDK.DebuggerModel.Events.GlobalObjectCleared,this.#c,this)}setCSSModel(e){if(this.#t)throw new Error("Cannot set CSSModel twice");this.#t=e,e.addEventListener(SDK.CSSModel.Events.StyleSheetAdded,(e=>queueMicrotask((()=>this.#d(e)))))}async addMessage(e,o){const s=new PresentationSourceFrameMessage(e,this.#r),t=this.#l(o)??this.#u(o)??this.#g(o);if(t&&await s.updateLocationSource(t),o.url){let e=this.#a.get(o.url);e||(e=[],this.#a.set(o.url,e)),e.push({source:o,presentation:s})}}#g(e){if(!e.url)return null;const o=Workspace.Workspace.WorkspaceImpl.instance().uiSourceCodeForURL(e.url);return o?new Workspace.UISourceCode.UILocation(o,e.line,e.column):null}#u(e){if(!this.#t||!e.url)return null;return this.#t.createRawLocationsByURL(e.url,e.line,e.column)[0]??null}#l(e){if(!this.#s)return null;if(e.scriptId)return this.#s.createRawLocationByScriptId(e.scriptId,e.line,e.column);const o=e.stackTrace&&e.stackTrace.callFrames?e.stackTrace.callFrames[0]:null;return o?this.#s.createRawLocationByScriptId(o.scriptId,o.lineNumber,o.columnNumber):e.url?this.#s.createRawLocationByURL(e.url,e.line,e.column):null}#n(e){const o=e.data,s=this.#a.get(o.sourceURL),t=[];for(const{presentation:e,source:a}of s??[]){const s=this.#l(a);s&&o.scriptId===s.scriptId&&t.push(e.updateLocationSource(s))}Promise.all(t).then(this.parsedScriptSourceForTest.bind(this))}parsedScriptSourceForTest(){}#i(e){const o=e.data,s=this.#a.get(o.url()),t=[];for(const{presentation:e,source:a}of s??[])t.push(e.updateLocationSource(new Workspace.UISourceCode.UILocation(o,a.line,a.column)));Promise.all(t).then(this.uiSourceCodeAddedForTest.bind(this))}uiSourceCodeAddedForTest(){}#d(e){const o=e.data,s=this.#a.get(o.sourceURL),t=[];for(const{source:e,presentation:a}of s??[])o.containsLocation(e.line,e.column)&&t.push(a.updateLocationSource(new SDK.CSSModel.CSSLocation(o,e.line,e.column)));Promise.all(t).then(this.styleSheetAddedForTest.bind(this))}styleSheetAddedForTest(){}clear(){this.#c()}#c(){const e=Array.from(this.#a.values()).flat();for(const{presentation:o}of e)o.dispose();this.#a.clear(),this.#r.disposeAll()}}class FrozenLiveLocation extends LiveLocationWithPool{#g;constructor(e,o,s){super(o,s),this.#g=e}async isIgnoreListed(){return!1}async uiLocation(){return this.#g}}export class PresentationSourceFrameMessage{#M;#p;#r;#h;constructor(e,o){this.#h=e,this.#r=o}async updateLocationSource(e){e instanceof SDK.DebuggerModel.Location?await DebuggerWorkspaceBinding.instance().createLiveLocation(e,this.#S.bind(this),this.#r):e instanceof SDK.CSSModel.CSSLocation?await CSSWorkspaceBinding.instance().createLiveLocation(e,this.#S.bind(this),this.#r):e instanceof Workspace.UISourceCode.UILocation&&(this.#p||(this.#p=new FrozenLiveLocation(e,this.#S.bind(this),this.#r),await this.#p.update()))}async#S(e){this.#M&&this.#M.removeMessage(this.#h),e!==this.#p&&(this.#M?.removeMessage(this.#h),this.#p?.dispose(),this.#p=e);const o=await e.uiLocation();o&&(this.#h.range=TextUtils.TextRange.TextRange.createFromLocation(o.lineNumber,o.columnNumber||0),this.#M=o.uiSourceCode,this.#M.addMessage(this.#h))}dispose(){this.#M?.removeMessage(this.#h),this.#p?.dispose()}}