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 | |
c7adedbc |
8 | function! RemoveUnchangedLine() |
9 | if match(getline('.'), "<{}>") >= 0 |
10 | normal! dd |
11 | return '' |
12 | else |
13 | return @z |
4c82fd0d |
14 | endif |
a55ef827 |
15 | endfun |
16 | |
f93358a2 |
17 | function! Snippet(abbr, str) |
9e14e28f |
18 | if type(a:str) == type([]) |
19 | return Snippet(a:abbr, join(a:str, "\n")) |
20 | endif |
21 | let st = g:snip_start_tag |
22 | let et = g:snip_end_tag |
23 | let cd = g:snip_elem_delim |
24 | let str = substitute(a:str, '<{.\{-}\zs:\ze.\{-}}>', cd, "") |
25 | let str = substitute(str, '<{', st, "") |
26 | let str = substitute(str, '}>', et, "") |
27 | exec 'Snippet '.a:abbr.' '.str |
28 | endfunction |
29 | |
f93358a2 |
30 | function! SnippetFile(filename) |
9e14e28f |
31 | let abbr = fnamemodify(a:filename, ':t:r') |
32 | let str = readfile(a:filename) |
33 | return Snippet(abbr, str) |
34 | endfunction |
35 | |
f26587a1 |
36 | function! ParameterizedModule() |
37 | let current_file = expand("%") |
38 | let t = substitute(current_file, '\.pm', '', '') |
39 | let t = substitute(t, 'lib/', '', '') |
40 | |
41 | " Sadly, this does not work because : is a special character in snippet |
42 | " tag names. Any ideas? |
43 | "let module = substitute(t, '/', '::', 'g') |
44 | let module = substitute(t, '/.*', '', '') |
45 | |
46 | return '<{' . module . '}>' |
47 | endfun |
48 | |
9e14e28f |
49 | call Snippet('class', [ |
f26587a1 |
50 | \"package ".ParameterizedModule().";", |
9e14e28f |
51 | \"use Moose;", |
52 | \"", |
c7adedbc |
53 | \"extends '<{SuperClass:RemoveUnchangedLine()}>;", |
9e14e28f |
54 | \"", |
55 | \"<{}>", |
56 | \"", |
57 | \"__PACKAGE__->meta->make_immutable;", |
58 | \"no Moose;", |
59 | \"", |
60 | \"1;"]) |
49ac30b5 |
61 | call Snippet('role', [ |
f26587a1 |
62 | \"package ".ParameterizedModule().";", |
49ac30b5 |
63 | \"use Moose::Role;", |
64 | \"", |
65 | \"<{}>", |
66 | \"", |
67 | \"no Moose::Role;", |
68 | \"", |
69 | \"1;"]) |
9e14e28f |
70 | call Snippet('has', [ |
71 | \"has <{attr}> => (", |
72 | \ "is => '<{rw}>',", |
73 | \ "isa => '<{Str}>',", |
c7adedbc |
74 | \ "<{options:RemoveUnchangedLine()}>", |
9e14e28f |
75 | \");"]) |
76 | call Snippet('hasl', [ |
77 | \"has <{attr}> => (", |
78 | \ "is => '<{rw}>',", |
79 | \ "isa => '<{Str}>',", |
80 | \ "lazy_build => 1,", |
c7adedbc |
81 | \ "<{options:RemoveUnchangedLine()}>", |
9e14e28f |
82 | \");", |
83 | \"", |
a75bdf8d |
84 | \"sub _build_<{attr}> {", |
9e14e28f |
85 | \ "my $self = shift;", |
86 | \ "<{}>", |
87 | \"}"]) |
88 | call Snippet('sub', [ |
89 | \"sub <{name}> {", |
90 | \ "my $self = shift;", |
c7adedbc |
91 | \ "my (<{arg:RemoveUnchangedLine()}>) = @_;", |
9e14e28f |
92 | \ "<{}>", |
93 | \"}"]) |
94 | call Snippet('around', [ |
95 | \"around <{name}> => sub {", |
babbac06 |
96 | \ "my $<{next}> = shift;", |
9e14e28f |
97 | \ "my $self = shift;", |
98 | \ "<{}>", |
99 | \"};"]) |
9ac2672c |
100 | call Snippet('before', [ |
101 | \"before <{name}> => sub {", |
102 | \ "my $self = shift;", |
103 | \ "<{}>", |
104 | \"};"]) |
105 | call Snippet('after', [ |
106 | \"after <{name}> => sub {", |
107 | \ "my $self = shift;", |
108 | \ "<{}>", |
109 | \"};"]) |
bb2ea7b7 |
110 | |
41348999 |
111 | " MooseX support |
112 | call Snippet('prole', [ |
f26587a1 |
113 | \"package ".ParameterizedModule().";", |
41348999 |
114 | \"use MooseX::Role::Parameterized;", |
115 | \"", |
116 | \"<{}>", |
117 | \"", |
118 | \"role {", |
119 | \"my $p = shift;", |
120 | \"", |
121 | \"<{}>", |
122 | \"};", |
123 | \"", |
124 | \"no MooseX::Role::Parameterized;", |
125 | \"", |
126 | \"1;"]) |
127 | |
9e14e28f |
128 | "for file in globpath(&rtp, 'snippets/*') |
129 | "call SnippetFile(file) |
130 | "endfor |