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