From db424ed281c8cf9c99406b248c195f0c8a4a6c38 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 29 Mar 2017 00:16:47 +0200 Subject: Allow copying lines from query console by using drag-and-drop (part of #2749) --- Source/SPQueryConsoleDataSource.m | 11 +++++ Source/SPQueryController.h | 17 +++++++ Source/SPQueryController.m | 85 +++++++++++++++++++---------------- Source/SPQueryControllerInitializer.m | 3 ++ 4 files changed, 77 insertions(+), 39 deletions(-) (limited to 'Source') diff --git a/Source/SPQueryConsoleDataSource.m b/Source/SPQueryConsoleDataSource.m index eaac2151..eb669051 100644 --- a/Source/SPQueryConsoleDataSource.m +++ b/Source/SPQueryConsoleDataSource.m @@ -97,4 +97,15 @@ static NSUInteger SPMessageTruncateCharacterLength = 256; #endif } +- (BOOL)tableView:(NSTableView *)aTableView writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)pboard +{ + NSString *string = [self sqlStringForRowIndexes:rowIndexes]; + if([string length]) { + [pboard declareTypes:@[NSStringPboardType] owner:self]; + return [pboard setString:string forType:NSStringPboardType]; + } + + return NO; +} + @end diff --git a/Source/SPQueryController.h b/Source/SPQueryController.h index 8d128fe3..12ff6d7b 100644 --- a/Source/SPQueryController.h +++ b/Source/SPQueryController.h @@ -84,6 +84,10 @@ extern NSString *SPTableViewDatabaseColumnID; + (SPQueryController *)sharedQueryController; +/** + * Calls -sqlStringForForRowIndexes: with the current selection and + * puts the output into the general Pasteboard (only if non-empty) + */ - (IBAction)copy:(id)sender; - (IBAction)clearConsole:(id)sender; - (IBAction)saveConsoleAs:(id)sender; @@ -103,4 +107,17 @@ extern NSString *SPTableViewDatabaseColumnID; - (NSUInteger)consoleMessageCount; +/** + * Returns the console messages specified by indexes as a string, each message separated by "\n". + * @param indexes The indexes of rows to be returned. + * Invalid indexes will be skipped silently. + * nil is treated as an empty set. + * + * If no (valid) indexes are given, @"" will be returned. + * The output may include other info like timestamp, host, etc. if shown in the table view, as part of a comment. + * + * THIS METHOD IS NOT THREAD-SAFE! + */ +- (NSString *)sqlStringForRowIndexes:(NSIndexSet *)indexes; + @end diff --git a/Source/SPQueryController.m b/Source/SPQueryController.m index dbdc3d44..41d71154 100644 --- a/Source/SPQueryController.m +++ b/Source/SPQueryController.m @@ -150,49 +150,13 @@ static SPQueryController *sharedQueryController = nil; */ - (void)copy:(id)sender { -#ifndef SP_CODA NSResponder *firstResponder = [[self window] firstResponder]; if ((firstResponder == consoleTableView) && ([consoleTableView numberOfSelectedRows] > 0)) { - - NSMutableString *string = [NSMutableString string]; + NSIndexSet *rows = [consoleTableView selectedRowIndexes]; - BOOL includeTimestamps = ![[consoleTableView tableColumnWithIdentifier:SPTableViewDateColumnID] isHidden]; - BOOL includeConnections = ![[consoleTableView tableColumnWithIdentifier:SPTableViewConnectionColumnID] isHidden]; - BOOL includeDatabases = ![[consoleTableView tableColumnWithIdentifier:SPTableViewDatabaseColumnID] isHidden]; - - [string setString:@""]; - - [rows enumerateIndexesUsingBlock:^(NSUInteger i, BOOL * _Nonnull stop) { - if (i < [messagesVisibleSet count]) { - SPConsoleMessage *message = NSArrayObjectAtIndex(messagesVisibleSet, i); - - if (includeTimestamps || includeConnections || includeDatabases) [string appendString:@"/* "]; - - NSDate *date = [message messageDate]; - if (includeTimestamps && date) { - [string appendString:[dateFormatter stringFromDate:date]]; - [string appendString:@" "]; - } - - NSString *connection = [message messageConnection]; - if (includeConnections && connection) { - [string appendString:connection]; - [string appendString:@" "]; - } - - NSString *database = [message messageDatabase]; - if (includeDatabases && database) { - [string appendString:database]; - [string appendString:@" "]; - } - - if (includeTimestamps || includeConnections || includeDatabases) [string appendString:@"*/ "]; - - [string appendFormat:@"%@\n", [message message]]; - } - }]; + NSString *string = [self sqlStringForRowIndexes:rows]; NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard]; @@ -200,7 +164,50 @@ static SPQueryController *sharedQueryController = nil; [pasteBoard declareTypes:@[NSStringPboardType] owner:nil]; [pasteBoard setString:string forType:NSStringPboardType]; } -#endif +} + +- (NSString *)sqlStringForRowIndexes:(NSIndexSet *)rows +{ + if(![rows count]) return @""; + + NSMutableString *string = [[NSMutableString alloc] init]; + + BOOL includeTimestamps = ![[consoleTableView tableColumnWithIdentifier:SPTableViewDateColumnID] isHidden]; + BOOL includeConnections = ![[consoleTableView tableColumnWithIdentifier:SPTableViewConnectionColumnID] isHidden]; + BOOL includeDatabases = ![[consoleTableView tableColumnWithIdentifier:SPTableViewDatabaseColumnID] isHidden]; + + [rows enumerateIndexesUsingBlock:^(NSUInteger i, BOOL * _Nonnull stop) { + if (i < [messagesVisibleSet count]) { + SPConsoleMessage *message = NSArrayObjectAtIndex(messagesVisibleSet, i); + + if (includeTimestamps || includeConnections || includeDatabases) [string appendString:@"/* "]; + + NSDate *date = [message messageDate]; + if (includeTimestamps && date) { + [string appendString:[dateFormatter stringFromDate:date]]; + [string appendString:@" "]; + } + + NSString *connection = [message messageConnection]; + if (includeConnections && connection) { + [string appendString:connection]; + [string appendString:@" "]; + } + + NSString *database = [message messageDatabase]; + if (includeDatabases && database) { + [string appendString:database]; + [string appendString:@" "]; + } + + if (includeTimestamps || includeConnections || includeDatabases) [string appendString:@"*/ "]; + + [string appendString:[message message]]; + [string appendString:@"\n"]; + } + }]; + + return [string autorelease]; } /** diff --git a/Source/SPQueryControllerInitializer.m b/Source/SPQueryControllerInitializer.m index fc03d107..f8292b37 100644 --- a/Source/SPQueryControllerInitializer.m +++ b/Source/SPQueryControllerInitializer.m @@ -85,6 +85,9 @@ static NSString *SPCompletionTokensSnippetsKey = @"function_argument_snippets"; { [[column dataCell] setFont:(useMonospacedFont) ? [NSFont fontWithName:SPDefaultMonospacedFontName size:monospacedFontSize] : [NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; } + + //allow drag-out copying of selected rows + [consoleTableView setDraggingSourceOperationMask:NSDragOperationCopy forLocal:NO]; #endif } -- cgit v1.2.3