Skip to content
Permalink
Browse files

Allow copying lines from query console by using drag-and-drop (part of

  • Loading branch information
dmoagx committed Mar 28, 2017
1 parent dbcea5c commit db424ed281c8cf9c99406b248c195f0c8a4a6c38
Showing with 77 additions and 39 deletions.
  1. +11 −0 Source/SPQueryConsoleDataSource.m
  2. +17 −0 Source/SPQueryController.h
  3. +46 −39 Source/SPQueryController.m
  4. +3 −0 Source/SPQueryControllerInitializer.m
@@ -97,4 +97,15 @@ - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColum
#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
@@ -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
@@ -150,57 +150,64 @@ - (oneway void)release { }
*/
- (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];

// Copy the string to the pasteboard
[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];
}

/**
@@ -85,6 +85,9 @@ - (void)awakeFromNib
{
[[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
}

0 comments on commit db424ed

Please sign in to comment.
You can’t perform that action at this time.