@@ -142,10 +142,7 @@ - (void)setVisible:(BOOL)visible
142142
143143 if (visible) {
144144 [self updateOffset ];
145-
146145 [self .textView.layoutManager setDelegate: self ];
147- } else {
148- [self .textView.layoutManager setDelegate: nil ];
149146 }
150147}
151148
@@ -161,42 +158,43 @@ - (NSDictionary *)layoutManager:(NSLayoutManager *)layoutManager
161158 return nil ;
162159 }
163160
164- // Delay invalidation for performance reasons.
161+ // Delay invalidation for performance reasons and attempt a full range invalidation later
165162 if (!self.shouldAllowFullSyntaxHighlight ) {
163+ [NSObject cancelPreviousPerformRequestsWithTarget: self selector: @selector (invalidateVisibleMinimapRange ) object: nil ];
164+ [self performSelector: @selector (invalidateVisibleMinimapRange ) withObject: nil afterDelay: kDurationBetweenInvalidations ];
166165
167- // Attempt a full range invalidation after
168- __weak typeof (self) weakSelf = self;
169- void (^invalidationBlock)() = ^{
170- weakSelf.shouldAllowFullSyntaxHighlight = YES ;
171- NSRange visibleMinimapRange = [weakSelf.textView visibleCharacterRange ];
172- [weakSelf.textView.layoutManager invalidateDisplayForCharacterRange: visibleMinimapRange];
173- };
174-
175- [self performBlock: invalidationBlock afterDelay: kDurationBetweenInvalidations cancelPreviousRequest: YES ];
176-
177- return @{NSForegroundColorAttributeName : [self .theme sourcePlainTextColor ]};
166+ return @{NSForegroundColorAttributeName : self.theme .sourcePlainTextColor };
178167 }
168+
169+ // Set background colors for comments and preprocessor directives
170+ short nodeType = [(DVTTextStorage *)[self .textView textStorage ] nodeTypeAtCharacterIndex: charIndex
171+ effectiveRange: effectiveCharRange
172+ context: self .editorTextView.syntaxColoringContext];
179173
180- // Rely on the colorAtCharacterIndex: method to update the effective range
181- NSColor *color = [(DVTTextStorage *)[self .editorTextView textStorage ] colorAtCharacterIndex: charIndex effectiveRange: effectiveCharRange context: nil ];
182-
183- // Background color for comments and preprocessor directives. Could query for nodeTypeAtCharacterIndex: but it's too slow.
184- DVTPointerArray *editorColors = [[DVTFontAndColorTheme currentTheme ] syntaxColorsByNodeType ];
185- if ([color isEqual: [editorColors pointerAtIndex: [DVTSourceNodeTypes registerNodeTypeNamed: kXcodeSyntaxCommentNodeName ]]] ||
186- [color isEqual: [editorColors pointerAtIndex: [DVTSourceNodeTypes registerNodeTypeNamed: kXcodeSyntaxCommentDocNodeName ]]] ||
187- [color isEqual: [editorColors pointerAtIndex: [DVTSourceNodeTypes registerNodeTypeNamed: kXcodeSyntaxCommentDocKeywordNodeName ]]])
174+ if (nodeType == [DVTSourceNodeTypes registerNodeTypeNamed: kXcodeSyntaxCommentNodeName ] ||
175+ nodeType == [DVTSourceNodeTypes registerNodeTypeNamed: kXcodeSyntaxCommentDocNodeName ] ||
176+ nodeType == [DVTSourceNodeTypes registerNodeTypeNamed: kXcodeSyntaxCommentDocKeywordNodeName ])
188177 {
189- return @{NSForegroundColorAttributeName : [ self .theme sourceTextBackgroundColor ] , NSBackgroundColorAttributeName : self.commentColor };
190- } else if ([color isEqual: [editorColors pointerAtIndex: [DVTSourceNodeTypes registerNodeTypeNamed: kXcodeSyntaxPreprocessorNodeName ]] ]) {
191- return @{NSForegroundColorAttributeName : [ self .theme sourceTextBackgroundColor ] , NSBackgroundColorAttributeName : self.preprocessorColor };
178+ return @{NSForegroundColorAttributeName : self.theme . sourceTextBackgroundColor , NSBackgroundColorAttributeName : self.commentColor };
179+ } else if (nodeType == [DVTSourceNodeTypes registerNodeTypeNamed: kXcodeSyntaxPreprocessorNodeName ]) {
180+ return @{NSForegroundColorAttributeName : self.theme . sourceTextBackgroundColor , NSBackgroundColorAttributeName : self.preprocessorColor };
192181 }
193182
194- DVTPointerArray *minimapColors = [self .theme syntaxColorsByNodeType ];
195- color = [minimapColors pointerAtIndex: [editorColors indexOfPointerIdenticalTo: color]];
183+ NSColor *color = [self .theme.syntaxColorsByNodeType pointerAtIndex: nodeType];
184+ if (color == nil ) {
185+ color = self.theme .sourcePlainTextColor ;
186+ }
196187
197188 return @{NSForegroundColorAttributeName : color};
198189}
199190
191+ - (void )invalidateVisibleMinimapRange
192+ {
193+ self.shouldAllowFullSyntaxHighlight = YES ;
194+ NSRange visibleMinimapRange = [self .textView visibleCharacterRange ];
195+ [self .textView.layoutManager invalidateDisplayForCharacterRange: visibleMinimapRange];
196+ }
197+
200198- (void )layoutManager : (NSLayoutManager *)layoutManager didCompleteLayoutForTextContainer : (NSTextContainer *)textContainer atEnd : (BOOL )layoutFinishedFlag
201199{
202200 self.shouldAllowFullSyntaxHighlight = NO ;
0 commit comments