为什么 vim 错误地缩进了我的 Perl 代码?
我在 Perl 中有一个子例程,应该像这样缩进:
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;
}
但是当我按 =%
(是的,cindent
已打开)并且光标位于子例程的起始括号上时块,它像这样缩进:
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;
}
为什么要这样做?我该如何修复它?
编辑:应该注意的是,我在 Windows 上使用 gvim 7.3。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许这是一种神奇的想法,但是……我曾经有:
在我的
_vimrc
中(在 Windows XP 上,自编译的gvim
,各种版本),我会得到各种各样的东西Perl、LaTeX 和 HTML 文件中有趣的缩进问题。现在,我已经做到了
,一切似乎都很顺利。 YMMV。
另外,我强烈推荐 Andy Lester 的 vim-perl。
Maybe this is magical thinking, but … I used to have:
in my
_vimrc
(on Windows XP, self-compiledgvim
, various versions), and I would get all sorts of interesting indentation problems in Perl, LaTeX, and HTML files.Now, I have
and everything seems to be hunk-dory. YMMV.
Also, I highly recommend Andy Lester's vim-perl.
cindent
特定于c
语言,与许多其他语言一起使用时会被破坏。您可能想要使用的是文件类型插件缩进
。您可以将其添加到.vimrc
中,vim 会立即找出大多数语言的正确语法/缩进。如果 vim 还没有语法/缩进指南,您也可以相当轻松地添加它们。cindent
is specific to thec
language and is broken when used with a lot of other languages. What you probably want to use isfiletype 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.我的系统使用
filetype indent on
正确缩进您的代码(与filetype
plugin
<代码>缩进)。 [Vim 7.2]My system indents your code correctly using
filetype indent on
(versusfiletype
plugin
indent on
). [Vim 7.2]将此问题追溯到 vim 正则表达式匹配器中的一个怪癖,在 perl.vim 缩进文件中,有几个地方正则表达式包含尝试使用 \[.... 来转义集合中的 [,
但无论出于何种原因\[ 匹配行中的任何 \ 或 [ 而不仅仅是 [
因此,要修复 vim 缩进文件,请在所有匹配语句中取消转义左括号,即...
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 \[....
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...