From 61fe97828bbacf55d72594cc0f9ddc476d444bf3 Mon Sep 17 00:00:00 2001 From: dominickm Date: Fri, 22 Jun 2012 23:45:00 -0300 Subject: [PATCH 1/3] _colorFor will now check to make sure that a scheme and syntax exists; if it doesn't it will load the build in PageScheme and PageSyntax. This fixes an issue where a user might see a black (illegible) textView in certain rare cases. --- RKSyntaxView/RKSyntaxView.m | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/RKSyntaxView/RKSyntaxView.m b/RKSyntaxView/RKSyntaxView.m index d388d64..4fa8dd6 100644 --- a/RKSyntaxView/RKSyntaxView.m +++ b/RKSyntaxView/RKSyntaxView.m @@ -30,7 +30,6 @@ - (void)awakeFromNib { - (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]; } @@ -57,6 +56,10 @@ - (void)loadScheme:(NSString *)schemeFilename { } - (NSColor *) _colorFor:(NSString *)key { + if (![self scheme]) { + [self loadScheme:@"PageScheme"]; + [self loadSyntax:@"PageSyntax"]; + } NSString *colorCode = [[self.scheme objectForKey:@"colors"] objectForKey:key]; if (!colorCode) return nil; NSColor *color = [NSColor colorFromHexRGB:colorCode]; From 00a7048d7d42c296b33c1d595e2dca2be756e577 Mon Sep 17 00:00:00 2001 From: dominickm Date: Sat, 23 Jun 2012 00:16:40 -0300 Subject: [PATCH 2/3] Adding custom init for creating a syntactView with defaults, however, this does not work with Nibs --- RKSyntaxView/RKSyntaxView.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/RKSyntaxView/RKSyntaxView.h b/RKSyntaxView/RKSyntaxView.h index 3047aa0..9d26e18 100644 --- a/RKSyntaxView/RKSyntaxView.h +++ b/RKSyntaxView/RKSyntaxView.h @@ -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 @@ -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 From ef0946a3eb00849a331c247a111729f258ab4c8f Mon Sep 17 00:00:00 2001 From: dominickm Date: Sat, 23 Jun 2012 00:17:44 -0300 Subject: [PATCH 3/3] Implementation for init with defaults --- still not compatible with Nibs, however, if loaded from a Nib the previous solution will be used -- preventing a crash --- RKSyntaxView/RKSyntaxView.m | 38 ++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/RKSyntaxView/RKSyntaxView.m b/RKSyntaxView/RKSyntaxView.m index 4fa8dd6..e9a1da3 100644 --- a/RKSyntaxView/RKSyntaxView.m +++ b/RKSyntaxView/RKSyntaxView.m @@ -13,6 +13,7 @@ @implementation RKSyntaxView @synthesize scheme=_scheme; @synthesize syntax=_syntax; +@synthesize defaultSchemePath, defaultSyntaxPath; #pragma mark - Lifecycle @@ -23,6 +24,18 @@ - (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]; } @@ -35,6 +48,8 @@ - (void)_setup { } - (void) dealloc { + [defaultSyntaxPath release]; + [defaultSchemePath release]; [super dealloc]; } @@ -51,14 +66,21 @@ - (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 loadScheme:@"PageScheme"]; - [self loadSyntax:@"PageSyntax"]; + 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; @@ -93,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