@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 1.57 kB
JavaScript
import*as Platform from"../../core/platform/platform.js";import{TextCursor}from"./TextCursor.js";import{SourceRange,TextRange}from"./TextRange.js";export class Text{valueInternal;lineEndingsInternal;constructor(n){this.valueInternal=n}lineEndings(){return this.lineEndingsInternal||(this.lineEndingsInternal=Platform.StringUtilities.findLineEndingIndexes(this.valueInternal)),this.lineEndingsInternal}value(){return this.valueInternal}lineCount(){return this.lineEndings().length}offsetFromPosition(n,t){return(n?this.lineEndings()[n-1]+1:0)+t}positionFromOffset(n){const t=this.lineEndings(),e=Platform.ArrayUtilities.lowerBound(t,n,Platform.ArrayUtilities.DEFAULT_COMPARATOR);return{lineNumber:e,columnNumber:n-(e&&t[e-1]+1)}}lineAt(n){const t=this.lineEndings(),e=n>0?t[n-1]+1:0,r=t[n];let i=this.valueInternal.substring(e,r);return i.length>0&&"\r"===i.charAt(i.length-1)&&(i=i.substring(0,i.length-1)),i}toSourceRange(n){const t=this.offsetFromPosition(n.startLine,n.startColumn),e=this.offsetFromPosition(n.endLine,n.endColumn);return new SourceRange(t,e-t)}toTextRange(n){const t=new TextCursor(this.lineEndings()),e=TextRange.createFromLocation(0,0);return t.resetTo(n.offset),e.startLine=t.lineNumber(),e.startColumn=t.columnNumber(),t.advance(n.offset+n.length),e.endLine=t.lineNumber(),e.endColumn=t.columnNumber(),e}replaceRange(n,t){const e=this.toSourceRange(n);return this.valueInternal.substring(0,e.offset)+t+this.valueInternal.substring(e.offset+e.length)}extract(n){const t=this.toSourceRange(n);return this.valueInternal.substr(t.offset,t.length)}}