d551e7cf44cd2d2b1857f78ac5cf0ae4ff6407cd
[gitmo/moose-dev-utils.git] / vim / moose_snippets.vim
1 " See http://www.vim.org/scripts/script.php?script_id=1318
2 " Written by Sartak and doy, feel free to add your own!
3
4 if !exists('loaded_snippet') || &cp
5     finish
6 endif
7
8 function! RemoveEmptySuperClass()
9     s/^extends '<{}>';\n//e
10     return @z
11 endfun
12
13 function! RemoveEmptyLine()
14     s/^\s*<{}>\s*\n//e
15     return @z
16 endfun
17
18 function! Snippet(abbr, str)
19     if type(a:str) == type([])
20         return Snippet(a:abbr, join(a:str, "\n"))
21     endif
22     let st = g:snip_start_tag
23     let et = g:snip_end_tag
24     let cd = g:snip_elem_delim
25     let str = substitute(a:str, '<{.\{-}\zs:\ze.\{-}}>', cd, "")
26     let str = substitute(str, '<{', st, "")
27     let str = substitute(str, '}>', et, "")
28     exec 'Snippet '.a:abbr.' '.str
29 endfunction
30
31 function! SnippetFile(filename)
32     let abbr = fnamemodify(a:filename, ':t:r')
33     let str = readfile(a:filename)
34     return Snippet(abbr, str)
35 endfunction
36
37 call Snippet('class', [
38             \"package <{ClassName}>;",
39             \"use Moose;",
40             \"",
41             \"extends '<{SuperClass:RemoveEmptySuperClass()}>;",
42             \"",
43             \"<{}>",
44             \"",
45             \"__PACKAGE__->meta->make_immutable;",
46             \"no Moose;",
47             \"",
48             \"1;"])
49 call Snippet('has', [
50             \"has <{attr}> => (",
51             \    "is  => '<{rw}>',",
52             \    "isa => '<{Str}>',",
53             \    "<{options:RemoveEmptyLine()}>",
54             \");"])
55 call Snippet('hasl', [
56             \"has <{attr}> => (",
57             \    "is         => '<{rw}>',",
58             \    "isa        => '<{Str}>',",
59             \    "lazy_build => 1,",
60             \    "<{options:RemoveEmptyLine()}>",
61             \");",
62             \"",
63             \"sub _build_<{attr}> {",
64             \    "my $self = shift;",
65             \    "<{}>",
66             \"}"])
67 call Snippet('sub', [
68             \"sub <{name}> {",
69             \    "my $self = shift;",
70             \    "<{}>",
71             \"}"])
72 call Snippet('around', [
73             \"around <{name}> => sub {",
74             \    "my $orig = shift;",
75             \    "my $self = shift;",
76             \    "<{}>",
77             \"};"])
78 call Snippet('before', [
79             \"before <{name}> => sub {",
80             \    "my $self = shift;",
81             \    "<{}>",
82             \"};"])
83 call Snippet('after', [
84             \"after <{name}> => sub {",
85             \    "my $self = shift;",
86             \    "<{}>",
87             \"};"])
88
89 "for file in globpath(&rtp, 'snippets/*')
90     "call SnippetFile(file)
91 "endfor