Debian Codegraph @

node/@xterm/xterm/src/common/buffer/BufferReflow.ts

raw
1 /**
2 * Copyright (c) 2019 The xterm.js authors. All rights reserved.
3 * @license MIT
4 */
5
6 import { BufferLine } from 'common/buffer/BufferLine';
7 import { CircularList } from 'common/CircularList';
8 import { IBufferLine, ICellData } from 'common/Types';
9
10 export interface INewLayoutResult {
11 layout: number[];
12 countRemoved: number;
13 }
14
15 /**
16 * Evaluates and returns indexes to be removed after a reflow larger occurs. Lines will be removed
17 * when a wrapped line unwraps.
18 * @param lines The buffer lines.
19 * @param oldCols The columns before resize
20 * @param newCols The columns after resize.
21 * @param bufferAbsoluteY The absolute y position of the cursor (baseY + cursorY).
22 * @param nullCell The cell data to use when filling in empty cells.
23 * @param reflowCursorLine Whether to reflow the line containing the cursor.
24 */
25 export function reflowLargerGetLinesToRemove(lines: CircularList<IBufferLine>, oldCols: number, newCols: number, bufferAbsoluteY: number, nullCell: ICellData, reflowCursorLine: boolean): number[] {
26 // Gather all BufferLines that need to be removed from the Buffer here so that they can be
27 // batched up and only committed once
28 const toRemove: number[] = [];
29
30 for (let y = 0; y < lines.length - 1; y++) {
31 // Check if this row is wrapped
32 let i = y;
33 let nextLine = lines.get(++i) as BufferLine;
34 if (!nextLine.isWrapped) {
35 continue;
36 }
37
38 // Check how many lines it's wrapped for
39 const wrappedLines: BufferLine[] = [lines.get(y) as BufferLine];
40 while (i < lines.length && nextLine.isWrapped) {
41 wrappedLines.push(nextLine);
42 nextLine = lines.get(++i) as BufferLine;
43 }
44
45 if (!reflowCursorLine) {
46 // If these lines contain the cursor don't touch them, the program will handle fixing up
47 // wrapped lines with the cursor
48 if (bufferAbsoluteY >= y && bufferAbsoluteY < i) {
49 y += wrappedLines.length - 1;
50 continue;
51 }
52 }
53
54 // Copy buffer data to new locations
55 let destLineIndex = 0;
56 let destCol = getWrappedLineTrimmedLength(wrappedLines, destLineIndex, oldCols);
57 let srcLineIndex = 1;
58 let srcCol = 0;
59 while (srcLineIndex < wrappedLines.length) {
60 const srcTrimmedTineLength = getWrappedLineTrimmedLength(wrappedLines, srcLineIndex, oldCols);
61 const srcRemainingCells = srcTrimmedTineLength - srcCol;
62 const destRemainingCells = newCols - destCol;
63 const cellsToCopy = Math.min(srcRemainingCells, destRemainingCells);
64
65 wrappedLines[destLineIndex].copyCellsFrom(wrappedLines[srcLineIndex], srcCol, destCol, cellsToCopy, false);
66
67 destCol += cellsToCopy;
68 if (destCol === newCols) {
69 destLineIndex++;
70 destCol = 0;
71 }
72 srcCol += cellsToCopy;
73 if (srcCol === srcTrimmedTineLength) {
74 srcLineIndex++;
75 srcCol = 0;
76 }
77
78 // Make sure the last cell isn't wide, if it is copy it to the current dest
79 if (destCol === 0 && destLineIndex !== 0) {
80 if (wrappedLines[destLineIndex - 1].getWidth(newCols - 1) === 2) {
81 wrappedLines[destLineIndex].copyCellsFrom(wrappedLines[destLineIndex - 1], newCols - 1, destCol++, 1, false);
82 // Null out the end of the last row
83 wrappedLines[destLineIndex - 1].setCell(newCols - 1, nullCell);
84 }
85 }
86 }
87
88 // Clear out remaining cells or fragments could remain;
89 wrappedLines[destLineIndex].replaceCells(destCol, newCols, nullCell);
90
91 // Work backwards and remove any rows at the end that only contain null cells
92 let countToRemove = 0;
93 for (let i = wrappedLines.length - 1; i > 0; i--) {
94 if (i > destLineIndex || wrappedLines[i].getTrimmedLength() === 0) {
95 countToRemove++;
96 } else {
97 break;
98 }
99 }
100
101 if (countToRemove > 0) {
102 toRemove.push(y + wrappedLines.length - countToRemove); // index
103 toRemove.push(countToRemove);
104 }
105
106 y += wrappedLines.length - 1;
107 }
108 return toRemove;
109 }
110
111 /**
112 * Creates and return the new layout for lines given an array of indexes to be removed.
113 * @param lines The buffer lines.
114 * @param toRemove The indexes to remove.
115 */
116 export function reflowLargerCreateNewLayout(lines: CircularList<IBufferLine>, toRemove: number[]): INewLayoutResult {
117 const layout: number[] = [];
118 // First iterate through the list and get the actual indexes to use for rows
119 let nextToRemoveIndex = 0;
120 let nextToRemoveStart = toRemove[nextToRemoveIndex];
121 let countRemovedSoFar = 0;
122 for (let i = 0; i < lines.length; i++) {
123 if (nextToRemoveStart === i) {
124 const countToRemove = toRemove[++nextToRemoveIndex];
125
126 // Tell markers that there was a deletion
127 lines.onDeleteEmitter.fire({
128 index: i - countRemovedSoFar,
129 amount: countToRemove
130 });
131
132 i += countToRemove - 1;
133 countRemovedSoFar += countToRemove;
134 nextToRemoveStart = toRemove[++nextToRemoveIndex];
135 } else {
136 layout.push(i);
137 }
138 }
139 return {
140 layout,
141 countRemoved: countRemovedSoFar
142 };
143 }
144
145 /**
146 * Applies a new layout to the buffer. This essentially does the same as many splice calls but it's
147 * done all at once in a single iteration through the list since splice is very expensive.
148 * @param lines The buffer lines.
149 * @param newLayout The new layout to apply.
150 */
151 export function reflowLargerApplyNewLayout(lines: CircularList<IBufferLine>, newLayout: number[]): void {
152 // Record original lines so they don't get overridden when we rearrange the list
153 const newLayoutLines: BufferLine[] = [];
154 for (let i = 0; i < newLayout.length; i++) {
155 newLayoutLines.push(lines.get(newLayout[i]) as BufferLine);
156 }
157
158 // Rearrange the list
159 for (let i = 0; i < newLayoutLines.length; i++) {
160 lines.set(i, newLayoutLines[i]);
161 }
162 lines.length = newLayout.length;
163 }
164
165 /**
166 * Gets the new line lengths for a given wrapped line. The purpose of this function it to pre-
167 * compute the wrapping points since wide characters may need to be wrapped onto the following line.
168 * This function will return an array of numbers of where each line wraps to, the resulting array
169 * will only contain the values `newCols` (when the line does not end with a wide character) and
170 * `newCols - 1` (when the line does end with a wide character), except for the last value which
171 * will contain the remaining items to fill the line.
172 *
173 * Calling this with a `newCols` value of `1` will lock up.
174 *
175 * @param wrappedLines The wrapped lines to evaluate.
176 * @param oldCols The columns before resize.
177 * @param newCols The columns after resize.
178 */
179 export function reflowSmallerGetNewLineLengths(wrappedLines: BufferLine[], oldCols: number, newCols: number): number[] {
180 const newLineLengths: number[] = [];
181 const cellsNeeded = wrappedLines.map((l, i) => getWrappedLineTrimmedLength(wrappedLines, i, oldCols)).reduce((p, c) => p + c);
182
183 // Use srcCol and srcLine to find the new wrapping point, use that to get the cellsAvailable and
184 // linesNeeded
185 let srcCol = 0;
186 let srcLine = 0;
187 let cellsAvailable = 0;
188 while (cellsAvailable < cellsNeeded) {
189 if (cellsNeeded - cellsAvailable < newCols) {
190 // Add the final line and exit the loop
191 newLineLengths.push(cellsNeeded - cellsAvailable);
192 break;
193 }
194 srcCol += newCols;
195 const oldTrimmedLength = getWrappedLineTrimmedLength(wrappedLines, srcLine, oldCols);
196 if (srcCol > oldTrimmedLength) {
197 srcCol -= oldTrimmedLength;
198 srcLine++;
199 }
200 const endsWithWide = wrappedLines[srcLine].getWidth(srcCol - 1) === 2;
201 if (endsWithWide) {
202 srcCol--;
203 }
204 const lineLength = endsWithWide ? newCols - 1 : newCols;
205 newLineLengths.push(lineLength);
206 cellsAvailable += lineLength;
207 }
208
209 return newLineLengths;
210 }
211
212 export function getWrappedLineTrimmedLength(lines: BufferLine[], i: number, cols: number): number {
213 // If this is the last row in the wrapped line, get the actual trimmed length
214 if (i === lines.length - 1) {
215 return lines[i].getTrimmedLength();
216 }
217 // Detect whether the following line starts with a wide character and the end of the current line
218 // is null, if so then we can be pretty sure the null character should be excluded from the line
219 // length]
220 const endsInNull = !(lines[i].hasContent(cols - 1)) && lines[i].getWidth(cols - 1) === 1;
221 const followingLineStartsWithWide = lines[i + 1].getWidth(0) === 2;
222 if (endsInNull && followingLineStartsWithWide) {
223 return cols - 1;
224 }
225 return cols;
226 }