-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSCKSyntaxHighlighter.m
More file actions
101 lines (87 loc) · 3.42 KB
/
SCKSyntaxHighlighter.m
File metadata and controls
101 lines (87 loc) · 3.42 KB
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
#import "SCKSyntaxHighlighter.h"
#import <Cocoa/Cocoa.h>
#import "SCKTextTypes.h"
#include <time.h>
static NSDictionary *noAttributes;
@implementation SCKSyntaxHighlighter
@synthesize tokenAttributes, semanticAttributes;
+ (void)initialize
{
static dispatch_once_t once = 0L;
dispatch_once(&once, ^{
noAttributes = [NSDictionary dictionary];
});
}
- (id)init
{
self = [super init];
NSDictionary *comment = @{NSForegroundColorAttributeName: [NSColor grayColor]};
NSDictionary *keyword = @{NSForegroundColorAttributeName: [NSColor redColor]};
NSDictionary *literal = @{NSForegroundColorAttributeName: [NSColor redColor]};
tokenAttributes = [@{
SCKTextTokenTypeComment: comment,
SCKTextTokenTypePunctuation: noAttributes,
SCKTextTokenTypeKeyword: keyword,
SCKTextTokenTypeLiteral: literal}
mutableCopy];
semanticAttributes = [@{
SCKTextTypeDeclRef: @{NSForegroundColorAttributeName: [NSColor blueColor]},
SCKTextTypeMessageSend: @{NSForegroundColorAttributeName: [NSColor brownColor]},
SCKTextTypeDeclaration: @{NSForegroundColorAttributeName: [NSColor greenColor]},
SCKTextTypeMacroInstantiation: @{NSForegroundColorAttributeName: [NSColor magentaColor]},
SCKTextTypeMacroDefinition: @{NSForegroundColorAttributeName: [NSColor magentaColor]},
SCKTextTypePreprocessorDirective: @{NSForegroundColorAttributeName: [NSColor orangeColor]},
SCKTextTypeReference: @{NSForegroundColorAttributeName: [NSColor purpleColor]}}
mutableCopy];
return self;
}
- (void)transformString:(NSMutableAttributedString *)source;
{
NSUInteger end = [source length];
NSUInteger i = 0;
NSRange r;
do
{
NSDictionary *attrs = [source attributesAtIndex:i
longestEffectiveRange:&r
inRange:NSMakeRange(i, end-i)];
i = r.location + r.length;
NSString *token = [attrs objectForKey:kSCKTextTokenType];
NSString *semantic = [attrs objectForKey:kSCKTextSemanticType];
NSDictionary *diagnostic = [attrs objectForKey:kSCKDiagnostic];
// Skip ranges that have attributes other than semantic markup
if ((nil == semantic) && (nil == token)) continue;
if (semantic == SCKTextTypePreprocessorDirective)
{
attrs = [semanticAttributes objectForKey: semantic];
}
else if (token == nil || token != SCKTextTokenTypeIdentifier)
{
attrs = [tokenAttributes objectForKey: token];
}
else
{
NSString *semantic = [attrs objectForKey:kSCKTextSemanticType];
attrs = [semanticAttributes objectForKey:semantic];
}
if (nil == attrs)
{
attrs = noAttributes;
}
[source setAttributes:attrs range:r];
// Re-apply the diagnostic
if (nil != diagnostic)
{
[source addAttribute:NSToolTipAttributeName
value:[diagnostic objectForKey: kSCKDiagnosticText]
range:r];
[source addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber numberWithInt: NSSingleUnderlineStyle]
range:r];
[source addAttribute:NSUnderlineColorAttributeName
value:[NSColor redColor]
range:r];
}
} while (i < end);
}
@end