forked from KosmicTask/Fragaria
-
Notifications
You must be signed in to change notification settings - Fork 3
/
FragariaAppDelegate.m
456 lines (324 loc) · 11.5 KB
/
FragariaAppDelegate.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//
// FragariaAppDelegate.m
// Fragaria
//
// Created by Jonathan on 30/04/2010.
// Copyright 2010 mugginsoft.com. All rights reserved.
//
#import "FragariaAppDelegate.h"
#import <MGSFragaria/MGSFragaria.h>
#import "MGSPreferencesController.h"
@implementation FragariaAppDelegate
@synthesize window;
#pragma mark -
#pragma mark NSApplicationDelegate
/*
- applicationDidFinishLaunching:
*/
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
#pragma unused(aNotification)
// create an instance
fragaria = [[MGSFragaria alloc] init];
// define initial object configuration
//
// see MGSFragaria.h for details
//
[fragaria setObject:self forKey:MGSFODelegate];
// set the syntax colouring delegate
[fragaria setObject:self forKey:MGSFOSyntaxColouringDelegate];
// define our syntax definition
[self setSyntaxDefinition:@"Objective-C"];
// embed editor in editView
[fragaria embedInView:editView];
//
// assign user defaults.
// a number of properties are derived from the user defaults system rather than the doc spec.
//
// see MGSFragariaPreferences.h for details
//
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:YES] forKey:MGSFragariaPrefsAutocompleteSuggestAutomatically];
// get initial text - in this case a test file from the bundle
NSString *path = [[NSBundle mainBundle] pathForResource:@"SMLSyntaxColouring" ofType:@"m"];
NSString *fileText = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
// set text
//[fragaria performSelector:@selector(setString:) withObject:fileText afterDelay:0];
[fragaria setString:fileText];
// access the NSTextView directly
NSTextView *textView = [fragaria objectForKey:ro_MGSFOTextView];
// define a syntax error
SMLSyntaxError *syntaxError = [[SMLSyntaxError new] autorelease];
syntaxError.description = @"Syntax errors can be defined";
syntaxError.line = 1;
syntaxError.character = 1;
syntaxError.length = 10;
fragaria.syntaxErrors = @[syntaxError];
#pragma unused(textView)
}
/*
- applicationShouldTerminateAfterLastWindowClosed:
*/
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
{
#pragma unused(theApplication)
return YES;
}
#pragma mark -
#pragma mark Actions
/*
- showPreferencesWindow
*/
- (IBAction)showPreferencesWindow:(id)sender
{
#pragma unused(sender)
[[MGSPreferencesController sharedPrefsWindowController] showWindow:self];
}
/*
reloadString:
*/
- (IBAction)reloadString:(id)sender
{
#pragma unused(sender)
[fragaria reloadString];
}
#pragma mark -
#pragma mark Pasteboard handling
/*
copyToPasteBoard
*/
- (IBAction)copyToPasteBoard:(id)sender
{
#pragma unused(sender)
NSAttributedString *attString = [fragaria attributedString];
NSData *data = [attString RTFFromRange:NSMakeRange(0, [attString length]) documentAttributes:nil];
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
[pasteboard clearContents];
[pasteboard setData:data forType:NSRTFPboardType];
}
#pragma mark -
#pragma mark Syntax definition handling
/*
- setSyntaxDefinition:
*/
- (void)setSyntaxDefinition:(NSString *)name
{
[fragaria setObject:name forKey:MGSFOSyntaxDefinitionName];
}
/*
- syntaxDefinition
*/
- (NSString *)syntaxDefinition
{
return [fragaria objectForKey:MGSFOSyntaxDefinitionName];
}
#pragma mark -
#pragma mark NSTextDelegate
/*
- textDidChange:
*/
- (void)textDidChange:(NSNotification *)notification
{
#pragma unused(notification)
[window setDocumentEdited:YES];
}
/*
- textDidBeginEditing:
*/
- (void)textDidBeginEditing:(NSNotification *)aNotification
{
NSLog(@"notification : %@", [aNotification name]);
}
/*
- textDidEndEditing:
*/
- (void)textDidEndEditing:(NSNotification *)aNotification
{
NSLog(@"notification : %@", [aNotification name]);
}
/*
- textShouldBeginEditing:
*/
- (BOOL)textShouldBeginEditing:(NSText *)aTextObject
{
#pragma unused(aTextObject)
return YES;
}
/*
- textShouldEndEditing:
*/
- (BOOL)textShouldEndEditing:(NSText *)aTextObject
{
#pragma unused(aTextObject)
return YES;
}
#pragma mark -
#pragma mark MGSFragariaTextViewDelegate
/*
- mgsTextDidPaste:
*/
- (void)mgsTextDidPaste:(NSNotification *)aNotification
{
/*
When this notification is received the paste will have been accepted.
Use this method to query the pasteboard for additional pasteboard content
that may be relevant to the application: eg: a plist that may contain custom data.
*/
NSLog(@"notification : %@", [aNotification name]);
}
#pragma mark -
#pragma mark SMLSyntaxColouringDelegate
/*
For more information on custom colouring see SMLSyntaxColouringDelegate.h
*/
/*
- fragariaDocument:shouldColourWithBlock:string:range:info
*/
- (BOOL)fragariaDocument:(id)document shouldColourWithBlock:(BOOL (^)(NSDictionary *, NSRange))colourWithBlock string:(NSString *)string range:(NSRange)range info:(NSDictionary *)info
{
#pragma unused(document, colourWithBlock, string, range, info)
// query info
BOOL willColour = [[info objectForKey:SMLSyntaxWillColour] boolValue];
NSDictionary *syntaxInfo = [info objectForKey:SMLSyntaxInfo];
// provide compiler comfort
(void)syntaxInfo, (void)willColour;
NSLog(@"Should colour document.");
// we can call colourWithBlock to perform initial colouring
// YES: Fragaria should colour document
// NO: Fragaria should not colour document
return YES;
}
/*
- fragariaDocument:shouldColourGroupWithBlock:string:range:info
*/
- (BOOL)fragariaDocument:(id)document shouldColourGroupWithBlock:(BOOL (^)(NSDictionary *, NSRange))colourWithBlock string:(NSString *)string range:(NSRange)range info:(NSDictionary *)info
{
#pragma unused(document, string)
BOOL fragariaShouldColour = YES;
// query info
NSString *group = [info objectForKey:SMLSyntaxGroup];
NSInteger groupID = [[info objectForKey:SMLSyntaxGroupID] integerValue];
BOOL willColour = [[info objectForKey:SMLSyntaxWillColour] boolValue];
NSDictionary *attributes = [info objectForKey:SMLSyntaxAttributes];
// for key values see SMLSyntaxDefinition.h
NSDictionary *syntaxInfo = [info objectForKey:SMLSyntaxInfo];
// provide compiler comfort
(void)syntaxInfo;
// follow the default behaviour. if we don't then colouring occurs even when we turn
// syntax colouring off in the preferences. this is fine in practice but confusing in a demo app.
fragariaShouldColour = willColour;
// this amount of logging makes the app sluggish
NSLog(@"%@ group : %@ id : %li caller will colour : %@", NSStringFromSelector(_cmd), group, groupID, (willColour ? @"YES" : @"NO"));
// group
switch (groupID) {
case kSMLSyntaxGroupNumber:
// we can call colourWithBlock to perform initial group colouration
if (NO) {
// colour the whole string with the number group colour
colourWithBlock(attributes, range);
fragariaShouldColour = NO;
}
break;
case kSMLSyntaxGroupCommand:
break;
case kSMLSyntaxGroupInstruction:
break;
case kSMLSyntaxGroupKeyword:
break;
case kSMLSyntaxGroupAutoComplete:
break;
case kSMLSyntaxGroupVariable:
break;
case kSMLSyntaxGroupFirstString:
break;
case kSMLSyntaxGroupSecondString:
break;
case kSMLSyntaxGroupAttribute:
break;
case kSMLSyntaxGroupSingleLineComment:
break;
case kSMLSyntaxGroupMultiLineComment:
// we can prevent colouring of this group by returning NO
if (NO) {
fragariaShouldColour = NO;
}
break;
case kSMLSyntaxGroupSecondStringPass2:
break;
}
// YES: Fragaria should colour group
// NO: Fragaria should not colour group
return fragariaShouldColour;
}
/*
- fragariaDocument:didColourGroupWithBlock:string:range:info
*/
- (void)fragariaDocument:(id)document didColourGroupWithBlock:(BOOL (^)(NSDictionary *, NSRange))colourWithBlock string:(NSString *)string range:(NSRange)range info:(NSDictionary *)info
{
#pragma unused(document, string)
// query info
NSString *group = [info objectForKey:SMLSyntaxGroup];
NSInteger groupID = [[info objectForKey:SMLSyntaxGroupID] integerValue];
BOOL willColour = [[info objectForKey:SMLSyntaxWillColour] boolValue];
NSDictionary *attributes = [info objectForKey:SMLSyntaxAttributes];
NSDictionary *syntaxInfo = [info objectForKey:SMLSyntaxInfo];
// compiler comfort
(void)syntaxInfo;
// this amount of logging makes the app sluggish
NSLog(@"%@ group : %@ id : %li caller will colour : %@", NSStringFromSelector(_cmd), group, groupID, (willColour ? @"YES" : @"NO"));
NSString *subString = [string substringWithRange:range];
NSScanner *rangeScanner = [NSScanner scannerWithString:subString];
[rangeScanner setScanLocation:0];
// group
switch (groupID) {
case kSMLSyntaxGroupNumber:
break;
case kSMLSyntaxGroupCommand:
break;
case kSMLSyntaxGroupInstruction:
break;
case kSMLSyntaxGroupKeyword:
{
// we can iterate over the string using an NSScanner to identiy our substrings or use a regex.
// in this simple case we just colour the occurence of a given string as a false keyword.
NSString *fauxKeyword = @"kosmic";
while (YES) {
// look for the keyword
[rangeScanner scanUpToString:fauxKeyword intoString:nil];
if ([rangeScanner isAtEnd]) break;
NSUInteger location = [rangeScanner scanLocation];
if ([rangeScanner scanString:fauxKeyword intoString:NULL]) {
NSRange colourRange = NSMakeRange(range.location + location, [rangeScanner scanLocation] - location);
// the block will colour the string
colourWithBlock(attributes, colourRange);
}
}
}
break;
case kSMLSyntaxGroupAutoComplete:
break;
case kSMLSyntaxGroupVariable:
break;
case kSMLSyntaxGroupFirstString:
break;
case kSMLSyntaxGroupSecondString:
break;
case kSMLSyntaxGroupAttribute:
break;
case kSMLSyntaxGroupSingleLineComment:
break;
case kSMLSyntaxGroupMultiLineComment:
break;
case kSMLSyntaxGroupSecondStringPass2:
break;
}
}
/*
- fragariaDocument:didColourWithBlock:string:range:info
*/
- (void)fragariaDocument:(id)document didColourWithBlock:(BOOL (^)(NSDictionary *, NSRange))colourWithBlock string:(NSString *)string range:(NSRange)range info:(NSDictionary *)info
{
#pragma unused(document, colourWithBlock, string, range, info)
NSLog(@"Did colour document.");
// we can call colourWithBlock to perform final colouring
}
@end