From 01fd0bed61eaa3ea3be6c5992bfe749aec090f9c Mon Sep 17 00:00:00 2001 From: Bibiko Date: Thu, 16 Jul 2009 13:58:27 +0000 Subject: =?UTF-8?q?=E2=80=A2=20rewrote=20=E2=8C=98/=20"comment=20line"=20c?= =?UTF-8?q?ompletely=20-=20=E2=8C=98/=20(un)comment=20current=20line=20onl?= =?UTF-8?q?y=20if=20no=20selection=20is=20given=20(by=20using=20"--=20")?= =?UTF-8?q?=20--=20caret=20jumps=20to=20the=20next=20line=20to=20(un)comme?= =?UTF-8?q?nt=20lines=20quickly=20--=20#=20sign=20will=20be=20recognized?= =?UTF-8?q?=20for=20uncommenting=20as=20well=20as=20if=20the=20entire=20li?= =?UTF-8?q?ne=20is=20wrapped=20into=20/*=20*/=20-=20=E2=8C=98/=20(un)comme?= =?UTF-8?q?nt=20current=20selection=20(by=20wrapping=20it=20into=20/*=20*/?= =?UTF-8?q?)=20--=20it=20(un)escapes=20present=20*/=20into=20*\/=20automat?= =?UTF-8?q?ically=20-=20=E2=8C=A5=E2=8C=98/=20(un)comment=20the=20current?= =?UTF-8?q?=20query=20(by=20wrapping=20it=20into=20/*=20*/)=20--=20the=20G?= =?UTF-8?q?UI=20element=20will=20be=20found=20in=20the=20Gear=20Menu=20by?= =?UTF-8?q?=20pressing=20=E2=8C=A5=20=E2=80=A2=20added=20shortcut=20?= =?UTF-8?q?=E2=8C=AB=20to=20Table=20Content's=20"Delete=20selected=20row(s?= =?UTF-8?q?)"=20tooltip?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Interfaces/English.lproj/DBView.xib | 77 +++++++++++++++++----- Source/CustomQuery.h | 6 +- Source/CustomQuery.m | 128 ++++++++++++++++++++++++++++-------- 3 files changed, 165 insertions(+), 46 deletions(-) diff --git a/Interfaces/English.lproj/DBView.xib b/Interfaces/English.lproj/DBView.xib index 0e4922d9..c8a2f328 100644 --- a/Interfaces/English.lproj/DBView.xib +++ b/Interfaces/English.lproj/DBView.xib @@ -1878,7 +1878,7 @@ content - + 256 YES @@ -1954,7 +1954,6 @@ {{1, 17}, {688, 454}} - 2 @@ -1988,7 +1987,6 @@ {{1, 0}, {688, 17}} - 4 @@ -1997,7 +1995,6 @@ {{6, 32}, {690, 472}} - 562 @@ -2485,6 +2482,7 @@ {{10, 7}, {700, 544}} + Content @@ -2493,7 +2491,7 @@ customQuery - + 256 YES @@ -2580,6 +2578,7 @@ {{1, 1}, {688, 155}} + @@ -2611,6 +2610,7 @@ {690, 157} + 530 @@ -2697,6 +2697,7 @@ {{1, 17}, {688, 228}} + 4 @@ -2729,6 +2730,7 @@ {{1, 0}, {688, 17}} + 4 @@ -2737,6 +2739,7 @@ {690, 246} + 562 @@ -3027,7 +3030,7 @@ - Comment current Query + Comment Line / 1048576 2147483647 @@ -3037,6 +3040,19 @@ 11 + + + YES + Comment Current Query + / + 1572864 + 2147483647 + + + _popUpItemAction: + 12 + + Completion List @@ -3117,7 +3133,7 @@ YES - 10 + 15 YES 1 YES @@ -3263,7 +3279,6 @@ {{10, 7}, {700, 544}} - Custom Query @@ -4302,14 +4317,14 @@ - + 134217731 YES YES YES - + @@ -15103,11 +15118,27 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 - commentCurrentQueryOrSelectionMenuItem + commentLineOrSelectionMenuItem - 6251 + 6252 + + + + gearMenuItemSelected: + + + + 6254 + + + + commentCurrentQueryMenuItem + + + + 6255 @@ -18042,6 +18073,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 + @@ -21328,6 +21360,11 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 + + 6253 + + + @@ -22618,6 +22655,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 6241.IBPluginDependency 6242.IBPluginDependency 6249.IBPluginDependency + 6253.IBPluginDependency 654.IBPluginDependency 654.ImportedFromIB2 655.IBPluginDependency @@ -23676,8 +23714,8 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin - {{105, 148}, {944, 550}} - {{105, 148}, {944, 550}} + {{148, 285}, {944, 550}} + {{148, 285}, {944, 550}} {{62, 352}, {845, 504}} @@ -23812,7 +23850,7 @@ aGUgYWN0aXZlIHNlbGVjdGlvbiAo4oyl4oyYUik ToolTip - Delete selected row(s) + RGVsZXRlIHNlbGVjdGVkIHJvdyhzKSAo4oyrKQ com.apple.InterfaceBuilder.CocoaPlugin @@ -23843,7 +23881,7 @@ aGUgYWN0aXZlIHNlbGVjdGlvbiAo4oyl4oyYUik com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin - {{314, 239}, {236, 288}} + {{314, 222}, {236, 305}} com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin @@ -24464,6 +24502,7 @@ aGUgYWN0aXZlIHNlbGVjdGlvbiAo4oyl4oyYUik com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin @@ -24683,7 +24722,7 @@ Y2hhbmdlIHRoZSBvcmRlcg - 6251 + 6255 @@ -25996,7 +26035,8 @@ Y2hhbmdlIHRoZSBvcmRlcg autopairMenuItem autouppercaseKeywordsMenuItem clearHistoryMenuItem - commentCurrentQueryOrSelectionMenuItem + commentCurrentQueryMenuItem + commentLineOrSelectionMenuItem completionListMenuItem copyQueryFavoriteButton customQueryView @@ -26034,6 +26074,7 @@ Y2hhbmdlIHRoZSBvcmRlcg NSMenuItem NSMenuItem NSMenuItem + NSMenuItem id CMCopyTable NSMenuItem diff --git a/Source/CustomQuery.h b/Source/CustomQuery.h index 9d6ea2df..47be44c7 100644 --- a/Source/CustomQuery.h +++ b/Source/CustomQuery.h @@ -63,7 +63,8 @@ IBOutlet NSMenuItem *autopairMenuItem; IBOutlet NSMenuItem *autohelpMenuItem; IBOutlet NSMenuItem *autouppercaseKeywordsMenuItem; - IBOutlet NSMenuItem *commentCurrentQueryOrSelectionMenuItem; + IBOutlet NSMenuItem *commentCurrentQueryMenuItem; + IBOutlet NSMenuItem *commentLineOrSelectionMenuItem; IBOutlet NSWindow *helpWebViewWindow; IBOutlet WebView *helpWebView; @@ -149,7 +150,8 @@ - (void)setFavorites; - (void)doPerformQueryService:(NSString *)query; - (void)selectCurrentQuery; -- (void)commentOutQuery; +- (void)commentOut; +- (void)commentOutCurrentQueryTakingSelection:(BOOL)takeSelection; - (NSString *)usedQuery; - (NSString *)argumentForRow:(NSUInteger)rowIndex ofTable:(NSString *)tableForColumn; diff --git a/Source/CustomQuery.m b/Source/CustomQuery.m index 59be27f8..afd24e8d 100644 --- a/Source/CustomQuery.m +++ b/Source/CustomQuery.m @@ -212,10 +212,17 @@ [textView shiftSelectionLeft]; } - // "Comment Current Query/Selection" menu item - Add or remove "-- " for each line - // in the current query or selection - if (sender == commentCurrentQueryOrSelectionMenuItem) { - [self commentOutQuery]; + // "Comment Line/Selection" menu item - Add or remove "-- " for each line + // in a line or selection resp. or wrap the selection into /* */ + // if the selection does not end at the end of a line (in-line comment) + if (sender == commentLineOrSelectionMenuItem) { + [self commentOut]; + } + + // "Comment Current Query" menu item - Add or remove "-- " for each line + // in the current query + if (sender == commentCurrentQueryMenuItem) { + [self commentOutCurrentQueryTakingSelection:NO]; } // "Completion List" menu item - used to autocomplete. Uses a different shortcut to avoid the menu button flickering @@ -934,39 +941,105 @@ } /* - * Add or remove "-- " for each line in the current query or selection + * Add or remove "/* *~/" for each line in the current query + * a given selection */ -- (void)commentOutQuery +- (void)commentOutCurrentQueryTakingSelection:(BOOL)takeSelection { - - if(!currentQueryRange.length && ![textView selectedRange].length) { - NSBeep(); - return; - } - - NSRange workingRange; - NSRange oldRange = [textView selectedRange]; + BOOL isUncomment = NO; - if(oldRange.length) - workingRange = oldRange; - else + NSRange oldRange = [textView selectedRange]; + + NSRange workingRange = oldRange; + if(!takeSelection) workingRange = currentQueryRange; - + NSMutableString *n = [NSMutableString string]; - [n setString:[NSString stringWithFormat:@"-- %@", [[textView string] substringWithRange:workingRange]]]; - [n replaceOccurrencesOfRegex:@"\\n(?=.)" withString:@"\n-- "]; - // comment out if at least one line is already commented out - if(!([n isMatchedByRegex:@"(\\n-- --|^-- --)"] && [n isMatchedByRegex:@"(\\n-- |^-- )(?!-)"])) { - [n replaceOccurrencesOfRegex:@"\\n-- -- " withString:@"\n"]; - [n replaceOccurrencesOfRegex:@"^-- -- " withString:@""]; + [n setString:[[textView string] substringWithRange:workingRange]]; + + // Escape given */ by *\/ + [n replaceOccurrencesOfRegex:@"\\*/(?=.)" withString:@"*\\\\/"]; + [n replaceOccurrencesOfRegex:@"\\*/(?=\\n)" withString:@"*\\\\/"]; + + // Wrap current query into /* */ + [n replaceOccurrencesOfRegex:@"^" withString:@"/* "]; + [n replaceOccurrencesOfRegex:@"$" withString:@" */"]; + + // Check if current query/selection is already commented out, if so uncomment it + if([n isMatchedByRegex:@"^/\\* \\s*/\\*\\s*(.|\\n)*?\\s*\\*/ \\*/\\s*$"]) { + [n replaceOccurrencesOfRegex:@"^/\\* \\s*/\\*\\s*" withString:@""]; + [n replaceOccurrencesOfRegex:@"\\s*\\*/ \\*/\\s*$" withString:@""]; + // unescape *\/ + [n replaceOccurrencesOfRegex:@"\\*\\\\/" withString:@"*/"]; + isUncomment = YES; } - [textView setSelectedRange:workingRange]; + // Replace current query/selection by (un)commented string + [textView setSelectedRange:workingRange]; [textView insertText:n]; + + // If commenting out locate the caret just after the first /* to allow to enter + // something like /*!400000 or similar + if(!isUncomment) + [textView setSelectedRange:NSMakeRange(workingRange.location+2,0)]; + +} + +/* + * Add or remove "-- " for each line in the current query or selection, + * if the selection is in-line wrap selection into /* block comments and + * place the caret after /* to allow to enter !xxxxxx e.g. + */ +- (void)commentOut +{ + + NSRange oldRange = [textView selectedRange]; + + if(oldRange.length) { + [self commentOutCurrentQueryTakingSelection:YES]; + } else { // single line + + // get the current line range + NSRange lineRange = [[textView string] lineRangeForRange:oldRange]; + NSMutableString *n = [NSMutableString string]; + + // Put "-- " in front of the current line + [n setString:[NSString stringWithFormat:@"-- %@", [[textView string] substringWithRange:lineRange]]]; + + // Check if current line is already commented out, if so uncomment it + // and preserve the original indention via regex:@"^-- (\\s*)" + if([n isMatchedByRegex:@"^-- \\s*(--\\s|#)"]) { + [n replaceOccurrencesOfRegex:@"^-- \\s*(--\\s|#)" + withString:[n substringWithRange:[n rangeOfRegex:@"^-- (\\s*)" + options:RKLNoOptions + inRange:NSMakeRange(0,[n length]) + capture:1 + error: nil]]]; + } else if ([n isMatchedByRegex:@"^-- \\s*/\\*.*?\\*/\\s*$"]) { + [n replaceOccurrencesOfRegex:@"^-- \\s*/\\* ?" + withString:[n substringWithRange:[n rangeOfRegex:@"^-- (\\s*)" + options:RKLNoOptions + inRange:NSMakeRange(0,[n length]) + capture:1 + error: nil]]]; + [n replaceOccurrencesOfRegex:@"\\*/\\s*$" + withString:[n substringWithRange:[n rangeOfRegex:@"\\*/(\\s*)$" + options:RKLNoOptions + inRange:NSMakeRange(0,[n length]) + capture:1 + error: nil]]]; + } + + // Replace current line by (un)commented string + // The caret will be placed at the beginning of the next line if present to + // allow a fast (un)commenting of lines + [textView setSelectedRange:lineRange]; + [textView insertText:n]; + + } - [textView setSelectedRange:NSMakeRange(NSMaxRange(currentQueryRange),0)]; } #pragma mark - @@ -1653,6 +1726,9 @@ currentQueryRange = NSMakeRange(0, 0); } + // disable "Comment Current Query" meun item if no current query is selectable + [commentCurrentQueryMenuItem setEnabled:(currentQueryRange.length) ? YES : NO]; + // If no text is selected, disable the button and action menu. if ( caretPosition == NSNotFound ) { [runSelectionButton setEnabled:NO]; -- cgit v1.2.3