robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
0639f6e41a
master
1" Vim indent file 2" Language: Luau 3" Maintainer: Lopy (@lopi-py) 4" Last Change: 2026 Jun 17 5 6" only load this indent file when no other was loaded 7if exists ( "b:did_indent" ) 8finish 9endif 10let b: did_indent = 1 11 12let s: cpo_save = & cpo 13set cpo & vim 14 15setlocal indentexpr = GetLuauIndent () 16 17" make Vim call GetLuauIndent() when it finds a closing keyword or delimiter 18setlocal indentkeys += 0=end,0=until,0=else,0=elseif,0=},0=),0=] 19 20setlocal autoindent 21 22let b: undo_indent = "setlocal autoindent< indentexpr< indentkeys<" 23 24" only define the function once 25if exists ( "*GetLuauIndent" ) 26let & cpo = s: cpo_save 27unlet s: cpo_save 28finish 29endif 30 31function GetLuauIndent () abort 32let ignorecase_save = & ignorecase 33try 34let & ignorecase = 0 35return s: GetLuauIndentIntern () 36finally 37let & ignorecase = ignorecase_save 38endtry 39endfunction 40 41function s: InDeclareClass ( lnum ) abort 42let save_cursor = getcurpos () 43call cursor ( a: lnum - 1 , 1 ) 44let lnum = search ( '^\s*\%(end\>\|declare\s\+class\>\)' , 'bcnW' ) 45call setpos ( '.' , save_cursor ) 46return lnum > 0 && getline ( lnum ) =~# '^\s*declare\s\+class\>' 47endfunction 48 49function s: IsStringOrComment ( lnum , col ) abort 50let name = synIDattr ( synID ( a: lnum , a: col , 1 ), "name" ) 51return name =~# '^luau\%(Comment\|.*String\)' 52endfunction 53 54function s: LineCommentStart ( lnum ) abort 55let line = getline ( a: lnum ) 56let midx = stridx ( line , '--' ) 57while midx != -1 58if ! s: IsStringOrComment ( a: lnum , midx + 1 ) 59return midx 60endif 61let midx = stridx ( line , '--' , midx + 2 ) 62endwhile 63return -1 64endfunction 65 66function s: IsCode ( lnum , col ) abort 67let comment = s: LineCommentStart ( a: lnum ) 68return ( comment == -1 || a: col <= comment ) && ! s: IsStringOrComment ( a: lnum , a: col ) 69endfunction 70 71function s: HasBlockCloser ( lnum ) abort 72let line = getline ( a: lnum ) 73let midx = match ( line , '\<\%(end\|until\)\>' ) 74while midx != -1 75if s: IsCode ( a: lnum , midx + 1 ) 76return 1 77endif 78let midx = match ( line , '\<\%(end\|until\)\>' , midx + 1 ) 79endwhile 80return 0 81endfunction 82 83function s: GetLuauIndentIntern () abort 84" find a non-blank line above the current line 85let prevlnum = prevnonblank ( v: lnum - 1 ) 86 87" hit the start of the file, use zero indent 88if prevlnum == 0 89return 0 90endif 91 92" add a 'shiftwidth' after lines that start a block 93let ind = indent ( prevlnum ) 94let prevline = getline ( prevlnum ) 95let attr = '@\%(\h\w*\|\[[^]]*\]\)\s*' 96let stmt = 'if\>\|for\>\|while\>\|repeat\>\|else\>\|elseif\>\|do\>\|then\>' 97let class = 'class\>\s\+\h\|declare\s\+class\>\s\+\h' 98let func = '\%(\%(public\s\+\)\=function\|local\s\+function\|const\s\+function\|type\s\+function\|return\s\+function\)' 99let declare_func = '^\s*\%(' .. attr .. '\)*declare\s\+function\>' 100let midx = -1 101if prevline =~# '^\s*\%(@\|\%(if\|for\|while\|repeat\|else\|elseif\|do\|then\|class\)\>\|declare\s\+class\>\)' 102let midx = match ( prevline , '^\s*\%(' .. attr .. '\)*\%(' .. stmt .. '\|' .. class .. '\)' ) 103endif 104if midx == -1 105if prevline =~# '^\s*\%(' .. attr .. '\)*declare\s\+extern\s\+type\>' 106let midx = match ( prevline , '\<with\>\s*\%(--.*\)\=$' ) 107endif 108if midx == -1 109let midx = match ( prevline , '\%({\|(\|\[\)\s*\%(--\%([^[].*\)\?\)\?$' ) 110if midx == -1 && stridx ( prevline , 'function' ) != -1 && prevline !~# declare_func 111let midx = match ( prevline , '\<' .. func .. '\>\s*\%(\k\|[.:]\)\{-}\s*\%(<[^>]*>\s*\)\=(' ) 112endif 113endif 114endif 115 116if midx == -1 && prevline =~ '^\s*)\s*\%(:.\+\)\=\s*\%(--.*\)\=$' 117let save_cursor = getcurpos () 118call cursor ( prevlnum , match ( prevline , ')' ) + 1 ) 119let [ par_lnum , par_col ] = searchpairpos ( '(' , '' , ')' , 'bnW' ) 120call setpos ( '.' , save_cursor ) 121if par_lnum > 0 122let funline = getline ( par_lnum ) 123let funidx = match ( funline , '\<' .. func .. '\>' ) 124if funidx != -1 && funidx < par_col && funline !~# declare_func 125let midx = match ( prevline , ')' ) 126endif 127endif 128endif 129 130if midx != -1 131" add 'shiftwidth' if this is not in a comment or string and the block 132" does not close on the same line 133if s: IsCode ( prevlnum , midx + 1 ) && ! s: HasBlockCloser ( prevlnum ) 134 \&& ! ( prevline =~# '^\s*\%(public\s\+\)\=function\>' && s: InDeclareClass ( prevlnum )) 135let ind = ind + shiftwidth () 136endif 137endif 138 139" subtract a 'shiftwidth' on end, else, elseif, until, '}', ')' and ']' 140let midx = match ( getline ( v: lnum ), '^\s*\%(end\>\|else\>\|elseif\>\|until\>\|}\|)\|\]\)' ) 141if midx != -1 142if s: IsCode ( v: lnum , midx + 1 ) 143let ind = ind - shiftwidth () 144endif 145endif 146 147return ind 148endfunction 149 150let & cpo = s: cpo_save 151unlet s: cpo_save 152 153" vim: nowrap sw=2 sts=2 ts=8 noet: