From 7e34cf68b09d6b95bfa29e1233b44370adfd18b3 Mon Sep 17 00:00:00 2001 From: Bibiko Date: Tue, 9 Nov 2010 21:23:38 +0000 Subject: =?UTF-8?q?=E2=80=A2=20further=20preparations=20for=20user-definab?= =?UTF-8?q?le=20bundle=20support?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/SPFileManagerAdditions.h | 1 + Source/SPFileManagerAdditions.m | 24 ++++++++++----- Source/SPTextView.h | 4 ++- Source/SPTextView.m | 65 ++++++++++++++++++++++++++++++++++------- 4 files changed, 75 insertions(+), 19 deletions(-) (limited to 'Source') diff --git a/Source/SPFileManagerAdditions.h b/Source/SPFileManagerAdditions.h index c12040e5..6ac7811c 100644 --- a/Source/SPFileManagerAdditions.h +++ b/Source/SPFileManagerAdditions.h @@ -25,5 +25,6 @@ @interface NSFileManager (SPFileManagerAdditions) - (NSString *)applicationSupportDirectoryForSubDirectory:(NSString*)subDirectory error:(NSError **)errorOut; +- (NSString *)applicationSupportDirectoryForSubDirectory:(NSString*)subDirectory createIfNotExists:(BOOL)create error:(NSError **)errorOut; @end diff --git a/Source/SPFileManagerAdditions.m b/Source/SPFileManagerAdditions.m index dec4bd3f..2ea9fb71 100644 --- a/Source/SPFileManagerAdditions.m +++ b/Source/SPFileManagerAdditions.m @@ -35,12 +35,16 @@ NSString* const DirectoryLocationDomain = @"DirectoryLocationDomain"; @implementation NSFileManager (SPFileManagerAdditions) -/* +/** * Return the application support folder of the current application for 'subDirectory'. * If this folder doesn't exist it will be created. If 'subDirectory' == nil it only returns * the application support folder of the current application. */ - (NSString*)applicationSupportDirectoryForSubDirectory:(NSString*)subDirectory error:(NSError **)errorOut +{ + return [self applicationSupportDirectoryForSubDirectory:subDirectory createIfNotExists:YES error:errorOut]; +} +- (NSString *)applicationSupportDirectoryForSubDirectory:(NSString*)subDirectory createIfNotExists:(BOOL)create error:(NSError **)errorOut; { // Based on Matt Gallagher on 06 May 2010 // @@ -113,14 +117,18 @@ NSString* const DirectoryLocationDomain = @"DirectoryLocationDomain"; } return nil; } - - // Create the path if it doesn't exist - NSError *error = nil; - BOOL success = [self createDirectoryAtPath:resolvedPath withIntermediateDirectories:YES attributes:nil error:&error]; - if (!success) { - if (errorOut) { - *errorOut = error; + + if(create) { + // Create the path if it doesn't exist + NSError *error = nil; + BOOL success = [self createDirectoryAtPath:resolvedPath withIntermediateDirectories:YES attributes:nil error:&error]; + if (!success) { + if (errorOut) { + *errorOut = error; + } + return nil; } + } else { return nil; } } diff --git a/Source/SPTextView.h b/Source/SPTextView.h index e0cb6a32..f5529e9e 100644 --- a/Source/SPTextView.h +++ b/Source/SPTextView.h @@ -86,7 +86,7 @@ NSRange queryRange; BOOL shouldHiliteQuery; - NSArray *bundleItems; + NSMutableArray *bundleItems; } @@ -148,4 +148,6 @@ - (void)reloadBundleItems; +- (IBAction)executeBundleItem:(id)sender; + @end diff --git a/Source/SPTextView.m b/Source/SPTextView.m index 910ddd6b..bbc8d1f4 100644 --- a/Source/SPTextView.m +++ b/Source/SPTextView.m @@ -130,7 +130,7 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse) isProcessingMirroredSnippets = NO; completionWasRefreshed = NO; - bundleItems = nil; + bundleItems = [[NSMutableArray alloc] initWithCapacity:1]; lineNumberView = [[NoodleLineNumberView alloc] initWithScrollView:scrollView]; [scrollView setVerticalRulerView:lineNumberView]; @@ -2904,8 +2904,42 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse) [[menu itemAtIndex:6] setHidden:YES]; } - if(bundleItems && [bundleItems count]) { - // TODO add bundle sub menu; + [self reloadBundleItems]; + + // Remove 'Bundles' sub menu and separator + NSMenuItem *bItem = [menu itemWithTag:10000000]; + if(bItem) { + NSInteger sepIndex = [menu indexOfItem:bItem]-1; + [menu removeItemAtIndex:sepIndex]; + [menu removeItem:bItem]; + } + + // Add 'Bundles' sub menu for custom query editor only so far if bundles with scope 'editor' were found + if(customQueryInstance && bundleItems && [bundleItems count]) { + [menu addItem:[NSMenuItem separatorItem]]; + NSMenuItem *bundleSubMenuItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Bundles", @"bundles menu item label") action:nil keyEquivalent:@""]; + [bundleSubMenuItem setTag:10000000]; + NSMenu *bundleMenu = [[[NSMenu alloc] init] autorelease]; + NSInteger i = 0; + for(NSDictionary *item in bundleItems) { + NSString *keyEq = [item objectForKey:@"keyEquivalent"]; + NSMenuItem *mItem = [[[NSMenuItem alloc] initWithTitle:[item objectForKey:@"label"] action:@selector(executeBundleItem:) keyEquivalent:[keyEq substringFromIndex:[keyEq length]-1]] autorelease]; + NSUInteger mask = 0; + if([keyEq rangeOfString:@"^"].length) + mask = mask | NSControlKeyMask; + if([keyEq rangeOfString:@"@"].length) + mask = mask | NSCommandKeyMask; + if([keyEq rangeOfString:@"~"].length) + mask = mask | NSAlternateKeyMask; + [mItem setKeyEquivalentModifierMask:mask]; + if([item objectForKey:@"tooltip"]) + [mItem setToolTip:[item objectForKey:@"tooltip"]]; + [mItem setTag:1000000 + i++]; + [bundleMenu addItem:mItem]; + } + [menu addItem:bundleSubMenuItem]; + [menu setSubmenu:bundleMenu forItem:bundleSubMenuItem]; + [bundleSubMenuItem release]; } return menu; @@ -3330,8 +3364,8 @@ return menu; - (void)reloadBundleItems { - NSFileManager *fm = [NSFileManager defaultManager]; - NSString *bundlePath = [[NSFileManager defaultManager] applicationSupportDirectoryForSubDirectory:SPBundleSupportFolder error:nil]; + return; //TODO + NSString *bundlePath = [[NSFileManager defaultManager] applicationSupportDirectoryForSubDirectory:SPBundleSupportFolder createIfNotExists:NO error:nil]; if(bundlePath) { NSError *error = nil; @@ -3344,8 +3378,9 @@ return menu; NSString *convError = nil; NSPropertyListFormat format; NSDictionary *cmdData = nil; - if(bundleItems) [bundleItems retain]; - NSData *pData = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@/%@/info.plist", bundlePath, bundle] options:NSUncachedRead error:&readError]; + [bundleItems removeAllObjects]; + NSString *infoPath = [NSString stringWithFormat:@"%@/%@/info.plist", bundlePath, bundle]; + NSData *pData = [NSData dataWithContentsOfFile:infoPath options:NSUncachedRead error:&readError]; cmdData = [[NSPropertyListSerialization propertyListFromData:pData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&convError] retain]; @@ -3356,14 +3391,24 @@ return menu; if (cmdData) [cmdData release]; } else { if([cmdData objectForKey:@"name"] && [[cmdData objectForKey:@"name"] length] - && [cmdData objectForKey:@"scope"] && [[cmdData objectForKey:@"scope"] isEqualToString:@"editor"]) - [bundleItems addObject:[cmdData objectForKey:@"name"]]; + && [cmdData objectForKey:@"scope"] && [[cmdData objectForKey:@"scope"] isEqualToString:@"editor"]) { + [bundleItems addObject:[NSDictionary dictionaryWithObjectsAndKeys:[cmdData objectForKey:@"name"], @"label", infoPath, @"path", [cmdData objectForKey:@"keyEquivalent"], @"keyEquivalent", nil]]; + } if (cmdData) [cmdData release]; } } } } + +} + +- (IBAction)executeBundleItem:(id)sender +{ + NSInteger idx = [sender tag] - 1000000; + if(idx >=0 && idx < [bundleItems count]) { + NSLog(@"%@", [[bundleItems objectAtIndex:idx] objectForKey:@"path"]); + } } #pragma mark - @@ -3390,7 +3435,7 @@ return menu; [prefs removeObserver:self forKeyPath:SPCustomQueryEditorTabStopWidth]; [prefs removeObserver:self forKeyPath:SPCustomQueryAutoUppercaseKeywords]; - if(bundleItems) [bundleItems retain]; + if(bundleItems) [bundleItems release]; if (completionIsOpen) [completionPopup close], completionIsOpen = NO; [prefs release]; -- cgit v1.2.3