Browse files

Added -textAttributesForComponentName:color: delegate method

The delegate can now provide all kinds of styles (not just color) for a component.
Also refactored styles a bit to avoid code duplication.
  • Loading branch information...
1 parent ca17d8c commit 256fabc1b7f7f35338987235b16818d61c517d79 @uliwitness committed Feb 5, 2012
Showing with 33 additions and 26 deletions.
  1. +2 −1 UKSyntaxColoredTextViewController.h
  2. +31 −25 UKSyntaxColoredTextViewController.m
View
3 UKSyntaxColoredTextViewController.h
@@ -37,7 +37,7 @@
// -----------------------------------------------------------------------------
#define TD_USER_DEFINED_IDENTIFIERS @"SyntaxColoring:UserIdentifiers" // Key in user defaults holding user-defined identifiers to colorize.
-#define TD_SYNTAX_COLORING_MODE_ATTR @"UKTextDocumentSyntaxColoringMode" // Anything we colorize gets this attribute.
+#define TD_SYNTAX_COLORING_MODE_ATTR @"UKTextDocumentSyntaxColoringMode" // Anything we colorize gets this attribute. The value is an NSString holding the component name.
@class UKSyntaxColoredTextViewController;
@@ -57,6 +57,7 @@
-(NSString*) syntaxDefinitionFilenameForTextViewController: (UKSyntaxColoredTextViewController*)sender; // Won't get called if you override syntaxDefinitionDictionaryForTextViewController:.
-(NSDictionary*) syntaxDefinitionDictionaryForTextViewController: (UKSyntaxColoredTextViewController*)sender;
-(NSArray*) userIdentifiersForKeywordComponentName: (NSString*)inModeName; // If you can parse your code & provide a list of identifiers the user uses, you can provide this method to tell the editor about them.
+-(NSDictionary*) textAttributesForComponentName: (NSString*)inModeName color: (NSColor*)inColor; // If you don't just want a color, provide an NSAttributedString attributes dictionary here.
@end
View
56 UKSyntaxColoredTextViewController.m
@@ -1034,6 +1034,32 @@ -(NSDictionary*) syntaxDefinitionDictionary
// -----------------------------------------------------------------------------
+// textAttributesForComponentName:color:
+// Return the styles to use for the given mode/color. This calls upon the
+// delegate to provide the styles, or if not, just set the color. This is
+// also responsible for setting the TD_SYNTAX_COLORING_MODE_ATTR attribute
+// so we can extend a range for partial recoloring to color the full block
+// comment or whatever which is being changed (in case the user types a
+// sequence that would end that block comment or similar).
+// -----------------------------------------------------------------------------
+
+-(NSDictionary*) textAttributesForComponentName: (NSString*)attr color: (NSColor*)col
+{
+ NSDictionary* vLocalStyles = [delegate respondsToSelector:@selector(textAttributesForComponentName:color:)] ? [delegate textAttributesForComponentName: attr color: col] : nil;
+ NSMutableDictionary*vStyles = [[[self defaultTextAttributes] mutableCopy] autorelease];
+ if( vLocalStyles )
+ [vStyles addEntriesFromDictionary: vLocalStyles];
+ else
+ [vStyles setObject: col forKey: NSForegroundColorAttributeName];
+
+ // Make sure partial recoloring works:
+ [vStyles setObject: attr forKey: TD_SYNTAX_COLORING_MODE_ATTR];
+
+ return vStyles;
+}
+
+
+// -----------------------------------------------------------------------------
// colorStringsFrom:to:inString:withColor:andMode:andEscapeChar:
// Apply syntax coloring to all strings. This is basically the same code
// as used for multi-line comments, except that it ignores the end
@@ -1045,11 +1071,7 @@ -(void) colorStringsFrom: (NSString*) startCh to: (NSString*) endCh inString: (N
{
NS_DURING
NSScanner* vScanner = [NSScanner scannerWithString: [s string]];
- NSMutableDictionary*vStyles = [[[self defaultTextAttributes] mutableCopy] autorelease];
- [vStyles addEntriesFromDictionary: [NSDictionary dictionaryWithObjectsAndKeys:
- col, NSForegroundColorAttributeName,
- attr, TD_SYNTAX_COLORING_MODE_ATTR,
- nil]];
+ NSDictionary* vStyles = [self textAttributesForComponentName: attr color: col];
BOOL vIsEndChar = NO;
unichar vEscChar = '\\';
BOOL vDelegateHandlesProgress = [delegate respondsToSelector: @selector(textViewControllerProgressedWhileSyntaxRecoloring:)];
@@ -1109,11 +1131,7 @@ -(void) colorCommentsFrom: (NSString*) startCh to: (NSString*) endCh inString: (
@try
{
NSScanner* vScanner = [NSScanner scannerWithString: [s string]];
- NSMutableDictionary*vStyles = [[[self defaultTextAttributes] mutableCopy] autorelease];
- [vStyles addEntriesFromDictionary: [NSDictionary dictionaryWithObjectsAndKeys:
- col, NSForegroundColorAttributeName,
- attr, TD_SYNTAX_COLORING_MODE_ATTR,
- nil]];
+ NSDictionary* vStyles = [self textAttributesForComponentName: attr color: col];
BOOL vDelegateHandlesProgress = [delegate respondsToSelector: @selector(textViewControllerProgressedWhileSyntaxRecoloring:)];
while( ![vScanner isAtEnd] )
@@ -1158,11 +1176,7 @@ -(void) colorOneLineComment: (NSString*) startCh inString: (NSMutableAttributedS
@try
{
NSScanner* vScanner = [NSScanner scannerWithString: [s string]];
- NSMutableDictionary*vStyles = [[[self defaultTextAttributes] mutableCopy] autorelease];
- [vStyles addEntriesFromDictionary: [NSDictionary dictionaryWithObjectsAndKeys:
- col, NSForegroundColorAttributeName,
- attr, TD_SYNTAX_COLORING_MODE_ATTR,
- nil]];
+ NSDictionary* vStyles = [self textAttributesForComponentName: attr color: col];
BOOL vDelegateHandlesProgress = [delegate respondsToSelector: @selector(textViewControllerProgressedWhileSyntaxRecoloring:)];
while( ![vScanner isAtEnd] )
@@ -1207,11 +1221,7 @@ -(void) colorIdentifier: (NSString*) ident inString: (NSMutableAttributedString*
@try
{
NSScanner* vScanner = [NSScanner scannerWithString: [s string]];
- NSMutableDictionary*vStyles = [[[self defaultTextAttributes] mutableCopy] autorelease];
- [vStyles addEntriesFromDictionary: [NSDictionary dictionaryWithObjectsAndKeys:
- col, NSForegroundColorAttributeName,
- attr, TD_SYNTAX_COLORING_MODE_ATTR,
- nil]];
+ NSDictionary* vStyles = [self textAttributesForComponentName: attr color: col];
int vStartOffs = 0;
BOOL vDelegateHandlesProgress = [delegate respondsToSelector: @selector(textViewControllerProgressedWhileSyntaxRecoloring:)];
@@ -1275,11 +1285,7 @@ -(void) colorTagFrom: (NSString*) startCh to: (NSString*)endCh inString: (NSMuta
@try
{
NSScanner* vScanner = [NSScanner scannerWithString: [s string]];
- NSMutableDictionary*vStyles = [[[self defaultTextAttributes] mutableCopy] autorelease];
- [vStyles addEntriesFromDictionary: [NSDictionary dictionaryWithObjectsAndKeys:
- col, NSForegroundColorAttributeName,
- attr, TD_SYNTAX_COLORING_MODE_ATTR,
- nil]];
+ NSDictionary* vStyles = [self textAttributesForComponentName: attr color: col];
BOOL vDelegateHandlesProgress = [delegate respondsToSelector: @selector(textViewControllerProgressedWhileSyntaxRecoloring:)];
while( ![vScanner isAtEnd] )

0 comments on commit 256fabc

Please sign in to comment.