lua.vim 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. " Vim indent file
  2. " Language: Lua script
  3. " Maintainer: Marcus Aurelius Farias <marcus.cf 'at' bol.com.br>
  4. " First Author: Max Ischenko <mfi 'at' ukr.net>
  5. " Last Change: 2017 Jun 13
  6. " Only load this indent file when no other was loaded.
  7. if exists("b:did_indent")
  8. finish
  9. endif
  10. let b:did_indent = 1
  11. setlocal indentexpr=GetLuaIndent()
  12. " To make Vim call GetLuaIndent() when it finds '\s*end' or '\s*until'
  13. " on the current line ('else' is default and includes 'elseif').
  14. setlocal indentkeys+=0=end,0=until
  15. setlocal autoindent
  16. " Only define the function once.
  17. if exists("*GetLuaIndent")
  18. finish
  19. endif
  20. function! GetLuaIndent()
  21. " Find a non-blank line above the current line.
  22. let prevlnum = prevnonblank(v:lnum - 1)
  23. " Hit the start of the file, use zero indent.
  24. if prevlnum == 0
  25. return 0
  26. endif
  27. " Add a 'shiftwidth' after lines that start a block:
  28. " 'function', 'if', 'for', 'while', 'repeat', 'else', 'elseif', '{'
  29. let ind = indent(prevlnum)
  30. let prevline = getline(prevlnum)
  31. let midx = match(prevline, '^\s*\%(if\>\|for\>\|while\>\|repeat\>\|else\>\|elseif\>\|do\>\|then\>\)')
  32. if midx == -1
  33. let midx = match(prevline, '{\s*$')
  34. if midx == -1
  35. let midx = match(prevline, '\<function\>\s*\%(\k\|[.:]\)\{-}\s*(')
  36. endif
  37. endif
  38. if midx != -1
  39. " Add 'shiftwidth' if what we found previously is not in a comment and
  40. " an "end" or "until" is not present on the same line.
  41. if synIDattr(synID(prevlnum, midx + 1, 1), "name") != "luaComment" && prevline !~ '\<end\>\|\<until\>'
  42. let ind = ind + shiftwidth()
  43. endif
  44. endif
  45. " Subtract a 'shiftwidth' on end, else, elseif, until and '}'
  46. " This is the part that requires 'indentkeys'.
  47. let midx = match(getline(v:lnum), '^\s*\%(end\>\|else\>\|elseif\>\|until\>\|}\)')
  48. if midx != -1 && synIDattr(synID(v:lnum, midx + 1, 1), "name") != "luaComment"
  49. let ind = ind - shiftwidth()
  50. endif
  51. return ind
  52. endfunction