-
Notifications
You must be signed in to change notification settings - Fork 36
/
IGKDocRecordManagedObject.m
638 lines (507 loc) · 19 KB
/
IGKDocRecordManagedObject.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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
//
// IGKDocRecordManagedObject.m
// Ingredients
//
// Created by Alex Gordon on 25/01/2010.
// Written in 2010 by Fileability.
//
#import "IGKDocRecordManagedObject.h"
#import "IGKDocSetManagedObject.h"
@interface IGKDocRecordManagedObject ()
+ (NSString *)entityNameFromURLComponentExtension:(NSString *)ext;
- (NSImage *)iconForSelectedState:(BOOL)isSelected;
- (CHSymbolButtonImageMask)iconMask;
@end
@implementation IGKDocRecordManagedObject
+ (IGKDocRecordManagedObject *)resolveURL:(NSURL *)url inContext:(NSManagedObjectContext *)ctx tableOfContentsMask:(IGKHTMLDisplayTypeMask *)tocMaskPointer
{
NSArray *components = [url pathComponents];
/*
ingr-doc:// <docset-family> / <docset-version> / <table-of-contents> / <item-name> . <item-type>
ingr-doc:// <docset-family> / <docset-version> / <table-of-contents> / <container-name> . <container-type> / <item-name> . <item-type>
*/
NSLog(@"resolve URL: %@ = %@", url, components);
//There should be at least 4 components
if ([components count] < 4)
return nil;
//Remove an initial "/" component
if ([[components objectAtIndex:0] isEqual:@"/"])
components = [components subarrayWithRange:NSMakeRange(1, [components count] - 1)];
components = [[NSArray arrayWithObject:[url host]] arrayByAddingObjectsFromArray:components];
// <docset-family>
NSString *docsetFamily = [components objectAtIndex:0];
if ([docsetFamily isEqual:@"mac"])
docsetFamily = @"macosx";
else if ([docsetFamily isEqual:@"iphone"])
docsetFamily = @"iphoneos";
// <docset-version>
NSString *docsetVersion = [components objectAtIndex:1];
NSFetchRequest *docsetFetch = [[NSFetchRequest alloc] init];
[docsetFetch setEntity:[NSEntityDescription entityForName:@"Docset" inManagedObjectContext:ctx]];
[docsetFetch setPredicate:[NSPredicate predicateWithFormat:@"platformFamily == %@ && platformVersion == %@", docsetFamily, docsetVersion]];
NSError *err = nil;
NSArray *docsets = [ctx executeFetchRequest:docsetFetch error:&err];
if (err || ![docsets count])
return nil;
IGKDocSetManagedObject *docset = [docsets objectAtIndex:0];
// <table-of-contents>
if (tocMaskPointer)
{
NSString *tableOfContents = [components objectAtIndex:2];
NSArray *tableOfContentsItems = [tableOfContents componentsSeparatedByString:@"."];
IGKHTMLDisplayTypeMask tocMask = IGKHTMLDisplayType_None;
for (NSString *n in tableOfContentsItems)
{
if ([n isEqual:@"all"])
tocMask |= IGKHTMLDisplayType_All;
else if ([n isEqual:@"overview"])
tocMask |= IGKHTMLDisplayType_Overview;
else if ([n isEqual:@"tasks"])
tocMask |= IGKHTMLDisplayType_Tasks;
else if ([n isEqual:@"properties"])
tocMask |= IGKHTMLDisplayType_Properties;
else if ([n isEqual:@"methods"])
tocMask |= IGKHTMLDisplayType_Methods;
else if ([n isEqual:@"notifications"])
tocMask |= IGKHTMLDisplayType_Notifications;
else if ([n isEqual:@"delegate"])
tocMask |= IGKHTMLDisplayType_Delegate;
else if ([n isEqual:@"misc"])
tocMask |= IGKHTMLDisplayType_Misc;
else if ([n isEqual:@"bindings"])
tocMask |= IGKHTMLDisplayType_BindingListings;
}
*tocMaskPointer = tocMask;
}
// <container-name> . <container-type>
IGKDocSetManagedObject *container = nil;
BOOL hasContainer = ([components count] >= 5);
if (hasContainer)
{
NSString *containerComponent = [components objectAtIndex:3];
NSString *containerName = [containerComponent stringByDeletingPathExtension];
NSString *containerExtension = [containerComponent pathExtension];
if (![containerName length] || ![containerExtension length])
return nil;
NSString *containerEntity = [self entityNameFromURLComponentExtension:containerExtension];
if (![containerEntity length])
return nil;
NSFetchRequest *containerFetchRequest = [[NSFetchRequest alloc] init];
[containerFetchRequest setEntity:[NSEntityDescription entityForName:containerEntity inManagedObjectContext:ctx]];
[containerFetchRequest setPredicate:[NSPredicate predicateWithFormat:@"name == %@ && docset == %@", containerName, docset]];
NSArray *containers = [ctx executeFetchRequest:containerFetchRequest error:&err];
if (err || ![containers count])
return nil;
container = [containers objectAtIndex:0];
}
// <item-name> . <item-type>
NSString *itemComponent = [components lastObject];
NSString *itemName = [itemComponent stringByDeletingPathExtension];
NSString *itemExtension = [itemComponent pathExtension];
if (![itemName length] || ![itemExtension length])
return nil;
if ([itemExtension isEqual:@"headerfile"])
{
//Get the path of the docset
// .../Developer/Documentation/DocSets/foo.docset
NSString *docsetPath = [docset valueForKey:@"path"];
//Get the path of the SDK
// .../Developer/SDKs/bar.sdk
NSString *developerDirectoryPath = [[[docsetPath stringByDeletingLastPathComponent] stringByDeletingLastPathComponent] stringByDeletingLastPathComponent];
NSString *sdkPath = [[developerDirectoryPath stringByAppendingPathComponent:@"SDKs"] stringByAppendingPathComponent:[docset sdkComponent]];
//Search in the SDK for itemName
NSArray *subpaths = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:sdkPath error:&err];
if (err || ![subpaths count])
return nil;
NSString *itemNameLower = [itemName lowercaseString];
for (NSString *subpath in subpaths)
{
if ([[[subpath lastPathComponent] lowercaseString] isEqual:itemNameLower])
{
NSString *fullpath = [sdkPath stringByAppendingPathComponent:subpath];
//Open the header in some application
BOOL worked = [[NSWorkspace sharedWorkspace] openFile:fullpath];
return nil;
}
}
return nil;
}
NSString *itemEntity = [self entityNameFromURLComponentExtension:itemExtension];
if (![itemEntity length])
return nil;
NSFetchRequest *itemFetchRequest = [[NSFetchRequest alloc] init];
[itemFetchRequest setEntity:[NSEntityDescription entityForName:itemEntity inManagedObjectContext:ctx]];
if (hasContainer)
[itemFetchRequest setPredicate:[NSPredicate predicateWithFormat:@"name == %@ && container == %@ && docset == %@", itemName, container, docset]];
else
[itemFetchRequest setPredicate:[NSPredicate predicateWithFormat:@"name == %@ && docset == %@", itemName, docset]];
NSArray *items = [ctx executeFetchRequest:itemFetchRequest error:&err];
if (err || ![items count])
return nil;
return [items objectAtIndex:0];
}
- (NSString *)pageTitle:(IGKHTMLDisplayTypeMask)mask
{
NSString *name = [self valueForKey:@"name"];
NSMutableArray *tocComponents = [[self class] tocComponentsForMask:mask];
//Remove "all"
[tocComponents removeObject:@"all"];
//Titleize
NSArray *tocComponentsArray = [tocComponents valueForKey:@"capitalizedString"];
//Join using commas
NSString *tocComponentsString = [tocComponentsArray componentsJoinedByString:@", "];
if ([tocComponentsString length])
return [name stringByAppendingFormat:@" (%@)", tocComponentsString];
else
return name;
}
- (NSMutableArray *)getSuperclasses
{
NSString *superclassName = [self valueForKey:@"superclassName"];
if (![superclassName length])
return nil;
NSMutableArray *superclasses = [[NSMutableArray alloc] init];
id superclass = self;
NSUInteger i;
for (i = 0; i < 10; i++)
{
NSString *superclassName = [superclass valueForKey:@"superclassName"];
if (![superclassName length])
break;
if ([superclassName isEqual:[superclasses lastObject]])
break;
[superclasses addObject:superclassName];
superclass = [[superclass findNearestClassWithName:superclassName] igk_firstObject];
if (!superclass)
break;
}
//If there's more than one superclass, get rid of the root class thing
if ([superclasses count] > 1)
{
for (NSString *sc in [superclasses copy])
{
if ([sc containsString:@"root class"])
{
[superclasses removeObject:sc];
}
}
}
return superclasses;
}
- (NSMutableArray *)getSubclasses
{
NSString *superclassName = [self valueForKey:@"superclassName"];
if (![superclassName length])
return nil;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"ObjCAbstractMethodContainer" inManagedObjectContext:[self managedObjectContext]]];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"superclassName=%@ && docset=%@", [self valueForKey:@"name"], [self valueForKey:@"docset"]]];
return [[[[self managedObjectContext] executeFetchRequest:fetchRequest error:NULL] valueForKey:@"name"] sortedArrayUsingSelector:@selector(compare:)];
}
- (id)findNearestClassWithName:(NSString *)className
{
NSLog(@"className = %@", className);
if (![className length])
return nil;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"ObjCAbstractMethodContainer" inManagedObjectContext:[self managedObjectContext]]];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"name=%@ && docset=%@", className, [self valueForKey:@"docset"]]];
return [[self managedObjectContext] executeFetchRequest:fetchRequest error:NULL];
}
+ (NSString *)entityNameFromURLComponentExtension:(NSString *)ext
{
if ([ext isEqual:@"class"])
return @"ObjCClass";
else if ([ext isEqual:@"functions"])
return @"FunctionContainer";
else if ([ext isEqual:@"category"])
return @"ObjCCategory";
else if ([ext isEqual:@"protocol"])
return @"ObjCProtocol";
else if ([ext isEqual:@"instance-method"])
return @"ObjCMethod";
else if ([ext isEqual:@"class-method"])
return @"ObjCMethod";
else if ([ext isEqual:@"property"])
return @"ObjCProperty";
else if ([ext isEqual:@"type"])
return @"CTypedef";
else if ([ext isEqual:@"enum"])
return @"CEnum";
else if ([ext isEqual:@"union"])
return @"CUnion";
else if ([ext isEqual:@"struct"])
return @"CStruct";
else if ([ext isEqual:@"function"])
return @"CFunction";
else if ([ext isEqual:@"global"])
return @"CGlobal";
else if ([ext isEqual:@"constant"])
return @"CConstant";
else if ([ext isEqual:@"macro"])
return @"CMacro";
else if ([ext isEqual:@"notification"])
return @"ObjCNotification";
else if ([ext isEqual:@"cpp-struct"])
return @"CppClassStruct";
else if ([ext isEqual:@"cpp-member-function"])
return @"CppMethod";
else if ([ext isEqual:@"cpp-namespace"])
return @"CppNamespace";
else if ([ext isEqual:@"bindings"])
return @"ObjCBindingsListing";
return @"DocRecord";
}
- (NSString *)URLComponentExtension
{
NSString *entityName = [[self entity] name];
if ([entityName isEqual:@"ObjCClass"])
return @"class";
if ([entityName isEqual:@"FunctionContainer"])
return @"functions";
else if ([entityName isEqual:@"ObjCCategory"])
return @"category";
else if([entityName isEqual:@"ObjCProtocol"])
return @"protocol";
else if([entityName isEqual:@"ObjCMethod"])
{
if ([[self valueForKey:@"isInstanceMethod"] boolValue])
return @"instance-method";
else
return @"class-method";
}
else if ([entityName isEqual:@"ObjCProperty"])
return @"property";
else if ([entityName isEqual:@"CTypedef"])
return @"type";
else if ([entityName isEqual:@"CEnum"])
return @"enum";
else if ([entityName isEqual:@"CUnion"])
return @"union";
else if ([entityName isEqual:@"CStruct"])
return @"struct";
else if ([entityName isEqual:@"CFunction"])
return @"function";
else if ([entityName isEqual:@"CGlobal"])
return @"global";
else if ([entityName isEqual:@"CConstant"])
return @"constant";
else if ([entityName isEqual:@"CMacro"])
return @"macro";
else if ([entityName isEqual:@"ObjCNotification"])
return @"notification";
else if ([entityName isEqual:@"CppClassStruct"])
return @"cpp-struct";
else if ([entityName isEqual:@"CppMethod"])
return @"cpp-member-function";
else if ([entityName isEqual:@"CppNamespace"])
return @"cpp-namespace";
else if ([entityName isEqual:@"ObjCBindingsListing"])
return @"bindings";
return @"unknown";
}
- (NSString *)URLComponent
{
NSString *n = [self valueForKey:@"name"];
if (!n)
n = @"unknown";
return [n stringByAppendingFormat:@".%@", [self URLComponentExtension]];
}
+ (NSMutableArray *)tocComponentsForMask:(IGKHTMLDisplayTypeMask)tocMask
{
NSMutableArray *tocComponents = [[NSMutableArray alloc] init];
if (tocMask & IGKHTMLDisplayType_All)
[tocComponents addObject:@"all"];
if (tocMask & IGKHTMLDisplayType_Overview)
[tocComponents addObject:@"overview"];
if (tocMask & IGKHTMLDisplayType_Tasks)
[tocComponents addObject:@"tasks"];
if (tocMask & IGKHTMLDisplayType_Properties)
[tocComponents addObject:@"properties"];
if (tocMask & IGKHTMLDisplayType_Methods)
[tocComponents addObject:@"methods"];
if (tocMask & IGKHTMLDisplayType_Notifications)
[tocComponents addObject:@"notifications"];
if (tocMask & IGKHTMLDisplayType_Delegate)
[tocComponents addObject:@"delegate"];
if (tocMask & IGKHTMLDisplayType_Misc)
[tocComponents addObject:@"misc"];
if (tocMask & IGKHTMLDisplayType_BindingListings)
[tocComponents addObject:@"bindings"];
if (![tocComponents count])
[tocComponents addObject:@"all"];
return tocComponents;
}
- (NSURL *)docURL:(IGKHTMLDisplayTypeMask)tocMask
{
IGKDocSetManagedObject *docset = [self valueForKey:@"docset"];
NSString *host = [docset shortPlatformName];
NSString *containerComponent = nil;
if ([self hasKey:@"container"])
containerComponent = [[self valueForKey:@"container"] URLComponent];
NSString *itemComponent = [self URLComponent];
NSString *tocComponent = nil;
NSArray *tocComponents = [[self class] tocComponentsForMask:tocMask];
tocComponent = [tocComponents componentsJoinedByString:@"."];
NSString *path = @"unknown";
if ([containerComponent length])
{
if ([itemComponent length])
path = [containerComponent stringByAppendingPathComponent:itemComponent];
else
path = containerComponent;
}
else
{
if ([itemComponent length])
path = itemComponent;
}
path = [[[@"/" stringByAppendingPathComponent:[docset shortVersionName]] stringByAppendingPathComponent:tocComponent] stringByAppendingPathComponent:path];
return [[NSURL alloc] initWithScheme:@"ingr-doc" host:host path:path];
}
- (NSString *)documentPath
{
return [[self valueForKey:@"heavyNonQueryables"] valueForKey:@"documentPath"];
}
- (void)setDocumentPath:(NSString *)docpath
{
[[self valueForKey:@"heavyNonQueryables"] setValue:docpath forKey:@"documentPath"];
}
- (void)awakeFromInsert
{
[super awakeFromInsert];
[self setValue:[NSNumber numberWithShort:[self priorityval]] forKey:@"priority"];
//DocRecordHeavyNonQueryables is an entity that holds medium-sized attributes of DocRecord that aren't directly queried (they aren't filtered in a WHERE clause)
NSEntityDescription *heavyNonQEntity = [NSEntityDescription entityForName:@"DocRecordHeavyNonQueryables" inManagedObjectContext:[self managedObjectContext]];
NSManagedObject *heavyNonQs = [[NSManagedObject alloc] initWithEntity:heavyNonQEntity insertIntoManagedObjectContext:[self managedObjectContext]];
[self setValue:heavyNonQs forKey:@"heavyNonQueryables"];
}
- (CHRecordPriority)priorityval
{
NSString *entityName = [[self entity] name];
if([entityName isEqual:@"ObjCMethod"] || [entityName isEqual:@"ObjCProperty"])
return CHPriorityMethod;
if([entityName isEqual:@"ObjCBindingsListing"])
return CHPriorityBindings;
else if([entityName isEqual:@"ObjCClass"])
return CHPriorityClass;
else if([entityName isEqual:@"FunctionContainer"])
return CHPriorityFunctionContainer;
else if([entityName isEqual:@"ObjCProtocol"])
return CHPriorityProtocol;
else if([entityName isEqual:@"ObjCCategory"])
return CHPriorityCategory;
else if([entityName isEqual:@"CTypedef"])
return CHPriorityType;
else if([entityName isEqual:@"CEnum"])
return CHPriorityType;
else if([entityName isEqual:@"CStruct"])
return CHPriorityType;
else if([entityName isEqual:@"CFunction"])
return CHPriorityFunction;
else if([entityName isEqual:@"CMacro"])
return CHPriorityFunction;
else if([entityName isEqual:@"CppClassStruct"])
return CHPriorityClass;
else if([entityName isEqual:@"CppMethod"])
return CHPriorityMethod;
else if([entityName isEqual:@"CUnion"])
return CHPriorityType;
return CHPriorityOther;
}
- (NSString *)xcontainername
{
return [[self xcontainer] valueForKey:@"name"];
}
- (IGKDocRecordManagedObject *)xcontainer
{
NSDictionary *relationships = [[self entity] relationshipsByName];
if ([relationships objectForKey:@"container"])
return [self valueForKey:@"container"];
return nil;
//return [self valueForKey:@"misccontainer"];
}
- (NSString *)xsuperclassname
{
return [self valueForSoftKey:@"superclassName"];
}
- (NSString *)xconforms
{
return @"";
}
- (NSString *)xdocset
{
return [[self valueForKey:@"docset"] localizedUserInterfaceName];
}
- (NSImage *)normalIcon
{
return [self iconForSelectedState:NO];
}
- (NSImage *)selectedIcon
{
return [self iconForSelectedState:YES];
}
- (CHSymbolButtonImageMask)iconMask
{
NSString *entityName = [[self entity] name];
return [[self class] iconMaskForEntity:entityName isInstanceMethod:[[self valueForSoftKey:@"isInstanceMethod"] boolValue]];
}
+ (CHSymbolButtonImageMask)iconMaskForEntity:(NSString *)entityName isInstanceMethod:(BOOL)instanceMethod
{
//FIXME: Maybe it would be better to use an NSDictionary -> NSNumber here instead
if([entityName isEqual:@"ObjCClass"])
return CHSymbolButtonObjcClass;
if([entityName isEqual:@"FunctionContainer"])
return CHSymbolButtonFunctionContainer;
else if([entityName isEqual:@"ObjCCategory"])
return CHSymbolButtonObjcCategory;
else if([entityName isEqual:@"ObjCProtocol"])
return CHSymbolButtonObjcProtocol;
else if([entityName isEqual:@"ObjCMethod"])
{
if (instanceMethod)
return CHSymbolButtonObjcMethod;
else
return CHSymbolButtonObjcMethod | CHSymbolButtonStaticScope;
}
else if([entityName isEqual:@"ObjCProperty"])
return CHSymbolButtonObjcProperty;
else if([entityName isEqual:@"CTypedef"])
return CHSymbolButtonTypedef;
else if([entityName isEqual:@"CEnum"])
return CHSymbolButtonEnum;
else if([entityName isEqual:@"CUnion"])
return CHSymbolButtonUnion;
else if([entityName isEqual:@"CStruct"])
return CHSymbolButtonStruct;
else if([entityName isEqual:@"CFunction"])
return CHSymbolButtonFunction;
else if([entityName isEqual:@"CGlobal"] || [entityName isEqual:@"CConstant"])
return CHSymbolButtonVariable | CHSymbolButtonGlobalScope;
else if([entityName isEqual:@"CMacro"])
return CHSymbolButtonMacro;
else if([entityName isEqual:@"ObjCNotification"])
return CHSymbolButtonNotification;
else if([entityName isEqual:@"CppClassStruct"])
return CHSymbolButtonCppClass;
else if([entityName isEqual:@"CppMethod"])
return CHSymbolButtonFunction | CHSymbolButtonInstanceScope;
else if([entityName isEqual:@"CppNamespace"])
return CHSymbolButtonCppNamespace;
else if([entityName isEqual:@"ObjCBindingsListing"])
return CHSymbolButtonObjcBindingsListing;
return 0;
}
- (NSImage *)iconForSelectedState:(BOOL)isSelected
{
NSInteger index = isSelected ? 1 : 0;
CHSymbolButtonImageMask mask = [self iconMask];
if (!mask)
return nil;
return [[CHSymbolButtonImage symbolImageWithMask:mask] objectAtIndex:index];
}
- (NSNumber *)lengthOfContent
{
return [self valueForKey:@"contentsLength"];
}
@end