From 729a8ba1c4d8ffe72d9f43addb5afac78b1df599 Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Sun, 19 Apr 2009 17:05:13 +0000 Subject: Add a slightly modified patch to lookup up the selected text in the custom query editor in the MySQL online documentation. Code contributed via issue #236. --- Source/CMTextView.h | 2 ++ Source/CMTextView.m | 55 +++++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/Source/CMTextView.h b/Source/CMTextView.h index 55bff15f..b7748ef9 100644 --- a/Source/CMTextView.h +++ b/Source/CMTextView.h @@ -32,6 +32,8 @@ BOOL delBackwardsWasPressed; NoodleLineNumberView *lineNumberView; + NSString *lookupInDocumentationTitle; + IBOutlet NSScrollView *scrollView; } diff --git a/Source/CMTextView.m b/Source/CMTextView.m index 427c66d1..c66fa134 100644 --- a/Source/CMTextView.m +++ b/Source/CMTextView.m @@ -41,9 +41,58 @@ YY_BUFFER_STATE yy_scan_string (const char *); #define kSQLkeyword @"SQLkw" // attribute for found SQL keywords #define kQuote @"Quote" +#define MYSQL_DOC_SEARCH_URL @"http://search.mysql.com/search?q=%@" @implementation CMTextView +/* + * Add a menu item to context menu for looking up mysql documentation. + */ +- (NSMenu *)menuForEvent:(NSEvent *)event +{ + // Set title of the menu item + lookupInDocumentationTitle = NSLocalizedString(@"Lookup In MySQL Documentation", @"Lookup In MySQL Documentation"); + + // Add the menu item if it doesn't yet exist + NSMenu *menu = [[self class] defaultMenu]; + + if ([[[self class] defaultMenu] itemWithTitle:lookupInDocumentationTitle] == nil) { + + [menu insertItem:[NSMenuItem separatorItem] atIndex:3]; + [menu insertItemWithTitle:lookupInDocumentationTitle action:@selector(lookupSelectionInDocumentation) keyEquivalent:@"" atIndex:4]; + } + + return menu; +} + +/* + * Open a search for the current selection on mysql.com + */ +- (void)lookupSelectionInDocumentation +{ + // Get the current selection and encode it to be used in a URL + NSString *keyword = [[[[self string] substringWithRange:[self selectedRange]] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; + + // Open MySQL Documentation search in browser using the terms + NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:MYSQL_DOC_SEARCH_URL, keyword]]; + + [[NSWorkspace sharedWorkspace] openURL:url]; +} + +/* + * Disable the lookup in documentation function when there is no selection. + */ +- (BOOL)validateMenuItem:(NSMenuItem *)menuItem +{ + // Enable or disable the lookup in documentation menu item depending on whether there is a + // selection and whether it is a reasonable length. + if ([menuItem action] == @selector(lookupSelectionInDocumentation)) { + return (([self selectedRange].length) || ([self selectedRange].length > 256)); + } + + return YES; +} + /* * Checks if the char after the current caret position/selection matches a supplied attribute */ @@ -61,7 +110,6 @@ YY_BUFFER_STATE yy_scan_string (const char *); return NO; } - /* * Checks if the caret is wrapped by auto-paired characters. * e.g. [| := caret]: "|" @@ -94,7 +142,6 @@ YY_BUFFER_STATE yy_scan_string (const char *); return NO; } - /* * If the textview has a selection, wrap it with the supplied prefix and suffix strings; * return whether or not any wrap was performed. @@ -133,10 +180,8 @@ YY_BUFFER_STATE yy_scan_string (const char *); [pb declareTypes:[NSArray arrayWithObject:NSRTFPboardType] owner:self]; [pb setData:rtf forType:NSRTFPboardType]; } - } - /* * Handle some keyDown events in order to provide autopairing functionality (if enabled). */ @@ -1301,7 +1346,6 @@ SYNTAX HIGHLIGHTING! //make sure that the notification is from the correct textStorage object if (textStore!=[self textStorage]) return; - NSColor *commentColor = [NSColor colorWithDeviceRed:0.000 green:0.455 blue:0.000 alpha:1.000]; NSColor *quoteColor = [NSColor colorWithDeviceRed:0.769 green:0.102 blue:0.086 alpha:1.000]; NSColor *keywordColor = [NSColor colorWithDeviceRed:0.200 green:0.250 blue:1.000 alpha:1.000]; @@ -1411,7 +1455,6 @@ SYNTAX HIGHLIGHTING! value: kWQval range: tokenRange ]; } - } @end -- cgit v1.2.3