Fixes for extends and RemoveEmptySuperClass
[gitmo/moose-dev-utils.git] / vim / moose_snippets.vim
index 1c7e7ef..5b97d8d 100644 (file)
 " 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! RemoveEmptySuperClass()
-    s/^extends '<{}>';\n//e
+    if @z == "SuperClass"
+        s/extends '<{}>';\n\n//e
+        return ""
+    endif
+
+    return @z
+endfun
+
+function! RemoveEmptyLine()
+    s/^\s*<{}>\s*\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! 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('role', [
+            \"package <{RoleName}>;",
+            \"use Moose::Role;",
+            \"",
+            \"<{}>",
+            \"",
+            \"no Moose::Role;",
+            \"",
+            \"1;"])
+call Snippet('has', [
+            \"has <{attr}> => (",
+            \    "is  => '<{rw}>',",
+            \    "isa => '<{Str}>',",
+            \    "<{options:RemoveEmptyLine()}>",
+            \");"])
+call Snippet('hasl', [
+            \"has <{attr}> => (",
+            \    "is         => '<{rw}>',",
+            \    "isa        => '<{Str}>',",
+            \    "lazy_build => 1,",
+            \    "<{options:RemoveEmptyLine()}>",
+            \");",
+            \"",
+            \"sub _build_<{attr}> {",
+            \    "my $self = shift;",
+            \    "<{}>",
+            \"}"])
+call Snippet('sub', [
+            \"sub <{name}> {",
+            \    "my $self = shift;",
+            \    "<{}>",
+            \"}"])
+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;",
+            \    "<{}>",
+            \"};"])
+
+" MooseX support
+call Snippet('prole', [
+            \"package <{RoleName}>;",
+            \"use MooseX::Role::Parameterized;",
+            \"",
+            \"<{}>",
+            \"",
+            \"role {",
+            \"my $p = shift;",
+            \"",
+            \"<{}>",
+            \"};",
+            \"",
+            \"no MooseX::Role::Parameterized;",
+            \"",
+            \"1;"])
 
+"for file in globpath(&rtp, 'snippets/*')
+    "call SnippetFile(file)
+"endfor