Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions RKSyntaxView/RKSyntaxView.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
@property (retain) NSDictionary *scheme;
@property (retain) NSDictionary *syntax;

@property (nonatomic, retain) NSString* defaultSchemePath;
@property (nonatomic, retain) NSString* defaultSyntaxPath;


- (id) initWithDefaultSchemePath:(NSString *)schemePath andSyntaxPath:(NSString *)syntaxPath; // do not use with Nibs

- (void) _setup;

#pragma mark - Handling text change
Expand All @@ -47,4 +53,7 @@
- (void) _setBackgroundColor:(NSColor *)color range:(NSRange)range content:(NSMutableAttributedString *)content;
- (void) _setFont:(NSFont *)font range:(NSRange)range content:(NSMutableAttributedString *)content;




@end
37 changes: 32 additions & 5 deletions RKSyntaxView/RKSyntaxView.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ @implementation RKSyntaxView

@synthesize scheme=_scheme;
@synthesize syntax=_syntax;
@synthesize defaultSchemePath, defaultSyntaxPath;

#pragma mark - Lifecycle

Expand All @@ -23,19 +24,32 @@ - (id)init {
return self;
}

- (id) initWithDefaultSchemePath:(NSString *)schemePath andSyntaxPath:(NSString *)syntaxPath {
self = [super init];
if (self) {
[self setDefaultSchemePath:schemePath];
[self setDefaultSyntaxPath:syntaxPath];
[self loadScheme:[self defaultSchemePath]];
[self loadSyntax:[self defaultSyntaxPath]];
[self _setup];
}
return self;
}

- (void)awakeFromNib {
[self _setup];
}

- (void)_setup {
[self setTextContainerInset:NSMakeSize(10.0, 10.0)];
[self highlight];

[self addObserver:self forKeyPath:@"string" options:0 context:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_textDidChange:) name:NSTextDidChangeNotification object:self];
}

- (void) dealloc {
[defaultSyntaxPath release];
[defaultSchemePath release];
[super dealloc];
}

Expand All @@ -52,11 +66,22 @@ - (void)_textDidChange:(NSNotification *)notif {
#pragma mark - Scheme

- (void)loadScheme:(NSString *)schemeFilename {
NSString *schemePath = [[NSBundle mainBundle] pathForResource:schemeFilename ofType:@"plist" inDirectory:nil];
self.scheme = [NSDictionary dictionaryWithContentsOfFile:schemePath];
if (schemeFilename) {
NSString *schemePath = [[NSBundle mainBundle] pathForResource:schemeFilename ofType:@"plist" inDirectory:nil];
self.scheme = [NSDictionary dictionaryWithContentsOfFile:schemePath];
}
}

- (NSColor *) _colorFor:(NSString *)key {
if (![self scheme] || ![self syntax]) {
if ([self defaultSyntaxPath] && [self defaultSchemePath]) {
[self loadSyntax:[self defaultSyntaxPath]];
[self loadScheme:[self defaultSchemePath]];
} else {
[self loadScheme:@"PageScheme"];
[self loadSyntax:@"PageSyntax"];
}
}
NSString *colorCode = [[self.scheme objectForKey:@"colors"] objectForKey:key];
if (!colorCode) return nil;
NSColor *color = [NSColor colorFromHexRGB:colorCode];
Expand Down Expand Up @@ -90,8 +115,10 @@ - (NSInteger) _defaultSize {
#pragma mark - Syntax

- (void)loadSyntax:(NSString *)syntaxFilename {
NSString *schemePath = [[NSBundle mainBundle] pathForResource:syntaxFilename ofType:@"plist" inDirectory:nil];
self.syntax = [NSDictionary dictionaryWithContentsOfFile:schemePath];
if (syntaxFilename) {
NSString *schemePath = [[NSBundle mainBundle] pathForResource:syntaxFilename ofType:@"plist" inDirectory:nil];
self.syntax = [NSDictionary dictionaryWithContentsOfFile:schemePath];
}
}

#pragma mark - Highlighting
Expand Down