@@ -10,44 +10,42 @@ extension STTextView {
1010 contentView. scroll ( point. applying ( . init( translationX: - ( gutterView? . frame. width ?? 0 ) , y: 0 ) ) )
1111 }
1212
13+ /// Adjusts a rect for scrolling by adding line fragment padding and gutter offset.
14+ private func adjustedScrollRect( _ rect: CGRect ) -> CGRect {
15+ var adjusted = rect
16+ if adjusted. width. isZero {
17+ adjusted = adjusted. inset ( by: . init( top: 0 , left: - textContainer. lineFragmentPadding, bottom: 0 , right: - textContainer. lineFragmentPadding) )
18+ }
19+ adjusted. origin. x -= gutterView? . frame. width ?? 0
20+ adjusted. size. width += gutterView? . frame. width ?? 0
21+ return adjusted
22+ }
23+
1324 @discardableResult
1425 func scrollToVisible( _ textRange: NSTextRange , type: NSTextLayoutManager . SegmentType ) -> Bool {
15- guard var rect = textLayoutManager. textSegmentFrame ( in: textRange, type: type) else {
26+ guard let rect = textLayoutManager. textSegmentFrame ( in: textRange, type: type) else {
1627 return false
1728 }
29+ return contentView. scrollToVisible ( adjustedScrollRect ( rect) )
30+ }
1831
19- if rect . width . isZero {
20- // add padding around the point to ensure the visibility the segment
21- // since the width of the segment is 0 for a selection
22- rect = rect . inset ( by : . init ( top : 0 , left : - textContainer . lineFragmentPadding , bottom : 0 , right : - textContainer . lineFragmentPadding ) )
32+ @ discardableResult
33+ func scrollToVisible ( _ location : NSTextLocation , type : NSTextLayoutManager . SegmentType ) -> Bool {
34+ guard let rect = textLayoutManager . textSegmentFrame ( at : location , type : type ) else {
35+ return false
2336 }
24-
25- // scroll to visible IN clip view (ignoring gutter view overlay)
26- // adjust rect to mimick it's size to include gutter overlay
27- rect. origin. x -= gutterView? . frame. width ?? 0
28- rect. size. width += gutterView? . frame. width ?? 0
29- return contentView. scrollToVisible ( rect)
37+ return contentView. scrollToVisible ( adjustedScrollRect ( rect) )
3038 }
3139
3240 override open func centerSelectionInVisibleArea( _ sender: Any ? ) {
3341 guard let selectionTextRange = textLayoutManager. textSelections. last? . textRanges. last,
34- var rect = textLayoutManager. textSegmentFrame ( in: selectionTextRange, type: . selection) else {
42+ let rect = textLayoutManager. textSegmentFrame ( in: selectionTextRange, type: . selection) else {
3543 return
3644 }
3745
38- if rect. width. isZero {
39- // add padding around the point to ensure the visibility the segment
40- // since the width of the segment is 0 for a selection
41- rect = rect. inset ( by: . init( top: 0 , left: - textContainer. lineFragmentPadding, bottom: 0 , right: - textContainer. lineFragmentPadding) )
42- }
43-
44- // scroll to visible IN clip view (ignoring gutter view overlay)
45- // adjust rect to mimick it's size to include gutter overlay
46- rect. origin. x -= gutterView? . frame. width ?? 0
47- rect. size. width += gutterView? . frame. width ?? 0
48-
46+ let adjusted = adjustedScrollRect ( rect)
4947 // put rect origin in the center
50- contentView. scroll ( rect . origin. applying ( . init( translationX: 0 , y: - visibleRect. height / 2 ) ) )
48+ contentView. scroll ( adjusted . origin. applying ( . init( translationX: 0 , y: - visibleRect. height / 2 ) ) )
5149 }
5250
5351 override open func pageUp( _ sender: Any ? ) {
0 commit comments