-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbufferreflow.go
More file actions
184 lines (166 loc) · 5.38 KB
/
bufferreflow.go
File metadata and controls
184 lines (166 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package xterm
// Ported from xterm.js src/common/buffer/BufferReflow.ts.
// NewLayoutResult holds the result of reflowLargerCreateNewLayout.
type NewLayoutResult struct {
layout []int
countRemoved int
}
// reflowLargerGetLinesToRemove evaluates wrapped lines and returns pairs of
// [startIndex, count] for lines that can be removed after a reflow to larger width.
func reflowLargerGetLinesToRemove(lines *CircularList[*BufferLine], oldCols, newCols int, bufferAbsoluteY int, nullCell *CellData, reflowCursorLine bool) []int {
toRemove := []int{}
for y := 0; y < lines.Length()-1; y++ {
i := y
nextLine := lines.Get(i + 1)
if !nextLine.IsWrapped {
continue
}
i++
wrappedLines := []*BufferLine{lines.Get(y)}
for i < lines.Length() && nextLine.IsWrapped {
wrappedLines = append(wrappedLines, nextLine)
i++
if i < lines.Length() {
nextLine = lines.Get(i)
}
}
if !reflowCursorLine {
if bufferAbsoluteY >= y && bufferAbsoluteY < i {
y += len(wrappedLines) - 1
continue
}
}
// Copy buffer data to new locations
destLineIndex := 0
destCol := getWrappedLineTrimmedLength(wrappedLines, destLineIndex, oldCols)
srcLineIndex := 1
srcCol := 0
for srcLineIndex < len(wrappedLines) {
srcTrimmedLength := getWrappedLineTrimmedLength(wrappedLines, srcLineIndex, oldCols)
srcRemainingCells := srcTrimmedLength - srcCol
destRemainingCells := newCols - destCol
cellsToCopy := min(srcRemainingCells, destRemainingCells)
wrappedLines[destLineIndex].CopyCellsFrom(wrappedLines[srcLineIndex], srcCol, destCol, cellsToCopy, false)
destCol += cellsToCopy
if destCol == newCols {
destLineIndex++
destCol = 0
}
srcCol += cellsToCopy
if srcCol == srcTrimmedLength {
srcLineIndex++
srcCol = 0
}
// Handle wide char at line boundary
if destCol == 0 && destLineIndex != 0 {
if wrappedLines[destLineIndex-1].GetWidth(newCols-1) == 2 {
wrappedLines[destLineIndex].CopyCellsFrom(wrappedLines[destLineIndex-1], newCols-1, 0, 1, false)
destCol++
wrappedLines[destLineIndex-1].SetCell(newCols-1, nullCell)
}
}
}
// Clear remaining cells
wrappedLines[destLineIndex].ReplaceCells(destCol, newCols, nullCell, false)
// Count removable trailing empty lines
countToRemove := 0
for i := len(wrappedLines) - 1; i > 0; i-- {
if i > destLineIndex || wrappedLines[i].GetTrimmedLength() == 0 {
countToRemove++
} else {
break
}
}
if countToRemove > 0 {
toRemove = append(toRemove, y+len(wrappedLines)-countToRemove)
toRemove = append(toRemove, countToRemove)
}
y += len(wrappedLines) - 1
}
return toRemove
}
// reflowLargerCreateNewLayout creates the new line layout after removing lines.
func reflowLargerCreateNewLayout(lines *CircularList[*BufferLine], toRemove []int) NewLayoutResult {
layout := []int{}
nextToRemoveIndex := 0
nextToRemoveStart := -1
if len(toRemove) > 0 {
nextToRemoveStart = toRemove[0]
}
countRemovedSoFar := 0
for i := 0; i < lines.Length(); i++ {
if nextToRemoveStart == i {
nextToRemoveIndex++
countToRemove := toRemove[nextToRemoveIndex]
lines.OnDeleteEmitter.Fire(DeleteEvent{Index: i - countRemovedSoFar, Amount: countToRemove})
i += countToRemove - 1
countRemovedSoFar += countToRemove
nextToRemoveIndex++
if nextToRemoveIndex < len(toRemove) {
nextToRemoveStart = toRemove[nextToRemoveIndex]
} else {
nextToRemoveStart = -1
}
} else {
layout = append(layout, i)
}
}
return NewLayoutResult{layout: layout, countRemoved: countRemovedSoFar}
}
// reflowLargerApplyNewLayout rearranges lines according to the new layout.
func reflowLargerApplyNewLayout(lines *CircularList[*BufferLine], newLayout []int) {
newLayoutLines := make([]*BufferLine, len(newLayout))
for i, idx := range newLayout {
newLayoutLines[i] = lines.Get(idx)
}
for i, line := range newLayoutLines {
lines.Set(i, line)
}
lines.SetLength(len(newLayout))
}
// reflowSmallerGetNewLineLengths computes the new line lengths when reflowing to smaller width.
func reflowSmallerGetNewLineLengths(wrappedLines []*BufferLine, oldCols, newCols int) []int {
newLineLengths := []int{}
cellsNeeded := 0
for i := range wrappedLines {
cellsNeeded += getWrappedLineTrimmedLength(wrappedLines, i, oldCols)
}
srcCol := 0
srcLine := 0
cellsAvailable := 0
for cellsAvailable < cellsNeeded {
if cellsNeeded-cellsAvailable < newCols {
newLineLengths = append(newLineLengths, cellsNeeded-cellsAvailable)
break
}
srcCol += newCols
oldTrimmedLength := getWrappedLineTrimmedLength(wrappedLines, srcLine, oldCols)
if srcCol > oldTrimmedLength {
srcCol -= oldTrimmedLength
srcLine++
}
endsWithWide := srcLine < len(wrappedLines) && wrappedLines[srcLine].GetWidth(srcCol-1) == 2
if endsWithWide {
srcCol--
}
lineLength := newCols
if endsWithWide {
lineLength = newCols - 1
}
newLineLengths = append(newLineLengths, lineLength)
cellsAvailable += lineLength
}
return newLineLengths
}
// getWrappedLineTrimmedLength returns the trimmed length of a line within a wrapped group.
func getWrappedLineTrimmedLength(lines []*BufferLine, i, cols int) int {
if i == len(lines)-1 {
return lines[i].GetTrimmedLength()
}
endsInNull := lines[i].HasContent(cols-1) == 0 && lines[i].GetWidth(cols-1) == 1
followingLineStartsWithWide := i+1 < len(lines) && lines[i+1].GetWidth(0) == 2
if endsInNull && followingLineStartsWithWide {
return cols - 1
}
return cols
}