简体   繁体   中英

Why is vim indenting my Perl code incorrectly?

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.

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 .

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.

My system indents your code correctly using (versus filetype indent on ). (与filetype indent on )正确地缩进代码。 [Vim 7.2]

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM