puppet.vim 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. " Vim indent file
  2. " Language: Puppet
  3. " Maintainer: Todd Zullinger <tmz@pobox.com>
  4. " Last Change: 2009 Aug 19
  5. " vim: set sw=4 sts=4:
  6. if exists("b:did_indent")
  7. finish
  8. endif
  9. let b:did_indent = 1
  10. setlocal autoindent smartindent
  11. setlocal indentexpr=GetPuppetIndent()
  12. setlocal indentkeys+=0],0)
  13. if exists("*GetPuppetIndent")
  14. finish
  15. endif
  16. " Check if a line is part of an include 'block', e.g.:
  17. " include foo,
  18. " bar,
  19. " baz
  20. function! s:PartOfInclude(lnum)
  21. let lnum = a:lnum
  22. while lnum
  23. let lnum = lnum - 1
  24. let line = getline(lnum)
  25. if line !~ ',$'
  26. break
  27. endif
  28. if line =~ '^\s*include\s\+[^,]\+,$'
  29. return 1
  30. endif
  31. endwhile
  32. return 0
  33. endfunction
  34. function! s:OpenBrace(lnum)
  35. call cursor(a:lnum, 1)
  36. return searchpair('{\|\[\|(', '', '}\|\]\|)', 'nbW')
  37. endfunction
  38. function! GetPuppetIndent()
  39. let pnum = prevnonblank(v:lnum - 1)
  40. if pnum == 0
  41. return 0
  42. endif
  43. let line = getline(v:lnum)
  44. let pline = getline(pnum)
  45. let ind = indent(pnum)
  46. if pline =~ '^\s*#'
  47. return ind
  48. endif
  49. if pline =~ '\({\|\[\|(\|:\)$'
  50. let ind += &sw
  51. elseif pline =~ ';$' && pline !~ '[^:]\+:.*[=+]>.*'
  52. let ind -= &sw
  53. elseif pline =~ '^\s*include\s\+.*,$'
  54. let ind += &sw
  55. endif
  56. if pline !~ ',$' && s:PartOfInclude(pnum)
  57. let ind -= &sw
  58. endif
  59. " Match } }, }; ] ]: ], ]; )
  60. if line =~ '^\s*\(}\(,\|;\)\?$\|]:\|],\|}]\|];\?$\|)\)'
  61. let ind = indent(s:OpenBrace(v:lnum))
  62. endif
  63. " Don't actually shift over for } else {
  64. if line =~ '^\s*}\s*els\(e\|if\).*{\s*$'
  65. let ind -= &sw
  66. endif
  67. " Don't indent resources that are one after another with a ->(ordering arrow)
  68. " file {'somefile':
  69. " ...
  70. " } ->
  71. "
  72. " package { 'mycoolpackage':
  73. " ...
  74. " }
  75. if line =~ '->$'
  76. let ind -= &sw
  77. endif
  78. return ind
  79. endfunction