aboutsummaryrefslogtreecommitdiffstats
path: root/Source
diff options
context:
space:
mode:
Diffstat (limited to 'Source')
-rw-r--r--Source/CustomQuery.h2
-rw-r--r--Source/CustomQuery.m70
2 files changed, 59 insertions, 13 deletions
diff --git a/Source/CustomQuery.h b/Source/CustomQuery.h
index 8a183475..770a4257 100644
--- a/Source/CustomQuery.h
+++ b/Source/CustomQuery.h
@@ -82,7 +82,7 @@
// Query actions
- (void)performQueries:(NSArray *)queries;
-- (NSString *)queryAtPosition:(long)position;
+- (NSString *)queryAtPosition:(long)position lookBehind:(BOOL *)doLookBehind;
// Accessors
- (NSArray *)currentResult;
diff --git a/Source/CustomQuery.m b/Source/CustomQuery.m
index 980116a3..b3aad504 100644
--- a/Source/CustomQuery.m
+++ b/Source/CustomQuery.m
@@ -86,7 +86,8 @@
// If the current selection is a single caret position, run the current query.
if (selectedRange.length == 0) {
- query = [self queryAtPosition:selectedRange.location];
+ BOOL doLookBehind = YES;
+ query = [self queryAtPosition:selectedRange.location lookBehind:&doLookBehind];
if (!query) {
NSBeep();
return;
@@ -562,13 +563,16 @@ sets the tableView columns corresponding to the mysql-result
* Retrieve the query at a position specified within the custom query
* text view. This will return nil if the position specified is beyond
* the available string or if an empty query would be returned.
+ * If lookBehind is set, returns the *previous* query, but only if the
+ * caret is after a query ending in a semicolon, on the same line, and
+ * separated only by whitespace.
*/
-- (NSString *)queryAtPosition:(long)position
+- (NSString *)queryAtPosition:(long)position lookBehind:(BOOL *)doLookBehind
{
SPSQLParser *customQueryParser;
NSArray *queries;
NSString *query = nil;
- int i, queryPosition = 0;
+ int i, queryPosition = 0, queryStartPosition;
// If the supplied position is negative or beyond the end of the string, return nil.
if (position < 0 || position > [[textView string] length])
@@ -582,13 +586,25 @@ sets the tableView columns corresponding to the mysql-result
// Walk along the array of queries to identify the current query - taking into account
// the extra semicolon at the end of each query
for (i = 0; i < [queries count]; i++ ) {
+ queryStartPosition = queryPosition;
queryPosition += [[queries objectAtIndex:i] length];
if (queryPosition >= position) {
+
+ // If lookbehind is enabled, determine whether it's valid
+ if (doLookBehind) {
+ if (i && [[[[textView string] substringWithRange:NSMakeRange(queryStartPosition, position - queryStartPosition)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0) {
+ query = [NSString stringWithString:[queries objectAtIndex:i-1]];
+ break;
+ }
+ *doLookBehind = NO;
+ }
+
query = [NSString stringWithString:[queries objectAtIndex:i]];
break;
}
queryPosition++;
}
+ if (doLookBehind && position == [[textView string] length] && !query) query = [queries lastObject];
[queries release];
@@ -1025,6 +1041,7 @@ traps enter key and
if ([textView selectedRange].length == 0) {
int selectionPosition = [textView selectedRange].location;
int movedRangeStart, movedRangeLength;
+ BOOL updateQueryButtons = FALSE;
NSRange oldSelection;
// Retrieve the old selection position
@@ -1036,19 +1053,48 @@ traps enter key and
// parsing overhead - which is cheap on small text strings but heavy of large queries.
movedRangeStart = (selectionPosition < oldSelection.location)?selectionPosition:oldSelection.location;
movedRangeLength = abs(selectionPosition - oldSelection.location);
- if (oldSelection.length > 0
- || movedRangeLength > 100
- || oldSelection.location > [[textView string] length]
- || [[textView string] rangeOfString:@";" options:0 range:NSMakeRange(movedRangeStart, movedRangeLength)].location != NSNotFound
- || (![runSelectionButton isEnabled] && selectionPosition > oldSelection.location
- && [[[[textView string] substringWithRange:NSMakeRange(movedRangeStart, movedRangeLength)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length])
- ) {
-
+ if (oldSelection.length > 0) updateQueryButtons = TRUE;
+ if (!updateQueryButtons && movedRangeLength > 100) updateQueryButtons = TRUE;
+ if (!updateQueryButtons && oldSelection.location > [[textView string] length]) updateQueryButtons = TRUE;
+ if (!updateQueryButtons && [[textView string] rangeOfString:@";" options:0 range:NSMakeRange(movedRangeStart, movedRangeLength)].location != NSNotFound) updateQueryButtons = TRUE;
+ if (!updateQueryButtons && ![runSelectionButton isEnabled] && selectionPosition > oldSelection.location
+ && [[[[textView string] substringWithRange:NSMakeRange(movedRangeStart, movedRangeLength)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]) updateQueryButtons = TRUE;
+ if (!updateQueryButtons && [[runSelectionButton title] isEqualToString:NSLocalizedString(@"Run Current", @"Title of button to run current query in custom query view")]) {
+ int charPosition;
+ unichar theChar;
+ for (charPosition = selectionPosition; charPosition > 0; charPosition--) {
+ theChar = [[textView string] characterAtIndex:charPosition-1];
+ if (theChar == ';') {
+ updateQueryButtons = TRUE;
+ break;
+ }
+ if (![[NSCharacterSet whitespaceAndNewlineCharacterSet] characterIsMember:theChar]) break;
+ }
+ }
+ if (!updateQueryButtons && [[runSelectionButton title] isEqualToString:NSLocalizedString(@"Run Previous", @"Title of button to run query just before text caret in custom query view")]) {
+ int charPosition;
+ unichar theChar;
+ for (charPosition = selectionPosition; charPosition > 0; charPosition--) {
+ theChar = [[textView string] characterAtIndex:charPosition-1];
+ if (theChar == ';') break;
+ if (![[NSCharacterSet whitespaceAndNewlineCharacterSet] characterIsMember:theChar]) {
+ updateQueryButtons = TRUE;
+ break;
+ }
+ }
+ }
+
+ if (updateQueryButtons) {
[runSelectionButton setTitle:NSLocalizedString(@"Run Current", @"Title of button to run current query in custom query view")];
[runSelectionMenuItem setTitle:NSLocalizedString(@"Run Current Query", @"Title of action menu item to run current query in custom query view")];
// If a valid query is present at the cursor position, enable the button
- if ([self queryAtPosition:selectionPosition]) {
+ BOOL isLookBehind = YES;
+ if ([self queryAtPosition:selectionPosition lookBehind:&isLookBehind]) {
+ if (isLookBehind) {
+ [runSelectionButton setTitle:NSLocalizedString(@"Run Previous", @"Title of button to run query just before text caret in custom query view")];
+ [runSelectionMenuItem setTitle:NSLocalizedString(@"Run Previous Query", @"Title of action menu item to run query just before text caret in custom query view")];
+ }
[runSelectionButton setEnabled:YES];
[runSelectionMenuItem setEnabled:YES];
} else {