more readable moose snippets
[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, 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 LazyBuilder()
14     let pattern = "has\\s\\+\\zs.\\{-}\\ze\\s\\+=>"
15     let lnum = search(pattern, 'bnW')
16     return matchstr(getline(lnum), pattern)
17 endfunction
18
19 function Snippet(abbr, str)
20     if type(a:str) == type([])
21         return Snippet(a:abbr, join(a:str, "\n"))
22     endif
23     let st = g:snip_start_tag
24     let et = g:snip_end_tag
25     let cd = g:snip_elem_delim
26     let str = substitute(a:str, '<{.\{-}\zs:\ze.\{-}}>', cd, "")
27     let str = substitute(str, '<{', st, "")
28     let str = substitute(str, '}>', et, "")
29     exec 'Snippet '.a:abbr.' '.str
30 endfunction
31
32 function SnippetFile(filename)
33     let abbr = fnamemodify(a:filename, ':t:r')
34     let str = readfile(a:filename)
35     return Snippet(abbr, str)
36 endfunction
37
38 call Snippet('class', [
39             \"package <{ClassName}>;",
40             \"use Moose;",
41             \"",
42             \"extends '<{SuperClass:RemoveEmptySuperClass()}>;",
43             \"",
44             \"<{}>",
45             \"",
46             \"__PACKAGE__->meta->make_immutable;",
47             \"no Moose;",
48             \"",
49             \"1;"])
50 call Snippet('has', [
51             \"has <{attr}> => (",
52             \    "is  => '<{rw}>',",
53             \    "isa => '<{Str}>',",
54             \    "<{}>",
55             \");"])
56 call Snippet('hasl', [
57             \"has <{attr}> => (",
58             \    "is         => '<{rw}>',",
59             \    "isa        => '<{Str}>',",
60             \    "lazy_build => 1,",
61             \    "<{}>",
62             \");",
63             \"",
64             \"sub _build_<{attr:LazyBuilder()}> {",
65             \    "my $self = shift;",
66             \    "<{}>",
67             \"}"])
68 call Snippet('sub', [
69             \"sub <{name}> {",
70             \    "my $self = shift;",
71             \    "<{}>",
72             \"}"])
73 call Snippet('around', [
74             \"around <{name}> => sub {",
75             \    "my $orig = shift;",
76             \    "my $self = shift;",
77             \    "<{}>",
78             \"};"])
79
80 "for file in globpath(&rtp, 'snippets/*')
81     "call SnippetFile(file)
82 "endfor