haml.vim 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. " Vim indent file
  2. " Language: Haml
  3. " Maintainer: Tim Pope <vimNOSPAM@tpope.org>
  4. " Last Change: 2022 Mar 15
  5. if exists("b:did_indent")
  6. finish
  7. endif
  8. runtime! indent/ruby.vim
  9. unlet! b:did_indent
  10. let b:did_indent = 1
  11. setlocal autoindent
  12. setlocal indentexpr=GetHamlIndent()
  13. setlocal indentkeys=o,O,*<Return>,},],0),!^F,=end,=else,=elsif,=rescue,=ensure,=when
  14. let b:undo_indent = "setl ai< inde< indk<"
  15. " Only define the function once.
  16. if exists("*GetHamlIndent")
  17. finish
  18. endif
  19. let s:attributes = '\%({.\{-\}}\|\[.\{-\}\]\)'
  20. let s:tag = '\%([%.#][[:alnum:]_-]\+\|'.s:attributes.'\)*[<>]*'
  21. if !exists('g:haml_self_closing_tags')
  22. let g:haml_self_closing_tags = 'base|link|meta|br|hr|img|input'
  23. endif
  24. function! GetHamlIndent()
  25. let lnum = prevnonblank(v:lnum-1)
  26. if lnum == 0
  27. return 0
  28. endif
  29. let line = substitute(getline(lnum),'\s\+$','','')
  30. let cline = substitute(substitute(getline(v:lnum),'\s\+$','',''),'^\s\+','','')
  31. let lastcol = strlen(line)
  32. let line = substitute(line,'^\s\+','','')
  33. let indent = indent(lnum)
  34. let cindent = indent(v:lnum)
  35. let sw = shiftwidth()
  36. if cline =~# '\v^-\s*%(elsif|else|when)>'
  37. let indent = cindent < indent ? cindent : indent - sw
  38. endif
  39. let increase = indent + sw
  40. if indent == indent(lnum)
  41. let indent = cindent <= indent ? -1 : increase
  42. endif
  43. let group = synIDattr(synID(lnum,lastcol,1),'name')
  44. if line =~ '^!!!'
  45. return indent
  46. elseif line =~ '^/\%(\[[^]]*\]\)\=$'
  47. return increase
  48. elseif group == 'hamlFilter'
  49. return increase
  50. elseif line =~ '^'.s:tag.'[&!]\=[=~-]\s*\%(\%(if\|else\|elsif\|unless\|case\|when\|while\|until\|for\|begin\|module\|class\|def\)\>\%(.*\<end\>\)\@!\|.*do\%(\s*|[^|]*|\)\=\s*$\)'
  51. return increase
  52. elseif line =~ '^'.s:tag.'[&!]\=[=~-].*,\s*$'
  53. return increase
  54. elseif line == '-#'
  55. return increase
  56. elseif group =~? '\v^(hamlSelfCloser)$' || line =~? '^%\v%('.g:haml_self_closing_tags.')>'
  57. return indent
  58. elseif group =~? '\v^%(hamlTag|hamlAttributesDelimiter|hamlObjectDelimiter|hamlClass|hamlId|htmlTagName|htmlSpecialTagName)$'
  59. return increase
  60. elseif synIDattr(synID(v:lnum,1,1),'name') ==? 'hamlRubyFilter'
  61. return GetRubyIndent()
  62. else
  63. return indent
  64. endif
  65. endfunction
  66. " vim:set sw=2: