UNPKG

@yuebai008/cli

Version:

Command line interface for rapid qg-minigame development

1 lines 3.51 kB
import*as i18n from"../../core/i18n/i18n.js";import*as Platform from"../../core/platform/platform.js";import*as CodeMirror from"../../third_party/codemirror.next/codemirror.next.js";import*as SourceFrame from"../../ui/legacy/components/source_frame/source_frame.js";import{Plugin}from"./Plugin.js";const UIStrings={ms:"ms",mb:"MB",kb:"kB"},str_=i18n.i18n.registerUIStrings("panels/sources/ProfilePlugin.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);class MemoryMarker extends CodeMirror.GutterMarker{value;constructor(e){super(),this.value=e}eq(e){return this.value===e.value}toDOM(){const e=document.createElement("div");e.className="cm-profileMarker";let r=this.value;const t=Platform.NumberUtilities.clamp(Math.log10(1+.002*r)/5,.02,1);let o,i;e.style.backgroundColor=`hsla(217, 100%, 70%, ${t.toFixed(3)})`,r/=1e3,r>=1e3?(o=i18nString(UIStrings.mb),r/=1e3,i=r>=20?0:1):(o=i18nString(UIStrings.kb),i=0),e.textContent=r.toFixed(i);const a=e.appendChild(document.createElement("span"));return a.className="cm-units",a.textContent=o,e}}class PerformanceMarker extends CodeMirror.GutterMarker{value;constructor(e){super(),this.value=e}eq(e){return this.value===e.value}toDOM(){const e=document.createElement("div");e.className="cm-profileMarker";const r=Platform.NumberUtilities.clamp(Math.log10(1+10*this.value)/5,.02,1);e.textContent=this.value.toFixed(1),e.style.backgroundColor=`hsla(44, 100%, 50%, ${r.toFixed(3)})`;const t=document.createElement("span");return t.className="cm-units",t.textContent=i18nString(UIStrings.ms),e.appendChild(t),e}}function markersFromProfileData(e,r,t){const o=t===SourceFrame.SourceFrame.DecoratorType.PERFORMANCE?PerformanceMarker:MemoryMarker,i=[];for(const[t,a]of e)if(t<=r.doc.lines){const{from:e}=r.doc.line(t);i.push(new o(a).range(e))}return CodeMirror.RangeSet.of(i,!0)}const makeLineLevelProfilePlugin=e=>class extends Plugin{updateEffect=CodeMirror.StateEffect.define();field;gutter;compartment=new CodeMirror.Compartment;constructor(r){super(r),this.field=CodeMirror.StateField.define({create:()=>CodeMirror.RangeSet.empty,update:(r,t)=>t.effects.reduce(((r,o)=>o.is(this.updateEffect)?markersFromProfileData(o.value,t.state,e):r),r.map(t.changes))}),this.gutter=CodeMirror.gutter({markers:e=>e.state.field(this.field),class:`cm-${e}Gutter`})}static accepts(e){return e.contentType().hasScripts()}getLineMap(){return this.uiSourceCode.getDecorationData(e)}editorExtension(){const r=this.getLineMap();return this.compartment.of(r?[this.field.init((t=>markersFromProfileData(r,t,e))),this.gutter,theme]:[])}decorationChanged(e,r){const t=Boolean(r.state.field(this.field,!1)),o=this.getLineMap();o?t?r.dispatch({effects:this.updateEffect.of(o)}):r.dispatch({effects:this.compartment.reconfigure([this.field.init((r=>markersFromProfileData(o,r,e))),this.gutter,theme])}):t&&r.dispatch({effects:this.compartment.reconfigure([])})}},theme=CodeMirror.EditorView.baseTheme({".cm-performanceGutter":{width:"60px",backgroundColor:"var(--color-background)",marginLeft:"3px"},".cm-memoryGutter":{width:"48px",backgroundColor:"var(--color-background)",marginLeft:"3px"},".cm-profileMarker":{textAlign:"right",paddingRight:"3px"},".cm-profileMarker .cm-units":{color:"var(--color-text-secondary)",fontSize:"75%",marginLeft:"3px"}});export const MemoryProfilePlugin=makeLineLevelProfilePlugin(SourceFrame.SourceFrame.DecoratorType.MEMORY);export const PerformanceProfilePlugin=makeLineLevelProfilePlugin(SourceFrame.SourceFrame.DecoratorType.PERFORMANCE);