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 | |
f26587a1 |
41 | function! ParameterizedModule() |
42 | let current_file = expand("%") |
43 | let t = substitute(current_file, '\.pm', '', '') |
44 | let t = substitute(t, 'lib/', '', '') |
45 | |
46 | " Sadly, this does not work because : is a special character in snippet |
47 | " tag names. Any ideas? |
48 | "let module = substitute(t, '/', '::', 'g') |
49 | let module = substitute(t, '/.*', '', '') |
50 | |
51 | return '<{' . module . '}>' |
52 | endfun |
53 | |
9e14e28f |
54 | call Snippet('class', [ |
f26587a1 |
55 | \"package ".ParameterizedModule().";", |
9e14e28f |
56 | \"use Moose;", |
57 | \"", |
4c82fd0d |
58 | \"extends '<{SuperClass:RemoveEmptySuperClass()}>';", |
9e14e28f |
59 | \"", |
60 | \"<{}>", |
61 | \"", |
62 | \"__PACKAGE__->meta->make_immutable;", |
63 | \"no Moose;", |
64 | \"", |
65 | \"1;"]) |
49ac30b5 |
66 | call Snippet('role', [ |
f26587a1 |
67 | \"package ".ParameterizedModule().";", |
49ac30b5 |
68 | \"use Moose::Role;", |
69 | \"", |
70 | \"<{}>", |
71 | \"", |
72 | \"no Moose::Role;", |
73 | \"", |
74 | \"1;"]) |
9e14e28f |
75 | call Snippet('has', [ |
76 | \"has <{attr}> => (", |
77 | \ "is => '<{rw}>',", |
78 | \ "isa => '<{Str}>',", |
a55ef827 |
79 | \ "<{options:RemoveEmptyLine()}>", |
9e14e28f |
80 | \");"]) |
81 | call Snippet('hasl', [ |
82 | \"has <{attr}> => (", |
83 | \ "is => '<{rw}>',", |
84 | \ "isa => '<{Str}>',", |
85 | \ "lazy_build => 1,", |
a55ef827 |
86 | \ "<{options:RemoveEmptyLine()}>", |
9e14e28f |
87 | \");", |
88 | \"", |
a75bdf8d |
89 | \"sub _build_<{attr}> {", |
9e14e28f |
90 | \ "my $self = shift;", |
91 | \ "<{}>", |
92 | \"}"]) |
93 | call Snippet('sub', [ |
94 | \"sub <{name}> {", |
95 | \ "my $self = shift;", |
96 | \ "<{}>", |
97 | \"}"]) |
98 | call Snippet('around', [ |
99 | \"around <{name}> => sub {", |
babbac06 |
100 | \ "my $<{next}> = shift;", |
9e14e28f |
101 | \ "my $self = shift;", |
102 | \ "<{}>", |
103 | \"};"]) |
9ac2672c |
104 | call Snippet('before', [ |
105 | \"before <{name}> => sub {", |
106 | \ "my $self = shift;", |
107 | \ "<{}>", |
108 | \"};"]) |
109 | call Snippet('after', [ |
110 | \"after <{name}> => sub {", |
111 | \ "my $self = shift;", |
112 | \ "<{}>", |
113 | \"};"]) |
bb2ea7b7 |
114 | |
41348999 |
115 | " MooseX support |
116 | call Snippet('prole', [ |
f26587a1 |
117 | \"package ".ParameterizedModule().";", |
41348999 |
118 | \"use MooseX::Role::Parameterized;", |
119 | \"", |
120 | \"<{}>", |
121 | \"", |
122 | \"role {", |
123 | \"my $p = shift;", |
124 | \"", |
125 | \"<{}>", |
126 | \"};", |
127 | \"", |
128 | \"no MooseX::Role::Parameterized;", |
129 | \"", |
130 | \"1;"]) |
131 | |
9e14e28f |
132 | "for file in globpath(&rtp, 'snippets/*') |
133 | "call SnippetFile(file) |
134 | "endfor |