summaryrefslogtreecommitdiffstats
path: root/ext/vim/indent
diff options
context:
space:
mode:
authorTodd Zullinger <tmz@pobox.com>2009-08-19 23:44:44 -0400
committerTodd Zullinger <tmz@pobox.com>2011-04-02 10:07:11 -0400
commit7f658e6a19e43856ebc6aafe4f3a6e94cb765e73 (patch)
tree52846c205cc68e82992a8fb474ccc29e27a3d5ee /ext/vim/indent
parent59559a31b8c28535d740267914de711b92870d08 (diff)
downloadpuppet-7f658e6a19e43856ebc6aafe4f3a6e94cb765e73.tar.gz
puppet-7f658e6a19e43856ebc6aafe4f3a6e94cb765e73.tar.xz
puppet-7f658e6a19e43856ebc6aafe4f3a6e94cb765e73.zip
vim: Initial ftplugin and indent support
Vim's indent support makes it easier to automatically indent puppet manifests.
Diffstat (limited to 'ext/vim/indent')
-rw-r--r--ext/vim/indent/puppet.vim76
1 files changed, 76 insertions, 0 deletions
diff --git a/ext/vim/indent/puppet.vim b/ext/vim/indent/puppet.vim
new file mode 100644
index 000000000..689e06879
--- /dev/null
+++ b/ext/vim/indent/puppet.vim
@@ -0,0 +1,76 @@
+" Vim indent file
+" Language: Puppet
+" Maintainer: Todd Zullinger <tmz@pobox.com>
+" Last Change: 2009 Aug 19
+" vim: set sw=4 sts=4:
+
+if exists("b:did_indent")
+ finish
+endif
+let b:did_indent = 1
+
+setlocal autoindent smartindent
+setlocal indentexpr=GetPuppetIndent()
+setlocal indentkeys+=0],0)
+
+if exists("*GetPuppetIndent")
+ finish
+endif
+
+" Check if a line is part of an include 'block', e.g.:
+" include foo,
+" bar,
+" baz
+function! s:PartOfInclude(lnum)
+ let lnum = a:lnum
+ while lnum
+ let lnum = lnum - 1
+ let line = getline(lnum)
+ if line !~ ',$'
+ break
+ endif
+ if line =~ '^\s*include\s\+[^,]\+,$'
+ return 1
+ endif
+ endwhile
+ return 0
+endfunction
+
+function! s:OpenBrace(lnum)
+ call cursor(a:lnum, 1)
+ return searchpair('{\|\[\|(', '', '}\|\]\|)', 'nbW')
+endfunction
+
+function! GetPuppetIndent()
+ let pnum = prevnonblank(v:lnum - 1)
+ if pnum == 0
+ return 0
+ endif
+
+ let line = getline(v:lnum)
+ let pline = getline(pnum)
+ let ind = indent(pnum)
+
+ if pline =~ '^\s*#'
+ return ind
+ endif
+
+ if pline =~ '\({\|\[\|(\|:\)$'
+ let ind += &sw
+ elseif pline =~ ';$' && pline !~ '[^:]\+:.*[=+]>.*'
+ let ind -= &sw
+ elseif pline =~ '^\s*include\s\+.*,$'
+ let ind += &sw
+ endif
+
+ if pline !~ ',$' && s:PartOfInclude(pnum)
+ let ind -= &sw
+ endif
+
+ " Match } }, }; ] ]: )
+ if line =~ '^\s*\(}\(,\|;\)\?$\|]:\?$\|)\)'
+ let ind = indent(s:OpenBrace(v:lnum))
+ endif
+
+ return ind
+endfunction