lua.vim 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. " 2022 Sep 07: b:undo_indent added by Doug Kearns
  7. " Only load this indent file when no other was loaded.
  8. if exists("b:did_indent")
  9. finish
  10. endif
  11. let b:did_indent = 1
  12. setlocal indentexpr=GetLuaIndent()
  13. " To make Vim call GetLuaIndent() when it finds '\s*end' or '\s*until'
  14. " on the current line ('else' is default and includes 'elseif').
  15. setlocal indentkeys+=0=end,0=until
  16. setlocal autoindent
  17. let b:undo_indent = "setlocal autoindent< indentexpr< indentkeys<"
  18. " Only define the function once.
  19. if exists("*GetLuaIndent")
  20. finish
  21. endif
  22. function! GetLuaIndent()
  23. " Find a non-blank line above the current line.
  24. let prevlnum = prevnonblank(v:lnum - 1)
  25. " Hit the start of the file, use zero indent.
  26. if prevlnum == 0
  27. return 0
  28. endif
  29. " Add a 'shiftwidth' after lines that start a block:
  30. " 'function', 'if', 'for', 'while', 'repeat', 'else', 'elseif', '{'
  31. let ind = indent(prevlnum)
  32. let prevline = getline(prevlnum)
  33. let midx = match(prevline, '^\s*\%(if\>\|for\>\|while\>\|repeat\>\|else\>\|elseif\>\|do\>\|then\>\)')
  34. if midx == -1
  35. let midx = match(prevline, '{\s*$')
  36. if midx == -1
  37. let midx = match(prevline, '\<function\>\s*\%(\k\|[.:]\)\{-}\s*(')
  38. endif
  39. endif
  40. if midx != -1
  41. " Add 'shiftwidth' if what we found previously is not in a comment and
  42. " an "end" or "until" is not present on the same line.
  43. if synIDattr(synID(prevlnum, midx + 1, 1), "name") != "luaComment" && prevline !~ '\<end\>\|\<until\>'
  44. let ind = ind + shiftwidth()
  45. endif
  46. endif
  47. " Subtract a 'shiftwidth' on end, else, elseif, until and '}'
  48. " This is the part that requires 'indentkeys'.
  49. let midx = match(getline(v:lnum), '^\s*\%(end\>\|else\>\|elseif\>\|until\>\|}\)')
  50. if midx != -1 && synIDattr(synID(v:lnum, midx + 1, 1), "name") != "luaComment"
  51. let ind = ind - shiftwidth()
  52. endif
  53. return ind
  54. endfunction