Commit | Line | Data |
bb2ea7b7 |
1 | " See http://www.vim.org/scripts/script.php?script_id=1318 |
9f4e6b22 |
2 | " Written by Sartak and doy, feel free to add your own! |
bb2ea7b7 |
3 | |
4 | if !exists('loaded_snippet') || &cp |
5 | finish |
6 | endif |
7 | |
f93358a2 |
8 | function! RemoveEmptySuperClass() |
4c82fd0d |
9 | if @z == "SuperClass" |
10 | s/extends '<{}>';\n\n//e |
11 | return "" |
12 | endif |
13 | |
9a5242eb |
14 | return @z |
15 | endfun |
16 | |
f93358a2 |
17 | function! RemoveEmptyLine() |
a55ef827 |
18 | s/^\s*<{}>\s*\n//e |
19 | return @z |
20 | endfun |
21 | |
f93358a2 |
22 | function! Snippet(abbr, str) |
9e14e28f |
23 | if type(a:str) == type([]) |
24 | return Snippet(a:abbr, join(a:str, "\n")) |
25 | endif |
26 | let st = g:snip_start_tag |
27 | let et = g:snip_end_tag |
28 | let cd = g:snip_elem_delim |
29 | let str = substitute(a:str, '<{.\{-}\zs:\ze.\{-}}>', cd, "") |
30 | let str = substitute(str, '<{', st, "") |
31 | let str = substitute(str, '}>', et, "") |
32 | exec 'Snippet '.a:abbr.' '.str |
33 | endfunction |
34 | |
f93358a2 |
35 | function! SnippetFile(filename) |
9e14e28f |
36 | let abbr = fnamemodify(a:filename, ':t:r') |
37 | let str = readfile(a:filename) |
38 | return Snippet(abbr, str) |
39 | endfunction |
40 | |
41 | call Snippet('class', [ |
42 | \"package <{ClassName}>;", |
43 | \"use Moose;", |
44 | \"", |
4c82fd0d |
45 | \"extends '<{SuperClass:RemoveEmptySuperClass()}>';", |
9e14e28f |
46 | \"", |
47 | \"<{}>", |
48 | \"", |
49 | \"__PACKAGE__->meta->make_immutable;", |
50 | \"no Moose;", |
51 | \"", |
52 | \"1;"]) |
49ac30b5 |
53 | call Snippet('role', [ |
54 | \"package <{RoleName}>;", |
55 | \"use Moose::Role;", |
56 | \"", |
57 | \"<{}>", |
58 | \"", |
59 | \"no Moose::Role;", |
60 | \"", |
61 | \"1;"]) |
9e14e28f |
62 | call Snippet('has', [ |
63 | \"has <{attr}> => (", |
64 | \ "is => '<{rw}>',", |
65 | \ "isa => '<{Str}>',", |
a55ef827 |
66 | \ "<{options:RemoveEmptyLine()}>", |
9e14e28f |
67 | \");"]) |
68 | call Snippet('hasl', [ |
69 | \"has <{attr}> => (", |
70 | \ "is => '<{rw}>',", |
71 | \ "isa => '<{Str}>',", |
72 | \ "lazy_build => 1,", |
a55ef827 |
73 | \ "<{options:RemoveEmptyLine()}>", |
9e14e28f |
74 | \");", |
75 | \"", |
a75bdf8d |
76 | \"sub _build_<{attr}> {", |
9e14e28f |
77 | \ "my $self = shift;", |
78 | \ "<{}>", |
79 | \"}"]) |
80 | call Snippet('sub', [ |
81 | \"sub <{name}> {", |
82 | \ "my $self = shift;", |
83 | \ "<{}>", |
84 | \"}"]) |
85 | call Snippet('around', [ |
86 | \"around <{name}> => sub {", |
babbac06 |
87 | \ "my $<{next}> = shift;", |
9e14e28f |
88 | \ "my $self = shift;", |
89 | \ "<{}>", |
90 | \"};"]) |
9ac2672c |
91 | call Snippet('before', [ |
92 | \"before <{name}> => sub {", |
93 | \ "my $self = shift;", |
94 | \ "<{}>", |
95 | \"};"]) |
96 | call Snippet('after', [ |
97 | \"after <{name}> => sub {", |
98 | \ "my $self = shift;", |
99 | \ "<{}>", |
100 | \"};"]) |
bb2ea7b7 |
101 | |
41348999 |
102 | " MooseX support |
103 | call Snippet('prole', [ |
104 | \"package <{RoleName}>;", |
105 | \"use MooseX::Role::Parameterized;", |
106 | \"", |
107 | \"<{}>", |
108 | \"", |
109 | \"role {", |
110 | \"my $p = shift;", |
111 | \"", |
112 | \"<{}>", |
113 | \"};", |
114 | \"", |
115 | \"no MooseX::Role::Parameterized;", |
116 | \"", |
117 | \"1;"]) |
118 | |
9e14e28f |
119 | "for file in globpath(&rtp, 'snippets/*') |
120 | "call SnippetFile(file) |
121 | "endfor |