为什么 vim 错误地缩进了我的 Perl 代码?

发布于 2024-12-11 22:40:21 字数 2042 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁月打碎记忆 2024-12-18 22:40:21

也许这是一种神奇的想法,但是……我曾经有:

filetype plugin on
filetype indent on 

在我的 _vimrc 中(在 Windows XP 上,自编译的 gvim,各种版本),我会得到各种各样的东西Perl、LaTeX 和 HTML 文件中有趣的缩进问题。

现在,我已经做到了

filetype indent on 
filetype plugin on

,一切似乎都很顺利。 YMMV。

另外,我强烈推荐 Andy Lester 的 vim-perl

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.

奶茶白久 2024-12-18 22:40:21

cindent 特定于 c 语言,与许多其他语言一起使用时会被破坏。您可能想要使用的是文件类型插件缩进。您可以将其添加到 .vimrc 中,vim 会立即找出大多数语言的正确语法/缩进。如果 vim 还没有语法/缩进指南,您也可以相当轻松地添加它们。

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.

羁〃客ぐ 2024-12-18 22:40:21

我的系统使用 filetype indent on 正确缩进您的代码(与 filetypeplugin<代码>缩进)。 [Vim 7.2]

My system indents your code correctly using filetype indent on (versus filetypepluginindent on). [Vim 7.2]

暮色兮凉城 2024-12-18 22:40:21

将此问题追溯到 vim 正则表达式匹配器中的一个怪癖,在 perl.vim 缩进文件中,有几个地方正则表达式包含尝试使用 \[.... 来转义集合中的 [,

 **let bracepos = match(line, '[(){}\[\]]', bracepos + 1)**

但无论出于何种原因\[ 匹配行中的任何 \ 或 [ 而不仅仅是 [
因此,要修复 vim 缩进文件,请在所有匹配语句中取消转义左括号,即...

 let bracepos = match(line, '[(){}[\]]', bracepos + 1)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文