X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=vim%2Fmoose_snippets.vim;h=a7484cedca4d11fbffb0e471e4c504a13c048d55;hb=bf6996ff2a1bd65a412d13cdd477a0e52b4642cb;hp=ceefb909e8860ef4f48a85f35e067f1b1006f436;hpb=bb2ea7b76f019f2ff95904a6a0b13345f9fa82ca;p=gitmo%2Fmoose-dev-utils.git diff --git a/vim/moose_snippets.vim b/vim/moose_snippets.vim index ceefb90..a7484ce 100644 --- a/vim/moose_snippets.vim +++ b/vim/moose_snippets.vim @@ -1,16 +1,148 @@ " See http://www.vim.org/scripts/script.php?script_id=1318 -" Written by Sartak, feel free to add your own! +" Written by Sartak and doy, feel free to add your own! if !exists('loaded_snippet') || &cp finish endif -let st = g:snip_start_tag -let et = g:snip_end_tag -let cd = g:snip_elem_delim +function! RemoveUnchangedLine() + if match(getline('.'), "<{}>") >= 0 + normal! dd + return '' + else + return @z + endif +endfun -exec "Snippet class package ".st."ClassName".et.";use Moose;".st.et."__PACKAGE__->meta->make_immutable;no Moose;1;" -exec "Snippet has has ".st."attr".et." => (is => 'rw',isa => 'Str',".st.et.");" -exec "Snippet hasl has ".st."attr".et." => (is => 'rw',isa => 'Str',lazy_build => 1,);sub _build_".st."attr".et." {my $self = shift;".st.et."}" -exec "Snippet sub sub ".st."name".et." {my $self = shift;".st.et."}" +function! RemoveUnchangedLineSemicolon() + if match(getline('.'), "<{}>") >= 0 + normal! ddkA; + normal! o + return '' + else + normal! A; + "normal! o " we can't actually change what line we're on :/ + return @z + endif +endfun +function! Snippet(abbr, str) + if type(a:str) == type([]) + return Snippet(a:abbr, join(a:str, "\n")) + endif + let st = g:snip_start_tag + let et = g:snip_end_tag + let cd = g:snip_elem_delim + let str = substitute(a:str, '<{.\{-}\zs:\ze.\{-}}>', cd, "") + let str = substitute(str, '<{', st, "") + let str = substitute(str, '}>', et, "") + exec 'Snippet '.a:abbr.' '.str +endfunction + +function! SnippetFile(filename) + let abbr = fnamemodify(a:filename, ':t:r') + let str = readfile(a:filename) + return Snippet(abbr, str) +endfunction + +function! ParameterizedModule() + let current_file = expand("%") + let t = substitute(current_file, '\.pm', '', '') + let t = substitute(t, 'lib/', '', '') + + " Sadly, this does not work because : is a special character in snippet + " tag names. Any ideas? + "let module = substitute(t, '/', '::', 'g') + let module = substitute(t, '/.*', '', '') + + return '<{' . module . '}>' +endfun + +call Snippet('class', [ + \"package ".ParameterizedModule().";", + \"use Moose;", + \"", + \"extends '<{SuperClass:RemoveUnchangedLine()}>';", + \"", + \"<{}>", + \"", + \"__PACKAGE__->meta->make_immutable;", + \"no Moose;", + \"", + \"1;"]) +call Snippet('role', [ + \"package ".ParameterizedModule().";", + \"use Moose::Role;", + \"", + \"<{}>", + \"", + \"no Moose::Role;", + \"", + \"1;"]) +call Snippet('has', [ + \"has <{attr}> => (", + \ "is => '<{rw}>',", + \ "isa => '<{Str}>',", + \ "<{options:RemoveUnchangedLine()}>", + \");"]) +call Snippet('hasl', [ + \"has <{attr}> => (", + \ "is => '<{rw}>',", + \ "isa => '<{Str}>',", + \ "lazy_build => 1,", + \ "<{options:RemoveUnchangedLine()}>", + \");", + \"", + \"sub _build_<{attr}> {", + \ "my $self = shift;", + \ "<{}>", + \"}"]) +call Snippet('sub', [ + \"sub <{name}> {", + \ "my $self = shift;", + \ "my (<{arg:RemoveUnchangedLine()}>) = @_;", + \ "<{}>", + \"}"]) +call Snippet('around', [ + \"around <{name}> => sub {", + \ "my $<{next}> = shift;", + \ "my $self = shift;", + \ "<{}>", + \"};"]) +call Snippet('before', [ + \"before <{name}> => sub {", + \ "my $self = shift;", + \ "<{}>", + \"};"]) +call Snippet('after', [ + \"after <{name}> => sub {", + \ "my $self = shift;", + \ "<{}>", + \"};"]) +call Snippet('subtype', [ + \"subtype '<{name}>'", + \ "=> as '<{supertype}>'", + \ "=> where { <{predicate:RemoveUnchangedLine()}> }", + \ "=> message { \"<{message:RemoveUnchangedLineSemicolon()}>\" }", + \""]) + +" MooseX support +call Snippet('prole', [ + \"package ".ParameterizedModule().";", + \"use MooseX::Role::Parameterized;", + \"", + \"<{}>", + \"", + \"role {", + \"my $p = shift;", + \"", + \"<{}>", + \"};", + \"", + \"no MooseX::Role::Parameterized;", + \"", + \"1;"]) + +"for file in globpath(&rtp, 'snippets/*') + "call SnippetFile(file) +"endfor