问题
I have a subroutine in Perl that should be indented like this:
sub GetFiles
{
my $pwd = shift;
my @input = @_;
my @returned;
my @DirectoryContent = &GetContentInformation(@input);
foreach (@DirectoryContent)
{
my %current = %{$_};
if ($current{'info'} =~ /<DIR>/)
{
my $RecurseDir = &GetRecurseDir($pwd, \%current);
push(@returned, &GetFiles($RecurseDir,
&GetDirectoryContents($RecurseDir)));
}
else
{
# clean up the data
my $size = $current{'info'};
# filesize will be in number of bytes
# remove file separators
#$size =~ s/,//g;
my $extension = &GetFileExtension($current{'name'});
delete($current{'info'});
$current{'size'} = $size;
$current{'extension'} = $extension;
# push(@returned, \%current);
}
}
@returned;
}
But when I press =%
(yes, cindent
is on) with the cursor on the starting bracket of the subroutine block, it indents it like this:
sub GetFiles
{
my $pwd = shift;
my @input = @_;
my @returned;
my @DirectoryContent = &GetContentInformation(@input);
foreach (@DirectoryContent)
{
my %current = %{$_};
if ($current{'info'} =~ /<DIR>/)
{
my $RecurseDir = &GetRecurseDir($pwd, \%current);
push(@returned, &GetFiles($RecurseDir, &GetDirectoryContents($RecurseDir)));
}
else
{
# clean up the data
my $size = $current{'info'};
# filesize will be in number of bytes
# remove file separators
#$size =~ s/,//g;
my $extension = &GetFileExtension($current{'name'});
delete($current{'info'});
$current{'size'} = $size;
$current{'extension'} = $extension;
# push(@returned, \%current);
}
}
@returned;
}
Why does it do that? How can I fix it?
EDIT: It should be noted that I am using gvim 7.3 on Windows.
回答1:
Maybe this is magical thinking, but … I used to have:
filetype plugin on
filetype indent on
in my _vimrc
(on Windows XP, self-compiled gvim
, various versions), and I would get all sorts of interesting indentation problems in Perl, LaTeX, and HTML files.
Now, I have
filetype indent on
filetype plugin on
and everything seems to be hunk-dory. YMMV.
Also, I highly recommend Andy Lester's vim-perl.
回答2:
cindent
is specific to the c
language and is broken when used with a lot of other languages. What you probably want to use is filetype plugin indent on
. You can add that to your .vimrc
and vim will figure out the correct syntax/indentation for most languages out of the box. You can also add syntax/indentation guides fairly easily if vim doesn't already have them.
回答3:
My system indents your code correctly using filetype indent on
(versus filetype
plugin
indent on
). [Vim 7.2]
回答4:
Tracked this issue down to a quirk in the vim regular expression matcher, in the perl.vim indent file there are several places where the regular expression includes an attempt to escape a [ in a collection with \[....
**let bracepos = match(line, '[(){}\[\]]', bracepos + 1)**
but for whatever reason the \[ matches any \ or [ in the line not just [ so to fix the vim indent file unescape the left brackets in all match statements, ie...
let bracepos = match(line, '[(){}[\]]', bracepos + 1)
来源:https://stackoverflow.com/questions/7905836/why-is-vim-indenting-my-perl-code-incorrectly