Search code examples
perlvimindentationvim-perl

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.


Solution

  • 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.