1818#import " DVTPointerArray.h"
1919#import " DVTSourceTextView.h"
2020#import " DVTSourceNodeTypes.h"
21+
2122#import " DVTFontAndColorTheme.h"
23+ #import " DVTPreferenceSetManager.h"
2224
2325const CGFloat kBackgroundColorShadowLevel = 0 .1f ;
2426const CGFloat kHighlightColorAlphaLevel = 0 .3f ;
@@ -48,6 +50,8 @@ @interface SCXcodeMinimapView () <NSLayoutManagerDelegate>
4850@property (nonatomic , strong ) NSColor *commentColor;
4951@property (nonatomic , strong ) NSColor *preprocessorColor;
5052
53+ @property (nonatomic , strong ) DVTFontAndColorTheme *theme;
54+
5155@end
5256
5357@implementation SCXcodeMinimapView
@@ -79,7 +83,7 @@ - (instancetype)initWithFrame:(NSRect)frame editor:(IDESourceCodeEditor *)editor
7983 [self addSubview: self .scrollView];
8084
8185 self.textView = [[DVTSourceTextView alloc ] initWithFrame: self .editorTextView.bounds];
82- [self .editorTextView.textStorage addLayoutManager :self .textView.layoutManager ];
86+ [self .textView setTextStorage :self .editorTextView.textStorage ];
8387 [self .textView setEditable: NO ];
8488 [self .textView setSelectable: NO ];
8589
@@ -154,40 +158,47 @@ - (NSDictionary *)layoutManager:(NSLayoutManager *)layoutManager
154158
155159 // Prevent full range invalidation for performance reasons.
156160 if (!self.shouldAllowFullSyntaxHighlight ) {
161+
162+ // Attempt a full range invalidation after all temporary attributes are set
163+ __weak typeof (self) weakSelf = self;
164+ void (^invalidationBlock)() = ^{
165+ weakSelf.shouldAllowFullSyntaxHighlight = YES ;
166+ NSRange visibleMinimapRange = [weakSelf.textView visibleCharacterRange ];
167+ [weakSelf.textView.layoutManager invalidateDisplayForCharacterRange: visibleMinimapRange];
168+ };
169+
157170 NSRange visibleEditorRange = [self .editorTextView visibleCharacterRange ];
158171 if (charIndex > visibleEditorRange.location + visibleEditorRange.length ) {
159172 *effectiveCharRange = NSMakeRange (visibleEditorRange.location + visibleEditorRange.length ,
160173 layoutManager.textStorage .length - visibleEditorRange.location - visibleEditorRange.length );
174+
175+ [self performBlock: invalidationBlock afterDelay: 0 .5f cancelPreviousRequest: YES ];
161176
162- return @{NSForegroundColorAttributeName : [[DVTFontAndColorTheme currentTheme ] sourcePlainTextColor ]};
177+ return @{NSForegroundColorAttributeName : [self .theme sourcePlainTextColor ]};
163178 }
164179
165180 if (charIndex < visibleEditorRange.location ) {
166181 *effectiveCharRange = NSMakeRange (0 , visibleEditorRange.location );
167- return @{NSForegroundColorAttributeName : [[DVTFontAndColorTheme currentTheme ] sourcePlainTextColor ]};
182+
183+ [self performBlock: invalidationBlock afterDelay: 0 .5f cancelPreviousRequest: YES ];
184+
185+ return @{NSForegroundColorAttributeName : [self .theme sourcePlainTextColor ]};
168186 }
169187 }
170188
171- // Attempt a full range invalidation after all temporary attributes are set
172- __weak typeof (self) weakSelf = self;
173- [self performBlock: ^{
174- weakSelf.shouldAllowFullSyntaxHighlight = YES ;
175- NSRange visibleMinimapRange = [weakSelf.textView visibleCharacterRange ];
176- [weakSelf.textView.layoutManager invalidateDisplayForCharacterRange: visibleMinimapRange];
177- } afterDelay: 0 .5f cancelPreviousRequest: YES ];
178-
179189 // Rely on the colorAtCharacterIndex: method to update the effective range
180190 DVTTextStorage *storage = [self .editorTextView textStorage ];
181191 NSColor *color = [storage colorAtCharacterIndex: charIndex effectiveRange: effectiveCharRange context: nil ];
182192
183- // Background color for comments and preprocessor directives
184- short currentNodeId = [storage nodeTypeAtCharacterIndex: charIndex effectiveRange: NULL context: nil ];
185- if (currentNodeId == [DVTSourceNodeTypes registerNodeTypeNamed: kXcodeSyntaxCommentNodeName ] ||
186- currentNodeId == [DVTSourceNodeTypes registerNodeTypeNamed: kXcodeSyntaxCommentDocNodeName ] ||
187- currentNodeId == [DVTSourceNodeTypes registerNodeTypeNamed: kXcodeSyntaxCommentDocKeywordNodeName ]) {
188- return @{NSForegroundColorAttributeName : [[DVTFontAndColorTheme currentTheme ] sourceTextBackgroundColor ], NSBackgroundColorAttributeName : self.commentColor };
189- } else if (currentNodeId == [DVTSourceNodeTypes registerNodeTypeNamed: kXcodeSyntaxPreprocessorNodeName ]) {
190- return @{NSForegroundColorAttributeName : [[DVTFontAndColorTheme currentTheme ] sourceTextBackgroundColor ], NSBackgroundColorAttributeName : self.preprocessorColor };
193+ // Background color for comments and preprocessor directives. Could query for nodeTypeAtCharacterIndex: but it's too slow.
194+ DVTPointerArray *colors = [[DVTFontAndColorTheme currentTheme ] syntaxColorsByNodeType ];
195+ if ([color isEqual: [colors pointerAtIndex: [DVTSourceNodeTypes registerNodeTypeNamed: kXcodeSyntaxCommentNodeName ]]] ||
196+ [color isEqual: [colors pointerAtIndex: [DVTSourceNodeTypes registerNodeTypeNamed: kXcodeSyntaxCommentDocNodeName ]]] ||
197+ [color isEqual: [colors pointerAtIndex: [DVTSourceNodeTypes registerNodeTypeNamed: kXcodeSyntaxCommentDocKeywordNodeName ]]])
198+ {
199+ return @{NSForegroundColorAttributeName : [self .theme sourceTextBackgroundColor ], NSBackgroundColorAttributeName : self.commentColor };
200+ } else if ([color isEqual: [colors pointerAtIndex: [DVTSourceNodeTypes registerNodeTypeNamed: kXcodeSyntaxPreprocessorNodeName ]]]) {
201+ return @{NSForegroundColorAttributeName : [self .theme sourceTextBackgroundColor ], NSBackgroundColorAttributeName : self.preprocessorColor };
191202 }
192203
193204 return @{NSForegroundColorAttributeName : color};
@@ -260,8 +271,12 @@ - (void)handleMouseEvent:(NSEvent *)theEvent
260271
261272- (void )updateTheme
262273{
263- DVTFontAndColorTheme *theme = [DVTFontAndColorTheme currentTheme ];
264- NSColor *backgroundColor = [theme.sourceTextBackgroundColor shadowWithLevel: kBackgroundColorShadowLevel ];
274+ // DVTPreferenceSetManager *preferenceSetManager = [DVTFontAndColorTheme preferenceSetsManager];
275+ // NSArray *preferenceSet = [preferenceSetManager availablePreferenceSets];
276+ // self.theme = [preferenceSet lastObject];
277+ self.theme = [DVTFontAndColorTheme currentTheme ];
278+
279+ NSColor *backgroundColor = [self .theme.sourceTextBackgroundColor shadowWithLevel: kBackgroundColorShadowLevel ];
265280
266281 [self .scrollView setBackgroundColor: backgroundColor];
267282 [self .textView setBackgroundColor: backgroundColor];
@@ -271,7 +286,7 @@ - (void)updateTheme
271286 blue: (1 .0f - [backgroundColor blueComponent ])
272287 alpha: kHighlightColorAlphaLevel ];
273288
274- DVTPointerArray *colors = [[DVTFontAndColorTheme currentTheme ] syntaxColorsByNodeType ];
289+ DVTPointerArray *colors = [self .theme syntaxColorsByNodeType ];
275290 self.commentColor = [colors pointerAtIndex: [DVTSourceNodeTypes registerNodeTypeNamed: kXcodeSyntaxCommentNodeName ]];
276291 self.commentColor = [NSColor colorWithCalibratedRed: self .commentColor.redComponent
277292 green: self .commentColor.greenComponent
@@ -280,9 +295,9 @@ - (void)updateTheme
280295
281296
282297 self.preprocessorColor = [colors pointerAtIndex: [DVTSourceNodeTypes registerNodeTypeNamed: kXcodeSyntaxPreprocessorNodeName ]];
283- self.preprocessorColor = [NSColor colorWithCalibratedRed: self .commentColor .redComponent
284- green: self .commentColor .greenComponent
285- blue: self .commentColor .blueComponent
298+ self.preprocessorColor = [NSColor colorWithCalibratedRed: self .preprocessorColor .redComponent
299+ green: self .preprocessorColor .greenComponent
300+ blue: self .preprocessorColor .blueComponent
286301 alpha: kHighlightColorAlphaLevel ];
287302
288303 [self .selectionView setSelectionColor: selectionColor];
0 commit comments