From: Jesse Luehrs Date: Tue, 5 May 2009 04:33:26 +0000 (-0500) Subject: more readable moose snippets X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2Fmoose-dev-utils.git;a=commitdiff_plain;h=9e14e28f32e3ece43fab98e4fec94d6502c36914 more readable moose snippets --- diff --git a/vim/moose_snippets.vim b/vim/moose_snippets.vim index 1c7e7ef..67a835d 100644 --- a/vim/moose_snippets.vim +++ b/vim/moose_snippets.vim @@ -5,18 +5,78 @@ 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! RemoveEmptySuperClass() +function RemoveEmptySuperClass() s/^extends '<{}>';\n//e return @z endfun -exec "Snippet class package ".st."ClassName".et.";use Moose;extends '".st."SuperClass:RemoveEmptySuperClass()".et."';".st.et."__PACKAGE__->meta->make_immutable;no Moose;1;" -exec "Snippet has has ".st."attr".et." => (is => 'rw',isa => '".st."Str".et."',".st.et.");" -exec "Snippet hasl has ".st."attr".et." => (is => 'rw',isa => '".st."Str".et."',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."}" -exec "Snippet around around ".st."name".et." => sub {my $next = shift;my $self = shift;".st.et."};" +function LazyBuilder() + let pattern = "has\\s\\+\\zs.\\{-}\\ze\\s\\+=>" + let lnum = search(pattern, 'bnW') + return matchstr(getline(lnum), pattern) +endfunction + +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 + +call Snippet('class', [ + \"package <{ClassName}>;", + \"use Moose;", + \"", + \"extends '<{SuperClass:RemoveEmptySuperClass()}>;", + \"", + \"<{}>", + \"", + \"__PACKAGE__->meta->make_immutable;", + \"no Moose;", + \"", + \"1;"]) +call Snippet('has', [ + \"has <{attr}> => (", + \ "is => '<{rw}>',", + \ "isa => '<{Str}>',", + \ "<{}>", + \");"]) +call Snippet('hasl', [ + \"has <{attr}> => (", + \ "is => '<{rw}>',", + \ "isa => '<{Str}>',", + \ "lazy_build => 1,", + \ "<{}>", + \");", + \"", + \"sub _build_<{attr:LazyBuilder()}> {", + \ "my $self = shift;", + \ "<{}>", + \"}"]) +call Snippet('sub', [ + \"sub <{name}> {", + \ "my $self = shift;", + \ "<{}>", + \"}"]) +call Snippet('around', [ + \"around <{name}> => sub {", + \ "my $orig = shift;", + \ "my $self = shift;", + \ "<{}>", + \"};"]) +"for file in globpath(&rtp, 'snippets/*') + "call SnippetFile(file) +"endfor