/
TDSourceCodeTextView.m
116 lines (88 loc) · 3.64 KB
/
TDSourceCodeTextView.m
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
//
// TDSourceCodeTextView.m
// TextTest
//
// Created by Todd Ditchendorf on 9/9/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "TDSourceCodeTextView.h"
#import "TDGutterView.h"
@interface TDSourceCodeTextView ()
- (void)registerForNotifications;
- (void)getRectsOfVisibleLines:(NSArray **)outRects startingLineNumber:(NSUInteger *)outRect;
- (NSUInteger)lineNumberForIndex:(NSUInteger)inIndex;
@end
@implementation TDSourceCodeTextView
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
self.gutterView = nil;
[super dealloc];
}
- (void)awakeFromNib {
[self registerForNotifications];
[self renderGutter];
}
- (void)textDidChange:(NSNotification *)n {
[self renderGutter];
}
- (void)viewBoundsChanged:(NSNotification *)n {
[self renderGutter];
}
- (void)renderGutter {
if (![[self window] isVisible]) return;
NSArray *rects = nil;
NSUInteger start = 0;
[self getRectsOfVisibleLines:&rects startingLineNumber:&start];
gutterView.lineNumberRects = rects;
gutterView.startLineNumber = start;
[gutterView setNeedsDisplay:YES];
}
- (void)registerForNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(textDidChange:)
name:NSTextDidChangeNotification
object:self];
[scrollView.contentView setPostsBoundsChangedNotifications:YES];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(viewBoundsChanged:)
name:NSViewBoundsDidChangeNotification
object:scrollView.contentView];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(viewBoundsChanged:)
name:NSWindowDidResizeNotification
object:self.window];
}
//- (void)showFindIndicatorForRange:(NSRange)charRange;
- (void)getRectsOfVisibleLines:(NSArray **)outRects startingLineNumber:(NSUInteger *)outStart {
NSMutableArray *result = [NSMutableArray array];
NSString *s = self.string;
NSLayoutManager *layoutMgr = self.textContainer.layoutManager;
NSRect boundingRect = [scrollView.contentView documentVisibleRect];
CGFloat scrollY = boundingRect.origin.y;
NSRange visibleGlyphRange = [layoutMgr glyphRangeForBoundingRect:boundingRect inTextContainer:self.textContainer];
NSUInteger index = visibleGlyphRange.location;
NSUInteger length = index + visibleGlyphRange.length;
(*outStart) = [self lineNumberForIndex:index + 1];
while (index < length) {
NSRange r = [s lineRangeForRange:NSMakeRange(index, 0)];
index = NSMaxRange(r);
NSRect rect = [layoutMgr lineFragmentRectForGlyphAtIndex:r.location effectiveRange:NULL withoutAdditionalLayout:YES];
rect.origin.y -= scrollY;
[result addObject:[NSValue valueWithRect:rect]];
}
(*outRects) = result;
}
- (NSUInteger)lineNumberForIndex:(NSUInteger)inIndex {
NSString *s = self.string;
NSUInteger numberOfLines, index, stringLength = s.length;
for (index = 0, numberOfLines = 0; index < stringLength; numberOfLines++) {
NSRange r = [s lineRangeForRange:NSMakeRange(index, 0)];
index = NSMaxRange(r);
if (inIndex <= index) {
break;
}
}
return numberOfLines;
}
@synthesize gutterView;
@end