aboutsummaryrefslogtreecommitdiffstats
path: root/Source
diff options
context:
space:
mode:
Diffstat (limited to 'Source')
-rw-r--r--Source/CMTextView.h2
-rw-r--r--Source/CMTextView.m219
-rw-r--r--Source/CustomQuery.h1
-rw-r--r--Source/CustomQuery.m15
-rw-r--r--Source/SPNarrowDownCompletion.m9
5 files changed, 190 insertions, 56 deletions
diff --git a/Source/CMTextView.h b/Source/CMTextView.h
index 71799f26..1dd0a59e 100644
--- a/Source/CMTextView.h
+++ b/Source/CMTextView.h
@@ -137,6 +137,8 @@ static inline id NSMutableAttributedStringAttributeAtIndex (NSMutableAttributedS
- (BOOL)checkForCaretInsideSnippet;
- (void)insertAsSnippet:(NSString*)theSnippet atRange:(NSRange)targetRange;
+- (void)showCompletionListFor:(NSString*)kind atRange:(NSRange)aRange fuzzySearch:(BOOL)fuzzySearchMode;
+
- (NSUInteger)characterIndexOfPoint:(NSPoint)aPoint;
- (void)insertFileContentOfFile:(NSString *)aPath;
diff --git a/Source/CMTextView.m b/Source/CMTextView.m
index 7a0cfae4..7fb6ceee 100644
--- a/Source/CMTextView.m
+++ b/Source/CMTextView.m
@@ -1081,6 +1081,159 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse)
}
/*
+ * Shows pre-defined completion list
+ */
+- (void)showCompletionListFor:(NSString*)kind atRange:(NSRange)aRange fuzzySearch:(BOOL)fuzzySearchMode
+{
+
+ // Cancel auto-completion timer
+ if([prefs boolForKey:SPCustomQueryAutoComplete])
+ [NSObject cancelPreviousPerformRequestsWithTarget:self
+ selector:@selector(doAutoCompletion)
+ object:nil];
+
+ NSMutableArray *possibleCompletions = [[[NSMutableArray alloc] initWithCapacity:0] autorelease];
+ NSArray *arr = nil;
+ if([kind isEqualToString:@"$SP_ASLIST_ALL_TABLES"]) {
+ NSString *currentDb = nil;
+
+ if ([[[[self window] delegate] valueForKeyPath:@"tablesListInstance"] valueForKey:@"selectedDatabase"] != nil)
+ currentDb = [[[[self window] delegate] valueForKeyPath:@"tablesListInstance"] valueForKeyPath:@"selectedDatabase"];
+
+ NSDictionary *dbs = [NSDictionary dictionaryWithDictionary:[mySQLConnection getDbStructure]];
+
+ if(currentDb != nil && dbs != nil && [dbs count] && [dbs objectForKey:currentDb]) {
+ NSArray *allTables = [[dbs objectForKey:currentDb] allKeys];
+ NSSortDescriptor *desc = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES selector:@selector(localizedCompare:)];
+ NSArray *sortedTables = [allTables sortedArrayUsingDescriptors:[NSArray arrayWithObject:desc]];
+ for(id table in sortedTables) {
+ NSDictionary * theTable = [[dbs objectForKey:currentDb] objectForKey:table];
+ NSInteger structtype = [[theTable objectForKey:@" struct_type "] intValue];
+ switch(structtype) {
+ case 0:
+ [possibleCompletions addObject:[NSDictionary dictionaryWithObjectsAndKeys:table, @"display", @"table-small-square", @"image", currentDb, @"path", [NSString stringWithFormat:@"%@.%@",currentDb,table], @"isRef", nil]];
+ break;
+ case 1:
+ [possibleCompletions addObject:[NSDictionary dictionaryWithObjectsAndKeys:table, @"display", @"table-view-small-square", @"image", currentDb, @"path", [NSString stringWithFormat:@"%@.%@",currentDb,table], @"isRef", nil]];
+ break;
+ }
+ }
+ } else {
+ arr = [NSArray arrayWithArray:[[[self delegate] valueForKeyPath:@"tablesListInstance"] allTableAndViewNames]];
+ if(arr == nil) {
+ arr = [NSArray array];
+ }
+ for(id w in arr)
+ [possibleCompletions addObject:[NSDictionary dictionaryWithObjectsAndKeys:w, @"display", @"table-small-square", @"image", @"", @"isRef", nil]];
+ }
+ }
+ else if([kind isEqualToString:@"$SP_ASLIST_ALL_DATABASES"]) {
+ arr = [NSArray arrayWithArray:[[[self delegate] valueForKeyPath:@"tablesListInstance"] allDatabaseNames]];
+ if(arr == nil) {
+ arr = [NSArray array];
+ }
+ for(id w in arr)
+ [possibleCompletions addObject:[NSDictionary dictionaryWithObjectsAndKeys:w, @"display", @"database-small", @"image", @"", @"isRef", nil]];
+ arr = [NSArray arrayWithArray:[[[self delegate] valueForKeyPath:@"tablesListInstance"] allSystemDatabaseNames]];
+ if(arr == nil) {
+ arr = [NSArray array];
+ }
+ for(id w in arr)
+ [possibleCompletions addObject:[NSDictionary dictionaryWithObjectsAndKeys:w, @"display", @"database-small", @"image", @"", @"isRef", nil]];
+ }
+ else if([kind isEqualToString:@"$SP_ASLIST_ALL_FIELDS_FROM_SELECTED_TABLE"]) {
+
+ NSString *currentDb = nil;
+ NSString *currentTable = nil;
+
+ if ([[[[self window] delegate] valueForKeyPath:@"tablesListInstance"] valueForKey:@"selectedDatabase"] != nil)
+ currentDb = [[[[self window] delegate] valueForKeyPath:@"tablesListInstance"] valueForKeyPath:@"selectedDatabase"];
+ if ([[[[self window] delegate] valueForKeyPath:@"tablesListInstance"] valueForKey:@"tableName"] != nil)
+ currentTable = [[[[self window] delegate] valueForKeyPath:@"tablesListInstance"] valueForKeyPath:@"tableName"];
+
+ NSDictionary *dbs = [NSDictionary dictionaryWithDictionary:[mySQLConnection getDbStructure]];
+ if(currentDb != nil && currentTable != nil && dbs != nil && [dbs count] && [dbs objectForKey:currentDb] && [[dbs objectForKey:currentDb] objectForKey:currentTable]) {
+ NSDictionary * theTable = [[dbs objectForKey:currentDb] objectForKey:currentTable];
+ NSArray *allFields = [theTable allKeys];
+ NSSortDescriptor *desc = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES selector:@selector(localizedCompare:)];
+ NSArray *sortedFields = [allFields sortedArrayUsingDescriptors:[NSArray arrayWithObject:desc]];
+ for(id field in sortedFields) {
+ if(![field hasPrefix:@" "]) {
+ NSString *typ = [theTable objectForKey:field];
+ // Check if type definition contains a , if so replace the bracket content by … and add
+ // the bracket content as "list" key to prevend the token field to split them by ,
+ if(typ && [typ rangeOfString:@","].length) {
+ NSString *t = [typ stringByReplacingOccurrencesOfRegex:@"\\(.*?\\)" withString:@"(…)"];
+ NSString *lst = [typ stringByMatching:@"\\(([^\\)]*?)\\)" capture:1L];
+ [possibleCompletions addObject:[NSDictionary dictionaryWithObjectsAndKeys:
+ field, @"display",
+ @"field-small-square", @"image",
+ [NSString stringWithFormat:@"%@⇠%@",currentTable,currentDb], @"path",
+ t, @"type",
+ lst, @"list",
+ [NSString stringWithFormat:@"%@.%@.%@",currentDb,currentTable,field], @"isRef",
+ nil]];
+ } else {
+ [possibleCompletions addObject:[NSDictionary dictionaryWithObjectsAndKeys:
+ field, @"display",
+ @"field-small-square", @"image",
+ [NSString stringWithFormat:@"%@⇠%@",currentTable,currentDb], @"path",
+ typ, @"type",
+ [NSString stringWithFormat:@"%@.%@.%@",currentDb,currentTable,field], @"isRef",
+ nil]];
+ }
+ }
+ }
+ [desc release];
+ } else {
+ arr = [NSArray arrayWithArray:[[[[self window] delegate] valueForKeyPath:@"tableDataInstance"] valueForKey:@"columnNames"]];
+ if(arr == nil) {
+ arr = [NSArray array];
+ }
+ for(id w in arr)
+ [possibleCompletions addObject:[NSDictionary dictionaryWithObjectsAndKeys:w, @"display", @"field-small-square", @"image", @"", @"isRef", nil]];
+ }
+ }
+ else {
+ NSLog(@"“%@” is not a valid completion list", kind);
+ NSBeep();
+ return;
+ }
+
+ SPNarrowDownCompletion* completionPopUp = [[SPNarrowDownCompletion alloc] initWithItems:possibleCompletions
+ alreadyTyped:@""
+ staticPrefix:@""
+ additionalWordCharacters:@"_."
+ caseSensitive:NO
+ charRange:aRange
+ parseRange:aRange
+ inView:self
+ dictMode:NO
+ dbMode:YES
+ tabTriggerMode:[self isSnippetMode]
+ fuzzySearch:fuzzySearchMode
+ backtickMode:NO
+ withDbName:@""
+ withTableName:@""
+ selectedDb:@""
+ caretMovedLeft:NO
+ autoComplete:NO
+ oneColumn:NO];
+
+ //Get the NSPoint of the first character of the current word
+ NSRange glyphRange = [[self layoutManager] glyphRangeForCharacterRange:NSMakeRange(aRange.location,1) actualCharacterRange:NULL];
+ NSRect boundingRect = [[self layoutManager] boundingRectForGlyphRange:glyphRange inTextContainer:[self textContainer]];
+ boundingRect = [self convertRect: boundingRect toView: NULL];
+ NSPoint pos = [[self window] convertBaseToScreen: NSMakePoint(boundingRect.origin.x + boundingRect.size.width,boundingRect.origin.y + boundingRect.size.height)];
+ // Adjust list location to be under the current word or insertion point
+ pos.y -= [[self font] pointSize]*1.25;
+ [completionPopUp setCaretPos:pos];
+ completionIsOpen = YES;
+ [completionPopUp orderFront:self];
+
+}
+
+/*
* Selects the current snippet defined by “currentSnippetIndex”
*/
- (void)selectCurrentSnippet
@@ -1116,50 +1269,8 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse)
SPNarrowDownCompletion* completionPopUp;
NSString *newSnip = [snip substringWithRange:NSMakeRange(1*offset,[snip length]-(2*offset))];
if([newSnip hasPrefix:@"$SP_ASLIST_"]) {
- NSMutableArray *possibleCompletions = [[[NSMutableArray alloc] initWithCapacity:5] autorelease];
- NSArray *arr = nil;
- if([newSnip isEqualToString:@"$SP_ASLIST_ALL_TABLES"]) {
- arr = [NSArray arrayWithArray:[[[self delegate] valueForKeyPath:@"tablesListInstance"] allTableAndViewNames]];
- if(arr == nil) {
- arr = [NSArray array];
- }
- for(id w in arr)
- [possibleCompletions addObject:[NSDictionary dictionaryWithObjectsAndKeys:w, @"display", @"table-small-square", @"image", @"", @"isRef", nil]];
- }
- else if([newSnip isEqualToString:@"$SP_ASLIST_ALL_DATABASES"]) {
- arr = [NSArray arrayWithArray:[[[self delegate] valueForKeyPath:@"tablesListInstance"] allDatabaseNames]];
- if(arr == nil) {
- arr = [NSArray array];
- }
- for(id w in arr)
- [possibleCompletions addObject:[NSDictionary dictionaryWithObjectsAndKeys:w, @"display", @"database-small", @"image", @"", @"isRef", nil]];
- arr = [NSArray arrayWithArray:[[[self delegate] valueForKeyPath:@"tablesListInstance"] allSystemDatabaseNames]];
- if(arr == nil) {
- arr = [NSArray array];
- }
- for(id w in arr)
- [possibleCompletions addObject:[NSDictionary dictionaryWithObjectsAndKeys:w, @"display", @"database-small", @"image", @"", @"isRef", nil]];
- }
-
- completionPopUp = [[SPNarrowDownCompletion alloc] initWithItems:possibleCompletions
- alreadyTyped:@""
- staticPrefix:@""
- additionalWordCharacters:@"_."
- caseSensitive:NO
- charRange:insertRange
- parseRange:insertRange
- inView:self
- dictMode:NO
- dbMode:YES
- tabTriggerMode:[self isSnippetMode]
- fuzzySearch:fuzzySearchMode
- backtickMode:NO
- withDbName:@""
- withTableName:@""
- selectedDb:@""
- caretMovedLeft:NO
- autoComplete:NO
- oneColumn:NO];
+ [self showCompletionListFor:newSnip atRange:NSMakeRange(r2.location, 0) fuzzySearch:fuzzySearchMode];
+ return;
} else {
NSArray *list = [[snip substringWithRange:NSMakeRange(1*offset,[snip length]-(2*offset))] componentsSeparatedByString:@"¦"];
NSMutableArray *possibleCompletions = [[[NSMutableArray alloc] initWithCapacity:[list count]] autorelease];
@@ -1185,17 +1296,17 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse)
caretMovedLeft:NO
autoComplete:NO
oneColumn:YES];
+ //Get the NSPoint of the first character of the current word
+ NSRange glyphRange = [[self layoutManager] glyphRangeForCharacterRange:NSMakeRange(r2.location,1) actualCharacterRange:NULL];
+ NSRect boundingRect = [[self layoutManager] boundingRectForGlyphRange:glyphRange inTextContainer:[self textContainer]];
+ boundingRect = [self convertRect: boundingRect toView: NULL];
+ NSPoint pos = [[self window] convertBaseToScreen: NSMakePoint(boundingRect.origin.x + boundingRect.size.width,boundingRect.origin.y + boundingRect.size.height)];
+ // Adjust list location to be under the current word or insertion point
+ pos.y -= [[self font] pointSize]*1.25;
+ [completionPopUp setCaretPos:pos];
+ completionIsOpen = YES;
+ [completionPopUp orderFront:self];
}
- //Get the NSPoint of the first character of the current word
- NSRange glyphRange = [[self layoutManager] glyphRangeForCharacterRange:NSMakeRange(r2.location,1) actualCharacterRange:NULL];
- NSRect boundingRect = [[self layoutManager] boundingRectForGlyphRange:glyphRange inTextContainer:[self textContainer]];
- boundingRect = [self convertRect: boundingRect toView: NULL];
- NSPoint pos = [[self window] convertBaseToScreen: NSMakePoint(boundingRect.origin.x + boundingRect.size.width,boundingRect.origin.y + boundingRect.size.height)];
- // Adjust list location to be under the current word or insertion point
- pos.y -= [[self font] pointSize]*1.25;
- [completionPopUp setCaretPos:pos];
- completionIsOpen = YES;
- [completionPopUp orderFront:self];
}
} else {
[self endSnippetSession];
diff --git a/Source/CustomQuery.h b/Source/CustomQuery.h
index 526010ad..ed4a1166 100644
--- a/Source/CustomQuery.h
+++ b/Source/CustomQuery.h
@@ -180,6 +180,7 @@
- (IBAction)saveQueryHistory:(id)sender;
- (IBAction)copyQueryHistory:(id)sender;
- (IBAction)clearQueryHistory:(id)sender;
+- (IBAction)showCompletionList:(id)sender;
// Query actions
- (void)performQueries:(NSArray *)queries withCallback:(SEL)customQueryCallbackMethod;
diff --git a/Source/CustomQuery.m b/Source/CustomQuery.m
index 2e26f08e..0d4207a0 100644
--- a/Source/CustomQuery.m
+++ b/Source/CustomQuery.m
@@ -2454,6 +2454,21 @@
[self helpTargetValidation];
}
+- (IBAction)showCompletionList:(id)sender
+{
+ NSRange insertRange = NSMakeRange([textView selectedRange].location, 0);
+ switch([sender tag]) {
+ case 8000:
+ [textView showCompletionListFor:@"$SP_ASLIST_ALL_DATABASES" atRange:insertRange fuzzySearch:NO];
+ break;
+ case 8001:
+ [textView showCompletionListFor:@"$SP_ASLIST_ALL_TABLES" atRange:insertRange fuzzySearch:NO];
+ break;
+ case 8002:
+ [textView showCompletionListFor:@"$SP_ASLIST_ALL_FIELDS_FROM_SELECTED_TABLE" atRange:insertRange fuzzySearch:NO];
+ break;
+ }
+}
/*
* Show the data for "HELP 'currentWord' invoked by autohelp"
*/
diff --git a/Source/SPNarrowDownCompletion.m b/Source/SPNarrowDownCompletion.m
index d7ac8048..a17c3aac 100644
--- a/Source/SPNarrowDownCompletion.m
+++ b/Source/SPNarrowDownCompletion.m
@@ -535,7 +535,7 @@
closeMe = YES;
return;
} else {
- [newFiltered addObject:[NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"No completions found", @"no completions found message"), @"display", @"", @"noCompletion", nil]];
+ [newFiltered addObject:[NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"No item found", @"no item found message"), @"display", @"", @"noCompletion", nil]];
}
}
}
@@ -751,7 +751,12 @@
- (void)insert_text:(NSString* )aString
{
- [theView setSelectedRange:theCharRange];
+ NSRange r = [theView selectedRange];
+ if(r.length)
+ [theView setSelectedRange:r];
+ else
+ [theView setSelectedRange:theCharRange];
+
[theView insertText:aString];
// If completion string contains backticks move caret out of the backticks