This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
/
screen-line-builder.js
423 lines (354 loc) · 14.9 KB
/
screen-line-builder.js
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* eslint-disable no-labels */
const Point = require('./point')
const HARD_TAB = 1 << 0
const LEADING_WHITESPACE = 1 << 2
const TRAILING_WHITESPACE = 1 << 3
const INVISIBLE_CHARACTER = 1 << 4
const INDENT_GUIDE = 1 << 5
const LINE_ENDING = 1 << 6
const FOLD = 1 << 7
let nextScreenLineId = 1
module.exports =
class ScreenLineBuilder {
constructor (displayLayer) {
this.displayLayer = displayLayer
}
buildScreenLines (startScreenRow, endScreenRow) {
this.requestedStartScreenRow = startScreenRow
this.requestedEndScreenRow = endScreenRow
this.displayLayer.populateSpatialIndexIfNeeded(this.displayLayer.buffer.getLineCount(), endScreenRow)
this.bufferRow = this.displayLayer.translateScreenPositionWithSpatialIndex(Point(startScreenRow, 0)).row
this.bufferRow = this.displayLayer.findBoundaryPrecedingBufferRow(this.bufferRow)
this.screenRow = this.displayLayer.translateBufferPositionWithSpatialIndex(Point(this.bufferRow, 0)).row
const endBufferRow = this.displayLayer.translateScreenPositionWithSpatialIndex(Point(endScreenRow, Infinity)).row
let didSeekDecorationIterator = false
const decorationIterator = this.displayLayer.buffer.languageMode.buildHighlightIterator()
const hunks = this.displayLayer.spatialIndex.getChangesInNewRange(Point(this.screenRow, 0), Point(endScreenRow, 0))
let hunkIndex = 0
this.containingScopeIds = []
this.scopeIdsToReopen = []
this.screenLines = []
this.bufferColumn = 0
this.beginLine()
// Loop through all characters spanning the given screen row range, building
// up screen lines based on the contents of the spatial index and the
// buffer.
screenRowLoop:
while (this.screenRow < endScreenRow) {
var cachedScreenLine = this.displayLayer.cachedScreenLines[this.screenRow]
if (cachedScreenLine) {
this.pushScreenLine(cachedScreenLine)
let nextHunk = hunks[hunkIndex]
while (nextHunk && nextHunk.newStart.row <= this.screenRow) {
if (nextHunk.newStart.row === this.screenRow) {
if (nextHunk.newEnd.row > nextHunk.newStart.row) {
this.screenRow++
this.bufferColumn = nextHunk.oldEnd.column
hunkIndex++
continue screenRowLoop
} else {
this.bufferRow = nextHunk.oldEnd.row
this.bufferColumn = nextHunk.oldEnd.column
}
}
hunkIndex++
nextHunk = hunks[hunkIndex]
}
this.screenRow++
this.bufferRow++
this.screenColumn = 0
this.bufferColumn = 0
continue
}
this.currentBuiltInClassNameFlags = 0
this.bufferLine = this.displayLayer.buffer.lineForRow(this.bufferRow)
if (this.bufferLine == null) break
this.trailingWhitespaceStartColumn = this.displayLayer.findTrailingWhitespaceStartColumn(this.bufferLine)
this.inLeadingWhitespace = true
this.inTrailingWhitespace = false
if (!didSeekDecorationIterator || this.compareBufferPosition(decorationIterator.getPosition()) > 0) {
didSeekDecorationIterator = true
this.scopeIdsToReopen = decorationIterator.seek(Point(this.bufferRow, this.bufferColumn), endBufferRow)
}
var prevCachedScreenLine = this.displayLayer.cachedScreenLines[this.screenRow - 1]
if (prevCachedScreenLine && prevCachedScreenLine.softWrapIndent >= 0) {
this.inLeadingWhitespace = false
if (prevCachedScreenLine.softWrapIndent > 0) this.emitIndentWhitespace(prevCachedScreenLine.softWrapIndent)
}
// This loop may visit multiple buffer rows if there are folds and
// multiple screen rows if there are soft wraps.
while (this.bufferColumn <= this.bufferLine.length) {
// Handle folds or soft wraps at the current position.
var nextHunk = hunks[hunkIndex]
while (nextHunk && nextHunk.oldStart.row === this.bufferRow && nextHunk.oldStart.column === this.bufferColumn) {
if (this.displayLayer.isSoftWrapHunk(nextHunk)) {
this.emitSoftWrap(nextHunk)
if (this.screenRow === endScreenRow) {
break screenRowLoop
}
} else {
this.emitFold(nextHunk, decorationIterator, endBufferRow)
}
hunkIndex++
nextHunk = hunks[hunkIndex]
}
var nextCharacter = this.bufferLine[this.bufferColumn]
if (this.bufferColumn >= this.trailingWhitespaceStartColumn) {
this.inTrailingWhitespace = true
this.inLeadingWhitespace = false
} else if (nextCharacter !== ' ' && nextCharacter !== '\t') {
this.inLeadingWhitespace = false
}
// Compute a token flags describing built-in decorations for the token
// containing the next character
var previousBuiltInTagFlags = this.currentBuiltInClassNameFlags
this.updateCurrentTokenFlags(nextCharacter)
if (this.emitBuiltInTagBoundary) {
this.emitCloseTag(this.getBuiltInScopeId(previousBuiltInTagFlags))
}
this.emitDecorationBoundaries(decorationIterator)
// Are we at the end of the line?
if (this.bufferColumn === this.bufferLine.length) {
this.emitLineEnding()
break
}
if (this.emitBuiltInTagBoundary) {
this.emitOpenTag(this.getBuiltInScopeId(this.currentBuiltInClassNameFlags))
}
// Emit the next character, handling hard tabs whitespace invisibles
// specially.
if (nextCharacter === '\t') {
this.emitHardTab()
} else if ((this.inLeadingWhitespace || this.inTrailingWhitespace) &&
nextCharacter === ' ' && this.displayLayer.invisibles.space) {
this.emitText(this.displayLayer.invisibles.space)
} else {
this.emitText(nextCharacter)
}
this.bufferColumn++
}
}
return this.screenLines
}
getBuiltInScopeId (flags) {
if (flags === 0) return 0
let scopeId = this.displayLayer.getBuiltInScopeId(flags)
if (scopeId === -1) {
let className = ''
if (flags & INVISIBLE_CHARACTER) className += 'invisible-character '
if (flags & HARD_TAB) className += 'hard-tab '
if (flags & LEADING_WHITESPACE) className += 'leading-whitespace '
if (flags & TRAILING_WHITESPACE) className += 'trailing-whitespace '
if (flags & LINE_ENDING) className += 'eol '
if (flags & INDENT_GUIDE) className += 'indent-guide '
if (flags & FOLD) className += 'fold-marker '
className = className.trim()
scopeId = this.displayLayer.registerBuiltInScope(flags, className)
}
return scopeId
}
beginLine () {
this.currentScreenLineText = ''
this.currentScreenLineTags = []
this.screenColumn = 0
this.currentTokenLength = 0
}
updateCurrentTokenFlags (nextCharacter) {
const previousBuiltInTagFlags = this.currentBuiltInClassNameFlags
this.currentBuiltInClassNameFlags = 0
this.emitBuiltInTagBoundary = false
if (nextCharacter === ' ' || nextCharacter === '\t') {
const showIndentGuides = this.displayLayer.showIndentGuides && (this.inLeadingWhitespace || this.trailingWhitespaceStartColumn === 0)
if (this.inLeadingWhitespace) this.currentBuiltInClassNameFlags |= LEADING_WHITESPACE
if (this.inTrailingWhitespace) this.currentBuiltInClassNameFlags |= TRAILING_WHITESPACE
if (nextCharacter === ' ') {
if ((this.inLeadingWhitespace || this.inTrailingWhitespace) && this.displayLayer.invisibles.space) {
this.currentBuiltInClassNameFlags |= INVISIBLE_CHARACTER
}
if (showIndentGuides) {
this.currentBuiltInClassNameFlags |= INDENT_GUIDE
if (this.screenColumn % this.displayLayer.tabLength === 0) this.emitBuiltInTagBoundary = true
}
} else { // nextCharacter === \t
this.currentBuiltInClassNameFlags |= HARD_TAB
if (this.displayLayer.invisibles.tab) this.currentBuiltInClassNameFlags |= INVISIBLE_CHARACTER
if (showIndentGuides && this.screenColumn % this.displayLayer.tabLength === 0) {
this.currentBuiltInClassNameFlags |= INDENT_GUIDE
}
this.emitBuiltInTagBoundary = true
}
}
if (!this.emitBuiltInTagBoundary) {
this.emitBuiltInTagBoundary = this.currentBuiltInClassNameFlags !== previousBuiltInTagFlags
}
}
emitDecorationBoundaries (decorationIterator) {
while (this.compareBufferPosition(decorationIterator.getPosition()) === 0) {
var closeScopeIds = decorationIterator.getCloseScopeIds()
for (let i = 0, n = closeScopeIds.length; i < n; i++) {
this.emitCloseTag(closeScopeIds[i])
}
var openScopeIds = decorationIterator.getOpenScopeIds()
for (let i = 0, n = openScopeIds.length; i < n; i++) {
this.emitOpenTag(openScopeIds[i])
}
decorationIterator.moveToSuccessor()
}
}
emitFold (nextHunk, decorationIterator, endBufferRow) {
this.emitCloseTag(this.getBuiltInScopeId(this.currentBuiltInClassNameFlags))
this.currentBuiltInClassNameFlags = 0
this.closeContainingScopes()
this.scopeIdsToReopen.length = 0
this.emitOpenTag(this.getBuiltInScopeId(FOLD))
this.emitText(this.displayLayer.foldCharacter)
this.emitCloseTag(this.getBuiltInScopeId(FOLD))
this.bufferRow = nextHunk.oldEnd.row
this.bufferColumn = nextHunk.oldEnd.column
this.scopeIdsToReopen = decorationIterator.seek(Point(this.bufferRow, this.bufferColumn), endBufferRow)
this.bufferLine = this.displayLayer.buffer.lineForRow(this.bufferRow)
this.trailingWhitespaceStartColumn = this.displayLayer.findTrailingWhitespaceStartColumn(this.bufferLine)
}
emitSoftWrap (nextHunk) {
this.emitCloseTag(this.getBuiltInScopeId(this.currentBuiltInClassNameFlags))
this.currentBuiltInClassNameFlags = 0
this.closeContainingScopes()
this.emitNewline(nextHunk.newEnd.column)
this.emitIndentWhitespace(nextHunk.newEnd.column)
}
emitLineEnding () {
this.emitCloseTag(this.getBuiltInScopeId(this.currentBuiltInClassNameFlags))
let lineEnding = this.displayLayer.buffer.lineEndingForRow(this.bufferRow)
const eolInvisible = this.displayLayer.eolInvisibles[lineEnding]
if (eolInvisible) {
let eolFlags = INVISIBLE_CHARACTER | LINE_ENDING
if (this.bufferLine.length === 0 && this.displayLayer.showIndentGuides) eolFlags |= INDENT_GUIDE
this.emitOpenTag(this.getBuiltInScopeId(eolFlags))
this.emitText(eolInvisible, false)
this.emitCloseTag(this.getBuiltInScopeId(eolFlags))
}
if (this.bufferLine.length === 0 && this.displayLayer.showIndentGuides) {
let whitespaceLength = this.displayLayer.leadingWhitespaceLengthForSurroundingLines(this.bufferRow)
this.emitIndentWhitespace(whitespaceLength)
}
this.closeContainingScopes()
// Ensure empty lines have at least one empty token to make it easier on
// the caller
if (this.currentScreenLineTags.length === 0) this.currentScreenLineTags.push(0)
this.emitNewline()
this.bufferRow++
this.bufferColumn = 0
}
emitNewline (softWrapIndent = -1) {
const screenLine = {
id: nextScreenLineId++,
lineText: this.currentScreenLineText,
tags: this.currentScreenLineTags,
softWrapIndent
}
this.pushScreenLine(screenLine)
this.displayLayer.cachedScreenLines[this.screenRow] = screenLine
this.screenRow++
this.beginLine()
}
emitIndentWhitespace (endColumn) {
if (this.displayLayer.showIndentGuides) {
let openedIndentGuide = false
while (this.screenColumn < endColumn) {
if (this.screenColumn % this.displayLayer.tabLength === 0) {
if (openedIndentGuide) {
this.emitCloseTag(this.getBuiltInScopeId(INDENT_GUIDE))
}
this.emitOpenTag(this.getBuiltInScopeId(INDENT_GUIDE))
openedIndentGuide = true
}
this.emitText(' ', false)
}
if (openedIndentGuide) this.emitCloseTag(this.getBuiltInScopeId(INDENT_GUIDE))
} else {
this.emitText(' '.repeat(endColumn - this.screenColumn), false)
}
}
emitHardTab () {
const distanceToNextTabStop = this.displayLayer.tabLength - (this.screenColumn % this.displayLayer.tabLength)
if (this.displayLayer.invisibles.tab) {
this.emitText(this.displayLayer.invisibles.tab)
this.emitText(' '.repeat(distanceToNextTabStop - 1))
} else {
this.emitText(' '.repeat(distanceToNextTabStop))
}
}
emitText (text, reopenTags = true) {
if (reopenTags) this.reopenTags()
this.currentScreenLineText += text
const length = text.length
this.screenColumn += length
this.currentTokenLength += length
}
emitTokenBoundary () {
if (this.currentTokenLength > 0) {
this.currentScreenLineTags.push(this.currentTokenLength)
this.currentTokenLength = 0
}
}
emitEmptyTokenIfNeeded () {
const lastTag = this.currentScreenLineTags[this.currentScreenLineTags.length - 1]
if (this.displayLayer.isOpenTag(lastTag)) {
this.currentScreenLineTags.push(0)
}
}
emitCloseTag (scopeId) {
this.emitTokenBoundary()
if (scopeId === 0) return
for (let i = this.scopeIdsToReopen.length - 1; i >= 0; i--) {
if (this.scopeIdsToReopen[i] === scopeId) {
this.scopeIdsToReopen.splice(i, 1)
return
}
}
this.emitEmptyTokenIfNeeded()
var containingScopeId
while ((containingScopeId = this.containingScopeIds.pop())) {
this.currentScreenLineTags.push(this.displayLayer.closeTagForScopeId(containingScopeId))
if (containingScopeId === scopeId) {
return
} else {
this.scopeIdsToReopen.unshift(containingScopeId)
}
}
}
emitOpenTag (scopeId, reopenTags = true) {
if (reopenTags) this.reopenTags()
this.emitTokenBoundary()
if (scopeId > 0) {
this.containingScopeIds.push(scopeId)
this.currentScreenLineTags.push(this.displayLayer.openTagForScopeId(scopeId))
}
}
closeContainingScopes () {
if (this.containingScopeIds.length > 0) this.emitEmptyTokenIfNeeded()
for (let i = this.containingScopeIds.length - 1; i >= 0; i--) {
const containingScopeId = this.containingScopeIds[i]
this.currentScreenLineTags.push(this.displayLayer.closeTagForScopeId(containingScopeId))
this.scopeIdsToReopen.unshift(containingScopeId)
}
this.containingScopeIds.length = 0
}
reopenTags () {
for (let i = 0, n = this.scopeIdsToReopen.length; i < n; i++) {
const scopeIdToReopen = this.scopeIdsToReopen[i]
this.containingScopeIds.push(scopeIdToReopen)
this.currentScreenLineTags.push(this.displayLayer.openTagForScopeId(scopeIdToReopen))
}
this.scopeIdsToReopen.length = 0
}
pushScreenLine (screenLine) {
if (this.requestedStartScreenRow <= this.screenRow && this.screenRow < this.requestedEndScreenRow) {
this.screenLines.push(screenLine)
}
}
compareBufferPosition (position) {
const rowComparison = this.bufferRow - position.row
return rowComparison === 0 ? (this.bufferColumn - position.column) : rowComparison
}
}