| 1 |
/** |
| 2 |
* Copyright (c) 2017 The xterm.js authors. All rights reserved. |
| 3 |
* @license MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
import { CircularList, IInsertEvent } from 'common/CircularList'; |
| 7 |
import { IdleTaskQueue } from 'common/TaskQueue'; |
| 8 |
import { IAttributeData, IBufferLine, ICellData, ICharset } from 'common/Types'; |
| 9 |
import { ExtendedAttrs } from 'common/buffer/AttributeData'; |
| 10 |
import { BufferLine, DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine'; |
| 11 |
import { getWrappedLineTrimmedLength, reflowLargerApplyNewLayout, reflowLargerCreateNewLayout, reflowLargerGetLinesToRemove, reflowSmallerGetNewLineLengths } from 'common/buffer/BufferReflow'; |
| 12 |
import { CellData } from 'common/buffer/CellData'; |
| 13 |
import { NULL_CELL_CHAR, NULL_CELL_CODE, NULL_CELL_WIDTH, WHITESPACE_CELL_CHAR, WHITESPACE_CELL_CODE, WHITESPACE_CELL_WIDTH } from 'common/buffer/Constants'; |
| 14 |
import { Marker } from 'common/buffer/Marker'; |
| 15 |
import { IBuffer } from 'common/buffer/Types'; |
| 16 |
import { DEFAULT_CHARSET } from 'common/data/Charsets'; |
| 17 |
import { IBufferService, IOptionsService } from 'common/services/Services'; |
| 18 |
|
| 19 |
export const MAX_BUFFER_SIZE = 4294967295; // 2^32 - 1 |
| 20 |
|
| 21 |
/** |
| 22 |
* This class represents a terminal buffer (an internal state of the terminal), where the |
| 23 |
* following information is stored (in high-level): |
| 24 |
* - text content of this particular buffer |
| 25 |
* - cursor position |
| 26 |
* - scroll position |
| 27 |
*/ |
| 28 |
export class Buffer implements IBuffer { |
| 29 |
public lines: CircularList<IBufferLine>; |
| 30 |
public ydisp: number = 0; |
| 31 |
public ybase: number = 0; |
| 32 |
public y: number = 0; |
| 33 |
public x: number = 0; |
| 34 |
public scrollBottom: number; |
| 35 |
public scrollTop: number; |
| 36 |
public tabs: { [column: number]: boolean | undefined } = {}; |
| 37 |
public savedY: number = 0; |
| 38 |
public savedX: number = 0; |
| 39 |
public savedCurAttrData = DEFAULT_ATTR_DATA.clone(); |
| 40 |
public savedCharset: ICharset | undefined = DEFAULT_CHARSET; |
| 41 |
public markers: Marker[] = []; |
| 42 |
private _nullCell: ICellData = CellData.fromCharData([0, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]); |
| 43 |
private _whitespaceCell: ICellData = CellData.fromCharData([0, WHITESPACE_CELL_CHAR, WHITESPACE_CELL_WIDTH, WHITESPACE_CELL_CODE]); |
| 44 |
private _cols: number; |
| 45 |
private _rows: number; |
| 46 |
private _isClearing: boolean = false; |
| 47 |
|
| 48 |
constructor( |
| 49 |
private _hasScrollback: boolean, |
| 50 |
private _optionsService: IOptionsService, |
| 51 |
private _bufferService: IBufferService |
| 52 |
) { |
| 53 |
this._cols = this._bufferService.cols; |
| 54 |
this._rows = this._bufferService.rows; |
| 55 |
this.lines = new CircularList<IBufferLine>(this._getCorrectBufferLength(this._rows)); |
| 56 |
this.scrollTop = 0; |
| 57 |
this.scrollBottom = this._rows - 1; |
| 58 |
this.setupTabStops(); |
| 59 |
} |
| 60 |
|
| 61 |
public getNullCell(attr?: IAttributeData): ICellData { |
| 62 |
if (attr) { |
| 63 |
this._nullCell.fg = attr.fg; |
| 64 |
this._nullCell.bg = attr.bg; |
| 65 |
this._nullCell.extended = attr.extended; |
| 66 |
} else { |
| 67 |
this._nullCell.fg = 0; |
| 68 |
this._nullCell.bg = 0; |
| 69 |
this._nullCell.extended = new ExtendedAttrs(); |
| 70 |
} |
| 71 |
return this._nullCell; |
| 72 |
} |
| 73 |
|
| 74 |
public getWhitespaceCell(attr?: IAttributeData): ICellData { |
| 75 |
if (attr) { |
| 76 |
this._whitespaceCell.fg = attr.fg; |
| 77 |
this._whitespaceCell.bg = attr.bg; |
| 78 |
this._whitespaceCell.extended = attr.extended; |
| 79 |
} else { |
| 80 |
this._whitespaceCell.fg = 0; |
| 81 |
this._whitespaceCell.bg = 0; |
| 82 |
this._whitespaceCell.extended = new ExtendedAttrs(); |
| 83 |
} |
| 84 |
return this._whitespaceCell; |
| 85 |
} |
| 86 |
|
| 87 |
public getBlankLine(attr: IAttributeData, isWrapped?: boolean): IBufferLine { |
| 88 |
return new BufferLine(this._bufferService.cols, this.getNullCell(attr), isWrapped); |
| 89 |
} |
| 90 |
|
| 91 |
public get hasScrollback(): boolean { |
| 92 |
return this._hasScrollback && this.lines.maxLength > this._rows; |
| 93 |
} |
| 94 |
|
| 95 |
public get isCursorInViewport(): boolean { |
| 96 |
const absoluteY = this.ybase + this.y; |
| 97 |
const relativeY = absoluteY - this.ydisp; |
| 98 |
return (relativeY >= 0 && relativeY < this._rows); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Gets the correct buffer length based on the rows provided, the terminal's |
| 103 |
* scrollback and whether this buffer is flagged to have scrollback or not. |
| 104 |
* @param rows The terminal rows to use in the calculation. |
| 105 |
*/ |
| 106 |
private _getCorrectBufferLength(rows: number): number { |
| 107 |
if (!this._hasScrollback) { |
| 108 |
return rows; |
| 109 |
} |
| 110 |
|
| 111 |
const correctBufferLength = rows + this._optionsService.rawOptions.scrollback; |
| 112 |
|
| 113 |
return correctBufferLength > MAX_BUFFER_SIZE ? MAX_BUFFER_SIZE : correctBufferLength; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Fills the buffer's viewport with blank lines. |
| 118 |
*/ |
| 119 |
public fillViewportRows(fillAttr?: IAttributeData): void { |
| 120 |
if (this.lines.length === 0) { |
| 121 |
if (fillAttr === undefined) { |
| 122 |
fillAttr = DEFAULT_ATTR_DATA; |
| 123 |
} |
| 124 |
let i = this._rows; |
| 125 |
while (i--) { |
| 126 |
this.lines.push(this.getBlankLine(fillAttr)); |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Clears the buffer to it's initial state, discarding all previous data. |
| 133 |
*/ |
| 134 |
public clear(): void { |
| 135 |
this.ydisp = 0; |
| 136 |
this.ybase = 0; |
| 137 |
this.y = 0; |
| 138 |
this.x = 0; |
| 139 |
this.lines = new CircularList<IBufferLine>(this._getCorrectBufferLength(this._rows)); |
| 140 |
this.scrollTop = 0; |
| 141 |
this.scrollBottom = this._rows - 1; |
| 142 |
this.setupTabStops(); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Resizes the buffer, adjusting its data accordingly. |
| 147 |
* @param newCols The new number of columns. |
| 148 |
* @param newRows The new number of rows. |
| 149 |
*/ |
| 150 |
public resize(newCols: number, newRows: number): void { |
| 151 |
// store reference to null cell with default attrs |
| 152 |
const nullCell = this.getNullCell(DEFAULT_ATTR_DATA); |
| 153 |
|
| 154 |
// count bufferlines with overly big memory to be cleaned afterwards |
| 155 |
let dirtyMemoryLines = 0; |
| 156 |
|
| 157 |
// Increase max length if needed before adjustments to allow space to fill |
| 158 |
// as required. |
| 159 |
const newMaxLength = this._getCorrectBufferLength(newRows); |
| 160 |
if (newMaxLength > this.lines.maxLength) { |
| 161 |
this.lines.maxLength = newMaxLength; |
| 162 |
} |
| 163 |
|
| 164 |
// if (this._cols > newCols) { |
| 165 |
// console.log('increase!'); |
| 166 |
// } |
| 167 |
|
| 168 |
// The following adjustments should only happen if the buffer has been |
| 169 |
// initialized/filled. |
| 170 |
if (this.lines.length > 0) { |
| 171 |
// Deal with columns increasing (reducing needs to happen after reflow) |
| 172 |
if (this._cols < newCols) { |
| 173 |
for (let i = 0; i < this.lines.length; i++) { |
| 174 |
// +boolean for fast 0 or 1 conversion |
| 175 |
dirtyMemoryLines += +this.lines.get(i)!.resize(newCols, nullCell); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
// Resize rows in both directions as needed |
| 180 |
let addToY = 0; |
| 181 |
if (this._rows < newRows) { |
| 182 |
for (let y = this._rows; y < newRows; y++) { |
| 183 |
if (this.lines.length < newRows + this.ybase) { |
| 184 |
if (this._optionsService.rawOptions.windowsMode || this._optionsService.rawOptions.windowsPty.backend !== undefined || this._optionsService.rawOptions.windowsPty.buildNumber !== undefined) { |
| 185 |
// Just add the new missing rows on Windows as conpty reprints the screen with it's |
| 186 |
// view of the world. Once a line enters scrollback for conpty it remains there |
| 187 |
this.lines.push(new BufferLine(newCols, nullCell)); |
| 188 |
} else { |
| 189 |
if (this.ybase > 0 && this.lines.length <= this.ybase + this.y + addToY + 1) { |
| 190 |
// There is room above the buffer and there are no empty elements below the line, |
| 191 |
// scroll up |
| 192 |
this.ybase--; |
| 193 |
addToY++; |
| 194 |
if (this.ydisp > 0) { |
| 195 |
// Viewport is at the top of the buffer, must increase downwards |
| 196 |
this.ydisp--; |
| 197 |
} |
| 198 |
} else { |
| 199 |
// Add a blank line if there is no buffer left at the top to scroll to, or if there |
| 200 |
// are blank lines after the cursor |
| 201 |
this.lines.push(new BufferLine(newCols, nullCell)); |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
} else { // (this._rows >= newRows) |
| 207 |
for (let y = this._rows; y > newRows; y--) { |
| 208 |
if (this.lines.length > newRows + this.ybase) { |
| 209 |
if (this.lines.length > this.ybase + this.y + 1) { |
| 210 |
// The line is a blank line below the cursor, remove it |
| 211 |
this.lines.pop(); |
| 212 |
} else { |
| 213 |
// The line is the cursor, scroll down |
| 214 |
this.ybase++; |
| 215 |
this.ydisp++; |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
// Reduce max length if needed after adjustments, this is done after as it |
| 222 |
// would otherwise cut data from the bottom of the buffer. |
| 223 |
if (newMaxLength < this.lines.maxLength) { |
| 224 |
// Trim from the top of the buffer and adjust ybase and ydisp. |
| 225 |
const amountToTrim = this.lines.length - newMaxLength; |
| 226 |
if (amountToTrim > 0) { |
| 227 |
this.lines.trimStart(amountToTrim); |
| 228 |
this.ybase = Math.max(this.ybase - amountToTrim, 0); |
| 229 |
this.ydisp = Math.max(this.ydisp - amountToTrim, 0); |
| 230 |
this.savedY = Math.max(this.savedY - amountToTrim, 0); |
| 231 |
} |
| 232 |
this.lines.maxLength = newMaxLength; |
| 233 |
} |
| 234 |
|
| 235 |
// Make sure that the cursor stays on screen |
| 236 |
this.x = Math.min(this.x, newCols - 1); |
| 237 |
this.y = Math.min(this.y, newRows - 1); |
| 238 |
if (addToY) { |
| 239 |
this.y += addToY; |
| 240 |
} |
| 241 |
this.savedX = Math.min(this.savedX, newCols - 1); |
| 242 |
|
| 243 |
this.scrollTop = 0; |
| 244 |
} |
| 245 |
|
| 246 |
this.scrollBottom = newRows - 1; |
| 247 |
|
| 248 |
if (this._isReflowEnabled) { |
| 249 |
this._reflow(newCols, newRows); |
| 250 |
|
| 251 |
// Trim the end of the line off if cols shrunk |
| 252 |
if (this._cols > newCols) { |
| 253 |
for (let i = 0; i < this.lines.length; i++) { |
| 254 |
// +boolean for fast 0 or 1 conversion |
| 255 |
dirtyMemoryLines += +this.lines.get(i)!.resize(newCols, nullCell); |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
this._cols = newCols; |
| 261 |
this._rows = newRows; |
| 262 |
|
| 263 |
this._memoryCleanupQueue.clear(); |
| 264 |
// schedule memory cleanup only, if more than 10% of the lines are affected |
| 265 |
if (dirtyMemoryLines > 0.1 * this.lines.length) { |
| 266 |
this._memoryCleanupPosition = 0; |
| 267 |
this._memoryCleanupQueue.enqueue(() => this._batchedMemoryCleanup()); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
private _memoryCleanupQueue = new IdleTaskQueue(); |
| 272 |
private _memoryCleanupPosition = 0; |
| 273 |
|
| 274 |
private _batchedMemoryCleanup(): boolean { |
| 275 |
let normalRun = true; |
| 276 |
if (this._memoryCleanupPosition >= this.lines.length) { |
| 277 |
// cleanup made it once through all lines, thus rescan in loop below to also catch shifted |
| 278 |
// lines, which should finish rather quick if there are no more cleanups pending |
| 279 |
this._memoryCleanupPosition = 0; |
| 280 |
normalRun = false; |
| 281 |
} |
| 282 |
let counted = 0; |
| 283 |
while (this._memoryCleanupPosition < this.lines.length) { |
| 284 |
counted += this.lines.get(this._memoryCleanupPosition++)!.cleanupMemory(); |
| 285 |
// cleanup max 100 lines per batch |
| 286 |
if (counted > 100) { |
| 287 |
return true; |
| 288 |
} |
| 289 |
} |
| 290 |
// normal runs always need another rescan afterwards |
| 291 |
// if we made it here with normalRun=false, we are in a final run |
| 292 |
// and can end the cleanup task for sure |
| 293 |
return normalRun; |
| 294 |
} |
| 295 |
|
| 296 |
private get _isReflowEnabled(): boolean { |
| 297 |
const windowsPty = this._optionsService.rawOptions.windowsPty; |
| 298 |
if (windowsPty && windowsPty.buildNumber) { |
| 299 |
return this._hasScrollback && windowsPty.backend === 'conpty' && windowsPty.buildNumber >= 21376; |
| 300 |
} |
| 301 |
return this._hasScrollback && !this._optionsService.rawOptions.windowsMode; |
| 302 |
} |
| 303 |
|
| 304 |
private _reflow(newCols: number, newRows: number): void { |
| 305 |
if (this._cols === newCols) { |
| 306 |
return; |
| 307 |
} |
| 308 |
|
| 309 |
// Iterate through rows, ignore the last one as it cannot be wrapped |
| 310 |
if (newCols > this._cols) { |
| 311 |
this._reflowLarger(newCols, newRows); |
| 312 |
} else { |
| 313 |
this._reflowSmaller(newCols, newRows); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
private _reflowLarger(newCols: number, newRows: number): void { |
| 318 |
const reflowCursorLine = this._optionsService.rawOptions.reflowCursorLine; |
| 319 |
const toRemove: number[] = reflowLargerGetLinesToRemove(this.lines, this._cols, newCols, this.ybase + this.y, this.getNullCell(DEFAULT_ATTR_DATA), reflowCursorLine); |
| 320 |
if (toRemove.length > 0) { |
| 321 |
const newLayoutResult = reflowLargerCreateNewLayout(this.lines, toRemove); |
| 322 |
reflowLargerApplyNewLayout(this.lines, newLayoutResult.layout); |
| 323 |
this._reflowLargerAdjustViewport(newCols, newRows, newLayoutResult.countRemoved); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
private _reflowLargerAdjustViewport(newCols: number, newRows: number, countRemoved: number): void { |
| 328 |
const nullCell = this.getNullCell(DEFAULT_ATTR_DATA); |
| 329 |
// Adjust viewport based on number of items removed |
| 330 |
let viewportAdjustments = countRemoved; |
| 331 |
while (viewportAdjustments-- > 0) { |
| 332 |
if (this.ybase === 0) { |
| 333 |
if (this.y > 0) { |
| 334 |
this.y--; |
| 335 |
} |
| 336 |
if (this.lines.length < newRows) { |
| 337 |
// Add an extra row at the bottom of the viewport |
| 338 |
this.lines.push(new BufferLine(newCols, nullCell)); |
| 339 |
} |
| 340 |
} else { |
| 341 |
if (this.ydisp === this.ybase) { |
| 342 |
this.ydisp--; |
| 343 |
} |
| 344 |
this.ybase--; |
| 345 |
} |
| 346 |
} |
| 347 |
this.savedY = Math.max(this.savedY - countRemoved, 0); |
| 348 |
} |
| 349 |
|
| 350 |
private _reflowSmaller(newCols: number, newRows: number): void { |
| 351 |
const reflowCursorLine = this._optionsService.rawOptions.reflowCursorLine; |
| 352 |
const nullCell = this.getNullCell(DEFAULT_ATTR_DATA); |
| 353 |
// Gather all BufferLines that need to be inserted into the Buffer here so that they can be |
| 354 |
// batched up and only committed once |
| 355 |
const toInsert = []; |
| 356 |
let countToInsert = 0; |
| 357 |
// Go backwards as many lines may be trimmed and this will avoid considering them |
| 358 |
for (let y = this.lines.length - 1; y >= 0; y--) { |
| 359 |
// Check whether this line is a problem |
| 360 |
let nextLine = this.lines.get(y) as BufferLine; |
| 361 |
if (!nextLine || !nextLine.isWrapped && nextLine.getTrimmedLength() <= newCols) { |
| 362 |
continue; |
| 363 |
} |
| 364 |
|
| 365 |
// Gather wrapped lines and adjust y to be the starting line |
| 366 |
const wrappedLines: BufferLine[] = [nextLine]; |
| 367 |
while (nextLine.isWrapped && y > 0) { |
| 368 |
nextLine = this.lines.get(--y) as BufferLine; |
| 369 |
wrappedLines.unshift(nextLine); |
| 370 |
} |
| 371 |
|
| 372 |
if (!reflowCursorLine) { |
| 373 |
// If these lines contain the cursor don't touch them, the program will handle fixing up |
| 374 |
// wrapped lines with the cursor |
| 375 |
const absoluteY = this.ybase + this.y; |
| 376 |
if (absoluteY >= y && absoluteY < y + wrappedLines.length) { |
| 377 |
continue; |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
const lastLineLength = wrappedLines[wrappedLines.length - 1].getTrimmedLength(); |
| 382 |
const destLineLengths = reflowSmallerGetNewLineLengths(wrappedLines, this._cols, newCols); |
| 383 |
const linesToAdd = destLineLengths.length - wrappedLines.length; |
| 384 |
let trimmedLines: number; |
| 385 |
if (this.ybase === 0 && this.y !== this.lines.length - 1) { |
| 386 |
// If the top section of the buffer is not yet filled |
| 387 |
trimmedLines = Math.max(0, this.y - this.lines.maxLength + linesToAdd); |
| 388 |
} else { |
| 389 |
trimmedLines = Math.max(0, this.lines.length - this.lines.maxLength + linesToAdd); |
| 390 |
} |
| 391 |
|
| 392 |
// Add the new lines |
| 393 |
const newLines: BufferLine[] = []; |
| 394 |
for (let i = 0; i < linesToAdd; i++) { |
| 395 |
const newLine = this.getBlankLine(DEFAULT_ATTR_DATA, true) as BufferLine; |
| 396 |
newLines.push(newLine); |
| 397 |
} |
| 398 |
if (newLines.length > 0) { |
| 399 |
toInsert.push({ |
| 400 |
// countToInsert here gets the actual index, taking into account other inserted items. |
| 401 |
// using this we can iterate through the list forwards |
| 402 |
start: y + wrappedLines.length + countToInsert, |
| 403 |
newLines |
| 404 |
}); |
| 405 |
countToInsert += newLines.length; |
| 406 |
} |
| 407 |
wrappedLines.push(...newLines); |
| 408 |
|
| 409 |
// Copy buffer data to new locations, this needs to happen backwards to do in-place |
| 410 |
let destLineIndex = destLineLengths.length - 1; // Math.floor(cellsNeeded / newCols); |
| 411 |
let destCol = destLineLengths[destLineIndex]; // cellsNeeded % newCols; |
| 412 |
if (destCol === 0) { |
| 413 |
destLineIndex--; |
| 414 |
destCol = destLineLengths[destLineIndex]; |
| 415 |
} |
| 416 |
let srcLineIndex = wrappedLines.length - linesToAdd - 1; |
| 417 |
let srcCol = lastLineLength; |
| 418 |
while (srcLineIndex >= 0) { |
| 419 |
const cellsToCopy = Math.min(srcCol, destCol); |
| 420 |
if (wrappedLines[destLineIndex] === undefined) { |
| 421 |
// Sanity check that the line exists, this has been known to fail for an unknown reason |
| 422 |
// which would stop the reflow from happening if an exception would throw. |
| 423 |
break; |
| 424 |
} |
| 425 |
wrappedLines[destLineIndex].copyCellsFrom(wrappedLines[srcLineIndex], srcCol - cellsToCopy, destCol - cellsToCopy, cellsToCopy, true); |
| 426 |
destCol -= cellsToCopy; |
| 427 |
if (destCol === 0) { |
| 428 |
destLineIndex--; |
| 429 |
destCol = destLineLengths[destLineIndex]; |
| 430 |
} |
| 431 |
srcCol -= cellsToCopy; |
| 432 |
if (srcCol === 0) { |
| 433 |
srcLineIndex--; |
| 434 |
const wrappedLinesIndex = Math.max(srcLineIndex, 0); |
| 435 |
srcCol = getWrappedLineTrimmedLength(wrappedLines, wrappedLinesIndex, this._cols); |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
// Null out the end of the line ends if a wide character wrapped to the following line |
| 440 |
for (let i = 0; i < wrappedLines.length; i++) { |
| 441 |
if (destLineLengths[i] < newCols) { |
| 442 |
wrappedLines[i].setCell(destLineLengths[i], nullCell); |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
// Adjust viewport as needed |
| 447 |
let viewportAdjustments = linesToAdd - trimmedLines; |
| 448 |
while (viewportAdjustments-- > 0) { |
| 449 |
if (this.ybase === 0) { |
| 450 |
if (this.y < newRows - 1) { |
| 451 |
this.y++; |
| 452 |
this.lines.pop(); |
| 453 |
} else { |
| 454 |
this.ybase++; |
| 455 |
this.ydisp++; |
| 456 |
} |
| 457 |
} else { |
| 458 |
// Ensure ybase does not exceed its maximum value |
| 459 |
if (this.ybase < Math.min(this.lines.maxLength, this.lines.length + countToInsert) - newRows) { |
| 460 |
if (this.ybase === this.ydisp) { |
| 461 |
this.ydisp++; |
| 462 |
} |
| 463 |
this.ybase++; |
| 464 |
} |
| 465 |
} |
| 466 |
} |
| 467 |
this.savedY = Math.min(this.savedY + linesToAdd, this.ybase + newRows - 1); |
| 468 |
} |
| 469 |
|
| 470 |
// Rearrange lines in the buffer if there are any insertions, this is done at the end rather |
| 471 |
// than earlier so that it's a single O(n) pass through the buffer, instead of O(n^2) from many |
| 472 |
// costly calls to CircularList.splice. |
| 473 |
if (toInsert.length > 0) { |
| 474 |
// Record buffer insert events and then play them back backwards so that the indexes are |
| 475 |
// correct |
| 476 |
const insertEvents: IInsertEvent[] = []; |
| 477 |
|
| 478 |
// Record original lines so they don't get overridden when we rearrange the list |
| 479 |
const originalLines: BufferLine[] = []; |
| 480 |
for (let i = 0; i < this.lines.length; i++) { |
| 481 |
originalLines.push(this.lines.get(i) as BufferLine); |
| 482 |
} |
| 483 |
const originalLinesLength = this.lines.length; |
| 484 |
|
| 485 |
let originalLineIndex = originalLinesLength - 1; |
| 486 |
let nextToInsertIndex = 0; |
| 487 |
let nextToInsert = toInsert[nextToInsertIndex]; |
| 488 |
this.lines.length = Math.min(this.lines.maxLength, this.lines.length + countToInsert); |
| 489 |
let countInsertedSoFar = 0; |
| 490 |
for (let i = Math.min(this.lines.maxLength - 1, originalLinesLength + countToInsert - 1); i >= 0; i--) { |
| 491 |
if (nextToInsert && nextToInsert.start > originalLineIndex + countInsertedSoFar) { |
| 492 |
// Insert extra lines here, adjusting i as needed |
| 493 |
for (let nextI = nextToInsert.newLines.length - 1; nextI >= 0; nextI--) { |
| 494 |
this.lines.set(i--, nextToInsert.newLines[nextI]); |
| 495 |
} |
| 496 |
i++; |
| 497 |
|
| 498 |
// Create insert events for later |
| 499 |
insertEvents.push({ |
| 500 |
index: originalLineIndex + 1, |
| 501 |
amount: nextToInsert.newLines.length |
| 502 |
}); |
| 503 |
|
| 504 |
countInsertedSoFar += nextToInsert.newLines.length; |
| 505 |
nextToInsert = toInsert[++nextToInsertIndex]; |
| 506 |
} else { |
| 507 |
this.lines.set(i, originalLines[originalLineIndex--]); |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
// Update markers |
| 512 |
let insertCountEmitted = 0; |
| 513 |
for (let i = insertEvents.length - 1; i >= 0; i--) { |
| 514 |
insertEvents[i].index += insertCountEmitted; |
| 515 |
this.lines.onInsertEmitter.fire(insertEvents[i]); |
| 516 |
insertCountEmitted += insertEvents[i].amount; |
| 517 |
} |
| 518 |
const amountToTrim = Math.max(0, originalLinesLength + countToInsert - this.lines.maxLength); |
| 519 |
if (amountToTrim > 0) { |
| 520 |
this.lines.onTrimEmitter.fire(amountToTrim); |
| 521 |
} |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
/** |
| 526 |
* Translates a buffer line to a string, with optional start and end columns. |
| 527 |
* Wide characters will count as two columns in the resulting string. This |
| 528 |
* function is useful for getting the actual text underneath the raw selection |
| 529 |
* position. |
| 530 |
* @param lineIndex The absolute index of the line being translated. |
| 531 |
* @param trimRight Whether to trim whitespace to the right. |
| 532 |
* @param startCol The column to start at. |
| 533 |
* @param endCol The column to end at. |
| 534 |
*/ |
| 535 |
public translateBufferLineToString(lineIndex: number, trimRight: boolean, startCol: number = 0, endCol?: number): string { |
| 536 |
const line = this.lines.get(lineIndex); |
| 537 |
if (!line) { |
| 538 |
return ''; |
| 539 |
} |
| 540 |
return line.translateToString(trimRight, startCol, endCol); |
| 541 |
} |
| 542 |
|
| 543 |
public getWrappedRangeForLine(y: number): { first: number, last: number } { |
| 544 |
let first = y; |
| 545 |
let last = y; |
| 546 |
// Scan upwards for wrapped lines |
| 547 |
while (first > 0 && this.lines.get(first)!.isWrapped) { |
| 548 |
first--; |
| 549 |
} |
| 550 |
// Scan downwards for wrapped lines |
| 551 |
while (last + 1 < this.lines.length && this.lines.get(last + 1)!.isWrapped) { |
| 552 |
last++; |
| 553 |
} |
| 554 |
return { first, last }; |
| 555 |
} |
| 556 |
|
| 557 |
/** |
| 558 |
* Setup the tab stops. |
| 559 |
* @param i The index to start setting up tab stops from. |
| 560 |
*/ |
| 561 |
public setupTabStops(i?: number): void { |
| 562 |
if (i !== null && i !== undefined) { |
| 563 |
if (!this.tabs[i]) { |
| 564 |
i = this.prevStop(i); |
| 565 |
} |
| 566 |
} else { |
| 567 |
this.tabs = {}; |
| 568 |
i = 0; |
| 569 |
} |
| 570 |
|
| 571 |
for (; i < this._cols; i += this._optionsService.rawOptions.tabStopWidth) { |
| 572 |
this.tabs[i] = true; |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
/** |
| 577 |
* Move the cursor to the previous tab stop from the given position (default is current). |
| 578 |
* @param x The position to move the cursor to the previous tab stop. |
| 579 |
*/ |
| 580 |
public prevStop(x?: number): number { |
| 581 |
if (x === null || x === undefined) { |
| 582 |
x = this.x; |
| 583 |
} |
| 584 |
while (!this.tabs[--x] && x > 0); |
| 585 |
return x >= this._cols ? this._cols - 1 : x < 0 ? 0 : x; |
| 586 |
} |
| 587 |
|
| 588 |
/** |
| 589 |
* Move the cursor one tab stop forward from the given position (default is current). |
| 590 |
* @param x The position to move the cursor one tab stop forward. |
| 591 |
*/ |
| 592 |
public nextStop(x?: number): number { |
| 593 |
if (x === null || x === undefined) { |
| 594 |
x = this.x; |
| 595 |
} |
| 596 |
while (!this.tabs[++x] && x < this._cols); |
| 597 |
return x >= this._cols ? this._cols - 1 : x < 0 ? 0 : x; |
| 598 |
} |
| 599 |
|
| 600 |
/** |
| 601 |
* Clears markers on single line. |
| 602 |
* @param y The line to clear. |
| 603 |
*/ |
| 604 |
public clearMarkers(y: number): void { |
| 605 |
this._isClearing = true; |
| 606 |
for (let i = 0; i < this.markers.length; i++) { |
| 607 |
if (this.markers[i].line === y) { |
| 608 |
this.markers[i].dispose(); |
| 609 |
this.markers.splice(i--, 1); |
| 610 |
} |
| 611 |
} |
| 612 |
this._isClearing = false; |
| 613 |
} |
| 614 |
|
| 615 |
/** |
| 616 |
* Clears markers on all lines |
| 617 |
*/ |
| 618 |
public clearAllMarkers(): void { |
| 619 |
this._isClearing = true; |
| 620 |
for (let i = 0; i < this.markers.length; i++) { |
| 621 |
this.markers[i].dispose(); |
| 622 |
} |
| 623 |
this.markers.length = 0; |
| 624 |
this._isClearing = false; |
| 625 |
} |
| 626 |
|
| 627 |
public addMarker(y: number): Marker { |
| 628 |
const marker = new Marker(y); |
| 629 |
this.markers.push(marker); |
| 630 |
marker.register(this.lines.onTrim(amount => { |
| 631 |
marker.line -= amount; |
| 632 |
// The marker should be disposed when the line is trimmed from the buffer |
| 633 |
if (marker.line < 0) { |
| 634 |
marker.dispose(); |
| 635 |
} |
| 636 |
})); |
| 637 |
marker.register(this.lines.onInsert(event => { |
| 638 |
if (marker.line >= event.index) { |
| 639 |
marker.line += event.amount; |
| 640 |
} |
| 641 |
})); |
| 642 |
marker.register(this.lines.onDelete(event => { |
| 643 |
// Delete the marker if it's within the range |
| 644 |
if (marker.line >= event.index && marker.line < event.index + event.amount) { |
| 645 |
marker.dispose(); |
| 646 |
} |
| 647 |
|
| 648 |
// Shift the marker if it's after the deleted range |
| 649 |
if (marker.line > event.index) { |
| 650 |
marker.line -= event.amount; |
| 651 |
} |
| 652 |
})); |
| 653 |
marker.register(marker.onDispose(() => this._removeMarker(marker))); |
| 654 |
return marker; |
| 655 |
} |
| 656 |
|
| 657 |
private _removeMarker(marker: Marker): void { |
| 658 |
if (!this._isClearing) { |
| 659 |
this.markers.splice(this.markers.indexOf(marker), 1); |
| 660 |
} |
| 661 |
} |
| 662 |
} |