more readable moose snippets
Jesse Luehrs [Tue, 5 May 2009 04:33:26 +0000 (23:33 -0500)]
vim/moose_snippets.vim

index 1c7e7ef..67a835d 100644 (file)
@@ -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.";<CR>use Moose;<CR>extends '".st."SuperClass:RemoveEmptySuperClass()".et."';<CR><CR>".st.et."<CR><CR>__PACKAGE__->meta->make_immutable;<CR>no Moose;<CR><CR>1;<CR>"
-exec "Snippet has has ".st."attr".et." => (<CR>is => 'rw',<CR>isa => '".st."Str".et."',<CR>".st.et."<CR>);"
-exec "Snippet hasl has ".st."attr".et." => (<CR>is => 'rw',<CR>isa => '".st."Str".et."',<CR>lazy_build => 1,<CR>);<CR><CR>sub _build_".st."attr".et." {<CR>my $self = shift;<CR>".st.et."<CR>}<CR><CR>"
-exec "Snippet sub sub ".st."name".et." {<CR>my $self = shift;<CR>".st.et."<CR>}<CR>"
-exec "Snippet around around ".st."name".et." => sub {<CR>my $next = shift;<CR>my $self = shift;<CR>".st.et."<CR>};<CR>"
+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