FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
atom/src/lines-component.coffee at master · tml/atom · GitHub
tml
atom
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
atom
/
src
/
lines-component.coffee
Copy path
More file actions
More file actions
Latest commit
History
History
History
368 lines (291 loc) · 13 KB
Breadcrumbs
atom
/
src
/
lines-component.coffee
Copy path
File metadata and controls
368 lines (291 loc) · 13 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
_
=
require
'
underscore-plus
'
React
=
require
'
react-atom-fork
'
{
div
,
span
}
=
require
'
reactionary-atom-fork
'
{
debounce
,
isEqual
,
isEqualForProperties
,
multiplyString
,
toArray
}
=
require
'
underscore-plus
'
{
$$
}
=
require
'
space-pen
'
Decoration
=
require
'
./decoration
'
CursorsComponent
=
require
'
./cursors-component
'
HighlightsComponent
=
require
'
./highlights-component
'
OverlayManager
=
require
'
./overlay-manager
'
DummyLineNode
=
$$
(
->
@
div
className
:
'
line
'
,
style
:
'
position: absolute; visibility: hidden;
'
,
=>
@
span
'
x
'
)[
0
]
AcceptFilter
=
{
acceptNode
:
->
NodeFilter
.
FILTER_ACCEPT
}
WrapperDiv
=
document
.
createElement
(
'
div
'
)
module
.
exports
=
LinesComponent
=
React
.
createClass
displayName
:
'
LinesComponent
'
render
:
->
{
performedInitialMeasurement
,
cursorBlinkPeriod
,
cursorBlinkResumeDelay
}
=
@props
if
performedInitialMeasurement
{
editor
,
overlayDecorations
,
highlightDecorations
,
scrollHeight
,
scrollWidth
,
placeholderText
,
backgroundColor
}
=
@props
{
lineHeightInPixels
,
defaultCharWidth
,
scrollViewHeight
,
scopedCharacterWidthsChangeCount
}
=
@props
{
scrollTop
,
scrollLeft
,
cursorPixelRects
}
=
@props
style
=
height
:
Math
.
max
(scrollHeight, scrollViewHeight)
width
:
scrollWidth
WebkitTransform
:
@
getTransform
()
backgroundColor
:
if
editor
.
isMini
()
then
null
else
backgroundColor
div
{
className
:
'
lines
'
, style},
div
className
:
'
placeholder-text
'
, placeholderText
if
placeholderText
?
CursorsComponent
{
cursorPixelRects, cursorBlinkPeriod, cursorBlinkResumeDelay, lineHeightInPixels,
defaultCharWidth, scopedCharacterWidthsChangeCount, performedInitialMeasurement
}
HighlightsComponent
{
editor, highlightDecorations, lineHeightInPixels, defaultCharWidth,
scopedCharacterWidthsChangeCount, performedInitialMeasurement
}
getTransform
:
->
{
scrollTop
,
scrollLeft
,
useHardwareAcceleration
}
=
@props
if
useHardwareAcceleration
"
translate3d(
#{
-
scrollLeft
}
px,
#{
-
scrollTop
}
px, 0px)
"
else
"
translate(
#{
-
scrollLeft
}
px,
#{
-
scrollTop
}
px)
"
componentWillMount
:
->
@measuredLines
=
new
WeakSet
@lineNodesByLineId
=
{}
@screenRowsByLineId
=
{}
@lineIdsByScreenRow
=
{}
@renderedDecorationsByLineId
=
{}
componentDidMount
:
->
if
@props
.
useShadowDOM
insertionPoint
=
document
.
createElement
(
'
content
'
)
insertionPoint
.
setAttribute
(
'
select
'
,
'
.overlayer
'
)
@
getDOMNode
().
appendChild
(insertionPoint)
insertionPoint
=
document
.
createElement
(
'
content
'
)
insertionPoint
.
setAttribute
(
'
select
'
,
'
atom-overlay
'
)
@overlayManager
=
new
OverlayManager
(
@props
.
hostElement
)
@
getDOMNode
().
appendChild
(insertionPoint)
else
@overlayManager
=
new
OverlayManager
(
@
getDOMNode
())
shouldComponentUpdate
:
(
newProps
)
->
return
true
unless
isEqualForProperties
(newProps,
@props
,
'
renderedRowRange
'
,
'
lineDecorations
'
,
'
highlightDecorations
'
,
'
lineHeightInPixels
'
,
'
defaultCharWidth
'
,
'
overlayDecorations
'
,
'
scrollTop
'
,
'
scrollLeft
'
,
'
showIndentGuide
'
,
'
scrollingVertically
'
,
'
visible
'
,
'
scrollViewHeight
'
,
'
mouseWheelScreenRow
'
,
'
scopedCharacterWidthsChangeCount
'
,
'
lineWidth
'
,
'
useHardwareAcceleration
'
,
'
placeholderText
'
,
'
performedInitialMeasurement
'
,
'
backgroundColor
'
,
'
cursorPixelRects
'
)
{
renderedRowRange
,
pendingChanges
}
=
newProps
return
false
unless
renderedRowRange
?
[
renderedStartRow
,
renderedEndRow
]
=
renderedRowRange
for
change
in
pendingChanges
if
change
.
screenDelta
is
0
return
true
unless
change
.
end
<
renderedStartRow
or
renderedEndRow
<=
change
.
start
else
return
true
unless
renderedEndRow
<=
change
.
start
false
componentDidUpdate
:
(
prevProps
)
->
{
visible
,
scrollingVertically
,
performedInitialMeasurement
}
=
@props
return
unless
performedInitialMeasurement
@
clearScreenRowCaches
()
unless
prevProps
.
lineHeightInPixels
is
@props
.
lineHeightInPixels
@
removeLineNodes
()
unless
isEqualForProperties
(prevProps,
@props
,
'
showIndentGuide
'
)
@
updateLines
(
@props
.
lineWidth
isnt
prevProps
.
lineWidth
)
@
measureCharactersInNewLines
()
if
visible
and
not
scrollingVertically
@overlayManager
?
.
render
(
@props
)
clearScreenRowCaches
:
->
@screenRowsByLineId
=
{}
@lineIdsByScreenRow
=
{}
updateLines
:
(
updateWidth
)
->
{
tokenizedLines
,
renderedRowRange
,
showIndentGuide
,
selectionChanged
,
lineDecorations
}
=
@props
[
startRow
]
=
renderedRowRange
@
removeLineNodes
(tokenizedLines)
@
appendOrUpdateVisibleLineNodes
(tokenizedLines, startRow, updateWidth)
removeLineNodes
:
(
visibleLines
=
[])
->
{
mouseWheelScreenRow
}
=
@props
visibleLineIds
=
new
Set
visibleLineIds
.
add
(
line
.
id
.
toString
())
for
line
in
visibleLines
node
=
@
getDOMNode
()
for
lineId, lineNode
of
@lineNodesByLineId
when
not
visibleLineIds
.
has
(lineId)
screenRow
=
@screenRowsByLineId
[lineId]
if
not
screenRow
?
or
screenRow
isnt
mouseWheelScreenRow
delete
@lineNodesByLineId
[lineId]
delete
@lineIdsByScreenRow
[screenRow]
if
@lineIdsByScreenRow
[screenRow]
is
lineId
delete
@screenRowsByLineId
[lineId]
delete
@renderedDecorationsByLineId
[lineId]
node
.
removeChild
(lineNode)
appendOrUpdateVisibleLineNodes
:
(
visibleLines
,
startRow
,
updateWidth
)
->
{
lineDecorations
}
=
@props
newLines
=
null
newLinesHTML
=
null
for
line, index
in
visibleLines
screenRow
=
startRow
+
index
if
@
hasLineNode
(
line
.
id
)
@
updateLineNode
(line, screenRow, updateWidth)
else
newLines
?=
[]
newLinesHTML
?=
"
"
newLines
.
push
(line)
newLinesHTML
+=
@
buildLineHTML
(line, screenRow)
@screenRowsByLineId
[
line
.
id
]
=
screenRow
@lineIdsByScreenRow
[screenRow]
=
line
.
id
@renderedDecorationsByLineId
[
line
.
id
]
=
lineDecorations[screenRow]
return
unless
newLines
?
WrapperDiv
.
innerHTML
=
newLinesHTML
newLineNodes
=
toArray
(
WrapperDiv
.
children
)
node
=
@
getDOMNode
()
for
line, i
in
newLines
lineNode
=
newLineNodes[i]
@lineNodesByLineId
[
line
.
id
]
=
lineNode
node
.
appendChild
(lineNode)
hasLineNode
:
(
lineId
)
->
@lineNodesByLineId
.
hasOwnProperty
(lineId)
buildLineHTML
:
(
line
,
screenRow
)
->
{
showIndentGuide
,
lineHeightInPixels
,
lineDecorations
,
lineWidth
}
=
@props
{
tokens
,
text
,
lineEnding
,
fold
,
isSoftWrapped
,
indentLevel
}
=
line
classes
=
'
'
if
decorations
=
lineDecorations[screenRow]
for
id, decoration
of
decorations
if
Decoration
.
isType
(decoration,
'
line
'
)
classes
+=
decoration
.
class
+
'
'
classes
+=
'
line
'
top
=
screenRow
*
lineHeightInPixels
lineHTML
=
"
<div class=
\"
#{
classes
}
\"
style=
\"
position: absolute; top:
#{
top
}
px; width:
#{
lineWidth
}
px;
\"
data-screen-row=
\"
#{
screenRow
}
\"
>
"
if
text
is
"
"
lineHTML
+=
@
buildEmptyLineInnerHTML
(line)
else
lineHTML
+=
@
buildLineInnerHTML
(line)
lineHTML
+=
'
<span class="fold-marker"></span>
'
if
fold
lineHTML
+=
"
</div>
"
lineHTML
buildEmptyLineInnerHTML
:
(
line
)
->
{
showIndentGuide
}
=
@props
{
indentLevel
,
tabLength
,
endOfLineInvisibles
}
=
line
if
showIndentGuide
and
indentLevel
>
0
invisibleIndex
=
0
lineHTML
=
'
'
for
i
in
[
0
...
indentLevel]
lineHTML
+=
"
<span class='indent-guide'>
"
for
j
in
[
0
...
tabLength]
if
invisible
=
endOfLineInvisibles
?
[invisibleIndex
++
]
lineHTML
+=
"
<span class='invisible-character'>
#{
invisible
}
</span>
"
else
lineHTML
+=
'
'
lineHTML
+=
"
</span>
"
while
invisibleIndex
<
endOfLineInvisibles
?
.
length
lineHTML
+=
"
<span class='invisible-character'>
#{
line
.
endOfLineInvisibles
[invisibleIndex
++
]
}
</span>
"
lineHTML
else
@
buildEndOfLineHTML
(line)
or
'
'
buildLineInnerHTML
:
(
line
)
->
{
editor
,
showIndentGuide
}
=
@props
{
tokens
,
text
}
=
line
innerHTML
=
"
"
scopeStack
=
[]
firstTrailingWhitespacePosition
=
text
.
search
(
/
\s
*
$
/
)
lineIsWhitespaceOnly
=
firstTrailingWhitespacePosition
is
0
for
token
in
tokens
innerHTML
+=
@
updateScopeStack
(scopeStack,
token
.
scopes
)
hasIndentGuide
=
not
editor
.
isMini
()
and
showIndentGuide
and
(
token
.
hasLeadingWhitespace
()
or
(
token
.
hasTrailingWhitespace
()
and
lineIsWhitespaceOnly))
innerHTML
+=
token
.
getValueAsHtml
({hasIndentGuide})
innerHTML
+=
@
popScope
(scopeStack)
while
scopeStack
.
length
>
0
innerHTML
+=
@
buildEndOfLineHTML
(line)
innerHTML
buildEndOfLineHTML
:
(
line
)
->
{
endOfLineInvisibles
}
=
line
html
=
'
'
if
endOfLineInvisibles
?
for
invisible
in
endOfLineInvisibles
html
+=
"
<span class='invisible-character'>
#{
invisible
}
</span>
"
html
updateScopeStack
:
(
scopeStack
,
desiredScopeDescriptor
)
->
html
=
"
"
#
Find a common prefix
for
scope, i
in
desiredScopeDescriptor
break
unless
scopeStack[i]
is
desiredScopeDescriptor[i]
#
Pop scopeDescriptor until we're at the common prefx
until
scopeStack
.
length
is
i
html
+=
@
popScope
(scopeStack)
#
Push onto common prefix until scopeStack equals desiredScopeDescriptor
for
j
in
[i
...
desiredScopeDescriptor
.
length
]
html
+=
@
pushScope
(scopeStack, desiredScopeDescriptor[j])
html
popScope
:
(
scopeStack
)
->
scopeStack
.
pop
()
"
</span>
"
pushScope
:
(
scopeStack
,
scope
)
->
scopeStack
.
push
(scope)
"
<span class=
\"
#{
scope
.
replace
(
/
\.
+
/
g
,
'
'
)
}
\"
>
"
updateLineNode
:
(
line
,
screenRow
,
updateWidth
)
->
{
lineHeightInPixels
,
lineDecorations
,
lineWidth
}
=
@props
lineNode
=
@lineNodesByLineId
[
line
.
id
]
decorations
=
lineDecorations[screenRow]
previousDecorations
=
@renderedDecorationsByLineId
[
line
.
id
]
if
previousDecorations
?
for
id, decoration
of
previousDecorations
if
Decoration
.
isType
(decoration,
'
line
'
)
and
not
@
hasDecoration
(decorations, decoration)
lineNode
.
classList
.
remove
(
decoration
.
class
)
if
decorations
?
for
id, decoration
of
decorations
if
Decoration
.
isType
(decoration,
'
line
'
)
and
not
@
hasDecoration
(previousDecorations, decoration)
lineNode
.
classList
.
add
(
decoration
.
class
)
lineNode
.
style
.
width
=
lineWidth
+
'
px
'
if
updateWidth
unless
@screenRowsByLineId
[
line
.
id
]
is
screenRow
lineNode
.
style
.
top
=
screenRow
*
lineHeightInPixels
+
'
px
'
lineNode
.
dataset
.
screenRow
=
screenRow
@screenRowsByLineId
[
line
.
id
]
=
screenRow
@lineIdsByScreenRow
[screenRow]
=
line
.
id
hasDecoration
:
(
decorations
,
decoration
)
->
decorations
?
and
decorations[
decoration
.
id
]
is
decoration
lineNodeForScreenRow
:
(
screenRow
)
->
@lineNodesByLineId
[
@lineIdsByScreenRow
[screenRow]]
measureLineHeightAndDefaultCharWidth
:
->
node
=
@
getDOMNode
()
node
.
appendChild
(DummyLineNode)
lineHeightInPixels
=
DummyLineNode
.
getBoundingClientRect
().
height
charWidth
=
DummyLineNode
.
firstChild
.
getBoundingClientRect
().
width
node
.
removeChild
(DummyLineNode)
{
editor
}
=
@props
editor
.
setLineHeightInPixels
(lineHeightInPixels)
editor
.
setDefaultCharWidth
(charWidth)
remeasureCharacterWidths
:
->
return
unless
@props
.
performedInitialMeasurement
@
clearScopedCharWidths
()
@
measureCharactersInNewLines
()
measureCharactersInNewLines
:
->
{
editor
,
tokenizedLines
,
renderedRowRange
}
=
@props
[
visibleStartRow
]
=
renderedRowRange
node
=
@
getDOMNode
()
editor
.
batchCharacterMeasurement
=>
for
tokenizedLine
in
tokenizedLines
unless
@measuredLines
.
has
(tokenizedLine)
lineNode
=
@lineNodesByLineId
[
tokenizedLine
.
id
]
@
measureCharactersInLine
(tokenizedLine, lineNode)
return
measureCharactersInLine
:
(
tokenizedLine
,
lineNode
)
->
{
editor
}
=
@props
rangeForMeasurement
=
null
iterator
=
null
charIndex
=
0
for
{value, scopes, hasPairedCharacter}
in
tokenizedLine
.
tokens
charWidths
=
editor
.
getScopedCharWidths
(scopes)
valueIndex
=
0
while
valueIndex
<
value
.
length
if
hasPairedCharacter
char
=
value
.
substr
(valueIndex,
2
)
charLength
=
2
valueIndex
+=
2
else
char
=
value[valueIndex]
charLength
=
1
valueIndex
++
continue
if
char
is
'
\0
'
unless
charWidths[char]
?
unless
textNode
?
rangeForMeasurement
?=
document
.
createRange
()
iterator
=
document
.
createNodeIterator
(lineNode,
NodeFilter
.
SHOW_TEXT
, AcceptFilter)
textNode
=
iterator
.
nextNode
()
textNodeIndex
=
0
nextTextNodeIndex
=
textNode
.
textContent
.
length
while
nextTextNodeIndex
<=
charIndex
textNode
=
iterator
.
nextNode
()
textNodeIndex
=
nextTextNodeIndex
nextTextNodeIndex
=
textNodeIndex
+
textNode
.
textContent
.
length
i
=
charIndex
-
textNodeIndex
rangeForMeasurement
.
setStart
(textNode, i)
rangeForMeasurement
.
setEnd
(textNode, i
+
charLength)
charWidth
=
rangeForMeasurement
.
getBoundingClientRect
().
width
editor
.
setScopedCharWidth
(scopes, char, charWidth)
charIndex
+=
charLength
@measuredLines
.
add
(tokenizedLine)
clearScopedCharWidths
:
->
@measuredLines
.
clear
()
@props
.
editor
.
clearScopedCharWidths
()
Back
|
FazBrowse Home
|
New Git URL