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