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.

1 > /*--------------------------------------------------------------------------------------------- textModel.ts ×190
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { CharCode } from '../../../../base/common/charCode.js';
7 > import { IDisposable } from '../../../../base/common/lifecycle.js';
8 > import * as strings from '../../../../base/common/strings.js';
9 > import { DefaultEndOfLine, ITextBuffer, ITextBufferBuilder, ITextBufferFactory } from '../../model.js';
10 > import { StringBuffer, createLineStarts, createLineStartsFast } from './pieceTreeBase.js';
11 > import { PieceTreeTextBuffer } from './pieceTreeTextBuffer.js';
12 >
13 > class PieceTreeTextBufferFactory implements ITextBufferFactory {
14 >
15 > constructor(
16 > private readonly _chunks: StringBuffer[], pieceTreeTextBufferBuilder.ts ×19
17 > private readonly _bom: string,
18 > private readonly _cr: number,
19 > private readonly _lf: number,
20 > private readonly _crlf: number,
21 > private readonly _containsRTL: boolean,
22 > private readonly _containsUnusualLineTerminators: boolean,
23 > private readonly _isBasicASCII: boolean,
24 > private readonly _normalizeEOL: boolean
25 > ) { }
27 > private _getEOL(defaultEOL: DefaultEndOfLine): '\r\n' | '\n' {
28 > const totalEOLCount = this._cr + this._lf + this._crlf; pieceTreeTextBufferBuilder.ts ×19
29 > const totalCRCount = this._cr + this._crlf;
30 > if (totalEOLCount === 0) {
31 > // This is an empty file or a file with precisely one line pieceTreeTextBufferBuilder.ts ×1
32 > return (defaultEOL === DefaultEndOfLine.LF ? '\n' : '\r\n');
33 > }
34 > if (totalCRCount > totalEOLCount / 2) { pieceTreeTextBufferBuilder.ts ×1
35 > // More than half of the file contains \r\n ending lines pieceTreeTextBufferBuilder.ts ×1
36 > return '\r\n';
37 > }
38 > // At least one line more ends in \n pieceTreeTextBufferBuilder.ts ×1
39 > return '\n';
42 > public create(defaultEOL: DefaultEndOfLine): { textBuffer: ITextBuffer; disposable: IDisposable } {
43 > const eol = this._getEOL(defaultEOL); pieceTreeTextBufferBuilder.ts ×19
44 > const chunks = this._chunks;
45 >
46 > if (this._normalizeEOL &&
47 > ((eol === '\r\n' && (this._cr > 0 || this._lf > 0)) pieceTreeTextBufferBuilder.ts ×1
48 > || (eol === '\n' && (this._cr > 0 || this._crlf > 0))) pieceTreeTextBufferBuilder.ts ×1
50 > // Normalize pieces pieceTreeTextBufferBuilder.ts ×1
51 > for (let i = 0, len = chunks.length; i < len; i++) {
52 > const str = chunks[i].buffer.replace(/\r\n|\r|\n/g, eol);
53 > const newLineStart = createLineStartsFast(str);
54 > chunks[i] = new StringBuffer(str, newLineStart);
55 > }
56 > }
58 > const textBuffer = new PieceTreeTextBuffer(chunks, this._bom, eol, this._containsRTL, this._containsUnusualLineTerminators, this._isBasicASCII, this._normalizeEOL);
59 > return { textBuffer: textBuffer, disposable: textBuffer };
60 > }
62 > public getFirstLineText(lengthLimit: number): string {
63 return this._chunks[0].buffer.substr(0, lengthLimit).split(/\r\n|\r|\n/)[0];
64 }
66 >
67 > export class PieceTreeTextBufferBuilder implements ITextBufferBuilder {
68 > private readonly chunks: StringBuffer[];
69 > private BOM: string;
70 >
71 > private _hasPreviousChar: boolean;
72 > private _previousChar: number;
73 > private readonly _tmpLineStarts: number[];
74 >
75 > private cr: number;
76 > private lf: number;
77 > private crlf: number;
78 > private containsRTL: boolean;
79 > private containsUnusualLineTerminators: boolean;
80 > private isBasicASCII: boolean;
81 >
82 > constructor() {
83 > this.chunks = []; pieceTreeTextBufferBuilder.ts ×19
84 > this.BOM = '';
85 >
86 > this._hasPreviousChar = false;
87 > this._previousChar = 0;
88 > this._tmpLineStarts = [];
89 >
90 > this.cr = 0;
91 > this.lf = 0;
92 > this.crlf = 0;
93 > this.containsRTL = false;
94 > this.containsUnusualLineTerminators = false;
95 > this.isBasicASCII = true;
96 > }
98 > public acceptChunk(chunk: string): void {
99 > if (chunk.length === 0) { pieceTreeTextBufferBuilder.ts ×19
101 > }
103 > if (this.chunks.length === 0) {
104 > if (strings.startsWithUTF8BOM(chunk)) {
105 > this.BOM = strings.UTF8_BOM_CHARACTER; pieceTreeTextBufferBuilder.ts ×1
106 > chunk = chunk.substr(1);
107 > }
109 >
110 > const lastChar = chunk.charCodeAt(chunk.length - 1);
111 > if (lastChar === CharCode.CarriageReturn || (lastChar >= 0xD800 && lastChar <= 0xDBFF)) { pieceTreeTextBufferBuilder.ts ×19
112 > // last character is \r or a high surrogate => keep it back pieceTreeTextBufferBuilder.ts ×2
113 > this._acceptChunk1(chunk.substr(0, chunk.length - 1), false);
114 > this._hasPreviousChar = true;
115 > this._previousChar = lastChar;
116 > } else { pieceTreeBase.ts ×7
117 > this._acceptChunk1(chunk, false); pieceTreeTextBufferBuilder.ts ×1
118 > this._hasPreviousChar = false;
119 > this._previousChar = lastChar;
120 > }
123 > private _acceptChunk1(chunk: string, allowEmptyStrings: boolean): void {
124 > if (!allowEmptyStrings && chunk.length === 0) { pieceTreeTextBufferBuilder.ts ×19
125 // Nothing to do
126 return;
127 }
129 > if (this._hasPreviousChar) {
130 this._acceptChunk2(String.fromCharCode(this._previousChar) + chunk);
132 > this._acceptChunk2(chunk);
133 > }
134 > }
136 > private _acceptChunk2(chunk: string): void {
137 > const lineStarts = createLineStarts(this._tmpLineStarts, chunk); pieceTreeTextBufferBuilder.ts ×19
138 >
139 > this.chunks.push(new StringBuffer(chunk, lineStarts.lineStarts));
140 > this.cr += lineStarts.cr;
141 > this.lf += lineStarts.lf;
142 > this.crlf += lineStarts.crlf;
143 >
144 > if (!lineStarts.isBasicASCII) {
145 > // this chunk contains non basic ASCII characters pieceTreeBase.ts ×1
146 > this.isBasicASCII = false;
147 > if (!this.containsRTL) {
148 > this.containsRTL = strings.containsRTL(chunk);
149 > }
150 > if (!this.containsUnusualLineTerminators) {
151 > this.containsUnusualLineTerminators = strings.containsUnusualLineTerminators(chunk);
152 > }
153 > }
156 > public finish(normalizeEOL: boolean = true): PieceTreeTextBufferFactory {
157 > this._finish(); pieceTreeTextBufferBuilder.ts ×19
158 > return new PieceTreeTextBufferFactory(
159 > this.chunks,
160 > this.BOM,
161 > this.cr,
162 > this.lf,
163 > this.crlf,
164 > this.containsRTL,
165 > this.containsUnusualLineTerminators,
166 > this.isBasicASCII,
167 > normalizeEOL
168 > );
169 > }
171 > private _finish(): void {
172 > if (this.chunks.length === 0) { pieceTreeTextBufferBuilder.ts ×19
173 > this._acceptChunk1('', true); pieceTreeTextBufferBuilder.ts ×1
174 > }
176 > if (this._hasPreviousChar) {
177 > this._hasPreviousChar = false; pieceTreeTextBufferBuilder.ts ×2
178 > // recreate last chunk
179 > const lastChunk = this.chunks[this.chunks.length - 1];
180 > lastChunk.buffer += String.fromCharCode(this._previousChar);
181 > const newLineStarts = createLineStartsFast(lastChunk.buffer);
182 > lastChunk.lineStarts = newLineStarts;
183 > if (this._previousChar === CharCode.CarriageReturn) {
184 > this.cr++;
185 > }
186 > }