aboutsummaryrefslogtreecommitdiff
path: root/vim/autoload/perl.vim
blob: 7d82560462e150ed91fd46e5fcad88cb71f492e9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
" Function to add boilerplate intelligently
function! perl#Boilerplate() abort

  " Flag whether the buffer started blank
  let blank = line2byte(line('$') + 1) <= 2

  " This is a .pm file, guess its package name from path
  if expand('%:e') ==# 'pm'

    let match = matchlist(expand('%:p'), '.*/lib/\(.\+\).pm$')
    if len(match)
      let package = substitute(match[1], '/', '::', 'g')
    else
      let package = expand('%:t:r')
    endif

  " Otherwise, just use 'main'
  else
    let package = 'main'
  endif

  " Lines always to add
  let lines = [
        \ 'package '.package.';',
        \ '',
        \ 'use strict;',
        \ 'use warnings;',
        \ 'use utf8;',
        \ '',
        \ 'use 5.006;',
        \ '',
        \ 'our $VERSION = ''0.01'';',
        \ ''
        \ ]

  " Conditional lines depending on package
  if package ==# 'main'
    let lines = ['#!perl'] + lines
  else
    let lines = lines + ['', '1;']
  endif

  " Add all the lines in the array
  for line in lines
    call append(line('.') - 1, line)
  endfor

  " If we started in a completely empty buffer, delete the current blank line
  if blank
    delete
  endif

  " If we added a trailing '1' for a package, move the cursor up two lines
  if package !=# 'main'
    -2
  endif

endfunction