Skip to content

Commit 6e39987

Browse files
committed
Added code folding support (#19 #28)
1 parent 82f8cb2 commit 6e39987

File tree

1 file changed

+38
-21
lines changed

1 file changed

+38
-21
lines changed

SCXcodeMinimap/SCXcodeMinimapView.m

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#import "DVTFontAndColorTheme.h"
2323
#import "DVTPreferenceSetManager.h"
2424

25+
#import "DVTFoldingManager.h"
26+
2527
const CGFloat kBackgroundColorShadowLevel = 0.1f;
2628
const CGFloat kHighlightColorAlphaLevel = 0.3f;
2729
const CGFloat kDurationBetweenInvalidations = 0.5f;
@@ -35,7 +37,7 @@
3537
static NSString * const IDESourceCodeEditorTextViewBoundsDidChangeNotification = @"IDESourceCodeEditorTextViewBoundsDidChangeNotification";
3638
static NSString * const DVTFontAndColorSourceTextSettingsChangedNotification = @"DVTFontAndColorSourceTextSettingsChangedNotification";
3739

38-
@interface SCXcodeMinimapView () <NSLayoutManagerDelegate>
40+
@interface SCXcodeMinimapView () <NSLayoutManagerDelegate, DVTFoldingManagerDelegate>
3941

4042
@property (nonatomic, strong) IDESourceCodeEditor *editor;
4143
@property (nonatomic, strong) NSScrollView *editorScrollView;
@@ -67,8 +69,11 @@ - (instancetype)initWithFrame:(NSRect)frame editor:(IDESourceCodeEditor *)editor
6769
if (self = [super initWithFrame:frame])
6870
{
6971
self.editor = editor;
72+
7073
self.editorScrollView = editor.scrollView;
74+
7175
self.editorTextView = editor.textView;
76+
[self.editorTextView.foldingManager setDelegate:self];
7277

7378
[self setWantsLayer:YES];
7479
[self setAutoresizingMask:NSViewMinXMargin | NSViewHeightSizable];
@@ -106,11 +111,11 @@ - (instancetype)initWithFrame:(NSRect)frame editor:(IDESourceCodeEditor *)editor
106111
}];
107112

108113
[[NSNotificationCenter defaultCenter] addObserverForName:SCXcodeMinimapHighlightCommentsChangeNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
109-
[weakSelf invalidateVisibleMinimapRange];
114+
[weakSelf invalidateDisplayForVisibleMinimapRange];
110115
}];
111116

112117
[[NSNotificationCenter defaultCenter] addObserverForName:SCXcodeMinimapHighlightPreprocessorChangeNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
113-
[weakSelf invalidateVisibleMinimapRange];
118+
[weakSelf invalidateDisplayForVisibleMinimapRange];
114119
}];
115120

116121
[[NSNotificationCenter defaultCenter] addObserverForName:SCXcodeMinimapHideEditorScrollerChangeNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
@@ -165,8 +170,8 @@ - (NSDictionary *)layoutManager:(NSLayoutManager *)layoutManager
165170

166171
// Delay invalidation for performance reasons and attempt a full range invalidation later
167172
if(!self.shouldAllowFullSyntaxHighlight) {
168-
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(invalidateVisibleMinimapRange) object:nil];
169-
[self performSelector:@selector(invalidateVisibleMinimapRange) withObject:nil afterDelay:kDurationBetweenInvalidations];
173+
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(invalidateDisplayForVisibleMinimapRange) object:nil];
174+
[self performSelector:@selector(invalidateDisplayForVisibleMinimapRange) withObject:nil afterDelay:kDurationBetweenInvalidations];
170175

171176
return @{NSForegroundColorAttributeName : self.theme.sourcePlainTextColor};
172177
}
@@ -201,16 +206,29 @@ - (NSDictionary *)layoutManager:(NSLayoutManager *)layoutManager
201206
return @{NSForegroundColorAttributeName : color};
202207
}
203208

204-
- (void)invalidateVisibleMinimapRange
209+
- (void)layoutManager:(NSLayoutManager *)layoutManager didCompleteLayoutForTextContainer:(NSTextContainer *)textContainer atEnd:(BOOL)layoutFinishedFlag
205210
{
206-
self.shouldAllowFullSyntaxHighlight = YES;
207-
NSRange visibleMinimapRange = [self.textView visibleCharacterRange];
208-
[self.textView.layoutManager invalidateDisplayForCharacterRange:visibleMinimapRange];
211+
self.shouldAllowFullSyntaxHighlight = NO;
209212
}
210213

211-
- (void)layoutManager:(NSLayoutManager *)layoutManager didCompleteLayoutForTextContainer:(NSTextContainer *)textContainer atEnd:(BOOL)layoutFinishedFlag
214+
#pragma mark - DVTFoldingManagerDelegate
215+
216+
- (void)foldingManager:(DVTFoldingManager *)foldingManager didFoldRange:(NSRange)range
212217
{
213-
self.shouldAllowFullSyntaxHighlight = NO;
218+
[(DVTLayoutManager *)self.editorTextView.layoutManager foldingManager:foldingManager didFoldRange:range];
219+
220+
[self.textView.foldingManager foldRange:range];
221+
222+
[self invalidateLayoutForVisibleMinimapRange];
223+
}
224+
225+
- (void)foldingManager:(DVTFoldingManager *)foldingManager didUnfoldRange:(NSRange)range
226+
{
227+
[(DVTLayoutManager *)self.editorTextView.layoutManager foldingManager:foldingManager didUnfoldRange:range];
228+
229+
[self.textView.foldingManager unfoldRange:range];
230+
231+
[self invalidateLayoutForVisibleMinimapRange];
214232
}
215233

216234
#pragma mark - Navigation
@@ -326,18 +344,17 @@ - (void)resizeWithOldSuperviewSize:(NSSize)oldSize
326344

327345
#pragma mark - Helpers
328346

329-
- (void)performBlock:(void (^)(void))block afterDelay:(NSTimeInterval)delay cancelPreviousRequest:(BOOL)cancel {
330-
if (cancel) {
331-
[NSObject cancelPreviousPerformRequestsWithTarget:self];
332-
}
333-
334-
[self performSelector:@selector(delayedAddOperation:)
335-
withObject:[NSBlockOperation blockOperationWithBlock:block]
336-
afterDelay:delay];
347+
- (void)invalidateDisplayForVisibleMinimapRange
348+
{
349+
self.shouldAllowFullSyntaxHighlight = YES;
350+
NSRange visibleMinimapRange = [self.textView visibleCharacterRange];
351+
[self.textView.layoutManager invalidateDisplayForCharacterRange:visibleMinimapRange];
337352
}
338353

339-
- (void)delayedAddOperation:(NSOperation *)operation {
340-
[[NSOperationQueue currentQueue] addOperation:operation];
354+
- (void)invalidateLayoutForVisibleMinimapRange
355+
{
356+
NSRange visibleMinimapRange = [self.textView visibleCharacterRange];
357+
[self.textView.layoutManager invalidateLayoutForCharacterRange:visibleMinimapRange actualCharacterRange:nil];
341358
}
342359

343360
@end

0 commit comments

Comments
 (0)