lua.vim 2.3 KB

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