FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
atom/src/display-buffer.coffee at master · Campos/atom · GitHub
Campos
/
atom
Public
forked from
atom/atom
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
atom
/
src
/
display-buffer.coffee
Copy path
More file actions
More file actions
Latest commit
History
History
History
1358 lines (1145 loc) · 47.1 KB
Breadcrumbs
atom
/
src
/
display-buffer.coffee
Copy path
File metadata and controls
1358 lines (1145 loc) · 47.1 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
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
_
=
require
'
underscore-plus
'
Serializable
=
require
'
serializable
'
{
CompositeDisposable
,
Emitter
}
=
require
'
event-kit
'
{
Point
,
Range
}
=
require
'
text-buffer
'
Grim
=
require
'
grim
'
TokenizedBuffer
=
require
'
./tokenized-buffer
'
RowMap
=
require
'
./row-map
'
Fold
=
require
'
./fold
'
Model
=
require
'
./model
'
Token
=
require
'
./token
'
Decoration
=
require
'
./decoration
'
Marker
=
require
'
./marker
'
class
BufferToScreenConversionError
extends
Error
constructor
:
(
@message
,
@metadata
)
->
super
Error
.
captureStackTrace
(
this
, BufferToScreenConversionError)
module
.
exports
=
class
DisplayBuffer
extends
Model
Serializable
.
includeInto
(
this
)
verticalScrollMargin
:
2
horizontalScrollMargin
:
6
scopedCharacterWidthsChangeCount
:
0
constructor
:
({tabLength,
@editorWidthInChars
,
@tokenizedBuffer
, buffer, ignoreInvisibles,
@largeFileMode
}
=
{})
->
super
@emitter
=
new
Emitter
@disposables
=
new
CompositeDisposable
@tokenizedBuffer
?=
new
TokenizedBuffer
({tabLength, buffer, ignoreInvisibles,
@largeFileMode
})
@buffer
=
@tokenizedBuffer
.
buffer
@charWidthsByScope
=
{}
@markers
=
{}
@foldsByMarkerId
=
{}
@decorationsById
=
{}
@decorationsByMarkerId
=
{}
@overlayDecorationsById
=
{}
@disposables
.
add
@tokenizedBuffer
.
observeGrammar
@subscribeToScopedConfigSettings
@disposables
.
add
@tokenizedBuffer
.
onDidChange
@handleTokenizedBufferChange
@disposables
.
add
@buffer
.
onDidCreateMarker
@handleBufferMarkerCreated
@disposables
.
add
@buffer
.
onDidUpdateMarkers
=>
@emitter
.
emit
'
did-update-markers
'
@foldMarkerAttributes
=
Object
.
freeze
({
class
:
'
fold
'
,
displayBufferId
:
@id
})
folds
=
(
new
Fold
(
this
, marker)
for
marker
in
@buffer
.
findMarkers
(
@
getFoldMarkerAttributes
()))
@
updateAllScreenLines
()
@
decorateFold
(fold)
for
fold
in
folds
subscribeToScopedConfigSettings
:
=>
@scopedConfigSubscriptions
?
.
dispose
()
@scopedConfigSubscriptions
=
subscriptions
=
new
CompositeDisposable
scopeDescriptor
=
@
getRootScopeDescriptor
()
oldConfigSettings
=
@configSettings
@configSettings
=
scrollPastEnd
:
atom
.
config
.
get
(
'
editor.scrollPastEnd
'
,
scope
:
scopeDescriptor)
softWrap
:
atom
.
config
.
get
(
'
editor.softWrap
'
,
scope
:
scopeDescriptor)
softWrapAtPreferredLineLength
:
atom
.
config
.
get
(
'
editor.softWrapAtPreferredLineLength
'
,
scope
:
scopeDescriptor)
softWrapHangingIndent
:
atom
.
config
.
get
(
'
editor.softWrapHangingIndent
'
,
scope
:
scopeDescriptor)
preferredLineLength
:
atom
.
config
.
get
(
'
editor.preferredLineLength
'
,
scope
:
scopeDescriptor)
subscriptions
.
add
atom
.
config
.
onDidChange
'
editor.softWrap
'
,
scope
:
scopeDescriptor, ({newValue})
=>
@configSettings
.
softWrap
=
newValue
@
updateWrappedScreenLines
()
subscriptions
.
add
atom
.
config
.
onDidChange
'
editor.softWrapHangingIndent
'
,
scope
:
scopeDescriptor, ({newValue})
=>
@configSettings
.
softWrapHangingIndent
=
newValue
@
updateWrappedScreenLines
()
subscriptions
.
add
atom
.
config
.
onDidChange
'
editor.softWrapAtPreferredLineLength
'
,
scope
:
scopeDescriptor, ({newValue})
=>
@configSettings
.
softWrapAtPreferredLineLength
=
newValue
@
updateWrappedScreenLines
()
if
@
isSoftWrapped
()
subscriptions
.
add
atom
.
config
.
onDidChange
'
editor.preferredLineLength
'
,
scope
:
scopeDescriptor, ({newValue})
=>
@configSettings
.
preferredLineLength
=
newValue
@
updateWrappedScreenLines
()
if
@
isSoftWrapped
()
and
atom
.
config
.
get
(
'
editor.softWrapAtPreferredLineLength
'
,
scope
:
scopeDescriptor)
subscriptions
.
add
atom
.
config
.
observe
'
editor.scrollPastEnd
'
,
scope
:
scopeDescriptor, (
value
)
=>
@configSettings
.
scrollPastEnd
=
value
@
updateWrappedScreenLines
()
if
oldConfigSettings
?
and
not
_
.
isEqual
(oldConfigSettings,
@configSettings
)
serializeParams
:
->
id
:
@id
softWrapped
:
@
isSoftWrapped
()
editorWidthInChars
:
@editorWidthInChars
scrollTop
:
@scrollTop
scrollLeft
:
@scrollLeft
tokenizedBuffer
:
@tokenizedBuffer
.
serialize
()
largeFileMode
:
@largeFileMode
deserializeParams
:
(
params
)
->
params
.
tokenizedBuffer
=
TokenizedBuffer
.
deserialize
(
params
.
tokenizedBuffer
)
params
copy
:
->
newDisplayBuffer
=
new
DisplayBuffer
({
@buffer
,
tabLength
:
@
getTabLength
(),
@largeFileMode
})
newDisplayBuffer
.
setScrollTop
(
@
getScrollTop
())
newDisplayBuffer
.
setScrollLeft
(
@
getScrollLeft
())
for
marker
in
@
findMarkers
(
displayBufferId
:
@id
)
marker
.
copy
(
displayBufferId
:
newDisplayBuffer
.
id
)
newDisplayBuffer
updateAllScreenLines
:
->
@maxLineLength
=
0
@screenLines
=
[]
@rowMap
=
new
RowMap
@
updateScreenLines
(
0
,
@buffer
.
getLineCount
(),
null
,
suppressChangeEvent
:
true
)
onDidChangeSoftWrapped
:
(
callback
)
->
@emitter
.
on
'
did-change-soft-wrapped
'
, callback
onDidChangeGrammar
:
(
callback
)
->
@tokenizedBuffer
.
onDidChangeGrammar
(callback)
onDidTokenize
:
(
callback
)
->
@tokenizedBuffer
.
onDidTokenize
(callback)
onDidChange
:
(
callback
)
->
@emitter
.
on
'
did-change
'
, callback
onDidChangeCharacterWidths
:
(
callback
)
->
@emitter
.
on
'
did-change-character-widths
'
, callback
onDidChangeScrollTop
:
(
callback
)
->
@emitter
.
on
'
did-change-scroll-top
'
, callback
onDidChangeScrollLeft
:
(
callback
)
->
@emitter
.
on
'
did-change-scroll-left
'
, callback
observeScrollTop
:
(
callback
)
->
callback
(
@scrollTop
)
@
onDidChangeScrollTop
(callback)
observeScrollLeft
:
(
callback
)
->
callback
(
@scrollLeft
)
@
onDidChangeScrollLeft
(callback)
observeDecorations
:
(
callback
)
->
callback
(decoration)
for
decoration
in
@
getDecorations
()
@
onDidAddDecoration
(callback)
onDidAddDecoration
:
(
callback
)
->
@emitter
.
on
'
did-add-decoration
'
, callback
onDidRemoveDecoration
:
(
callback
)
->
@emitter
.
on
'
did-remove-decoration
'
, callback
onDidCreateMarker
:
(
callback
)
->
@emitter
.
on
'
did-create-marker
'
, callback
onDidUpdateMarkers
:
(
callback
)
->
@emitter
.
on
'
did-update-markers
'
, callback
emitDidChange
:
(
eventProperties
,
refreshMarkers
=
true
)
->
@
emit
'
changed
'
, eventProperties
if
Grim
.
includeDeprecatedAPIs
@emitter
.
emit
'
did-change
'
, eventProperties
if
refreshMarkers
@
refreshMarkerScreenPositions
()
@
emit
'
markers-updated
'
if
Grim
.
includeDeprecatedAPIs
@emitter
.
emit
'
did-update-markers
'
updateWrappedScreenLines
:
->
start
=
0
end
=
@
getLastRow
()
@
updateAllScreenLines
()
screenDelta
=
@
getLastRow
()
-
end
bufferDelta
=
0
@
emitDidChange
({start, end, screenDelta, bufferDelta})
#
Sets the visibility of the tokenized buffer.
#
#
visible - A {Boolean} indicating of the tokenized buffer is shown
setVisible
:
(
visible
)
->
@tokenizedBuffer
.
setVisible
(visible)
getVerticalScrollMargin
:
->
Math
.
min
(
@verticalScrollMargin
, (
@
getHeight
()
-
@
getLineHeightInPixels
())
/
2
)
setVerticalScrollMargin
:
(
@verticalScrollMargin
)
->
@verticalScrollMargin
getVerticalScrollMarginInPixels
:
->
scrollMarginInPixels
=
@
getVerticalScrollMargin
()
*
@
getLineHeightInPixels
()
maxScrollMarginInPixels
=
(
@
getHeight
()
-
@
getLineHeightInPixels
())
/
2
Math
.
min
(scrollMarginInPixels, maxScrollMarginInPixels)
getHorizontalScrollMargin
:
->
Math
.
min
(
@horizontalScrollMargin
, (
@
getWidth
()
-
@
getDefaultCharWidth
())
/
2
)
setHorizontalScrollMargin
:
(
@horizontalScrollMargin
)
->
@horizontalScrollMargin
getHorizontalScrollMarginInPixels
:
->
scrollMarginInPixels
=
@
getHorizontalScrollMargin
()
*
@
getDefaultCharWidth
()
maxScrollMarginInPixels
=
(
@
getWidth
()
-
@
getDefaultCharWidth
())
/
2
Math
.
min
(scrollMarginInPixels, maxScrollMarginInPixels)
getHorizontalScrollbarHeight
:
->
@horizontalScrollbarHeight
setHorizontalScrollbarHeight
:
(
@horizontalScrollbarHeight
)
->
@horizontalScrollbarHeight
getVerticalScrollbarWidth
:
->
@verticalScrollbarWidth
setVerticalScrollbarWidth
:
(
@verticalScrollbarWidth
)
->
@verticalScrollbarWidth
getHeight
:
->
if
@height
?
@height
else
if
@
horizontallyScrollable
()
@
getScrollHeight
()
+
@
getHorizontalScrollbarHeight
()
else
@
getScrollHeight
()
setHeight
:
(
@height
)
->
@height
getClientHeight
:
(
reentrant
)
->
if
@
horizontallyScrollable
(reentrant)
@
getHeight
()
-
@
getHorizontalScrollbarHeight
()
else
@
getHeight
()
getClientWidth
:
(
reentrant
)
->
if
@
verticallyScrollable
(reentrant)
@
getWidth
()
-
@
getVerticalScrollbarWidth
()
else
@
getWidth
()
horizontallyScrollable
:
(
reentrant
)
->
return
false
unless
@width
?
return
false
if
@
isSoftWrapped
()
if
reentrant
@
getScrollWidth
()
>
@
getWidth
()
else
@
getScrollWidth
()
>
@
getClientWidth
(
true
)
verticallyScrollable
:
(
reentrant
)
->
return
false
unless
@height
?
if
reentrant
@
getScrollHeight
()
>
@
getHeight
()
else
@
getScrollHeight
()
>
@
getClientHeight
(
true
)
getWidth
:
->
if
@width
?
@width
else
if
@
verticallyScrollable
()
@
getScrollWidth
()
+
@
getVerticalScrollbarWidth
()
else
@
getScrollWidth
()
setWidth
:
(
newWidth
)
->
oldWidth
=
@width
@width
=
newWidth
@
updateWrappedScreenLines
()
if
newWidth
isnt
oldWidth
and
@
isSoftWrapped
()
@
setScrollTop
(
@
getScrollTop
())
#
Ensure scrollTop is still valid in case horizontal scrollbar disappeared
@width
getScrollTop
:
->
@scrollTop
setScrollTop
:
(
scrollTop
)
->
scrollTop
=
Math
.
round
(
Math
.
max
(
0
,
Math
.
min
(
@
getMaxScrollTop
(), scrollTop)))
unless
scrollTop
is
@scrollTop
@scrollTop
=
scrollTop
@emitter
.
emit
'
did-change-scroll-top
'
,
@scrollTop
@scrollTop
getMaxScrollTop
:
->
@
getScrollHeight
()
-
@
getClientHeight
()
getScrollBottom
:
->
@scrollTop
+
@
getClientHeight
()
setScrollBottom
:
(
scrollBottom
)
->
@
setScrollTop
(scrollBottom
-
@
getClientHeight
())
@
getScrollBottom
()
getScrollLeft
:
->
@scrollLeft
setScrollLeft
:
(
scrollLeft
)
->
scrollLeft
=
Math
.
round
(
Math
.
max
(
0
,
Math
.
min
(
@
getScrollWidth
()
-
@
getClientWidth
(), scrollLeft)))
unless
scrollLeft
is
@scrollLeft
@scrollLeft
=
scrollLeft
@emitter
.
emit
'
did-change-scroll-left
'
,
@scrollLeft
@scrollLeft
getMaxScrollLeft
:
->
@
getScrollWidth
()
-
@
getClientWidth
()
getScrollRight
:
->
@scrollLeft
+
@width
setScrollRight
:
(
scrollRight
)
->
@
setScrollLeft
(scrollRight
-
@width
)
@
getScrollRight
()
getLineHeightInPixels
:
->
@lineHeightInPixels
setLineHeightInPixels
:
(
@lineHeightInPixels
)
->
@lineHeightInPixels
getDefaultCharWidth
:
->
@defaultCharWidth
setDefaultCharWidth
:
(
defaultCharWidth
)
->
if
defaultCharWidth
isnt
@defaultCharWidth
@defaultCharWidth
=
defaultCharWidth
@
computeScrollWidth
()
defaultCharWidth
getCursorWidth
:
->
1
getScopedCharWidth
:
(
scopeNames
,
char
)
->
@
getScopedCharWidths
(scopeNames)[char]
getScopedCharWidths
:
(
scopeNames
)
->
scope
=
@charWidthsByScope
for
scopeName
in
scopeNames
scope[scopeName]
?=
{}
scope
=
scope[scopeName]
scope
.
charWidths
?=
{}
scope
.
charWidths
batchCharacterMeasurement
:
(
fn
)
->
oldChangeCount
=
@scopedCharacterWidthsChangeCount
@batchingCharacterMeasurement
=
true
fn
()
@batchingCharacterMeasurement
=
false
@
characterWidthsChanged
()
if
oldChangeCount
isnt
@scopedCharacterWidthsChangeCount
setScopedCharWidth
:
(
scopeNames
,
char
,
width
)
->
@
getScopedCharWidths
(scopeNames)[char]
=
width
@scopedCharacterWidthsChangeCount
++
@
characterWidthsChanged
()
unless
@batchingCharacterMeasurement
characterWidthsChanged
:
->
@
computeScrollWidth
()
@
emit
'
character-widths-changed
'
,
@scopedCharacterWidthsChangeCount
if
Grim
.
includeDeprecatedAPIs
@emitter
.
emit
'
did-change-character-widths
'
,
@scopedCharacterWidthsChangeCount
clearScopedCharWidths
:
->
@charWidthsByScope
=
{}
getScrollHeight
:
->
lineHeight
=
@
getLineHeightInPixels
()
return
0
unless
lineHeight
>
0
scrollHeight
=
@
getLineCount
()
*
lineHeight
if
@height
?
and
@configSettings
.
scrollPastEnd
scrollHeight
=
scrollHeight
+
@height
-
(lineHeight
*
3
)
scrollHeight
getScrollWidth
:
->
@scrollWidth
#
Returns an {Array} of two numbers representing the first and the last visible rows.
getVisibleRowRange
:
->
return
[
0
,
0
]
unless
@
getLineHeightInPixels
()
>
0
startRow
=
Math
.
floor
(
@
getScrollTop
()
/
@
getLineHeightInPixels
())
endRow
=
Math
.
ceil
((
@
getScrollTop
()
+
@
getHeight
())
/
@
getLineHeightInPixels
())
-
1
endRow
=
Math
.
min
(
@
getLineCount
(), endRow)
[startRow, endRow]
intersectsVisibleRowRange
:
(
startRow
,
endRow
)
->
[
visibleStart
,
visibleEnd
]
=
@
getVisibleRowRange
()
not
(endRow
<=
visibleStart
or
visibleEnd
<=
startRow)
selectionIntersectsVisibleRowRange
:
(
selection
)
->
{
start
,
end
}
=
selection
.
getScreenRange
()
@
intersectsVisibleRowRange
(
start
.
row
,
end
.
row
+
1
)
scrollToScreenRange
:
(
screenRange
,
options
)
->
verticalScrollMarginInPixels
=
@
getVerticalScrollMarginInPixels
()
horizontalScrollMarginInPixels
=
@
getHorizontalScrollMarginInPixels
()
{
top
,
left
}
=
@
pixelRectForScreenRange
(
new
Range
(
screenRange
.
start
,
screenRange
.
start
))
{
top
:
endTop
,
left
:
endLeft
,
height
:
endHeight
}
=
@
pixelRectForScreenRange
(
new
Range
(
screenRange
.
end
,
screenRange
.
end
))
bottom
=
endTop
+
endHeight
right
=
endLeft
if
options
?
.
center
desiredScrollCenter
=
(top
+
bottom)
/
2
unless
@
getScrollTop
()
<
desiredScrollCenter
<
@
getScrollBottom
()
desiredScrollTop
=
desiredScrollCenter
-
@
getHeight
()
/
2
desiredScrollBottom
=
desiredScrollCenter
+
@
getHeight
()
/
2
else
desiredScrollTop
=
top
-
verticalScrollMarginInPixels
desiredScrollBottom
=
bottom
+
verticalScrollMarginInPixels
desiredScrollLeft
=
left
-
horizontalScrollMarginInPixels
desiredScrollRight
=
right
+
horizontalScrollMarginInPixels
if
options
?
.
reversed
?
true
if
desiredScrollBottom
>
@
getScrollBottom
()
@
setScrollBottom
(desiredScrollBottom)
if
desiredScrollTop
<
@
getScrollTop
()
@
setScrollTop
(desiredScrollTop)
if
desiredScrollRight
>
@
getScrollRight
()
@
setScrollRight
(desiredScrollRight)
if
desiredScrollLeft
<
@
getScrollLeft
()
@
setScrollLeft
(desiredScrollLeft)
else
if
desiredScrollTop
<
@
getScrollTop
()
@
setScrollTop
(desiredScrollTop)
if
desiredScrollBottom
>
@
getScrollBottom
()
@
setScrollBottom
(desiredScrollBottom)
if
desiredScrollLeft
<
@
getScrollLeft
()
@
setScrollLeft
(desiredScrollLeft)
if
desiredScrollRight
>
@
getScrollRight
()
@
setScrollRight
(desiredScrollRight)
scrollToScreenPosition
:
(
screenPosition
,
options
)
->
@
scrollToScreenRange
(
new
Range
(screenPosition, screenPosition), options)
scrollToBufferPosition
:
(
bufferPosition
,
options
)
->
@
scrollToScreenPosition
(
@
screenPositionForBufferPosition
(bufferPosition), options)
pixelRectForScreenRange
:
(
screenRange
)
->
if
screenRange
.
end
.
row
>
screenRange
.
start
.
row
top
=
@
pixelPositionForScreenPosition
(
screenRange
.
start
).
top
left
=
0
height
=
(
screenRange
.
end
.
row
-
screenRange
.
start
.
row
+
1
)
*
@
getLineHeightInPixels
()
width
=
@
getScrollWidth
()
else
{
top
,
left
}
=
@
pixelPositionForScreenPosition
(
screenRange
.
start
,
false
)
height
=
@
getLineHeightInPixels
()
width
=
@
pixelPositionForScreenPosition
(
screenRange
.
end
,
false
).
left
-
left
{top, left, width, height}
#
Retrieves the current tab length.
#
#
Returns a {Number}.
getTabLength
:
->
@tokenizedBuffer
.
getTabLength
()
#
Specifies the tab length.
#
#
tabLength - A {Number} that defines the new tab length.
setTabLength
:
(
tabLength
)
->
@tokenizedBuffer
.
setTabLength
(tabLength)
setIgnoreInvisibles
:
(
ignoreInvisibles
)
->
@tokenizedBuffer
.
setIgnoreInvisibles
(ignoreInvisibles)
setSoftWrapped
:
(
softWrapped
)
->
if
softWrapped
isnt
@softWrapped
@softWrapped
=
softWrapped
@
updateWrappedScreenLines
()
softWrapped
=
@
isSoftWrapped
()
@
emit
'
soft-wrap-changed
'
, softWrapped
if
Grim
.
includeDeprecatedAPIs
@emitter
.
emit
'
did-change-soft-wrapped
'
, softWrapped
softWrapped
else
@
isSoftWrapped
()
isSoftWrapped
:
->
if
@largeFileMode
false
else
@softWrapped
?
@configSettings
.
softWrap
?
false
#
Set the number of characters that fit horizontally in the editor.
#
#
editorWidthInChars - A {Number} of characters.
setEditorWidthInChars
:
(
editorWidthInChars
)
->
if
editorWidthInChars
>
0
previousWidthInChars
=
@editorWidthInChars
@editorWidthInChars
=
editorWidthInChars
if
editorWidthInChars
isnt
previousWidthInChars
and
@
isSoftWrapped
()
@
updateWrappedScreenLines
()
#
Returns the editor width in characters for soft wrap.
getEditorWidthInChars
:
->
width
=
@width
?
@
getScrollWidth
()
width
-=
@
getVerticalScrollbarWidth
()
if
width
?
and
@defaultCharWidth
>
0
Math
.
max
(
0
,
Math
.
floor
(width
/
@defaultCharWidth
))
else
@editorWidthInChars
getSoftWrapColumn
:
->
if
@configSettings
.
softWrapAtPreferredLineLength
Math
.
min
(
@
getEditorWidthInChars
(),
@configSettings
.
preferredLineLength
)
else
@
getEditorWidthInChars
()
#
Gets the screen line for the given screen row.
#
#
* `screenRow` - A {Number} indicating the screen row.
#
#
Returns {TokenizedLine}
tokenizedLineForScreenRow
:
(
screenRow
)
->
if
@largeFileMode
if
line
=
@tokenizedBuffer
.
tokenizedLineForRow
(screenRow)
if
line
.
text
.
length
>
@maxLineLength
@maxLineLength
=
line
.
text
.
length
@longestScreenRow
=
screenRow
line
else
@screenLines
[screenRow]
#
Gets the screen lines for the given screen row range.
#
#
startRow - A {Number} indicating the beginning screen row.
#
endRow - A {Number} indicating the ending screen row.
#
#
Returns an {Array} of {TokenizedLine}s.
tokenizedLinesForScreenRows
:
(
startRow
,
endRow
)
->
if
@largeFileMode
@tokenizedBuffer
.
tokenizedLinesForRows
(startRow, endRow)
else
@screenLines
[startRow
..
endRow]
#
Gets all the screen lines.
#
#
Returns an {Array} of {TokenizedLine}s.
getTokenizedLines
:
->
if
@largeFileMode
@tokenizedBuffer
.
tokenizedLinesForRows
(
0
,
@
getLastRow
())
else
new
Array
(
@screenLines
...
)
indentLevelForLine
:
(
line
)
->
@tokenizedBuffer
.
indentLevelForLine
(line)
#
Given starting and ending screen rows, this returns an array of the
#
buffer rows corresponding to every screen row in the range
#
#
startScreenRow - The screen row {Number} to start at
#
endScreenRow - The screen row {Number} to end at (default: the last screen row)
#
#
Returns an {Array} of buffer rows as {Numbers}s.
bufferRowsForScreenRows
:
(
startScreenRow
,
endScreenRow
)
->
if
@largeFileMode
[startScreenRow
..
endScreenRow]
else
for
screenRow
in
[startScreenRow
..
endScreenRow]
@rowMap
.
bufferRowRangeForScreenRow
(screenRow)[
0
]
#
Creates a new fold between two row numbers.
#
#
startRow - The row {Number} to start folding at
#
endRow - The row {Number} to end the fold
#
#
Returns the new {Fold}.
createFold
:
(
startRow
,
endRow
)
->
unless
@largeFileMode
foldMarker
=
@
findFoldMarker
({startRow, endRow})
?
@buffer
.
markRange
([[startRow,
0
], [endRow,
Infinity
]],
@
getFoldMarkerAttributes
())
@
foldForMarker
(foldMarker)
isFoldedAtBufferRow
:
(
bufferRow
)
->
@
largestFoldContainingBufferRow
(bufferRow)
?
isFoldedAtScreenRow
:
(
screenRow
)
->
@
largestFoldContainingBufferRow
(
@
bufferRowForScreenRow
(screenRow))
?
#
Destroys the fold with the given id
destroyFoldWithId
:
(
id
)
->
@foldsByMarkerId
[id]
?
.
destroy
()
#
Removes any folds found that contain the given buffer row.
#
#
bufferRow - The buffer row {Number} to check against
unfoldBufferRow
:
(
bufferRow
)
->
fold
.
destroy
()
for
fold
in
@
foldsContainingBufferRow
(bufferRow)
return
#
Given a buffer row, this returns the largest fold that starts there.
#
#
Largest is defined as the fold whose difference between its start and end points
#
are the greatest.
#
#
bufferRow - A {Number} indicating the buffer row
#
#
Returns a {Fold} or null if none exists.
largestFoldStartingAtBufferRow
:
(
bufferRow
)
->
@
foldsStartingAtBufferRow
(bufferRow)[
0
]
#
Public: Given a buffer row, this returns all folds that start there.
#
#
bufferRow - A {Number} indicating the buffer row
#
#
Returns an {Array} of {Fold}s.
foldsStartingAtBufferRow
:
(
bufferRow
)
->
for
marker
in
@
findFoldMarkers
(
startRow
:
bufferRow)
@
foldForMarker
(marker)
#
Given a screen row, this returns the largest fold that starts there.
#
#
Largest is defined as the fold whose difference between its start and end points
#
are the greatest.
#
#
screenRow - A {Number} indicating the screen row
#
#
Returns a {Fold}.
largestFoldStartingAtScreenRow
:
(
screenRow
)
->
@
largestFoldStartingAtBufferRow
(
@
bufferRowForScreenRow
(screenRow))
#
Given a buffer row, this returns the largest fold that includes it.
#
#
Largest is defined as the fold whose difference between its start and end rows
#
is the greatest.
#
#
bufferRow - A {Number} indicating the buffer row
#
#
Returns a {Fold}.
largestFoldContainingBufferRow
:
(
bufferRow
)
->
@
foldsContainingBufferRow
(bufferRow)[
0
]
#
Returns the folds in the given row range (exclusive of end row) that are
#
not contained by any other folds.
outermostFoldsInBufferRowRange
:
(
startRow
,
endRow
)
->
folds
=
[]
lastFoldEndRow
=
-
1
for
marker
in
@
findFoldMarkers
(
intersectsRowRange
:
[startRow, endRow])
range
=
marker
.
getRange
()
if
range
.
start
.
row
>
lastFoldEndRow
lastFoldEndRow
=
range
.
end
.
row
if
startRow
<=
range
.
start
.
row
<=
range
.
end
.
row
<
endRow
folds
.
push
(
@
foldForMarker
(marker))
folds
#
Public: Given a buffer row, this returns folds that include it.
#
#
#
bufferRow - A {Number} indicating the buffer row
#
#
Returns an {Array} of {Fold}s.
foldsContainingBufferRow
:
(
bufferRow
)
->
for
marker
in
@
findFoldMarkers
(
intersectsRow
:
bufferRow)
@
foldForMarker
(marker)
#
Given a buffer row, this converts it into a screen row.
#
#
bufferRow - A {Number} representing a buffer row
#
#
Returns a {Number}.
screenRowForBufferRow
:
(
bufferRow
)
->
if
@largeFileMode
bufferRow
else
@rowMap
.
screenRowRangeForBufferRow
(bufferRow)[
0
]
lastScreenRowForBufferRow
:
(
bufferRow
)
->
if
@largeFileMode
bufferRow
else
@rowMap
.
screenRowRangeForBufferRow
(bufferRow)[
1
]
-
1
#
Given a screen row, this converts it into a buffer row.
#
#
screenRow - A {Number} representing a screen row
#
#
Returns a {Number}.
bufferRowForScreenRow
:
(
screenRow
)
->
if
@largeFileMode
screenRow
else
@rowMap
.
bufferRowRangeForScreenRow
(screenRow)[
0
]
#
Given a buffer range, this converts it into a screen position.
#
#
bufferRange - The {Range} to convert
#
#
Returns a {Range}.
screenRangeForBufferRange
:
(
bufferRange
,
options
)
->
bufferRange
=
Range
.
fromObject
(bufferRange)
start
=
@
screenPositionForBufferPosition
(
bufferRange
.
start
, options)
end
=
@
screenPositionForBufferPosition
(
bufferRange
.
end
, options)
new
Range
(start, end)
#
Given a screen range, this converts it into a buffer position.
#
#
screenRange - The {Range} to convert
#
#
Returns a {Range}.
bufferRangeForScreenRange
:
(
screenRange
)
->
screenRange
=
Range
.
fromObject
(screenRange)
start
=
@
bufferPositionForScreenPosition
(
screenRange
.
start
)
end
=
@
bufferPositionForScreenPosition
(
screenRange
.
end
)
new
Range
(start, end)
pixelRangeForScreenRange
:
(
screenRange
,
clip
=
true
)
->
{
start
,
end
}
=
Range
.
fromObject
(screenRange)
{
start
:
@
pixelPositionForScreenPosition
(start, clip),
end
:
@
pixelPositionForScreenPosition
(end, clip)}
pixelPositionForScreenPosition
:
(
screenPosition
,
clip
=
true
)
->
screenPosition
=
Point
.
fromObject
(screenPosition)
screenPosition
=
@
clipScreenPosition
(screenPosition)
if
clip
targetRow
=
screenPosition
.
row
targetColumn
=
screenPosition
.
column
defaultCharWidth
=
@defaultCharWidth
top
=
targetRow
*
@lineHeightInPixels
left
=
0
column
=
0
iterator
=
@
tokenizedLineForScreenRow
(targetRow).
getTokenIterator
()
while
iterator
.
next
()
charWidths
=
@
getScopedCharWidths
(
iterator
.
getScopes
())
valueIndex
=
0
value
=
iterator
.
getText
()
while
valueIndex
<
value
.
length
if
iterator
.
isPairedCharacter
()
char
=
value
charLength
=
2
valueIndex
+=
2
else
char
=
value[valueIndex]
charLength
=
1
valueIndex
++
return
{top, left}
if
column
is
targetColumn
left
+=
charWidths[char]
?
defaultCharWidth
unless
char
is
'
\0
'
column
+=
charLength
{top, left}
screenPositionForPixelPosition
:
(
pixelPosition
)
->
targetTop
=
pixelPosition
.
top
targetLeft
=
pixelPosition
.
left
defaultCharWidth
=
@defaultCharWidth
row
=
Math
.
floor
(targetTop
/
@
getLineHeightInPixels
())
targetLeft
=
0
if
row
<
0
targetLeft
=
Infinity
if
row
>
@
getLastRow
()
row
=
Math
.
min
(row,
@
getLastRow
())
row
=
Math
.
max
(
0
, row)
left
=
0
column
=
0
iterator
=
@
tokenizedLineForScreenRow
(row).
getTokenIterator
()
while
iterator
.
next
()
charWidths
=
@
getScopedCharWidths
(
iterator
.
getScopes
())
value
=
iterator
.
getText
()
valueIndex
=
0
while
valueIndex
<
value
.
length
if
iterator
.
isPairedCharacter
()
char
=
value
charLength
=
2
valueIndex
+=
2
else
char
=
value[valueIndex]
charLength
=
1
valueIndex
++
charWidth
=
charWidths[char]
?
defaultCharWidth
break
if
targetLeft
<=
left
+
(charWidth
/
2
)
left
+=
charWidth
column
+=
charLength
new
Point
(row, column)
pixelPositionForBufferPosition
:
(
bufferPosition
)
->
@
pixelPositionForScreenPosition
(
@
screenPositionForBufferPosition
(bufferPosition))
#
Gets the number of screen lines.
#
#
Returns a {Number}.
getLineCount
:
->
if
@largeFileMode
@tokenizedBuffer
.
getLineCount
()
else
@screenLines
.
length
#
Gets the number of the last screen line.
#
#
Returns a {Number}.
getLastRow
:
->
@
getLineCount
()
-
1
#
Gets the length of the longest screen line.
#
#
Returns a {Number}.
getMaxLineLength
:
->
@maxLineLength
#
Gets the row number of the longest screen line.
#
#
Return a {}
getLongestScreenRow
:
->
@longestScreenRow
#
Given a buffer position, this converts it into a screen position.
#
#
bufferPosition - An object that represents a buffer position. It can be either
#
an {Object} (`{row, column}`), {Array} (`[row, column]`), or {Point}
#
options - A hash of options with the following keys:
#
wrapBeyondNewlines:
#
wrapAtSoftNewlines:
#
#
Returns a {Point}.
screenPositionForBufferPosition
:
(
bufferPosition
,
options
)
->
throw
new
Error
(
"
This TextEditor has been destroyed
"
)
if
@
isDestroyed
()
{
row
,
column
}
=
@buffer
.
clipPosition
(bufferPosition)
[
startScreenRow
,
endScreenRow
]
=
@rowMap
.
screenRowRangeForBufferRow
(row)
for
screenRow
in
[startScreenRow
...
endScreenRow]
screenLine
=
@
tokenizedLineForScreenRow
(screenRow)
unless
screenLine
?
throw
new
BufferToScreenConversionError
"
No screen line exists when converting buffer row to screen row
"
,
softWrapEnabled
:
@
isSoftWrapped
()
foldCount
:
@
findFoldMarkers
().
length
lastBufferRow
:
@buffer
.
getLastRow
()
lastScreenRow
:
@
getLastRow
()
maxBufferColumn
=
screenLine
.
getMaxBufferColumn
()
if
screenLine
.
isSoftWrapped
()
and
column
>
maxBufferColumn
continue
else
if
column
<=
maxBufferColumn
screenColumn
=
screenLine
.
screenColumnForBufferColumn
(column)
else
screenColumn
=
Infinity
break
@
clipScreenPosition
([screenRow, screenColumn], options)
#
Given a buffer position, this converts it into a screen position.
#
#
screenPosition - An object that represents a buffer position. It can be either
#
an {Object} (`{row, column}`), {Array} (`[row, column]`), or {Point}
#
options - A hash of options with the following keys:
#
wrapBeyondNewlines:
#
wrapAtSoftNewlines:
#
#
Returns a {Point}.
bufferPositionForScreenPosition
:
(
screenPosition
,
options
)
->
{
row
,
column
}
=
@
clipScreenPosition
(
Point
.
fromObject
(screenPosition), options)
[
bufferRow
]
=
@rowMap
.
bufferRowRangeForScreenRow
(row)
new
Point
(bufferRow,
@
tokenizedLineForScreenRow
(row).
bufferColumnForScreenColumn
(column))
#
Retrieves the grammar's token scopeDescriptor for a buffer position.
#
#
bufferPosition - A {Point} in the {TextBuffer}
#
#
Returns a {ScopeDescriptor}.
scopeDescriptorForBufferPosition
:
(
bufferPosition
)
->
@tokenizedBuffer
.
scopeDescriptorForPosition
(bufferPosition)
bufferRangeForScopeAtPosition
:
(
selector
,
position
)
->
@tokenizedBuffer
.
bufferRangeForScopeAtPosition
(selector, position)
#
Retrieves the grammar's token for a buffer position.
#
#
bufferPosition - A {Point} in the {TextBuffer}.
#
#
Returns a {Token}.
tokenForBufferPosition
:
(
bufferPosition
)
->
@tokenizedBuffer
.
tokenForPosition
(bufferPosition)
#
Get the grammar for this buffer.
#
#
Returns the current {Grammar} or the {NullGrammar}.
getGrammar
:
->
@tokenizedBuffer
.
grammar
#
Sets the grammar for the buffer.
#
#
grammar - Sets the new grammar rules
setGrammar
:
(
grammar
)
->
@tokenizedBuffer
.
setGrammar
(grammar)
#
Reloads the current grammar.
reloadGrammar
:
->
@tokenizedBuffer
.
reloadGrammar
()
#
Given a position, this clips it to a real position.
#
#
For example, if `position`'s row exceeds the row count of the buffer,
#
or if its column goes beyond a line's length, this "sanitizes" the value
#
to a real position.
#
#
position - The {Point} to clip
#
options - A hash with the following values:
#
wrapBeyondNewlines: if `true`, continues wrapping past newlines
#
wrapAtSoftNewlines: if `true`, continues wrapping past soft newlines
#
skipSoftWrapIndentation: if `true`, skips soft wrap indentation without wrapping to the previous line
#
screenLine: if `true`, indicates that you're using a line number, not a row number
#
#
Returns the new, clipped {Point}. Note that this could be the same as `position` if no clipping was performed.
clipScreenPosition
:
(
screenPosition
,
options
=
{})
->
{
wrapBeyondNewlines
,
wrapAtSoftNewlines
,
skipSoftWrapIndentation
}
=
options
{
row
,
column
}
=
Point
.
fromObject
(screenPosition)
if
row
<
0
row
=
0
column
=
0
else
if
row
>
@
getLastRow
()
row
=
@
getLastRow
()
column
=
Infinity
else
if
column
<
0
column
=
0
screenLine
=
@
tokenizedLineForScreenRow
(row)
maxScreenColumn
=
screenLine
.
getMaxScreenColumn
()
if
screenLine
.
isSoftWrapped
()
and
column
>=
maxScreenColumn
if
wrapAtSoftNewlines
row
++
column
=
@
tokenizedLineForScreenRow
(row).
clipScreenColumn
(
0
)
else
column
=
screenLine
.
clipScreenColumn
(maxScreenColumn
-
1
)
else
if
screenLine
.
isColumnInsideSoftWrapIndentation
(column)
if
skipSoftWrapIndentation
column
=
screenLine
.
clipScreenColumn
(
0
)
else
row
--
column
=
@
tokenizedLineForScreenRow
(row).
getMaxScreenColumn
()
-
1
else
if
wrapBeyondNewlines
and
column
>
maxScreenColumn
and
row
<
@
getLastRow
()
row
++
column
=
0
else
column
=
screenLine
.
clipScreenColumn
(column, options)
new
Point
(row, column)
#
Clip the start and end of the given range to valid positions on screen.
#
See {::clipScreenPosition} for more information.
#
#
* `range` The {Range} to clip.
#
* `options` (optional) See {::clipScreenPosition} `options`.
#
Returns a {Range}.
clipScreenRange
:
(
range
,
options
)
->
start
=
@
clipScreenPosition
(
range
.
start
, options)
end
=
@
clipScreenPosition
(
range
.
end
, options)
new
Range
(start, end)
#
Calculates a {Range} representing the start of the {TextBuffer} until the end.
#
#
Returns a {Range}.
rangeForAllLines
:
->
new
Range
([
0
,
0
],
@
clipScreenPosition
([
Infinity
,
Infinity
]))
decorationForId
:
(
id
)
->
@decorationsById
[id]
getDecorations
:
(
propertyFilter
)
->
allDecorations
=
[]
for
markerId, decorations
of
@decorationsByMarkerId
allDecorations
.
push
(decorations
...
)
if
decorations
?
if
propertyFilter
?
allDecorations
=
allDecorations
.
filter
(decoration)
->
for
key, value
of
propertyFilter
return
false
unless
decoration
.
properties
[key]
is
value
true
allDecorations
getLineDecorations
:
(
propertyFilter
)
->
@
getDecorations
(propertyFilter).
filter
(decoration)
->
decoration
.
isType
(
'
line
'
)
getLineNumberDecorations
:
(
propertyFilter
)
->
@
getDecorations
(propertyFilter).
filter
(decoration)
->
decoration
.
isType
(
'
line-number
'
)
getHighlightDecorations
:
(
propertyFilter
)
->
@
getDecorations
(propertyFilter).
filter
(decoration)
->
decoration
.
isType
(
'
highlight
'
)
getOverlayDecorations
:
(
propertyFilter
)
->
result
=
[]
for
id, decoration
of
@overlayDecorationsById
result
.
push
(decoration)
if
propertyFilter
?
result
.
filter
(decoration)
->
for
key, value
of
propertyFilter
return
false
unless
decoration
.
properties
[key]
is
value
true
else
result
decorationsForScreenRowRange
:
(
startScreenRow
,
endScreenRow
)
->
decorationsByMarkerId
=
{}
for
marker
in
@
findMarkers
(
intersectsScreenRowRange
:
[startScreenRow, endScreenRow])
if
decorations
=
@decorationsByMarkerId
[
marker
.
id
]
decorationsByMarkerId[
marker
.
id
]
=
decorations
decorationsByMarkerId
decorateMarker
:
(
marker
,
decorationParams
)
->
marker
=
@
getMarker
(
marker
.
id
)
decoration
=
new
Decoration
(marker,
this
, decorationParams)
decorationDestroyedDisposable
=
decoration
.
onDidDestroy
=>
@
removeDecoration
(decoration)
@disposables
.
remove
(decorationDestroyedDisposable)
@disposables
.
add
(decorationDestroyedDisposable)
@decorationsByMarkerId
[
marker
.
id
]
?=
[]
@decorationsByMarkerId
[
marker
.
id
].
push
(decoration)
@overlayDecorationsById
[
decoration
.
id
]
=
decoration
if
decoration
.
isType
(
'
overlay
'
)
@decorationsById
[
decoration
.
id
]
=
decoration
@
emit
'
decoration-added
'
, decoration
if
Grim
.
includeDeprecatedAPIs
@emitter
.
emit
'
did-add-decoration
'
, decoration
decoration
removeDecoration
:
(
decoration
)
->
{
marker
}
=
decoration
return
unless
decorations
=
@decorationsByMarkerId
[
marker
.
id
]
index
=
decorations
.
indexOf
(decoration)
if
index
>
-
1
decorations
.
splice
(index,
1
)
delete
@decorationsById
[
decoration
.
id
]
@
emit
'
decoration-removed
'
, decoration
if
Grim
.
includeDeprecatedAPIs
@emitter
.
emit
'
did-remove-decoration
'
, decoration
delete
@decorationsByMarkerId
[
marker
.
id
]
if
decorations
.
length
is
0
delete
@overlayDecorationsById
[
decoration
.
id
]
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL