blob: ce6cfe18cdf3897c741d2bcfeb1c6328330250aa [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
2" Language: Lua script
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003" Maintainer: Marcus Aurelius Farias <marcus.cf 'at' bol.com.br>
4" First Author: Max Ischenko <mfi 'at' ukr.net>
Bram Moolenaar3ec574f2017-06-13 18:12:01 +02005" Last Change: 2017 Jun 13
Bram Moolenaar9712ff12022-09-18 13:04:22 +01006" 2022 Sep 07: b:undo_indent added by Doug Kearns
Yinzuo Jiangc0f75052024-08-04 18:47:25 +02007" 2024 Jul 27: by Vim project: match '(', ')' in function GetLuaIndentIntern()
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008
9" Only load this indent file when no other was loaded.
10if exists("b:did_indent")
11 finish
12endif
13let b:did_indent = 1
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
Bram Moolenaarf461c8e2005-06-25 23:04:51 +000015setlocal indentexpr=GetLuaIndent()
16
17" To make Vim call GetLuaIndent() when it finds '\s*end' or '\s*until'
18" on the current line ('else' is default and includes 'elseif').
19setlocal indentkeys+=0=end,0=until
20
21setlocal autoindent
22
Bram Moolenaar9712ff12022-09-18 13:04:22 +010023let b:undo_indent = "setlocal autoindent< indentexpr< indentkeys<"
24
Bram Moolenaar071d4272004-06-13 20:20:40 +000025" Only define the function once.
Bram Moolenaard4755bb2004-09-02 19:12:26 +000026if exists("*GetLuaIndent")
27 finish
28endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar071d4272004-06-13 20:20:40 +000030function! GetLuaIndent()
beardedsakimonkey9fa35b12023-08-20 19:21:51 +000031 let ignorecase_save = &ignorecase
32 try
33 let &ignorecase = 0
34 return GetLuaIndentIntern()
35 finally
36 let &ignorecase = ignorecase_save
37 endtry
38endfunction
39
40function! GetLuaIndentIntern()
Bram Moolenaar071d4272004-06-13 20:20:40 +000041 " Find a non-blank line above the current line.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000042 let prevlnum = prevnonblank(v:lnum - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000043
44 " Hit the start of the file, use zero indent.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000045 if prevlnum == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 return 0
47 endif
48
Bram Moolenaarf461c8e2005-06-25 23:04:51 +000049 " Add a 'shiftwidth' after lines that start a block:
Yinzuo Jiangc0f75052024-08-04 18:47:25 +020050 " 'function', 'if', 'for', 'while', 'repeat', 'else', 'elseif', '{', '('
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000051 let ind = indent(prevlnum)
52 let prevline = getline(prevlnum)
53 let midx = match(prevline, '^\s*\%(if\>\|for\>\|while\>\|repeat\>\|else\>\|elseif\>\|do\>\|then\>\)')
54 if midx == -1
Yinzuo Jiangc0f75052024-08-04 18:47:25 +020055 let midx = match(prevline, '\%({\|(\)\s*\%(--\%([^[].*\)\?\)\?$')
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000056 if midx == -1
57 let midx = match(prevline, '\<function\>\s*\%(\k\|[.:]\)\{-}\s*(')
58 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000059 endif
60
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000061 if midx != -1
62 " Add 'shiftwidth' if what we found previously is not in a comment and
63 " an "end" or "until" is not present on the same line.
64 if synIDattr(synID(prevlnum, midx + 1, 1), "name") != "luaComment" && prevline !~ '\<end\>\|\<until\>'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020065 let ind = ind + shiftwidth()
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000066 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000067 endif
68
Yinzuo Jiangc0f75052024-08-04 18:47:25 +020069 " Subtract a 'shiftwidth' on end, else, elseif, until, '}' and ')'
Bram Moolenaar071d4272004-06-13 20:20:40 +000070 " This is the part that requires 'indentkeys'.
Yinzuo Jiangc0f75052024-08-04 18:47:25 +020071 let midx = match(getline(v:lnum), '^\s*\%(end\>\|else\>\|elseif\>\|until\>\|}\|)\)')
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000072 if midx != -1 && synIDattr(synID(v:lnum, midx + 1, 1), "name") != "luaComment"
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020073 let ind = ind - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +000074 endif
75
76 return ind
77endfunction