From 3cc8ce2d5e7ba9fe3bb9a78348a1f5bb56182bf3 Mon Sep 17 00:00:00 2001 From: Bibiko Date: Wed, 29 Apr 2009 21:40:58 +0000 Subject: =?UTF-8?q?=E2=80=A2=20MySQL=20Help=20-=20first=20trial=20to=20imp?= =?UTF-8?q?lement=20an=20"autoHelp"=20function=20This=20function=20calls?= =?UTF-8?q?=20showHelpForCurrentWord=20(or=20selection)=201=20sec=20after?= =?UTF-8?q?=20stopping=20typing=20and=20shows=20the=20Help=20in=20the=20He?= =?UTF-8?q?lp=20window=20if=20the=20caret=20is=20not=20inside=20of=20quote?= =?UTF-8?q?s.=20To=20invoke=20it=20set=20autohelpEnabled=3DYES=20in=20CMTe?= =?UTF-8?q?xtView.m's=20awakeFromNib=20method.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/CMTextView.h | 4 ++++ Source/CMTextView.m | 68 ++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 61 insertions(+), 11 deletions(-) (limited to 'Source') diff --git a/Source/CMTextView.h b/Source/CMTextView.h index 4dee7079..67a553b6 100644 --- a/Source/CMTextView.h +++ b/Source/CMTextView.h @@ -30,6 +30,7 @@ BOOL autoindentIgnoresEnter; BOOL autouppercaseKeywordsEnabled; BOOL delBackwardsWasPressed; + BOOL autohelpEnabled; NoodleLineNumberView *lineNumberView; NSString *lookupInDocumentationTitle; @@ -52,7 +53,10 @@ - (BOOL) autopair; - (void) setAutouppercaseKeywords:(BOOL)enableAutouppercaseKeywords; - (BOOL) autouppercaseKeywords; +- (void) setAutohelp:(BOOL)enableAutohelp; +- (BOOL) autohelp; - (void) selectLineNumber:(unsigned int)lineNumber ignoreLeadingNewLines:(BOOL)ignLeadingNewLines; - (unsigned int) getLineNumberForCharacterIndex:(unsigned int)anIndex; +- (void) autoHelp; @end diff --git a/Source/CMTextView.m b/Source/CMTextView.m index 01037358..89262a4b 100644 --- a/Source/CMTextView.m +++ b/Source/CMTextView.m @@ -40,7 +40,8 @@ YY_BUFFER_STATE yy_scan_string (const char *); #define kAPlinked @"Linked" // attribute for a via auto-pair inserted char #define kAPval @"linked" #define kWQquoted @"Quoted" // set via lex to indicate a quoted string -#define kWQval @"quoted" +#define kWquoted @"isQuoted" +#define kWQval @"aValue" #define kSQLkeyword @"SQLkw" // attribute for found SQL keywords #define kQuote @"Quote" @@ -241,6 +242,11 @@ YY_BUFFER_STATE yy_scan_string (const char *); - (void) keyDown:(NSEvent *)theEvent { + if(autohelpEnabled) // cancel autoHelp request + [NSObject cancelPreviousPerformRequestsWithTarget:self + selector:@selector(autoHelp) + object:nil]; + long allFlags = (NSShiftKeyMask|NSControlKeyMask|NSAlternateKeyMask|NSCommandKeyMask); // Check if user pressed ⌥ to allow composing of accented characters. @@ -401,7 +407,9 @@ YY_BUFFER_STATE yy_scan_string (const char *); // The default action is to perform the normal key-down action. [super keyDown:theEvent]; - + if(autohelpEnabled) + [self performSelector:@selector(autoHelp) withObject:nil afterDelay:1]; + } @@ -1653,6 +1661,14 @@ YY_BUFFER_STATE yy_scan_string (const char *); autopairEnabled = enableAutopair; } +/* + * Set whether MySQL Help should be automatically invoked while typing. + */ +- (void)setAutohelp:(BOOL)enableAutohelp +{ + autohelpEnabled = enableAutohelp; +} + /* * Retrieve whether this text view automatically creates the matching closing char for ", ', ` and ( chars. */ @@ -1670,13 +1686,21 @@ YY_BUFFER_STATE yy_scan_string (const char *); } /* - * Retrieve whether SQL keywords should be automaticallyuppercased. + * Retrieve whether SQL keywords should be automatically uppercased. */ - (BOOL)autouppercaseKeywords { return autouppercaseKeywordsEnabled; } +/* + * Retrieve whether MySQL Help should be automatically invoked while typing. + */ +- (BOOL)autohelp +{ + return autohelpEnabled; +} + /******************* SYNTAX HIGHLIGHTING! @@ -1693,14 +1717,34 @@ SYNTAX HIGHLIGHTING! autopairEnabled = YES; autoindentIgnoresEnter = NO; autouppercaseKeywordsEnabled = YES; + autohelpEnabled = NO; delBackwardsWasPressed = NO; - lineNumberView = [[NoodleLineNumberView alloc] initWithScrollView:scrollView]; - [scrollView setVerticalRulerView:lineNumberView]; - [scrollView setHasHorizontalRuler:NO]; - [scrollView setHasVerticalRuler:YES]; - [scrollView setRulersVisible:YES]; + lineNumberView = [[NoodleLineNumberView alloc] initWithScrollView:scrollView]; + [scrollView setVerticalRulerView:lineNumberView]; + [scrollView setHasHorizontalRuler:NO]; + [scrollView setHasVerticalRuler:YES]; + [scrollView setRulersVisible:YES]; + +} + + +- (void)autoHelp +{ + if(!autohelpEnabled) return; + + if([self selectedRange].length) + { + [[[[self window] delegate] valueForKeyPath:@"customQueryInstance"] performSelector:@selector(showHelpForCurrentWord:) withObject:self afterDelay:0.1]; + return; + } + + long cursorPosition = [self selectedRange].location; + if (cursorPosition >= [[self string] length]) cursorPosition--; + if(cursorPosition > -1 && (![[[self textStorage] attribute:kQuote atIndex:cursorPosition effectiveRange:nil] isEqualToString:kWquoted]||[[self textStorage] attribute:kSQLkeyword atIndex:cursorPosition effectiveRange:nil])) + [[[[self window] delegate] valueForKeyPath:@"customQueryInstance"] performSelector:@selector(showHelpForCurrentWord:) withObject:self afterDelay:0.1]; + } - (void)textStorageDidProcessEditing:(NSNotification *)notification @@ -1714,6 +1758,8 @@ SYNTAX HIGHLIGHTING! * Some sample code from Andrew Choi ( http://members.shaw.ca/akochoi-old/blog/2003/11-09/index.html#3 ) has been reused. */ { + + NSTextStorage *textStore = [notification object]; //make sure that the notification is from the correct textStorage object @@ -1739,7 +1785,6 @@ SYNTAX HIGHLIGHTING! //first remove the old colors [textStore removeAttribute:NSForegroundColorAttributeName range:textRange]; - //initialise flex yyuoffset = 0; yyuleng = 0; yy_switch_to_buffer(yy_scan_string([[textStore string] UTF8String])); @@ -1823,11 +1868,12 @@ SYNTAX HIGHLIGHTING! range: tokenRange ]; // Add an attribute to be used to distinguish quotes from keywords etc. // used e.g. in completion suggestions - if(token == SPT_DOUBLE_QUOTED_TEXT || token == SPT_SINGLE_QUOTED_TEXT) + if(token == SPT_DOUBLE_QUOTED_TEXT || token == SPT_SINGLE_QUOTED_TEXT || SPT_BACKTICK_QUOTED_TEXT) [textStore addAttribute: kQuote - value: kWQval + value: kWquoted range: tokenRange ]; } + } @end -- cgit v1.2.3