UNPKG

@yuebai008/cli

Version:

Command line interface for rapid qg-minigame development

1 lines 10.2 kB
import*as Common from"../../../core/common/common.js";import*as i18n from"../../../core/i18n/i18n.js";import*as WindowBoundsService from"../../../services/window_bounds/window_bounds.js";import*as CM from"../../../third_party/codemirror.next/codemirror.next.js";import*as UI from"../../legacy/legacy.js";import*as CodeHighlighter from"../code_highlighter/code_highlighter.js";import*as Icon from"../icon_button/icon_button.js";import{editorTheme}from"./theme.js";const LINES_TO_SCAN_FOR_INDENTATION_GUESSING=1e3,UIStrings={codeEditor:"Code editor",sSuggestionSOfS:"{PH1}, suggestion {PH2} of {PH3}"},str_=i18n.i18n.registerUIStrings("ui/components/text_editor/config.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_),empty=[];export const dynamicSetting=CM.Facet.define();export class DynamicSetting{settingName;getExtension;compartment=new CM.Compartment;constructor(t,e){this.settingName=t,this.getExtension=e}settingValue(){return Common.Settings.Settings.instance().moduleSetting(this.settingName).get()}instance(){return[this.compartment.of(this.getExtension(this.settingValue())),dynamicSetting.of(this)]}sync(t,e){const o=this.compartment.get(t),n=this.getExtension(e);return o===n?null:this.compartment.reconfigure(n)}static bool(t,e,o=empty){return new DynamicSetting(t,(t=>t?e:o))}static none=[]}export const tabMovesFocus=DynamicSetting.bool("textEditorTabMovesFocus",[],CM.keymap.of([{key:"Tab",run:t=>!!t.state.doc.length&&CM.indentMore(t),shift:t=>!!t.state.doc.length&&CM.indentLess(t)}]));const disableConservativeCompletion=CM.StateEffect.define();export const conservativeCompletion=CM.StateField.define({create:()=>!0,update:(t,e)=>"active"!==CM.completionStatus(e.state)||(CM.selectedCompletionIndex(e.startState)??0)===(CM.selectedCompletionIndex(e.state)??0)&&!e.effects.some((t=>t.is(disableConservativeCompletion)))&&t});function acceptCompletionIfNotConservative(t){return!t.state.field(conservativeCompletion,!1)&&CM.acceptCompletion(t)}function acceptCompletionIfAtEndOfLine(t){const e=t.state.selection.main.head,o=t.state.doc.lineAt(e);return!!(e-o.from>=o.length)&&CM.acceptCompletion(t)}function moveCompletionSelectionIfNotConservative(t,e="option"){return o=>{if("active"!==CM.completionStatus(o.state))return!1;if(o.state.field(conservativeCompletion,!1))return o.dispatch({effects:disableConservativeCompletion.of(null)}),announceSelectedCompletionInfo(o),!0;const n=CM.moveCompletionSelection(t,e)(o);return announceSelectedCompletionInfo(o),n}}function moveCompletionSelectionBackwardWrapper(){return t=>"active"===CM.completionStatus(t.state)&&(CM.moveCompletionSelection(!1)(t),announceSelectedCompletionInfo(t),!0)}function announceSelectedCompletionInfo(t){const e=i18nString(UIStrings.sSuggestionSOfS,{PH1:CM.selectedCompletion(t.state)?.label||"",PH2:(CM.selectedCompletionIndex(t.state)||0)+1,PH3:CM.currentCompletions(t.state).length});UI.ARIAUtils.alert(e)}export const autocompletion=new DynamicSetting("textEditorAutocompletion",(t=>[CM.autocompletion({activateOnTyping:t,icons:!1,optionClass:t=>"secondary"===t.type?"cm-secondaryCompletion":"",tooltipClass:t=>t.field(conservativeCompletion,!1)?"cm-conservativeCompletion":"",defaultKeymap:!1}),CM.Prec.highest(CM.keymap.of([{key:"End",run:acceptCompletionIfAtEndOfLine},{key:"ArrowRight",run:acceptCompletionIfAtEndOfLine},{key:"Ctrl-Space",run:CM.startCompletion},{key:"Escape",run:CM.closeCompletion},{key:"ArrowDown",run:moveCompletionSelectionIfNotConservative(!0)},{key:"ArrowUp",run:moveCompletionSelectionBackwardWrapper()},{mac:"Ctrl-n",run:moveCompletionSelectionIfNotConservative(!0)},{mac:"Ctrl-p",run:moveCompletionSelectionBackwardWrapper()},{key:"PageDown",run:CM.moveCompletionSelection(!0,"page")},{key:"PageUp",run:CM.moveCompletionSelection(!1,"page")},{key:"Enter",run:acceptCompletionIfNotConservative}]))]));export const bracketMatching=DynamicSetting.bool("textEditorBracketMatching",CM.bracketMatching());export const codeFolding=DynamicSetting.bool("textEditorCodeFolding",[CM.foldGutter({markerDOM(t){const e=t?"triangle-down":"triangle-right",o=new Icon.Icon.Icon;return o.setAttribute("class",t?"cm-foldGutterElement":"cm-foldGutterElement cm-foldGutterElement-folded"),o.data={iconName:e,color:"var(--icon-fold-marker)",width:"14px",height:"14px"},o}}),CM.keymap.of(CM.foldKeymap)]);export function guessIndent(t){const e=Object.create(null);let o=0;for(let n=t.iterLines(1,Math.min(t.lines+1,1e3));!n.next().done;){let t=/^\s*/.exec(n.value)[0];if(t.length!==n.value.length&&t.length&&"*"!==n.value[t.length]){if("\t"===t[0])t="\t";else if(/[^ ]/.test(t))continue;o++,e[t]=(e[t]||0)+1}}const n=.05*o;return Object.entries(e).reduce(((t,[e,o])=>o<n||t&&t.length<e.length?t:e),null)??Common.Settings.Settings.instance().moduleSetting("textEditorIndent").get()}const deriveIndentUnit=CM.Prec.highest(CM.indentUnit.compute([],(t=>guessIndent(t.doc))));export const autoDetectIndent=DynamicSetting.bool("textEditorAutoDetectIndent",deriveIndentUnit);function matcher(t){return CM.ViewPlugin.define((e=>({decorations:t.createDeco(e),update(e){this.decorations=t.updateDeco(e,this.decorations)}})),{decorations:t=>t.decorations})}const WhitespaceDeco=new Map;function getWhitespaceDeco(t){const e=WhitespaceDeco.get(t);if(e)return e;const o=CM.Decoration.mark({attributes:"\t"===t?{class:"cm-highlightedTab"}:{class:"cm-highlightedSpaces","data-display":"·".repeat(t.length)}});return WhitespaceDeco.set(t,o),o}const showAllWhitespace=matcher(new CM.MatchDecorator({regexp:/\t| +/g,decoration:t=>getWhitespaceDeco(t[0]),boundary:/\S/})),showTrailingWhitespace=matcher(new CM.MatchDecorator({regexp:/\s+$/g,decoration:CM.Decoration.mark({class:"cm-trailingWhitespace"}),boundary:/\S/}));export const showWhitespace=new DynamicSetting("showWhitespacesInEditor",(t=>"all"===t?showAllWhitespace:"trailing"===t?showTrailingWhitespace:empty));export const allowScrollPastEof=DynamicSetting.bool("allowScrollPastEof",CM.scrollPastEnd());const cachedIndentUnit=Object.create(null);function getIndentUnit(t){let e=cachedIndentUnit[t];return e||(e=cachedIndentUnit[t]=CM.indentUnit.of(t)),e}export const indentUnit=new DynamicSetting("textEditorIndent",getIndentUnit);export const domWordWrap=DynamicSetting.bool("domWordWrap",CM.EditorView.lineWrapping);function detectLineSeparator(t){return/\r\n/.test(t)&&!/(^|[^\r])\n/.test(t)?CM.EditorState.lineSeparator.of("\r\n"):[]}const baseKeymap=CM.keymap.of([{key:"Tab",run:CM.acceptCompletion},{key:"Ctrl-m",run:CM.cursorMatchingBracket,shift:CM.selectMatchingBracket},{key:"Mod-/",run:CM.toggleComment},{key:"Mod-d",run:CM.selectNextOccurrence},{key:"Alt-ArrowLeft",mac:"Ctrl-ArrowLeft",run:CM.cursorSyntaxLeft,shift:CM.selectSyntaxLeft},{key:"Alt-ArrowRight",mac:"Ctrl-ArrowRight",run:CM.cursorSyntaxRight,shift:CM.selectSyntaxRight},{key:"Ctrl-ArrowLeft",mac:"Alt-ArrowLeft",run:CM.cursorGroupLeft,shift:CM.selectGroupLeft},{key:"Ctrl-ArrowRight",mac:"Alt-ArrowRight",run:CM.cursorGroupRight,shift:CM.selectGroupRight},...CM.standardKeymap,...CM.historyKeymap]);function themeIsDark(){const t=Common.Settings.Settings.instance().moduleSetting("uiTheme").get();return"systemPreferred"===t?window.matchMedia("(prefers-color-scheme: dark)").matches:"dark"===t}export const dummyDarkTheme=CM.EditorView.theme({},{dark:!0});export const themeSelection=new CM.Compartment;export function theme(){return[editorTheme,themeIsDark()?themeSelection.of(dummyDarkTheme):themeSelection.of([])]}let sideBarElement=null;function getTooltipSpace(){return sideBarElement||(sideBarElement=WindowBoundsService.WindowBoundsService.WindowBoundsServiceImpl.instance().getDevToolsBoundingElement()),sideBarElement.getBoundingClientRect()}export function baseConfiguration(t){return[theme(),CM.highlightSpecialChars(),CM.highlightSelectionMatches(),CM.history(),CM.drawSelection(),CM.EditorState.allowMultipleSelections.of(!0),CM.indentOnInput(),CM.syntaxHighlighting(CodeHighlighter.CodeHighlighter.highlightStyle),baseKeymap,CM.EditorView.clickAddsSelectionRange.of((t=>t.altKey||t.ctrlKey)),tabMovesFocus.instance(),bracketMatching.instance(),indentUnit.instance(),CM.Prec.lowest(CM.EditorView.contentAttributes.of({"aria-label":i18nString(UIStrings.codeEditor)})),t instanceof CM.Text?[]:detectLineSeparator(t),CM.tooltips({parent:getTooltipHost(),tooltipSpace:getTooltipSpace})]}export const closeBrackets=[CM.closeBrackets(),CM.keymap.of(CM.closeBracketsKeymap)];let tooltipHost=null;function getTooltipHost(){if(!tooltipHost){const t=CM.EditorState.create({extensions:[editorTheme,themeIsDark()?dummyDarkTheme:[],CM.syntaxHighlighting(CodeHighlighter.CodeHighlighter.highlightStyle),CM.showTooltip.of({pos:0,create:()=>({dom:document.createElement("div")})})]}).facet(CM.EditorView.styleModule),e=document.body.appendChild(document.createElement("div"));e.className="editor-tooltip-host",tooltipHost=e.attachShadow({mode:"open"}),CM.StyleModule.mount(tooltipHost,t)}return tooltipHost}class CompletionHint extends CM.WidgetType{text;constructor(t){super(),this.text=t}eq(t){return this.text===t.text}toDOM(){const t=document.createElement("span");return t.className="cm-completionHint",t.textContent=this.text,t}}export const showCompletionHint=CM.ViewPlugin.fromClass(class{decorations=CM.Decoration.none;currentHint=null;update(t){const e=this.currentHint=this.topCompletion(t.state);!e||t.state.field(conservativeCompletion,!1)?this.decorations=CM.Decoration.none:this.decorations=CM.Decoration.set([CM.Decoration.widget({widget:new CompletionHint(e),side:1}).range(t.state.selection.main.head)])}topCompletion(t){const e=CM.selectedCompletion(t);if(!e)return null;let{label:o,apply:n}=e;if("string"==typeof n&&(o=n,n=void 0),n||o.length>100||o.indexOf("\n")>-1||"secondary"===e.type)return null;const i=t.selection.main.head,r=t.doc.lineAt(i);if(i!==r.to)return null;const c=("'"===o[0]?/'(\\.|[^'\\])*$/:'"'===o[0]?/"(\\.|[^"\\])*$/:/#?[\w$]+$/).exec(r.text);return c&&!o.startsWith(c[0])?null:o.slice(c?c[0].length:0)}},{decorations:t=>t.decorations});export function contentIncludingHint(t){const e=t.plugin(showCompletionHint);let o=t.state.doc.toString();if(e&&e.currentHint){const{head:n}=t.state.selection.main;o=o.slice(0,n)+e.currentHint+o.slice(n)}return o}