aboutsummaryrefslogtreecommitdiffstats
path: root/Source
diff options
context:
space:
mode:
authorrowanbeentje <rowan@beent.je>2009-04-03 00:08:28 +0000
committerrowanbeentje <rowan@beent.je>2009-04-03 00:08:28 +0000
commitb7a565dd0196dcd77143874b27162d4300182019 (patch)
tree732ef42200affccae1a92d86aeadb49d63f52e7f /Source
parent6e4d0662303ecd6b3f953ff3313965835a9b8074 (diff)
downloadsequelpro-b7a565dd0196dcd77143874b27162d4300182019.tar.gz
sequelpro-b7a565dd0196dcd77143874b27162d4300182019.tar.bz2
sequelpro-b7a565dd0196dcd77143874b27162d4300182019.zip
- Add the ability for CMTextView to automatically capitalise SQL keywords in the text view, currently off by default but saved from preferences. Thanks again to Hans-Jörg Bibiko for this patch; see Issue #218.
Diffstat (limited to 'Source')
-rw-r--r--Source/CMTextView.h3
-rw-r--r--Source/CMTextView.m87
-rw-r--r--Source/CustomQuery.h1
-rw-r--r--Source/CustomQuery.m11
-rw-r--r--Source/MainController.m1
5 files changed, 81 insertions, 22 deletions
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
@@ -488,6 +489,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