Skip to content

Commit 9c027b6

Browse files
author
Mário Barbosa
committed
Highlighting key code blocks for better orientation
Supporting, #pragma, #import, line comment and block comment
1 parent f8f053c commit 9c027b6

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

SCXcodeMinimap/SCMiniMapView.m

100644100755
Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,17 @@ - (void)updateTextView
206206
*stop = YES;
207207
}];
208208

209+
[weakSelf highlightMutableAttributedString:mutableAttributedString withPattern:@"^(#import.*\n)" color:[NSColor brownColor]];
210+
[weakSelf highlightMutableAttributedString:mutableAttributedString withPattern:@"^(#pragma mark.+\n)" color:[NSColor brownColor]];
211+
[weakSelf highlightMutableAttributedString:mutableAttributedString withPattern:@"^(//.*\n)" color:[NSColor greenColor]];
212+
[weakSelf highlightMutableAttributedString:mutableAttributedString withPattern:@"^(/\\*(?>(?:(?>[^*]+)|\\*(?!/))*)\\*/\n)" color:[NSColor greenColor]];
209213

210214
[style setTabStops:@[]];
211215
[style setDefaultTabInterval:style.defaultTabInterval * kDefaultZoomLevel];
212216

213-
[mutableAttributedString setAttributes:@{NSFontAttributeName: weakSelf.font, NSParagraphStyleAttributeName : style} range:NSMakeRange(0, mutableAttributedString.length)];
217+
[mutableAttributedString addAttribute:NSFontAttributeName value:weakSelf.font range:NSMakeRange(0, mutableAttributedString.length)];
218+
[mutableAttributedString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, mutableAttributedString.length)];
219+
214220

215221
//Send the text storage update off to the main thread
216222
dispatch_async(dispatch_get_main_queue(), ^{
@@ -223,6 +229,21 @@ - (void)updateTextView
223229
});
224230
}
225231

232+
- (void)highlightMutableAttributedString:(NSMutableAttributedString *)attributedString withPattern:(NSString *)pattern color:(NSColor *)color
233+
{
234+
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
235+
options:NSRegularExpressionCaseInsensitive|NSRegularExpressionAnchorsMatchLines error:nil];
236+
237+
[regex enumerateMatchesInString:[attributedString string]
238+
options:0
239+
range:NSMakeRange(0, attributedString.length)
240+
usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop)
241+
{
242+
[attributedString addAttribute:NSBackgroundColorAttributeName value:color?:color range:[match rangeAtIndex:1]];
243+
}];
244+
245+
}
246+
226247
- (void)calculateLinesFromString:(NSString *)string
227248
{
228249
NSInteger count = 0;

0 commit comments

Comments
 (0)