aboutsummaryrefslogtreecommitdiffstats
path: root/Source
diff options
context:
space:
mode:
authorBibiko <bibiko@eva.mpg.de>2010-03-07 22:07:58 +0000
committerBibiko <bibiko@eva.mpg.de>2010-03-07 22:07:58 +0000
commitee8c83d433d0351dffda4ce273e3d1278154f769 (patch)
tree04839edbaf31f4cf9c598dce58e24d65448f6472 /Source
parentfb4e3024ae282b557eef755b974c3e081dac0f3e (diff)
downloadsequelpro-ee8c83d433d0351dffda4ce273e3d1278154f769.tar.gz
sequelpro-ee8c83d433d0351dffda4ce273e3d1278154f769.tar.bz2
sequelpro-ee8c83d433d0351dffda4ce273e3d1278154f769.zip
• added to CustomQuery's CMTextView the option to set auto-completion on/off, settable in Prefs and gear menu
- if inserted completion is marked as function it inserts snippet (${}1:) so far; function parameters as snippets follows soon
Diffstat (limited to 'Source')
-rw-r--r--Source/CMTextView.h7
-rw-r--r--Source/CMTextView.m56
-rw-r--r--Source/CustomQuery.m6
-rw-r--r--Source/SPConstants.h2
-rw-r--r--Source/SPConstants.m2
-rw-r--r--Source/SPNarrowDownCompletion.h3
-rw-r--r--Source/SPNarrowDownCompletion.m19
7 files changed, 75 insertions, 20 deletions
diff --git a/Source/CMTextView.h b/Source/CMTextView.h
index 53afe0df..558bc284 100644
--- a/Source/CMTextView.h
+++ b/Source/CMTextView.h
@@ -73,6 +73,7 @@ static inline id NSMutableAttributedStringAttributeAtIndex (NSMutableAttributedS
NSInteger currentSnippetIndex;
BOOL snippetWasJustInserted;
+ BOOL completionIsOpen;
NSColor *queryHiliteColor;
NSColor *queryEditorBackgroundColor;
@@ -99,6 +100,7 @@ static inline id NSMutableAttributedStringAttributeAtIndex (NSMutableAttributedS
@property(retain) NSColor* otherTextColor;
@property(assign) NSRange queryRange;
@property(assign) BOOL shouldHiliteQuery;
+@property(assign) BOOL completionIsOpen;
- (IBAction)showMySQLHelpForCurrentWord:(id)sender;
@@ -128,12 +130,13 @@ static inline id NSMutableAttributedStringAttributeAtIndex (NSMutableAttributedS
- (void) doSyntaxHighlighting;
- (NSBezierPath*)roundedBezierPathAroundRange:(NSRange)aRange;
- (void) setConnection:(MCPConnection *)theConnection withVersion:(NSInteger)majorVersion;
-- (void) doCompletionByUsingSpellChecker:(BOOL)isDictMode fuzzyMode:(BOOL)fuzzySearch;
+- (void) doCompletionByUsingSpellChecker:(BOOL)isDictMode fuzzyMode:(BOOL)fuzzySearch autoCompleteMode:(BOOL)autoCompleteMode;
+- (void) doAutoCompletion;
- (NSArray *)suggestionsForSQLCompletionWith:(NSString *)currentWord dictMode:(BOOL)isDictMode browseMode:(BOOL)dbBrowseMode withTableName:(NSString*)aTableName withDbName:(NSString*)aDbName;
- (void) selectCurrentQuery;
- (BOOL)checkForCaretInsideSnippet;
-- (void)insertFavoriteAsSnippet:(NSString*)theSnippet atRange:(NSRange)targetRange;
+- (void)insertAsSnippet:(NSString*)theSnippet atRange:(NSRange)targetRange;
- (NSUInteger)characterIndexOfPoint:(NSPoint)aPoint;
- (void)insertFileContentOfFile:(NSString *)aPath;
diff --git a/Source/CMTextView.m b/Source/CMTextView.m
index e32e1234..30c4d7a5 100644
--- a/Source/CMTextView.m
+++ b/Source/CMTextView.m
@@ -98,6 +98,7 @@ static inline NSPoint SPPointOnLine(NSPoint a, NSPoint b, CGFloat t) { return NS
@synthesize otherTextColor;
@synthesize queryRange;
@synthesize shouldHiliteQuery;
+@synthesize completionIsOpen;
/*
* Sort function (mainly used to sort the words in the textView)
@@ -126,6 +127,7 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse)
startListeningToBoundChanges = NO;
textBufferSizeIncreased = NO;
snippetControlCounter = -1;
+ completionIsOpen = NO;
lineNumberView = [[NoodleLineNumberView alloc] initWithScrollView:scrollView];
[scrollView setVerticalRulerView:lineNumberView];
@@ -177,6 +179,7 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse)
[prefs addObserver:self forKeyPath:SPCustomQueryEditorVariableColor options:NSKeyValueObservingOptionNew context:NULL];
[prefs addObserver:self forKeyPath:SPCustomQueryEditorTextColor options:NSKeyValueObservingOptionNew context:NULL];
[prefs addObserver:self forKeyPath:SPCustomQueryEditorTabStopWidth options:NSKeyValueObservingOptionNew context:NULL];
+ [prefs addObserver:self forKeyPath:SPCustomQueryAutoUppercaseKeywords options:NSKeyValueObservingOptionNew context:NULL];
}
@@ -234,6 +237,8 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse)
[self performSelector:@selector(doSyntaxHighlighting) withObject:nil afterDelay:0.1];
} else if ([keyPath isEqualToString:SPCustomQueryEditorTabStopWidth]) {
[self setTabStops];
+ } else if ([keyPath isEqualToString:SPCustomQueryAutoUppercaseKeywords]) {
+ [self setAutouppercaseKeywords:[prefs boolForKey:SPCustomQueryAutoUppercaseKeywords]];
}
}
@@ -467,7 +472,21 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse)
}
-- (void) doCompletionByUsingSpellChecker:(BOOL)isDictMode fuzzyMode:(BOOL)fuzzySearch
+- (void) doAutoCompletion
+{
+ NSRange r = [self selectedRange];
+
+ if(![[self delegate] isKindOfClass:[CustomQuery class]] || r.length || snippetControlCounter > -1) return;
+
+ if(r.location) {
+ if([[[self textStorage] attribute:kQuote atIndex:r.location-1 effectiveRange:nil] isEqualToString:kQuoteValue])
+ return;
+ if(![[NSCharacterSet whitespaceAndNewlineCharacterSet] characterIsMember:[[self string] characterAtIndex:r.location-1]])
+ [self doCompletionByUsingSpellChecker:NO fuzzyMode:NO autoCompleteMode:YES];
+ }
+}
+
+- (void) doCompletionByUsingSpellChecker:(BOOL)isDictMode fuzzyMode:(BOOL)fuzzySearch autoCompleteMode:(BOOL)autoCompleteMode
{
if(![self isEditable]) return;
@@ -475,7 +494,7 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse)
[self breakUndoCoalescing];
NSUInteger caretPos = NSMaxRange([self selectedRange]);
- // [self setSelectedRange:NSMakeRange(caretPos, 0)];
+
BOOL caretMovedLeft = NO;
// Check if caret is located after a ` - if so move caret inside
@@ -501,14 +520,12 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse)
// Break for long stuff
if(completionRange.length>100000) return;
-
NSString* allow; // additional chars which not close the popup
if(isDictMode)
allow= @"_";
else
allow= @"_. ";
-
BOOL dbBrowseMode = NO;
NSInteger backtickMode = 0; // 0 none, 1 rigth only, 2 left only, 3 both
BOOL caseInsensitive = YES;
@@ -669,7 +686,8 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse)
withDbName:dbName
withTableName:tableName
selectedDb:currentDb
- caretMovedLeft:caretMovedLeft];
+ caretMovedLeft:caretMovedLeft
+ autoComplete:autoCompleteMode];
//Get the NSPoint of the first character of the current word
NSRange glyphRange = [[self layoutManager] glyphRangeForCharacterRange:NSMakeRange(completionRange.location,1) actualCharacterRange:NULL];
@@ -685,6 +703,7 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse)
pos.y -= [[self font] pointSize]*1.25;
[completionPopUp setCaretPos:pos];
+ completionIsOpen = YES;
[completionPopUp orderFront:self];
[completionPopUp insertCommonPrefix];
@@ -1083,7 +1102,7 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse)
/*
* Inserts a chosen query favorite and initialze a snippet session if user defined any
*/
-- (void)insertFavoriteAsSnippet:(NSString*)theSnippet atRange:(NSRange)targetRange
+- (void)insertAsSnippet:(NSString*)theSnippet atRange:(NSRange)targetRange
{
// Do not allow the insertion of a query favorite if snippets are active
@@ -1496,14 +1515,21 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse)
long curFlags = ([theEvent modifierFlags] & allFlags);
if ([theEvent keyCode] == 53 && [self isEditable]){ // ESC key for internal completion
+
+ // Cancel autocompletion trigger
+ if([prefs boolForKey:SPCustomQueryAutoComplete])
+ [NSObject cancelPreviousPerformRequestsWithTarget:self
+ selector:@selector(doAutoCompletion)
+ object:nil];
+
if(curFlags==(NSControlKeyMask))
- [self doCompletionByUsingSpellChecker:NO fuzzyMode:YES];
+ [self doCompletionByUsingSpellChecker:NO fuzzyMode:YES autoCompleteMode:NO];
else
- [self doCompletionByUsingSpellChecker:NO fuzzyMode:NO];
+ [self doCompletionByUsingSpellChecker:NO fuzzyMode:NO autoCompleteMode:NO];
return;
}
if (insertedCharacter == NSF5FunctionKey && [self isEditable]){ // F5 for completion based on spell checker
- [self doCompletionByUsingSpellChecker:YES fuzzyMode:NO];
+ [self doCompletionByUsingSpellChecker:YES fuzzyMode:NO autoCompleteMode:NO];
return;
}
@@ -1557,7 +1583,7 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse)
if(snippetControlCounter < 0 && [[[self window] delegate] fileURL]) {
NSArray *snippets = [[SPQueryController sharedQueryController] queryFavoritesForFileURL:[[[self window] delegate] fileURL] andTabTrigger:tabTrigger includeGlobals:YES];
if([snippets count] > 0 && [(NSString*)[(NSDictionary*)[snippets objectAtIndex:0] objectForKey:@"query"] length]) {
- [self insertFavoriteAsSnippet:[(NSDictionary*)[snippets objectAtIndex:0] objectForKey:@"query"] atRange:targetRange];
+ [self insertAsSnippet:[(NSDictionary*)[snippets objectAtIndex:0] objectForKey:@"query"] atRange:targetRange];
return;
}
}
@@ -3391,6 +3417,16 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse)
[self performSelector:@selector(autoHelp) withObject:nil afterDelay:[[[prefs valueForKey:SPCustomQueryAutoHelpDelay] retain] doubleValue]];
}
+ // Cancel autocompletion trigger
+ if([prefs boolForKey:SPCustomQueryAutoComplete])
+ [NSObject cancelPreviousPerformRequestsWithTarget:self
+ selector:@selector(doAutoCompletion)
+ object:nil];
+
+ // Start autocompletion if enabled
+ if([prefs boolForKey:SPCustomQueryAutoComplete] && !completionIsOpen && editedMask != 1)
+ [self performSelector:@selector(doAutoCompletion) withObject:nil afterDelay:[[[prefs valueForKey:SPCustomQueryAutoCompleteDelay] retain] doubleValue]];
+
// Cancel calling doSyntaxHighlighting for large text
if([[self string] length] > SP_TEXT_SIZE_TRIGGER_FOR_PARTLY_PARSING)
[NSObject cancelPreviousPerformRequestsWithTarget:self
diff --git a/Source/CustomQuery.m b/Source/CustomQuery.m
index e586557e..e3d26d74 100644
--- a/Source/CustomQuery.m
+++ b/Source/CustomQuery.m
@@ -222,7 +222,7 @@
}
// The actual query strings have been already stored as tooltip
- [textView insertFavoriteAsSnippet:[[queryFavoritesButton selectedItem] toolTip] atRange:NSMakeRange([textView selectedRange].location, 0)];
+ [textView insertAsSnippet:[[queryFavoritesButton selectedItem] toolTip] atRange:NSMakeRange([textView selectedRange].location, 0)];
}
}
@@ -325,9 +325,9 @@
// on normal autocomplete usage.
if (sender == completionListMenuItem) {
if([[NSApp currentEvent] modifierFlags] & (NSControlKeyMask))
- [textView doCompletionByUsingSpellChecker:NO fuzzyMode:YES];
+ [textView doCompletionByUsingSpellChecker:NO fuzzyMode:YES autoCompleteMode:NO];
else
- [textView doCompletionByUsingSpellChecker:NO fuzzyMode:NO];
+ [textView doCompletionByUsingSpellChecker:NO fuzzyMode:NO autoCompleteMode:NO];
}
// "Editor font..." menu item to bring up the font panel
diff --git a/Source/SPConstants.h b/Source/SPConstants.h
index 12f2a38f..83ce6996 100644
--- a/Source/SPConstants.h
+++ b/Source/SPConstants.h
@@ -135,6 +135,8 @@ extern NSString *SPCustomQueryUpdateAutoHelp;
extern NSString *SPCustomQueryAutoHelpDelay;
extern NSString *SPCustomQueryHighlightCurrentQuery;
extern NSString *SPCustomQueryEditorTabStopWidth;
+extern NSString *SPCustomQueryAutoComplete;
+extern NSString *SPCustomQueryAutoCompleteDelay;
// AutoUpdate Prefpane
extern NSString *SPLastUsedVersion;
diff --git a/Source/SPConstants.m b/Source/SPConstants.m
index bea60f8b..aa2c3508 100644
--- a/Source/SPConstants.m
+++ b/Source/SPConstants.m
@@ -103,6 +103,8 @@ NSString *SPCustomQueryUpdateAutoHelp = @"CustomQueryUpdateAutoHelp";
NSString *SPCustomQueryAutoHelpDelay = @"CustomQueryAutoHelpDelay";
NSString *SPCustomQueryHighlightCurrentQuery = @"CustomQueryHighlightCurrentQuery";
NSString *SPCustomQueryEditorTabStopWidth = @"CustomQueryEditorTabStopWidth";
+NSString *SPCustomQueryAutoComplete = @"CustomQueryAutoComplete";
+NSString *SPCustomQueryAutoCompleteDelay = @"CustomQueryAutoCompleteDelay";
// AutoUpdate Prefpane
NSString *SPLastUsedVersion = @"LastUsedVersion";
diff --git a/Source/SPNarrowDownCompletion.h b/Source/SPNarrowDownCompletion.h
index 96558b9d..6901ffa1 100644
--- a/Source/SPNarrowDownCompletion.h
+++ b/Source/SPNarrowDownCompletion.h
@@ -49,6 +49,7 @@
BOOL noFilterString;
BOOL cursorMovedLeft;
BOOL commaInsertionMode;
+ BOOL autoCompletionMode;
NSInteger backtickMode;
NSFont *tableFont;
NSRange theCharRange;
@@ -68,7 +69,7 @@
charRange:(NSRange)initRange parseRange:(NSRange)parseRange inView:(id)aView
dictMode:(BOOL)mode dbMode:(BOOL)theDbMode tabTriggerMode:(BOOL)tabTriggerMode fuzzySearch:(BOOL)fuzzySearch
backtickMode:(NSInteger)theBackTickMode withDbName:(NSString*)dbName withTableName:(NSString*)tableName
- selectedDb:(NSString*)selectedDb caretMovedLeft:(BOOL)caretMovedLeft;
+ selectedDb:(NSString*)selectedDb caretMovedLeft:(BOOL)caretMovedLeft autoComplete:(BOOL)autoComplete;
- (void)setCaretPos:(NSPoint)aPos;
- (void)insert_text:(NSString* )aString;
- (void)insertCommonPrefix;
diff --git a/Source/SPNarrowDownCompletion.m b/Source/SPNarrowDownCompletion.m
index 032e80b3..8e5bebec 100644
--- a/Source/SPNarrowDownCompletion.m
+++ b/Source/SPNarrowDownCompletion.m
@@ -136,7 +136,7 @@
charRange:(NSRange)initRange parseRange:(NSRange)parseRange inView:(id)aView
dictMode:(BOOL)mode dbMode:(BOOL)theDbMode tabTriggerMode:(BOOL)tabTriggerMode fuzzySearch:(BOOL)fuzzySearch
backtickMode:(NSInteger)theBackTickMode withDbName:(NSString*)dbName withTableName:(NSString*)tableName
- selectedDb:(NSString*)selectedDb caretMovedLeft:(BOOL)caretMovedLeft
+ selectedDb:(NSString*)selectedDb caretMovedLeft:(BOOL)caretMovedLeft autoComplete:(BOOL)autoComplete
{
if(self = [self init])
{
@@ -145,6 +145,8 @@
if(aUserString)
[mutablePrefix appendString:aUserString];
+ autoCompletionMode = autoComplete;
+
fuzzyMode = fuzzySearch;
if(fuzzyMode)
[theTableView setBackgroundColor:[NSColor colorWithCalibratedRed:0.9f green:0.9f blue:0.9f alpha:1.0f]];
@@ -371,6 +373,7 @@
- (void)checkSpaceForAllowedCharacter
{
[textualInputCharacters removeCharactersInString:@" "];
+ if(autoCompletionMode) return;
if(spaceCounter < 1)
for(id w in filtered){
if([[w objectForKey:@"match"] ?: [w objectForKey:@"display"] rangeOfString:@" "].length) {
@@ -444,11 +447,16 @@
}
if(![newFiltered count]) {
- if([[self filterString] hasSuffix:@"."]) {
- [theView doCompletionByUsingSpellChecker:dictMode fuzzyMode:fuzzyMode];
+ if(autoCompletionMode) {
closeMe = YES;
+ return;
+ } else {
+ if([[self filterString] hasSuffix:@"."]) {
+ [theView doCompletionByUsingSpellChecker:dictMode fuzzyMode:fuzzyMode autoCompleteMode:NO];
+ closeMe = YES;
+ }
+ [newFiltered addObject:[NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"No completions found", @"no completions found message"), @"display", @"", @"noCompletion", nil]];
}
- [newFiltered addObject:[NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"No completions found", @"no completions found message"), @"display", @"", @"noCompletion", nil]];
}
NSPoint old = NSMakePoint([self frame].origin.x, [self frame].origin.y + [self frame].size.height);
@@ -615,6 +623,7 @@
[NSApp sendEvent:event];
}
}
+ [theView setCompletionIsOpen:NO];
[self close];
usleep(70); // tiny delay to suppress while continously pressing of ESC overlapping
}
@@ -665,6 +674,8 @@
// If completion string contains backticks move caret out of the backticks
if(backtickMode && !triggerMode)
[theView performSelector:@selector(moveRight:)];
+ else if([[[filtered objectAtIndex:[theTableView selectedRow]] objectForKey:@"image"] hasPrefix:@"func"])
+ [theView insertAsSnippet:@"(${1:})" atRange:[theView selectedRange]];
}
- (void)completeAndInsertSnippet