Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 181 lines (124 sloc) 4.8 kB
c62d72f @ccgus Added an editor for jstalk
authored
1 //
2 // VPJSTViewController.m
3 // VPJSTalkPlugin
4 //
5 // Created by August Mueller on 10/12/09.
6 // Copyright 2009 Flying Meat Inc. All rights reserved.
7 //
8
9 #import "VPJSTViewController.h"
10 #import "JavaScriptPluginEnabler.h"
76838d9 Silly new plugin to mess with Mail.app.
August Mueller authored
11 //#import "MarkerLineNumberView.h"
c62d72f @ccgus Added an editor for jstalk
authored
12 #import <JSTalk/JSTalk.h>
13
14 const NSString *kUTTypeJSTalkSource = @"org.jstalk.jstalk-source";
15
16 @implementation VPJSTViewController
17
18 + (void) load {
19 [VPItemController registerItemControllerClass:self];
20 }
21
22 + (BOOL) canHandleItemWithUTI:(NSString*)uti {
23
24 if (UTTypeConformsTo((CFStringRef)uti, (CFStringRef)kUTTypeJSTalkSource)) {
25 return YES;
26 }
27
28 return NO;
29 }
30
31 + (VPItemController*) itemController {
32
33 VPJSTViewController *newController = [[[VPJSTViewController alloc] initWithNibName:@"JSTalkEditView" bundle:[NSBundle bundleForClass:self]] autorelease];
34 [newController loadView];
35
36 return newController;
37 }
38
39 + (NSString*) newItemDescription {
40 return NSLocalizedString(@"JSTalk Source", @"JSTalk Source");
41 }
42
43 + (NSString*) defaultUTI {
44 return (NSString*)kUTTypeJSTalkSource;
45 }
46
47 + (NSData*) defaultDataForItemWithDisplayName:(NSString*)displayName contextInfo:(NSDictionary*) contextInfo {
48
49 NSString *s = [NSString stringWithFormat:@"print(\"Hello %@\");", displayName];
50
51 return [s dataUsingEncoding:NSUTF8StringEncoding];
52 }
53
54 - (void)dealloc {
55 [[NSNotificationCenter defaultCenter] removeObserver:self];
56
57 [_tokenizer autorelease];
58 _tokenizer = 0x00;
59
60 [super dealloc];
61 }
62
63 - (void)awakeFromNib {
64
65 }
66
67 - (void) close {
68 [super close];
69 }
70
71 - (void)setItem:(id<VPData>)newItem {
72
73 [super setItem:newItem];
74
75 NSData *textData = [[self item] data];
76
77 NSString *text = [[NSString alloc] initWithData:textData encoding:NSUTF8StringEncoding];
78
79 if (text) {
80 [[[textView textStorage] mutableString] setString:text];
81 }
82
83 // FIXME: make this a pref
84 [[textView textStorage] setAttributes:[NSDictionary dictionaryWithObject:[NSFont fontWithName:@"Monaco" size:10] forKey:NSFontAttributeName] range:NSMakeRange(0, [[textView textStorage] length]) ];
76838d9 Silly new plugin to mess with Mail.app.
August Mueller authored
85 /*
c62d72f @ccgus Added an editor for jstalk
authored
86 lineNumberView = [[MarkerLineNumberView alloc] initWithScrollView:[textView enclosingScrollView]];
87 [[textView enclosingScrollView] setVerticalRulerView:lineNumberView];
88 [[textView enclosingScrollView] setHasHorizontalRuler:NO];
89 [[textView enclosingScrollView] setHasVerticalRuler:YES];
90 [[textView enclosingScrollView] setRulersVisible:YES];
76838d9 Silly new plugin to mess with Mail.app.
August Mueller authored
91 */
c62d72f @ccgus Added an editor for jstalk
authored
92 [[textView textStorage] setDelegate:self];
93 [self parseCode:nil];
94 }
95
96 - (NSData*) dataRepresentation {
97
98 NSString *text = [[textView textStorage] mutableString];
99
100 return [text dataUsingEncoding:NSUTF8StringEncoding];
101 }
102
103 - (JSTTextView*)textView {
104 // VPWindowController will call this guy.
105 return textView;
106 }
107
108 - (BOOL) isDirty {
109 return [[[[[self view] window] windowController] document] isDocumentEdited];
110 }
111
112 - (void) executeScript:(id)sender {
113 [JavaScriptPluginEnablerGlobalHACKHACKHACK handleRunAsJavaScript:[[[self view] window] windowController]];
114 }
115
116 - (void) textStorageDidProcessEditing:(NSNotification *)note {
117 [self parseCode:nil];
118 }
119
120
121
122 - (void) parseCode:(id)sender {
123
124 // we should really do substrings...
125
126 NSString *sourceString = [[textView textStorage] string];
127 TDTokenizer *tokenizer = [TDTokenizer tokenizerWithString:sourceString];
128
129 tokenizer.commentState.reportsCommentTokens = YES;
130 tokenizer.whitespaceState.reportsWhitespaceTokens = YES;
131
132 TDToken *eof = [TDToken EOFToken];
133 TDToken *tok = nil;
134
135 [[textView textStorage] beginEditing];
136
137 NSUInteger sourceLoc = 0;
138
139 while ((tok = [tokenizer nextToken]) != eof) {
140
141 NSColor *fontColor = [NSColor blackColor];
142
143 if (tok.quotedString) {
144 fontColor = [NSColor darkGrayColor];
145 }
146 else if (tok.isNumber) {
147 fontColor = [NSColor blueColor];
148 }
149 else if (tok.isComment) {
150 fontColor = [NSColor redColor];
151 }
152 else if (tok.isWord) {
153 NSColor *c = [_keywords objectForKey:[tok stringValue]];
154 fontColor = c ? c : fontColor;
155 }
156
157 NSUInteger strLen = [[tok stringValue] length];
158
159 if (fontColor) {
160 [[textView textStorage] addAttribute:NSForegroundColorAttributeName value:fontColor range:NSMakeRange(sourceLoc, strLen)];
161 }
162
163 sourceLoc += strLen;
164 }
165
166
167 [[textView textStorage] endEditing];
168
169 }
170
76838d9 Silly new plugin to mess with Mail.app.
August Mueller authored
171 - (void) runPageAsScript:(id)sender {
172 [self executeScript:sender];
173 }
174
175 - (void) openJSTalkWebsite:(id)sender {
176 [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://jstalk.org/"]];
177 }
178
c62d72f @ccgus Added an editor for jstalk
authored
179
180 @end
Something went wrong with that request. Please try again.