|
|
- class Text{constructor(){}lineAt(pos){if(pos<0||pos>this.length)throw new RangeError(`Invalid position ${pos} in document of length ${this.length}`);return this.lineInner(pos,false,1,0)}line(n){if(n<1||n>this.lines)throw new RangeError(`Invalid line number ${n} in ${this.lines}-line document`);return this.lineInner(n,true,1,0)}replace(from,to,text){let parts=[];this.decompose(0,from,parts,2);if(text.length)text.decompose(0,text.length,parts,1|2);this.decompose(to,this.length,parts,1);return TextNode.from(parts,this.length-(to-from)+text.length)}append(other){return this.replace(this.length,this.length,other)}slice(from,to=this.length){let parts=[];this.decompose(from,to,parts,0);return TextNode.from(parts,to-from)}eq(other){if(other==this)return true;if(other.length!=this.length||other.lines!=this.lines)return false;let start=this.scanIdentical(other,1),end=this.length-this.scanIdentical(other,-1);let a=new RawTextCursor(this),b=new RawTextCursor(other);for(let skip=start,pos=start;;){a.next(skip);b.next(skip);skip=0;if(a.lineBreak!=b.lineBreak||a.done!=b.done||a.value!=b.value)return false;pos+=a.value.length;if(a.done||pos>=end)return true}}iter(dir=1){return new RawTextCursor(this,dir)}iterRange(from,to=this.length){return new PartialTextCursor(this,from,to)}iterLines(from,to){let inner;if(from==null){inner=this.iter()}else{if(to==null)to=this.lines+1;let start=this.line(from).from;inner=this.iterRange(start,Math.max(start,to==this.lines+1?this.length:to<=1?0:this.line(to-1).to))}return new LineCursor(inner)}toString(){return this.sliceString(0)}toJSON(){let lines=[];this.flatten(lines);return lines}static of(text){if(text.length==0)throw new RangeError("A document must have at least one line");if(text.length==1&&!text[0])return Text.empty;return text.length<=32?new TextLeaf(text):TextNode.from(TextLeaf.split(text,[]))}}class TextLeaf extends Text{constructor(text,length=textLength(text)){super();this.text=text;this.length=length}get lines(){return this.text.length}get children(){return null}lineInner(target,isLine,line,offset){for(let i=0;;i++){let string=this.text[i],end=offset+string.length;if((isLine?line:end)>=target)return new Line(offset,end,line,string);offset=end+1;line++}}decompose(from,to,target,open){let text=from<=0&&to>=this.length?this:new TextLeaf(sliceText(this.text,from,to),Math.min(to,this.length)-Math.max(0,from));if(open&1){let prev=target.pop();let joined=appendText(text.text,prev.text.slice(),0,text.length);if(joined.length<=32){target.push(new TextLeaf(joined,prev.length+text.length))}else{let mid=joined.length>>1;target.push(new TextLeaf(joined.slice(0,mid)),new TextLeaf(joined.slice(mid)))}}else{target.push(text)}}replace(from,to,text){if(!(text instanceof TextLeaf))return super.replace(from,to,text);let lines=appendText(this.text,appendText(text.text,sliceText(this.text,0,from)),to);let newLen=this.length+text.length-(to-from);if(lines.length<=32)return new TextLeaf(lines,newLen);return TextNode.from(TextLeaf.split(lines,[]),newLen)}sliceString(from,to=this.length,lineSep="\n"){let result="";for(let pos=0,i=0;pos<=to&&i<this.text.length;i++){let line=this.text[i],end=pos+line.length;if(pos>from&&i)result+=lineSep;if(from<end&&to>pos)result+=line.slice(Math.max(0,from-pos),to-pos);pos=end+1}return result}flatten(target){for(let line of this.text)target.push(line)}scanIdentical(){return 0}static split(text,target){let part=[],len=-1;for(let line of text){part.push(line);len+=line.length+1;if(part.length==32){target.push(new TextLeaf(part,len));part=[];len=-1}}if(len>-1)target.push(new TextLeaf(part,len));return target}}class TextNode extends Text{constructor(children,length){super();this.children=children;this.length=length;this.lines=0;for(let child of children)this.lines+=child.lines}lineInner(target,isLine,line,offset){for(let i=0;;i++){let child=this.children[i],end=offset+child.length,endLine=line+child.lines-1;if((isLine?endLine:end)>=target)return child.lineInner(target,isLine,line,offset);offset=end+1;line=endLine+1}}decompose(from,to,target,open){for(let i=0,pos=0;pos<=to&&i<this.chi
|