Skip to content
Browse files

Added "OneLineCommentPrefix" key to syntax definitions, and use that …

…in comment/uncomment code now. Also fixed undo, I think. However, it's highly inefficient right now. Will have to make this surgically restore stuff.
  • Loading branch information...
1 parent a5657e2 commit c4b98c05c0886693abdd5378acf2a490e1d996ba @uliwitness committed Mar 7, 2010
Showing with 98 additions and 45 deletions.
  1. +16 −1 README.txt
  2. +18 −7 Syntax Definitions/Objective C.plist
  3. +64 −37 UKSyntaxColoredTextDocument.m
View
17 README.txt
@@ -26,7 +26,9 @@ ADAPTING TO OTHER PROGRAMMING LANGUAGES
To adapt the syntax coloring to another language, change the
SyntaxDefinition.plist file using Apple's Property List Editor. A syntax
definition file is an NSDictionary saved as a property list (and can thus be
-edited using Apple's Property List Editor). It currently contains two entries.
+edited using Apple's Property List Editor). It currently contains two required
+entries.
+
The first has the key "Components", which is an array of dictionaries each
describing one "thing" that can be colorized. Here's what the various keys mean:
@@ -83,6 +85,19 @@ The second entry is "FileNameSuffixes", which is an array of strings, each
designating one file name extension which UKMultiSyntaxColoredTextDocument will
use to determine the syntax coloring file to use for an opened file.
+Currently, you can also put the following optional keys in the dictionary:
+
+OneLineCommentPrefix - A string that will be used by the action method
+ -toggleCommentForSelection: to determine what
+ comment character sequence to look for/to add.
+ Note that if this entry has leading or trailing
+ whitespace, that whitespace will be added when
+ inserting a new comment, but when trying to determine
+ whether a line has a comment that should be removed,
+ it will also match the string without the whitespace.
+ This could cause problems in languages where
+ whitespace is syntactically relevant.
+
WHAT LICENSE IS THIS UNDER?
View
25 Syntax Definitions/Objective C.plist
@@ -1,7 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
-<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
+ <key>OneLineCommentPrefix</key>
+ <string>// </string>
<key>Components</key>
<array>
<dict>
@@ -12,13 +14,13 @@
<integer>1</integer>
</array>
<key>End</key>
- <string>"</string>
+ <string>&quot;</string>
<key>EscapeChar</key>
<string>\</string>
<key>Name</key>
<string>Strings</string>
<key>Start</key>
- <string>"</string>
+ <string>&quot;</string>
<key>Type</key>
<string>String</string>
</dict>
@@ -27,9 +29,9 @@
<string>ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@_1234567890</string>
<key>Color</key>
<array>
- <real>0.20000000000000001</real>
+ <real>0.2</real>
<real>0.5</real>
- <real>0.20000000000000001</real>
+ <real>0.2</real>
</array>
<key>Keywords</key>
<array>
@@ -39,14 +41,24 @@
<string>self</string>
<string>return</string>
<string>@selector</string>
+ <string>@encode</string>
<string>@interface</string>
- <string>@protocol</string>
<string>@implementation</string>
+ <string>@protocol</string>
+ <string>@end</string>
+ <string>@property</string>
+ <string>@synthesize</string>
+ <string>@synchronized</string>
<string>@class</string>
<string>@try</string>
<string>@catch</string>
<string>@finally</string>
+ <string>@private</string>
+ <string>@protected</string>
+ <string>@required</string>
+ <string>@optional</string>
<string>enum</string>
+ <string>in</string>
<string>typedef</string>
<string>unsigned</string>
<string>signed</string>
@@ -57,7 +69,6 @@
<string>short</string>
<string>float</string>
<string>double</string>
- <string>@end</string>
<string>nil</string>
<string>else</string>
<string>if</string>
View
101 UKSyntaxColoredTextDocument.m
@@ -554,9 +554,19 @@ -(void) goToRangeFrom: (int)startCh toChar: (int)endCh
}
+-(void) restoreText: (NSString*)textToRestore
+{
+ [[self undoManager] disableUndoRegistration];
+ [textView setString: textToRestore];
+ [[self undoManager] enableUndoRegistration];
+}
+
+
-(IBAction) indentSelection: (id)sender
{
- [[self undoManager] registerUndoWithTarget: self selector: @selector(unindentSelection:) object: nil];
+ [[self undoManager] beginUndoGrouping];
+ NSString* prevText = [[[[textView textStorage] string] copy] autorelease];
+ [[self undoManager] registerUndoWithTarget: self selector: @selector(restoreText:) object: prevText];
NSRange selRange = [textView selectedRange],
nuSelRange = selRange;
@@ -584,6 +594,7 @@ -(IBAction) indentSelection: (id)sender
[str insertString: @"\t" atIndex: nuSelRange.location];
nuSelRange.length++;
[textView setSelectedRange: nuSelRange];
+ [[self undoManager] endUndoGrouping];
}
@@ -603,8 +614,10 @@ -(IBAction) unindentSelection: (id)sender
if( selRange.length == 0 )
return;
- [[self undoManager] registerUndoWithTarget: self selector: @selector(indentSelection:) object: nil];
-
+ [[self undoManager] beginUndoGrouping];
+ NSString* prevText = [[[[textView textStorage] string] copy] autorelease];
+ [[self undoManager] registerUndoWithTarget: self selector: @selector(restoreText:) object: prevText];
+
for( x = lastIndex; x >= selRange.location; x-- )
{
if( [str characterAtIndex: x] == '\n'
@@ -651,6 +664,7 @@ -(IBAction) unindentSelection: (id)sender
}
[textView setSelectedRange: nuSelRange];
+ [[self undoManager] endUndoGrouping];
}
@@ -661,20 +675,16 @@ -(IBAction) unindentSelection: (id)sender
-(IBAction) toggleCommentForSelection: (id)sender
{
- [[self undoManager] registerUndoWithTarget: self selector: @selector(toggleCommentForSelection:) object: nil];
-
NSRange selRange = [textView selectedRange];
unsigned x;
NSMutableString* str = [[textView textStorage] mutableString];
if( selRange.length == 0 )
selRange.length++;
-// NSLog(@"selection %d,%d", selRange.location, selRange.length);
-
// Are we at the end of a line?
- if ([str characterAtIndex:selRange.location] == '\n' ||
- [str characterAtIndex:selRange.location] == '\r')
+ if ([str characterAtIndex: selRange.location] == '\n' ||
+ [str characterAtIndex: selRange.location] == '\r')
{
if( selRange.location > 0 )
{
@@ -684,10 +694,10 @@ -(IBAction) toggleCommentForSelection: (id)sender
}
// Move the selection to the start of a line
- while (selRange.location >= 0)
+ while( selRange.location > 0 )
{
-// NSLog(@"Checking charater %c", [str characterAtIndex:selRange.location]);
- if ([str characterAtIndex:selRange.location] == '\n' || [str characterAtIndex:selRange.location] == '\r')
+ if( [str characterAtIndex: selRange.location] == '\n'
+ || [str characterAtIndex: selRange.location] == '\r')
{
selRange.location++;
selRange.length--;
@@ -698,60 +708,77 @@ -(IBAction) toggleCommentForSelection: (id)sender
}
// Select up to the end of a line
- while ( (selRange.location+selRange.length-1) < [str length] &&
- !([str characterAtIndex:selRange.location+selRange.length-1] == '\n' ||
- [str characterAtIndex:selRange.location+selRange.length-1] == '\r'))
+ while ( (selRange.location +selRange.length) < [str length]
+ && !([str characterAtIndex:selRange.location+selRange.length-1] == '\n'
+ || [str characterAtIndex:selRange.location+selRange.length-1] == '\r') )
{
selRange.length++;
}
if (selRange.length == 0)
return;
- // Unselect any trailing returns so we don't indent the next line after a full-line selection.
- while([str characterAtIndex:selRange.location+selRange.length-1] == '\n' ||
- [str characterAtIndex:selRange.location+selRange.length-1] == '\r')
+ [[self undoManager] beginUndoGrouping];
+ NSString* prevText = [[[[textView textStorage] string] copy] autorelease];
+ [[self undoManager] registerUndoWithTarget: self selector: @selector(restoreText:) object: prevText];
+
+ // Unselect any trailing returns so we don't comment the next line after a full-line selection.
+ while( [str characterAtIndex: selRange.location +selRange.length -1] == '\n' ||
+ [str characterAtIndex: selRange.location +selRange.length -1] == '\r'
+ && selRange.length > 0 )
{
selRange.length--;
}
-// NSLog(@"Selected range: '%@'", [str substringWithRange:selRange]);
NSRange nuSelRange = selRange;
+ NSString* commentPrefix = [[self syntaxDefinitionDictionary] objectForKey: @"OneLineCommentPrefix"];
+ if( !commentPrefix || [commentPrefix length] == 0 )
+ commentPrefix = @"# ";
+ NSInteger commentPrefixLength = [commentPrefix length];
+ NSString* trimmedCommentPrefix = [commentPrefix stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
+ if( !trimmedCommentPrefix || [trimmedCommentPrefix length] == 0 ) // Comments apparently *are* whitespace.
+ trimmedCommentPrefix = commentPrefix;
+ NSInteger trimmedCommentPrefixLength = [trimmedCommentPrefix length];
+
for( x = selRange.location +selRange.length -1; x >= selRange.location; x-- )
{
- if( [str characterAtIndex: x] == '\n'
- || [str characterAtIndex: x] == '\r' )
+ BOOL hitEnd = (x == selRange.location);
+ BOOL hitLineBreak = [str characterAtIndex: x] == '\n' || [str characterAtIndex: x] == '\r';
+ if( hitLineBreak || hitEnd )
{
-// NSLog(@"Checking char %c", [str characterAtIndex:x+1]);
- if( [str characterAtIndex:x+1] == '%' )
+ NSUInteger startOffs = x+1;
+ if( hitEnd && !hitLineBreak )
+ startOffs = x;
+ NSInteger possibleCommentLength = 0;
+ if( commentPrefixLength <= (selRange.length +selRange.location -startOffs) )
+ possibleCommentLength = commentPrefixLength;
+ else if( trimmedCommentPrefixLength <= (selRange.length +selRange.location -startOffs) )
+ possibleCommentLength = trimmedCommentPrefixLength;
+
+ NSString * lineStart = [str substringWithRange: NSMakeRange( startOffs, possibleCommentLength )];
+ BOOL haveWhitespaceToo = [lineStart hasPrefix: commentPrefix];
+ if( [lineStart hasPrefix: trimmedCommentPrefix] )
{
- [str deleteCharactersInRange:NSMakeRange(x+1, 1)];
- nuSelRange.length--;
+ NSInteger commentLength = haveWhitespaceToo ? commentPrefixLength : trimmedCommentPrefixLength;
+ [str deleteCharactersInRange: NSMakeRange(startOffs, commentLength)];
+ nuSelRange.length -= commentLength;
}
else
{
- [str insertString: @"%" atIndex: x+1];
- nuSelRange.length++;
+ [str insertString: commentPrefix atIndex: startOffs];
+ nuSelRange.length += commentPrefixLength;
}
}
if( x == 0 )
break;
}
- if( [str characterAtIndex:nuSelRange.location] == '%' )
- {
- [str deleteCharactersInRange:NSMakeRange( nuSelRange.location, 1 )];
- nuSelRange.length--;
- }
- else
- {
- [str insertString: @"%" atIndex: nuSelRange.location];
- nuSelRange.length++;
- }
[textView setSelectedRange: nuSelRange];
+ [[self undoManager] endUndoGrouping];
+
}

0 comments on commit c4b98c0

Please sign in to comment.
Something went wrong with that request. Please try again.