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/SPTextView.m | 65 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 10 deletions(-) (limited to 'Source/SPTextView.m') 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