Remove empty space :)
[gitmo/moose-dev-utils.git] / vim / moose_snippets.vim
index 1c7e7ef..eb5260d 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! RemoveUnchangedLine()
+    if match(getline('.'), "<{}>") >= 0
+        normal! dd
+        return ''
+    else
+        return @z
+    endif
+endfun
+
+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! RemoveEmptySuperClass()
-    s/^extends '<{}>';\n//e
-    return @z
+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
 
-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>"
+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