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 --- Source/CustomQuery.m | 128 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 102 insertions(+), 26 deletions(-) (limited to 'Source/CustomQuery.m') 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