src/vs/editor/common/model/pieceTreeTextBuffer/pieceTreeTextBufferBuilder.ts
188 LOC · 182 covered · 6 uncovered · 47 ranges · 1708 concepts · 16 introducers · 861 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
textModel.ts ×190
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { CharCode } from '../../../../base/common/charCode.js';
import { IDisposable } from '../../../../base/common/lifecycle.js';
import * as strings from '../../../../base/common/strings.js';
import { DefaultEndOfLine, ITextBuffer, ITextBufferBuilder, ITextBufferFactory } from '../../model.js';
import { StringBuffer, createLineStarts, createLineStartsFast } from './pieceTreeBase.js';
import { PieceTreeTextBuffer } from './pieceTreeTextBuffer.js';
class PieceTreeTextBufferFactory implements ITextBufferFactory {
constructor(
private readonly _bom: string,
private readonly _cr: number,
private readonly _lf: number,
private readonly _crlf: number,
private readonly _containsRTL: boolean,
private readonly _containsUnusualLineTerminators: boolean,
private readonly _isBasicASCII: boolean,
private readonly _normalizeEOL: boolean
) { }
private _getEOL(defaultEOL: DefaultEndOfLine): '\r\n' | '\n' {
const totalCRCount = this._cr + this._crlf;
if (totalEOLCount === 0) {
return (defaultEOL === DefaultEndOfLine.LF ? '\n' : '\r\n');
}
return '\r\n';
}
return '\n';
public create(defaultEOL: DefaultEndOfLine): { textBuffer: ITextBuffer; disposable: IDisposable } {
const chunks = this._chunks;
if (this._normalizeEOL &&
for (let i = 0, len = chunks.length; i < len; i++) {
const str = chunks[i].buffer.replace(/\r\n|\r|\n/g, eol);
const newLineStart = createLineStartsFast(str);
chunks[i] = new StringBuffer(str, newLineStart);
}
}
const textBuffer = new PieceTreeTextBuffer(chunks, this._bom, eol, this._containsRTL, this._containsUnusualLineTerminators, this._isBasicASCII, this._normalizeEOL);
return { textBuffer: textBuffer, disposable: textBuffer };
}
public getFirstLineText(lengthLimit: number): string {
return this._chunks[0].buffer.substr(0, lengthLimit).split(/\r\n|\r|\n/)[0];
}
export class PieceTreeTextBufferBuilder implements ITextBufferBuilder {
private readonly chunks: StringBuffer[];
private BOM: string;
private _hasPreviousChar: boolean;
private _previousChar: number;
private readonly _tmpLineStarts: number[];
private cr: number;
private lf: number;
private crlf: number;
private containsRTL: boolean;
private containsUnusualLineTerminators: boolean;
private isBasicASCII: boolean;
constructor() {
this.BOM = '';
this._hasPreviousChar = false;
this._previousChar = 0;
this._tmpLineStarts = [];
this.cr = 0;
this.lf = 0;
this.crlf = 0;
this.containsRTL = false;
this.containsUnusualLineTerminators = false;
this.isBasicASCII = true;
}
public acceptChunk(chunk: string): void {
}
if (this.chunks.length === 0) {
if (strings.startsWithUTF8BOM(chunk)) {
chunk = chunk.substr(1);
}
const lastChar = chunk.charCodeAt(chunk.length - 1);
if (lastChar === CharCode.CarriageReturn || (lastChar >= 0xD800 && lastChar <= 0xDBFF)) {
pieceTreeTextBufferBuilder.ts ×19
this._acceptChunk1(chunk.substr(0, chunk.length - 1), false);
this._hasPreviousChar = true;
this._previousChar = lastChar;
this._hasPreviousChar = false;
this._previousChar = lastChar;
}
private _acceptChunk1(chunk: string, allowEmptyStrings: boolean): void {
// Nothing to do
return;
}
if (this._hasPreviousChar) {
this._acceptChunk2(String.fromCharCode(this._previousChar) + chunk);
this._acceptChunk2(chunk);
}
}
private _acceptChunk2(chunk: string): void {
const lineStarts = createLineStarts(this._tmpLineStarts, chunk);
pieceTreeTextBufferBuilder.ts ×19
this.chunks.push(new StringBuffer(chunk, lineStarts.lineStarts));
this.cr += lineStarts.cr;
this.lf += lineStarts.lf;
this.crlf += lineStarts.crlf;
if (!lineStarts.isBasicASCII) {
this.isBasicASCII = false;
if (!this.containsRTL) {
this.containsRTL = strings.containsRTL(chunk);
}
if (!this.containsUnusualLineTerminators) {
this.containsUnusualLineTerminators = strings.containsUnusualLineTerminators(chunk);
}
}
public finish(normalizeEOL: boolean = true): PieceTreeTextBufferFactory {
return new PieceTreeTextBufferFactory(
this.chunks,
this.BOM,
this.cr,
this.lf,
this.crlf,
this.containsRTL,
this.containsUnusualLineTerminators,
this.isBasicASCII,
normalizeEOL
);
}
private _finish(): void {
}
if (this._hasPreviousChar) {
// recreate last chunk
const lastChunk = this.chunks[this.chunks.length - 1];
lastChunk.buffer += String.fromCharCode(this._previousChar);
const newLineStarts = createLineStartsFast(lastChunk.buffer);
lastChunk.lineStarts = newLineStarts;
if (this._previousChar === CharCode.CarriageReturn) {
this.cr++;
}
}