From b7a565dd0196dcd77143874b27162d4300182019 Mon Sep 17 00:00:00 2001 From: rowanbeentje Date: Fri, 3 Apr 2009 00:08:28 +0000 Subject: =?UTF-8?q?=20-=20Add=20the=20ability=20for=20CMTextView=20to=20au?= =?UTF-8?q?tomatically=20capitalise=20SQL=20keywords=20in=20the=20text=20v?= =?UTF-8?q?iew,=20currently=20off=20by=20default=20but=20saved=20from=20pr?= =?UTF-8?q?eferences.=20=20=20Thanks=20again=20to=20Hans-J=C3=B6rg=20Bibik?= =?UTF-8?q?o=20for=20this=20patch;=20see=20Issue=20#218.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/CMTextView.h | 3 ++ Source/CMTextView.m | 87 ++++++++++++++++++++++++++++++++++++------------- Source/CustomQuery.h | 1 + Source/CustomQuery.m | 11 +++++++ Source/MainController.m | 1 + 5 files changed, 81 insertions(+), 22 deletions(-) (limited to 'Source') diff --git a/Source/CMTextView.h b/Source/CMTextView.h index 4dadb33d..cec4c67b 100644 --- a/Source/CMTextView.h +++ b/Source/CMTextView.h @@ -27,6 +27,7 @@ BOOL autoindentEnabled; BOOL autopairEnabled; BOOL autoindentIgnoresEnter; + BOOL autouppercaseKeywordsEnabled; } - (BOOL) isNextCharMarkedBy:(id)attribute; @@ -42,5 +43,7 @@ - (BOOL) autoindentIgnoresEnter; - (void) setAutopair:(BOOL)enableAutopair; - (BOOL) autopair; +- (void) setAutouppercaseKeywords:(BOOL)enableAutouppercaseKeywords; +- (BOOL) autouppercaseKeywords; @end diff --git a/Source/CMTextView.m b/Source/CMTextView.m index 67d2b39b..7685728d 100644 --- a/Source/CMTextView.m +++ b/Source/CMTextView.m @@ -34,10 +34,11 @@ typedef struct yy_buffer_state *YY_BUFFER_STATE; void yy_switch_to_buffer(YY_BUFFER_STATE); 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 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 kSQLkeyword @"SQLkw" // attribute for found SQL keywords @implementation CMTextView @@ -487,6 +488,20 @@ YY_BUFFER_STATE yy_scan_string (const char *); } +/* + * Hook to invoke the auto-uppercasing of SQL keywords after pasting + */ +- (void)paste:(id)sender +{ + // Insert the content of the pasteboard + NSPasteboard *pb = [NSPasteboard generalPasteboard]; + [self insertText:[pb stringForType:NSStringPboardType]]; + + // Invoke the auto-uppercasing of SQL keywords via an additional trigger + [self insertText:@""]; +} + + /* List of keywords for autocompletion. If you add a keyword here, it should also be added to the flex file SPEditorTokens.l @@ -817,15 +832,38 @@ it should also be added to the flex file SPEditorTokens.l return autopairEnabled; } +/* + * Set whether SQL keywords should be automatically uppercased. + */ +- (void)setAutouppercaseKeywords:(BOOL)enableAutouppercaseKeywords +{ + autouppercaseKeywordsEnabled = enableAutouppercaseKeywords; +} + +/* + * Retrieve whether SQL keywords should be automaticallyuppercased. + */ +- (BOOL)autouppercaseKeywords +{ + return autouppercaseKeywordsEnabled; +} + + /******************* SYNTAX HIGHLIGHTING! *******************/ - (void)awakeFromNib /* -sets self as delegate for the textView's textStorage to enable syntax highlighting -*/ + * Sets self as delegate for the textView's textStorage to enable syntax highlighting, + * and set defaults for general usage + */ { [[self textStorage] setDelegate:self]; + + autoindentEnabled = YES; + autopairEnabled = YES; + autoindentIgnoresEnter = NO; + autouppercaseKeywordsEnabled = YES; } - (void)textStorageDidProcessEditing:(NSNotification *)notification @@ -896,31 +934,36 @@ sets self as delegate for the textView's textStorage to enable syntax highlighti // otherwise a bug in the lex code could cause the the TextView to crash tokenRange = NSIntersectionRange(tokenRange, textRange); if (!tokenRange.length) continue; - + + // Is the current token is marked as SQL keyword, uppercase it if required. + if (autouppercaseKeywordsEnabled && + [[self textStorage] attribute:kSQLkeyword atIndex:tokenRange.location effectiveRange:nil]) + { + // Note: Register it for undo doesn't work ?=> unreliable single char undo + // Replace it + [self replaceCharactersInRange:tokenRange withString:[[[self string] substringWithRange:tokenRange] uppercaseString]]; + } + [textStore addAttribute: NSForegroundColorAttributeName value: tokenColor range: tokenRange ]; - // this attr is used in the auto-pairing (keyDown:) - // to disable auto-pairing if caret is inside of any token found by lex - // maybe change it later (only for quotes) => discussion + + // Add an attribute to be used in the auto-pairing (keyDown:) + // to disable auto-pairing if caret is inside of any token found by lex. + // For discussion: maybe change it later (only for quotes not keywords?) [textStore addAttribute: kWQquoted value: kWQval range: tokenRange ]; + + // Mark each SQL keyword for auto-uppercasing and do it for the next textStorageDidProcessEditing: event. + // Performing it one token later allows words which start as reserved keywords to be entered. + if(token == SPT_RESERVED_WORD) + [textStore addAttribute: kSQLkeyword + value: kWQval + range: tokenRange ]; } } -- (id) init -{ - if (self = [super init]) { - autoindentEnabled = YES; - autopairEnabled = YES; - autoindentIgnoresEnter = NO; - } - - return self; -} - - @end diff --git a/Source/CustomQuery.h b/Source/CustomQuery.h index 9b75c906..5d314316 100644 --- a/Source/CustomQuery.h +++ b/Source/CustomQuery.h @@ -53,6 +53,7 @@ IBOutlet NSMenuItem *completionListMenuItem; IBOutlet NSMenuItem *autoindentMenuItem; IBOutlet NSMenuItem *autopairMenuItem; + IBOutlet NSMenuItem *autouppercaseKeywordsMenuItem; NSArray *queryResult; NSUserDefaults *prefs; diff --git a/Source/CustomQuery.m b/Source/CustomQuery.m index 3d6c18d0..6b89f6a4 100644 --- a/Source/CustomQuery.m +++ b/Source/CustomQuery.m @@ -201,6 +201,15 @@ closes the sheet [autopairMenuItem setState:enableAutopair?NSOnState:NSOffState]; [textView setAutopair:enableAutopair]; } + + // "Auto-uppercase keywords" toggle + if (sender == autouppercaseKeywordsMenuItem) { + BOOL enableAutouppercaseKeywords = ([autouppercaseKeywordsMenuItem state] == NSOffState); + [prefs setBool:enableAutouppercaseKeywords forKey:@"CustomQueryAutouppercaseKeywords"]; + [prefs synchronize]; + [autouppercaseKeywordsMenuItem setState:enableAutouppercaseKeywords?NSOnState:NSOffState]; + [textView setAutouppercaseKeywords:enableAutouppercaseKeywords]; + } } @@ -603,6 +612,8 @@ sets the connection (received from TableDocument) and makes things that have to [textView setAutoindentIgnoresEnter:YES]; [autopairMenuItem setState:([prefs boolForKey:@"CustomQueryAutopair"]?NSOnState:NSOffState)]; [textView setAutopair:[prefs boolForKey:@"CustomQueryAutopair"]]; + [autouppercaseKeywordsMenuItem setState:([prefs boolForKey:@"CustomQueryAutouppercaseKeywords"]?NSOnState:NSOffState)]; + [textView setAutouppercaseKeywords:[prefs boolForKey:@"CustomQueryAutouppercaseKeywords"]]; [queryFavoritesView registerForDraggedTypes:[NSArray arrayWithObjects:@"SequelProPasteboard", nil]]; while ( (column = [enumerator nextObject]) ) { diff --git a/Source/MainController.m b/Source/MainController.m index 4e21c4d5..f68c69e3 100644 --- a/Source/MainController.m +++ b/Source/MainController.m @@ -673,6 +673,7 @@ checks for updates and opens download page in default browser [NSNumber numberWithInt:0], @"lastUsedVersion", [NSNumber numberWithBool:YES], @"CustomQueryAutopair", [NSNumber numberWithBool:YES], @"CustomQueryAutoindent", + [NSNumber numberWithBool:NO], @"CustomQueryAutouppercaseKeywords", nil]]; // For versions prior to r336, where column widths have been saved, walk through them and remove -- cgit v1.2.3