Results tagged “vim”

Vim script to fix the current file's package name

In our work project, there are lots of Test::Class-based modules, and they live in lib/ so other distributions can use them. Anyway, when writing a new module of tests, I don't start from an empty file but rather copy one of the existing test modules.

So I have to change the package name, but I've long felt that this can be automated - after all, if the module file is lib/Foo/Bar.pm, vim should be able to deduce that this is package Foo::Bar and change the package name accordingly.

The script below does that; it makes a few assumptions: that your modules live in a lib/ directory; that the package line already exists (but presumably with the wrong name) and that the package name to be replaced is in the first line that starts with 'package', followed by the name.

It's simple enough so you can adapt it to your needs. I hope you find this useful!

nnoremap <Leader>pa :<C-u>call PerlReplacePackageName()<CR>

function! PerlPackageNameFromFile()
    let filename = expand('%:p')
    let package = substitute(filename, '^.*/lib/', '', '')
    let package = substitute(package, '\.pm$', '', '')
    let package = substitute(package, '/', '::', 'g')
    return package
endfunction

function! PerlReplacePackageName()
    let package = PerlPackageNameFromFile()
    let pos = getpos('.')
    1,/^package\s/s/^package\s\+\zs[A-Za-z_0-9:]\+\ze\(\s\+{\|;\)/\=package/
    call setpos('.', pos)
endfunction

Thanks to kana1 for his corrections!

vim: add a 'use' statement without moving the cursor

You're writing Perl code in vim and have just typed a package name - maybe you want to create an object of this class:

some_statement;
my $o = Some::Class->new;
do_something_with($o);

You obviously need to write use Some::Class at the top. So you either move the cursor near the top and add the line, then jump to the previous line number, or maybe you split the window, move to the new viewport, make the change, then close that viewport.

There is a better way. Here is a vim function for adding a use statement to the top of your file. I prefer it to go below my last existing use statement, except for special cases like use strict, use constant etc. If there isn't any such regular use statement, I want the new use statement to appear below the ABSTRACT (a Dist::Zilla-related comment) or below the package statement. Failing that, I want the use statement to appear below any existing use statement. If there isn't one of those either, below the shebang line. If we don't even have that, just put it at the top of the file.

Your aesthetic requirements may vary, so change the function accordingly.

This is my first real Vim script function that I've written, so excuse the 'baby Vim'.

" perl: add 'use' statement
"
" make sure you have
"   setlocal iskeyword=48-57,_,A-Z,a-z,:
" so colons are recognized as part of a keyword

function! PerlAddUseStatement()
    let s:package = input('Package? ', expand('<cword>'))
    " skip if that use statement already exists
    if (search('^use\s\+'.s:package, 'bnw') == 0)
        " below the last use statement, except for some special cases
        let s:line = search('^use\s\+\(constant\|strict\|warnings\|parent\|base\|5\)\@!','bnw')
        " otherwise, below the ABSTRACT (see Dist::Zilla)
        if (s:line == 0)
            let s:line = search('^# ABSTRACT','bnw')
        endif
        " otherwise, below the package statement
        if (s:line == 0)
            let s:line = search('^package\s\+','bnw')
        endif
        " if there isn't a package statement either, put it below
        " the last use statement, no matter what it is
        if (s:line == 0)
            let s:line = search('^use\s\+','bnw')
        endif
        " if there are no package or use statements, it might be a
        " script; put it below the shebang line
        if (s:line == 0)
            let s:line = search('^#!','bnw')
        endif
        " if s:line still is 0, it just goes at the top
        call append(s:line, 'use ' . s:package . ';')
    endif
endfunction

map ,us :<C-u>call PerlAddUseStatement()<CR>
1

About hanekomu

user-pic