📄 stlfilt.pl
字号:
}
}
}
}
elsif ($c eq "'")
{
$in_quotes = !$in_quotes;
}
$nesting_key[$pos] = $nesting_level; # track nesting level at each column
$quoting_key[$pos] = $in_quotes; # track nesting level at each column
}
# STEP 0: If there's an unmatched back-quote before the end of the line,
# and we're not in col. 1, and it isn't just 'id', wrap before the back-quote:
if (length($line) > $width and substr($line, 0, $width-1) =~ /('$id[^']*$)/) #` Fix syntax highlighting for emacs
{
$name_pos = $-[0];
if ($name_pos != 0)
{
print "*********** PRINT DBG 6 width=$width indent=$indentation *********\n" if $pdbg;
print2 ($indentation, (substr($line, 0, $name_pos) . "\n"));
$line = substr($line, $name_pos);
$nesting_level = $nesting_key[$name_pos - 1];
$in_quotes = $quoting_key[$name_pos - 1];
next WRAPLOOP;
}
}
# STEP 1: If there's an incomplete paren pair on the current line:
if (@delims)
{
#
# STEP 1A: Find the first open paren on the line whose matching close paren doesn't fit:
#
$open_pos = $delim_index[0];
while (1)
{
$beforeit = substr($line, 0, $open_pos);
# STEP 1Ai: If it's immediately preceded by >::ident[::ident...]
# or foo.template bar
# or foo.bar(...).template baz
if (($beforeit =~ /[>)](\s*(::|\.|->)?\s*$id)+$/) and $-[0] != 0)
{
# STEP 1A1a: If the opening angle bracket matching the 1st char of the r.e. above is on
# this line, move back to that position and go to 1Ai
$open_pos = $-[0];
if ($delim_opener[$open_pos] != -1)
{
$open_pos = $delim_opener[$open_pos];
next;
}
# STEP 1A1b: Else move to the first char of the r.e. above and go on to 1B (this is
# already where $open_pos indicates)
}
last;
}
if ($first_unmatched_close < $open_pos)
{
print "******** PRINT DBG 6A width=$width indent=$indentation ******\n" if $pdbg;
print2 ($indentation, (substr($line, 0, $first_unmatched_close) . "\n"));
$line = substr($line, $first_unmatched_close);
$nesting_level = $nesting_key[$first_unmatched_close - 1];
$in_quotes = $quoting_key[$first_unmatched_close - 1];
next WRAPLOOP;
}
#
# STEP 1B: If we're on an open paren, and there's a comma earlier on the line at the same level,
# and the last such comma is not the first non-ws char on the line, wrap just before it
# and don't indent:
#
$c = substr($line, $open_pos, 1);
if (exists $open_delims{$c}) # if it's an open paren...
{
if ($in_quotes and scanback "$line")
{
# We've found the comma
if ($comma_wrap eq 'B' and $nesting_level > 0)
{
# Wrap just before $comma_pos:
print "******** PRINT DBG 7 width=$width indent=$indentation ******\n" if $pdbg;
print2 ($indentation, (substr($line, 0, $comma_pos) . "\n"));
$line = substr($line, $comma_pos);
$nesting_level = $nesting_key[$comma_pos - 1];
$in_quotes = $quoting_key[$comma_pos - 1];
}
else
{
# Wrap just after $comma_pos:
print "******** PRINT DBG 8 width=$width indent=$indentation ******\n" if $pdbg;
print2 ($indentation, (substr($line, 0, $comma_pos +1) . "\n"));
while (substr($line, $comma_pos + 1, 1) eq ' ')
{
$comma_pos++;
}
$line = substr($line, $comma_pos + 1);
$nesting_level = $nesting_key[$comma_pos];
$in_quotes = $quoting_key[$comma_pos];
}
next WRAPLOOP;
}
}
# STEP 1C: Else if it's an open paren:
# if $nesting_level is 0, wrap before the pattern
# if line begins at col. 1, wrap just after the open and indent the next line
$c = substr($line, $open_pos, 1);
if (exists $open_delims{$c}) # if it's an open paren...
{
print "*********** PRINT DBG 9 width=$width indent=$indentation *********\n" if $pdbg;
print2 ($indentation, (substr($line, 0, $open_pos + 1) . "\n"));
$width -= $tabsize;
$line = " " . substr($line, $open_pos + 1); # special case xtra leading indent
$nesting_level = $nesting_key[$open_pos];
$in_quotes = $quoting_key[$open_pos];
next WRAPLOOP;
}
else # It must be a close paren. Wrap just before it and unindent the next line:
{
print "*********** PRINT DBG 10 width=$width indent=$indentation *********\n" if $pdbg;
print2 ($indentation, (substr($line, 0, $open_pos) . "\n"));
$width += $tabsize;
$line = substr($line, $open_pos);
$nesting_level = $nesting_key[$open_pos - 1];
$in_quotes = $quoting_key[$open_pos - 1];
next WRAPLOOP;
}
}
elsif ($first_unmatched_close < $pos)
{
print "*********** PRINT DBG 10A width=$width indent=$indentation *********\n" if $pdbg;
print2 ($indentation, (substr($line, 0, $first_unmatched_close) . "\n"));
$line = substr($line, $first_unmatched_close);
$nesting_level = $nesting_key[$first_unmatched_close - 1];
$in_quotes = $quoting_key[$first_unmatched_close - 1];
next WRAPLOOP;
}
else
{
# STEP 2: If there is a comma at the current level of paren nesting AND
# we're not at a nesting level of 0:
$open_pos = $pos;
if (!$at_left and $in_quotes and scanback "$line")
{
if ($comma_wrap eq 'B' and $nesting_level > 0)
{
print "*********** PRINT DBG 11: width=$width indent=$indentation *********\n" if $pdbg;
print2 ($indentation, (substr($line, 0, $comma_pos) . "\n"));
$line = substr($line, $comma_pos);
$nesting_level = $nesting_key[$comma_pos - 1];
$in_quotes = $quoting_key[$comma_pos - 1];
}
else
{
print "*********** PRINT DBG 12: width=$width indent=$indentation *********\n" if $pdbg;
print2 ($indentation, (substr($line, 0, $comma_pos + 1) . "\n"));
while ($comma_pos < ($width - 1) and substr($line, $comma_pos + 1, 1) eq ' ')
{
$comma_pos++;
}
$line = substr($line, $comma_pos + 1);
$nesting_level = $nesting_key[$comma_pos];
$in_quotes = $quoting_key[$comma_pos];
}
}
else
{
# extra by LZ: if last part of line is "'identifier" we have an unbalanced
# nesting level, then break before the "'"
if (length($line) > $width )
{
$beforeit = substr($line, 0, $open_pos);
if ($line =~ /'$id/ or $line =~/'[+-=!*<>%^&|\/~]+/)
{
$name_pos = $-[0];
if ($name_pos != 0 and $name_pos < $width and
!(($beforeit =~ /'$id'/ or $beforeit =~ /'[+-=!*<>%^&|\/~]+'/)
and $+[0] < $width)) #'))) fix syntax highlighting for emacs
{
if (exists $close_delims{substr($line, 0, 1)}) # special case for leading close
{
print "*********** PRINT DBG 13: width=$width indent=$indentation *********\n" if $pdbg;
break_and_print_plain2 (($tab x ($indentation - 1)) .
substr($line, 0, $name_pos) . "\n");
}
else
{
print "*********** PRINT DBG 14: width=$width indent=$indentation *********\n" if $pdbg;
break_and_print_plain2 (($tab x $indentation) .
substr($line, 0, $name_pos) . "\n");
}
$line = substr($line, $name_pos);
$nesting_level = $nesting_key[$name_pos - 1];
$in_quotes = $quoting_key[$name_pos - 1];
next WRAPLOOP;
}
}
}
# STEP 3: Just break according to standard alrogithm
print "*********** PRINT DBG 15: width=$width indent=$indentation *********\n" if $pdbg;
$line = break_and_print_fragment ( $indentation, $width, $line);
$nesting_level = $nesting_key[$broke_at];
$in_quotes = $quoting_key[$broke_at];
return if $line eq "";
}
}
} # WRAPLOOP
}
#
# break entire line using "plain" rules:
# usage: break_and_print_plain line
#
sub break_and_print_plain {
my $line = shift(@_);
if ($output_width != 0)
{
return if ($line = break_and_print_fragment (0, $output_width, "$line")) eq "";
while (($line = break_and_print_fragment (1, $output_width - $tabsize, "$line")) ne "")
{}
}
else
{
$line =~ s/\s+\n/\n/g; # delete trailing space on a line
print "$line";
}
}
#
# Process line using "Plain" break algorithm up to first line break,
# return remainder of line for subsequent processing:
# usage: break_and_print_fragment indent line
#
# No matter what, don't break in the middle of a pathname...so as not
# to mess up tools that locate errors in files. If the line length has
# to exceed the /width setting, so be it in that case...
#
sub break_and_print_fragment {
my $indent = shift(@_);
my $width = shift(@_) - 1;
my $line = shift(@_);
$nl_pos = index($line, "\n");
if ($nl_pos == -1 or $nl_pos > $width)
{
if (length($line) > $width)
{
if ($prefix ne "" and $line =~ /\(\d+\):\s/g and pos($line) > $width) # never break within pathname
{
substr($line, pos($line), 0) = "\n";
}
else
{
$pos = $width;
$theChar = substr($line, $pos, 1);
$theCharBefore = ($pos > 0) ? substr($line, $pos - 1, 1) : ' ';
$theCharAfter = substr($line, $pos + 1, 1);
while ( ($theChar !~ /[\n '`,:]/ and $pos > 0 and !($theChar =~ /\w/ and $theCharBefore !~ /\w/))
or ($theChar !~ /\s/ and $theCharBefore =~ /[:\/\\.]/)
or ($theChar eq ':' and $theCharAfter =~ /[ \/\\.]/)
or ($theChar eq "'" and $theCharBefore !~ /\s/)
or ($theChar =~ /[A-Za-z]/ and $theCharBefore =~ /['"`]/)
)
{
$pos--;
$theCharAfter = $theChar;
$theChar = substr($line, $pos, 1);
$theCharBefore = ($pos > 0) ? substr($line, $pos - 1, 1) : ' ';
}
$pos-- if $pos > 0 and $theChar eq ':' and substr($line, $pos-1, 1) eq ':';
if ($pos == 0)
{
$pos += $width;
substr($line, $pos, 0) = "\n";
}
else
{
substr($line, $pos, $theChar eq ' ' ? 1 : 0) = "\n";
$pos += ($theChar eq ' ' ? 1 : 2);
}
}
}
}
$line =~ s/ +\n/\n/g; # delete trailing space on a line
$nl_pos = index($line, "\n");
if ($nl_pos == -1)
{
print2 ($indent, $line);
return "";
}
print2 ($indent, substr($line, 0, $nl_pos + 1));
$whats_left = substr($line, $nl_pos + 1);
$broke_at = $nl_pos - 1;
return "$whats_left";
}
#
# call break_and_print_plain/fragment, adjust leading comma
#
sub break_and_print_plain2 {
my $line = shift(@_);
$line =~ s/^(\s*) ,/$1,/; # Adjust leading
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -