From 64567b12a8e9449ea3e23e20f0d236a9bde24dad Mon Sep 17 00:00:00 2001 From: Bibiko Date: Fri, 5 Feb 2010 11:37:30 +0000 Subject: =?UTF-8?q?=E2=80=A2=20refactored=20the=20entire=20history=20logic?= =?UTF-8?q?=20in=20Custom=20Query=20-=20queries=20which=20are=20longer=20t?= =?UTF-8?q?han=20then=2064=20chars=20are=20truncated=20in=20title=20and=20?= =?UTF-8?q?=20queries=20which=20are=20longer=20than=20then=20256=20chars?= =?UTF-8?q?=20are=20truncated=20in=20tooltip=20to=20avoid=20processing=20t?= =?UTF-8?q?ime=20to=20load=20very=20long=20queries=20from=20history;=20thi?= =?UTF-8?q?s=20also=20decreases=20the=20memory=20usage=20-=20added=20"Inse?= =?UTF-8?q?rt=20Next/Previous=20History=20Query"=20logic=20bound=20to=20^?= =?UTF-8?q?=E2=86=91=20and=20^=E2=86=93=20-=20fixed=20tiny=20GUI=20spacing?= =?UTF-8?q?=20problem=20in=20Custom=20Query?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/CustomQuery.h | 5 +++ Source/CustomQuery.m | 105 ++++++++++++++++++++++++++++++++------------- Source/SPQueryController.h | 2 + Source/SPQueryController.m | 26 +++++++++++ 4 files changed, 107 insertions(+), 31 deletions(-) (limited to 'Source') diff --git a/Source/CustomQuery.h b/Source/CustomQuery.h index e92dc4f0..8994a7e3 100644 --- a/Source/CustomQuery.h +++ b/Source/CustomQuery.h @@ -100,6 +100,8 @@ IBOutlet NSMenuItem *autouppercaseKeywordsMenuItem; IBOutlet NSMenuItem *commentCurrentQueryMenuItem; IBOutlet NSMenuItem *commentLineOrSelectionMenuItem; + IBOutlet NSMenuItem *previousHistoryMenuItem; + IBOutlet NSMenuItem *nextHistoryMenuItem; IBOutlet NSWindow *helpWebViewWindow; IBOutlet WebView *helpWebView; @@ -146,6 +148,9 @@ NSUInteger numberOfQueries; NSUInteger queryTextViewStartPosition; + + NSInteger currentHistoryOffsetIndex; + BOOL historyItemWasJustInserted; } // IBAction methods diff --git a/Source/CustomQuery.m b/Source/CustomQuery.m index 2d1dae4d..b26cf828 100644 --- a/Source/CustomQuery.m +++ b/Source/CustomQuery.m @@ -242,7 +242,7 @@ if(replaceContent) [textView setSelectedRange:NSMakeRange(0,[[textView string] length])]; - [textView insertText:[queryHistoryButton titleOfSelectedItem]]; + [textView insertText:[[[SPQueryController sharedQueryController] historyForFileURL:[tableDocumentInstance fileURL]] objectAtIndex:[queryHistoryButton indexOfSelectedItem]-7]]; } } @@ -262,6 +262,46 @@ - (IBAction)gearMenuItemSelected:(id)sender { + if ( sender == previousHistoryMenuItem ) { + NSInteger numberOfHistoryItems = [[SPQueryController sharedQueryController] numberOfHistoryItemsForFileURL:[tableDocumentInstance fileURL]]; + currentHistoryOffsetIndex++; + NSLog(@"%ld + %ld", numberOfHistoryItems, currentHistoryOffsetIndex); + if ( numberOfHistoryItems > 0 && currentHistoryOffsetIndex < numberOfHistoryItems && currentHistoryOffsetIndex >= 0) { + historyItemWasJustInserted = YES; + // if(![textView selectedRange].length) + // [textView setSelectedRange:currentQueryRange]; + [textView breakUndoCoalescing]; + NSString *historyString = [[[SPQueryController sharedQueryController] historyForFileURL:[tableDocumentInstance fileURL]] objectAtIndex:currentHistoryOffsetIndex]; + NSRange rangeOfInsertedString = NSMakeRange([textView selectedRange].location, [historyString length]); + [textView insertText:historyString]; + [textView setSelectedRange:rangeOfInsertedString]; + } else { + currentHistoryOffsetIndex--; + NSBeep(); + } + historyItemWasJustInserted = NO; + } + + if ( sender == nextHistoryMenuItem ) { + NSInteger numberOfHistoryItems = [[SPQueryController sharedQueryController] numberOfHistoryItemsForFileURL:[tableDocumentInstance fileURL]]; + currentHistoryOffsetIndex--; + NSLog(@"%ld - %ld", numberOfHistoryItems, currentHistoryOffsetIndex); + if ( numberOfHistoryItems > 0 && currentHistoryOffsetIndex < numberOfHistoryItems && currentHistoryOffsetIndex >= 0) { + historyItemWasJustInserted = YES; + // if(![textView selectedRange].length) + // [textView setSelectedRange:currentQueryRange]; + [textView breakUndoCoalescing]; + NSString *historyString = [[[SPQueryController sharedQueryController] historyForFileURL:[tableDocumentInstance fileURL]] objectAtIndex:currentHistoryOffsetIndex]; + NSRange rangeOfInsertedString = NSMakeRange([textView selectedRange].location, [historyString length]); + [textView insertText:historyString]; + [textView setSelectedRange:rangeOfInsertedString]; + } else { + currentHistoryOffsetIndex++; + NSBeep(); + } + historyItemWasJustInserted = NO; + } + // "Shift Right" menu item - indent the selection with an additional tab. if (sender == shiftRightMenuItem) { [textView shiftSelectionRight]; @@ -682,16 +722,13 @@ // Register new history item [[SPQueryController sharedQueryController] addHistory:usedQuery forFileURL:[tableDocumentInstance fileURL]]; - // Add it to the document's current popup list - if([queryHistoryButton numberOfItems] > 7) - [queryHistoryButton insertItemWithTitle:usedQuery atIndex:7]; - else - [queryHistoryButton addItemWithTitle:usedQuery]; - - // Check for max history - NSUInteger maxHistoryItems = [[prefs objectForKey:SPCustomQueryMaxHistoryItems] integerValue]; - while ( [queryHistoryButton numberOfItems] > maxHistoryItems + 7 ) + // Refresh history popup menu + NSMenu* historyMenu = [queryHistoryButton menu]; + while([queryHistoryButton numberOfItems] > 7) [queryHistoryButton removeItemAtIndex:[queryHistoryButton numberOfItems]-1]; + + for(id historyMenuItem in [[SPQueryController sharedQueryController] historyMenuItemsForFileURL:[tableDocumentInstance fileURL]]) + [historyMenu addItem:historyMenuItem]; } @@ -1293,7 +1330,14 @@ if ( [[SPQueryController sharedQueryController] historyForFileURL:[tableDocumentInstance fileURL]] ) { - [queryHistoryButton addItemsWithTitles:[[SPQueryController sharedQueryController] historyForFileURL:[tableDocumentInstance fileURL]]]; + NSMenu* historyMenu = [queryHistoryButton menu]; + // remove items up to the last separator beginning from the end + while([queryHistoryButton numberOfItems] > 7) + [queryHistoryButton removeItemAtIndex:[queryHistoryButton numberOfItems]-1]; + + // Add history items + for(id historyMenuItem in [[SPQueryController sharedQueryController] historyMenuItemsForFileURL:[tableDocumentInstance fileURL]]) + [historyMenu addItem:historyMenuItem]; } // Populate query favorites @@ -2104,6 +2148,9 @@ [runSelectionMenuItem setEnabled:YES]; } } + + if(!historyItemWasJustInserted) + currentHistoryOffsetIndex = -1; } #pragma mark - @@ -2783,16 +2830,7 @@ - (NSString *)buildHistoryString { - NSMutableString *history = [NSMutableString string]; - NSMenu *menu = [queryHistoryButton menu]; - NSInteger i; - - for (i = 7; i < [menu numberOfItems]; i++) { - [history appendString:[[menu itemAtIndex:i] title]]; - [history appendString:@";\n"]; - } - - return history; + return [[[SPQueryController sharedQueryController] historyForFileURL:[tableDocumentInstance fileURL]] componentsJoinedByString:@";\n"]; } /* * This method is called as part of Key Value Observing which is used to watch for prefernce changes which effect the interface. @@ -2828,9 +2866,9 @@ // Clear the global history list if doc is Untitled if ([tableDocumentInstance isUntitled]) [prefs setObject:[NSArray array] forKey:SPQueryHistory]; - // otherwise remove all document-based history items from the queryController - else - [[SPQueryController sharedQueryController] replaceHistoryByArray:[NSMutableArray array] forFileURL:[tableDocumentInstance fileURL]]; + + // Remove items in the query controller + [[SPQueryController sharedQueryController] replaceHistoryByArray:[NSMutableArray array] forFileURL:[tableDocumentInstance fileURL]]; } return; @@ -2906,6 +2944,7 @@ */ - (BOOL)validateMenuItem:(NSMenuItem *)menuItem { + // Control "Save ... to Favorites" if ( [menuItem tag] == SP_SAVE_SELECTION_FAVORTITE_MENUITEM_TAG ) { if ([[textView string] length] < 1) return NO; @@ -2923,12 +2962,12 @@ } // Avoid selecting button list headers - if ( [menuItem tag] == SP_FAVORITE_HEADER_MENUITEM_TAG ) { + else if ( [menuItem tag] == SP_FAVORITE_HEADER_MENUITEM_TAG ) { return NO; } // Control Clear History menu item title according to isUntitled - if ( [menuItem tag] == SP_HISTORY_CLEAR_MENUITEM_TAG ) { + else if ( [menuItem tag] == SP_HISTORY_CLEAR_MENUITEM_TAG ) { if ( [tableDocumentInstance isUntitled] ) { [menuItem setTitle:NSLocalizedString(@"Clear Global History", @"clear global history menu item title")]; [menuItem setToolTip:NSLocalizedString(@"Clear the global history list", @"clear the global history list tooltip message")]; @@ -2939,7 +2978,7 @@ } // Check for History items - if ( [menuItem tag] >= SP_HISTORY_COPY_MENUITEM_TAG && [menuItem tag] <= SP_HISTORY_CLEAR_MENUITEM_TAG ) { + else if ( [menuItem tag] >= SP_HISTORY_COPY_MENUITEM_TAG && [menuItem tag] <= SP_HISTORY_CLEAR_MENUITEM_TAG ) { return ([queryHistoryButton numberOfItems]-7); } @@ -2983,6 +3022,9 @@ resultData = [[SPDataStorage alloc] init]; editedRow = -1; + currentHistoryOffsetIndex = -1; + historyItemWasJustInserted = NO; + prefs = [NSUserDefaults standardUserDefaults]; } @@ -3011,13 +3053,14 @@ */ - (IBAction)filterQueryHistory:(id)sender { - NSUInteger i; NSMenu *menu = [queryHistoryButton menu]; + NSUInteger numberOfItems = [menu numberOfItems]; + NSUInteger i; NSString *searchPattern = [queryHistorySearchField stringValue]; - - for (i = 7; i < [menu numberOfItems]; i++) + NSArray *history = [[SPQueryController sharedQueryController] historyForFileURL:[tableDocumentInstance fileURL]]; + for (i = 7; i < numberOfItems; i++) { - [[menu itemAtIndex:i] setHidden:(![[[menu itemAtIndex:i] title] isMatchedByRegex:[NSString stringWithFormat:@"(?i).*%@.*", searchPattern]])]; + [[menu itemAtIndex:i] setHidden:(![[history objectAtIndex:i-7] isMatchedByRegex:[NSString stringWithFormat:@"(?i).*%@.*", searchPattern]])]; } } diff --git a/Source/SPQueryController.h b/Source/SPQueryController.h index 2a7af9ca..a1948b4b 100644 --- a/Source/SPQueryController.h +++ b/Source/SPQueryController.h @@ -94,6 +94,8 @@ - (NSMutableArray *)favoritesForFileURL:(NSURL *)fileURL; - (NSMutableArray *)historyForFileURL:(NSURL *)fileURL; +- (NSArray *)historyMenuItemsForFileURL:(NSURL *)fileURL; +- (NSUInteger)numberOfHistoryItemsForFileURL:(NSURL *)fileURL; - (NSMutableDictionary *)contentFilterForFileURL:(NSURL *)fileURL; - (NSArray *)queryFavoritesForFileURL:(NSURL *)fileURL andTabTrigger:(NSString *)tabTrigger includeGlobals:(BOOL)includeGlobals; diff --git a/Source/SPQueryController.m b/Source/SPQueryController.m index 9f28c226..ae83a25d 100644 --- a/Source/SPQueryController.m +++ b/Source/SPQueryController.m @@ -644,6 +644,32 @@ static SPQueryController *sharedQueryController = nil; return [NSMutableArray array]; } +- (NSArray *)historyMenuItemsForFileURL:(NSURL *)fileURL +{ + if([historyContainer objectForKey:[fileURL absoluteString]]) { + NSMutableArray *returnArray = [[NSMutableArray arrayWithCapacity:[[historyContainer objectForKey:[fileURL absoluteString]] count]] autorelease]; + NSMenuItem *historyMenuItem; + for(id history in [historyContainer objectForKey:[fileURL absoluteString]]) { + historyMenuItem = [[[NSMenuItem alloc] initWithTitle:([history length] > 64) ? [NSString stringWithFormat:@"%@…", [history substringToIndex:63]] : history + action:NULL + keyEquivalent:@""] autorelease]; + [historyMenuItem setToolTip:([history length] > 256) ? [NSString stringWithFormat:@"%@…", [history substringToIndex:255]] : history]; + [returnArray addObject:historyMenuItem]; + } + + return returnArray; + } + + return [NSArray array]; +} + +- (NSUInteger)numberOfHistoryItemsForFileURL:(NSURL *)fileURL +{ + if([historyContainer objectForKey:[fileURL absoluteString]]) + return [[historyContainer objectForKey:[fileURL absoluteString]] count]; + else + return 0; +} - (NSMutableDictionary *)contentFilterForFileURL:(NSURL *)fileURL { if([contentFilterContainer objectForKey:[fileURL absoluteString]]) -- cgit v1.2.3