| 1 | /* Copyright (C) 2008-2009 Peter Speck |
|---|
| 2 | * |
|---|
| 3 | * This program is free software: you can redistribute it and/or modify |
|---|
| 4 | * it under the terms of the GNU General Public License as published by |
|---|
| 5 | * the Free Software Foundation, either version 3 of the License, or |
|---|
| 6 | * (at your option) any later version. |
|---|
| 7 | * |
|---|
| 8 | * This program is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * GNU General Public License for more details. |
|---|
| 12 | * |
|---|
| 13 | * You should have received a copy of the GNU General Public License |
|---|
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | #import "CodeEditHelper.h" |
|---|
| 19 | #import "JavascriptSyntaxHighlighter.h" |
|---|
| 20 | |
|---|
| 21 | @implementation CodeEditHelper |
|---|
| 22 | |
|---|
| 23 | static inline NSString* soft(NSString* s) |
|---|
| 24 | { |
|---|
| 25 | return s ? s : @""; |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | - (id)initWithDelegate:(id<CodeEditDelegate>)delegate |
|---|
| 29 | { |
|---|
| 30 | if (!(self = [super init])) |
|---|
| 31 | return nil; |
|---|
| 32 | _delegate = delegate; |
|---|
| 33 | return self; |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | - (void)dealloc |
|---|
| 37 | { |
|---|
| 38 | [super dealloc]; |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | - (void)syntaxHighlight:(NSMutableAttributedString*)attributedString |
|---|
| 42 | isJavascript:(BOOL)isJavascript |
|---|
| 43 | { |
|---|
| 44 | //DebugNSLog(@"syntaxHighlight%@: %@", (js ? @"Javascript" : @"JavaRegExp"), [textStorage string]); |
|---|
| 45 | // |
|---|
| 46 | // This method is called when changing colors, |
|---|
| 47 | // so we must guard against recursive calls of textStorageDidProcessEditing |
|---|
| 48 | _isFormatting = YES; |
|---|
| 49 | if (isJavascript) |
|---|
| 50 | [JavascriptSyntaxHighlighter highlightJavascript:attributedString]; |
|---|
| 51 | else |
|---|
| 52 | [JavascriptSyntaxHighlighter highlightJavaRegExp:attributedString]; |
|---|
| 53 | _isFormatting = NO; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | - (void)syntaxHighlightJavascript:(NSTextStorage*)textStorage |
|---|
| 57 | { |
|---|
| 58 | [self syntaxHighlight:textStorage isJavascript:YES]; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | - (void)delayedSyntaxHighlightJavascript:(NSTextStorage*)textStorage |
|---|
| 62 | { |
|---|
| 63 | [self performSelectorOnMainThread:@selector(syntaxHighlightJavascript:) |
|---|
| 64 | withObject:textStorage |
|---|
| 65 | waitUntilDone:NO]; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | - (void)setCodeTabs:(NSTextStorage*)storage |
|---|
| 69 | { |
|---|
| 70 | static NSMutableParagraphStyle *style = NULL; |
|---|
| 71 | if (!style) { |
|---|
| 72 | style = [[NSMutableParagraphStyle alloc] init]; |
|---|
| 73 | [style setParagraphStyle:[NSParagraphStyle defaultParagraphStyle]]; |
|---|
| 74 | [style setTabStops:[NSArray array]]; // delete default tabs |
|---|
| 75 | for (int x = 15; x < 500; x += 15) { |
|---|
| 76 | NSTextTab* tab = [[NSTextTab alloc] |
|---|
| 77 | initWithType:NSLeftTabStopType |
|---|
| 78 | location:x]; |
|---|
| 79 | [style addTabStop:tab]; |
|---|
| 80 | [tab release]; |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | [storage addAttribute:NSParagraphStyleAttributeName |
|---|
| 84 | value:style |
|---|
| 85 | range:NSMakeRange(0, [storage length])]; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | - (void)setCodeTextView:(NSTextView*)textView |
|---|
| 89 | withText:(NSString*)txt |
|---|
| 90 | { |
|---|
| 91 | static NSFont* font = NULL; // NSTextView forgets the font when using setString, so force it. |
|---|
| 92 | if (!font) { |
|---|
| 93 | //font = [[NSFont fontWithName:@"Monaco" size:10] retain]; |
|---|
| 94 | font = [[NSFont systemFontOfSize:11] retain]; |
|---|
| 95 | } |
|---|
| 96 | [textView setString:soft(txt)]; |
|---|
| 97 | [textView setFont:font]; |
|---|
| 98 | [self setCodeTabs:[textView textStorage]]; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | - (void)textStorageDidProcessEditing:(NSNotification *)noti |
|---|
| 102 | { |
|---|
| 103 | if (_isFormatting || ![[noti object] isKindOfClass:[NSTextStorage class]]) |
|---|
| 104 | return; |
|---|
| 105 | NSTextStorage* ts = (NSTextStorage*)[noti object]; |
|---|
| 106 | if ([_delegate isJavascriptTextStorage:ts]) |
|---|
| 107 | [self syntaxHighlight:ts isJavascript:YES]; |
|---|
| 108 | [self setCodeTabs:ts]; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | // returns NULL if results in empty string. |
|---|
| 112 | - (NSString*)trimmedTextViewString:(NSTextView*)textView |
|---|
| 113 | { |
|---|
| 114 | NSString* txt = [CodeEditHelper trimmedString:[textView string]]; |
|---|
| 115 | return [txt length] ? txt : NULL; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | // returns NULL if results in empty string. |
|---|
| 119 | + (NSString*)trimmedString:(NSString*)src |
|---|
| 120 | { |
|---|
| 121 | NSCharacterSet* wsp = [NSCharacterSet whitespaceAndNewlineCharacterSet]; |
|---|
| 122 | NSInteger len = [src length]; |
|---|
| 123 | NSInteger end = len - 1; |
|---|
| 124 | while (end >= 0 && [wsp characterIsMember:[src characterAtIndex:end]]) |
|---|
| 125 | end--; |
|---|
| 126 | NSInteger start = 0; |
|---|
| 127 | while (start <= end && [wsp characterIsMember:[src characterAtIndex:start]]) |
|---|
| 128 | start++; |
|---|
| 129 | if (start > end) |
|---|
| 130 | return NULL; |
|---|
| 131 | if (start == 0 && end == len - 1) |
|---|
| 132 | return src; |
|---|
| 133 | return [src substringWithRange:NSMakeRange(start, end - start + 1)]; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | @end |
|---|