纰缪绸缪

Un/Comment Script for Xcode

Xcode自带的Un/Comment功能太烂了。自己写了一个稍微好一点的:

#!/usr/bin/perl -w
 
my $outputString = "";
my $perlCmt = "#";
my $cCmt = "//";
 
my $filePath = "%%%{PBXFilePath}%%%";
my $cmt;
if ($filePath =~ /(\.h)|(\.hpp)|(\.cpp)|(\.c)|(\.m)|(\.thrift)$/) {
    $cmt = $cCmt;
} else {
    $cmt = $perlCmt;
}
 
my @selection = <STDIN>;       # read the selection from standard input
 
# no chars in selection, so create an empty selection
if (!@selection) {
    push @selection, "";
};
 
my $shouldComment = 0;
 
foreach my $line (@selection) {
    if ($line !~ /^\s*$/ && $line !~ /^\s*$cmt/) {
        $shouldComment = 1;
        last;
    }
}
 
 
if ($shouldComment) {
    # commenting 
    foreach my $line (@selection) {
        if ($line !~ /^\s+$/) {
            $line =~ s/^(\s*)/$1$cmt/g;
        }
        $outputString .= $line;
    }
} else {
    # un-commenting
    foreach my $line (@selection) {
        $line =~ s/^(\s*)$cmt\s?/$1/g;
        $outputString .= $line;
    }
}
 
 
print "%%%{PBXSelection}%%%";
print $outputString;
print "%%%{PBXSelection}%%%";
0 comments
Submit comment