lua.vim (2370B)
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 9 " Only load this indent file when no other was loaded. 10 if exists("b:did_indent") 11 finish 12 endif 13 let b:did_indent = 1 14 15 setlocal 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'). 19 setlocal indentkeys+=0=end,0=until 20 21 setlocal autoindent 22 23 let b:undo_indent = "setlocal autoindent< indentexpr< indentkeys<" 24 25 " Only define the function once. 26 if exists("*GetLuaIndent") 27 finish 28 endif 29 30 function! GetLuaIndent() 31 let ignorecase_save = &ignorecase 32 try 33 let &ignorecase = 0 34 return GetLuaIndentIntern() 35 finally 36 let &ignorecase = ignorecase_save 37 endtry 38 endfunction 39 40 function! GetLuaIndentIntern() 41 " Find a non-blank line above the current line. 42 let prevlnum = prevnonblank(v:lnum - 1) 43 44 " Hit the start of the file, use zero indent. 45 if prevlnum == 0 46 return 0 47 endif 48 49 " Add a 'shiftwidth' after lines that start a block: 50 " 'function', 'if', 'for', 'while', 'repeat', 'else', 'elseif', '{', '(' 51 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 55 let midx = match(prevline, '\%({\|(\)\s*\%(--\%([^[].*\)\?\)\?$') 56 if midx == -1 57 let midx = match(prevline, '\<function\>\s*\%(\k\|[.:]\)\{-}\s*(') 58 endif 59 endif 60 61 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\>' 65 let ind = ind + shiftwidth() 66 endif 67 endif 68 69 " Subtract a 'shiftwidth' on end, else, elseif, until, '}' and ')' 70 " This is the part that requires 'indentkeys'. 71 let midx = match(getline(v:lnum), '^\s*\%(end\>\|else\>\|elseif\>\|until\>\|}\|)\)') 72 if midx != -1 && synIDattr(synID(v:lnum, midx + 1, 1), "name") != "luaComment" 73 let ind = ind - shiftwidth() 74 endif 75 76 return ind 77 endfunction