robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
0639f6e41a
master
1" Vim filetype plugin file 2" Language: Luau 3" Maintainer: Lopy (@lopi-py) 4" Last Change: 2026 Jun 16 5 6if exists ( "b:did_ftplugin" ) 7finish 8endif 9let b: did_ftplugin = 1 10 11let s: cpo_save = & cpo 12set cpo & vim 13 14setlocal comments = :---,:-- 15setlocal commentstring = --\ %s 16setlocal formatoptions -= tformatoptions += croql 17 18let & l: define = '\<\%(function\|local\%(\s\+function\)\=\|const\%(\s\+function\)\=\|type\%(\s\+function\)\=\|export\s\+type\%(\s\+function\)\=\|class\|declare\s\+\%(function\|class\|extern\s\+type\)\)\>' 19let & l: include = '\<require\s*(' 20setlocal includeexpr = s :LuauInclude(v:fname) 21setlocal suffixesadd = .luau,.lua 22 23let b: undo_ftplugin = "setl cms< com< def< fo< inc< inex< sua<" 24 25let s: attr = '@\%(\h\w*\|\[[^]]*\]\)\s*' 26let s: line_end = '\s*\%(--.*\)\=$' 27let s: end_pat = '^\s*end\>' .. s: line_end 28let s: end_block = '^\s*\%(' .. s: attr .. '\)*if\>.\{-}\<then\>' .. 29 \'\|^\s*\%(' .. s: attr .. '\)*class\>\s\+\h' .. 30 \'\|^\s*\%(' .. s: attr .. '\)*declare\s\+class\>\s\+\h' .. 31 \'\|^\s*\%(' .. s: attr .. '\)*declare\s\+extern\s\+type\>.\{-}\<with\>' .. s: line_end 32 33if exists ( "loaded_matchit" ) && ! exists ( "b:match_words" ) 34let b: match_ignorecase = 0 35let s: match_tail = ':\<return\>\|^\s*\%(else\|elseif\)\>:' .. s: end_pat .. 36 \',^\s*repeat\>:\<until\>,' .. 37 \'\%(--\)\=\[\(=*\)\[:\]\1\]' 38let s: match_words = '\<\%(do\|function\)\>\|' .. s: end_block .. s: match_tail 39let s: match_words_no_function = '\<do\>\|' .. s: end_block .. s: match_tail 40let b: match_words = "LuauMatchWords()" 41let b: match_skip = 'LuauMatchSkip()' 42let b: undo_ftplugin ..= " | unlet! b:match_words b:match_ignorecase b:match_skip" 43endif 44 45if ( has ( "gui_win32" ) || has ( "gui_gtk" )) && ! exists ( "b:browsefilter" ) 46let b: browsefilter = "Luau Source Files (*.luau)\t*.luau\n" 47if has ( "win32" ) 48let b: browsefilter ..= "All Files (*.*)\t*\n" 49else 50let b: browsefilter ..= "All Files (*)\t*\n" 51endif 52let b: undo_ftplugin ..= " | unlet! b:browsefilter" 53endif 54 55if has ( "folding" ) && get ( g:, "luau_folding" , 0 ) 56setlocal foldmethod = expr 57setlocal foldexpr = s :LuauFold() 58let b: luau_lasttick = -1 59let b: undo_ftplugin ..= " | setl foldexpr< foldmethod< | unlet! b:luau_lasttick b:luau_foldlists" 60endif 61 62" the rest of the file needs to be sourced only once per Vim session 63if exists ( "s:loaded_luau" ) || & cp 64let & cpo = s: cpo_save 65unlet s: cpo_save 66finish 67endif 68let s: loaded_luau = 1 69 70function s: LuauInclude ( fname ) abort 71let fname = a: fname 72if fname =~# '^@' 73return fname 74endif 75for path in [ fname , fname .. ".luau" , fname .. ".lua" , fname .. "/init.luau" , fname .. "/init.lua" ] 76if filereadable ( path ) 77return path 78endif 79endfor 80return fname 81endfunction 82 83function s: IsStringOrComment ( lnum , col ) abort 84let name = synIDattr ( synID ( a: lnum , a: col , 1 ), "name" ) 85return name =~# '^luau\%(Comment\|.*String\)' 86endfunction 87 88function s: LineCommentStart ( lnum ) abort 89let line = getline ( a: lnum ) 90let midx = stridx ( line , '--' ) 91while midx != -1 92if ! s: IsStringOrComment ( a: lnum , midx + 1 ) 93return midx 94endif 95let midx = stridx ( line , '--' , midx + 2 ) 96endwhile 97return -1 98endfunction 99 100function s: InDeclareClass ( lnum ) abort 101let save_cursor = getcurpos () 102call cursor ( a: lnum - 1 , 1 ) 103let lnum = search ( '^\s*\%(end\>\|declare\s\+class\>\)' , 'bcnW' ) 104call setpos ( '.' , save_cursor ) 105return lnum > 0 && getline ( lnum ) =~# '^\s*declare\s\+class\>' 106endfunction 107 108function LuauMatchSkip () abort 109let lnum = line ( "." ) 110let col = col ( "." ) 111let comment = s: LineCommentStart ( lnum ) 112return s: IsStringOrComment ( lnum , col ) 113 \|| ( comment != -1 && col > comment ) 114 \|| ( expand ( "<cword>" ) ==# "function" && s: InDeclareClass ( lnum )) 115endfunction 116 117function LuauMatchWords () abort 118if exists ( "s:match_words_no_function" ) 119 \&& expand ( "<cword>" ) ==# "function" && s: InDeclareClass ( line ( "." )) 120return s: match_words_no_function 121endif 122return get ( s:, "match_words" , "" ) 123endfunction 124 125let s: fold_guard = '^\s*\%(@\|\%(do\|if\|repeat\|for\|while\|public' .. 126 \'\|function\|return\|local\|const\|type\|export\|class' .. 127 \'\|declare\|end\|until\)\>\)' 128 129let s: patterns = [ 130 \[ '^\s*do\>' .. s: line_end , s: end_pat , 'block' ], 131 \[ '^\s*if\>.\{-}\<then\>' .. s: line_end , s: end_pat , 'block' ], 132 \[ '^\s*repeat\>' .. s: line_end , '^\s*until\>.*' , 'block' ], 133 \[ '^\s*for\>.\{-}\<do\>' .. s: line_end , s: end_pat , 'block' ], 134 \[ '^\s*while\>.\{-}\<do\>' .. s: line_end , s: end_pat , 'block' ], 135 \[ '^\s*\%(' .. s: attr .. '\)*\%(public\s\+\)\=function\>.*' , s: end_pat , 'function' ], 136 \[ '^\s*return\s\+function\>.*' , s: end_pat , 'function' ], 137 \[ '^\s*\%(' .. s: attr .. '\)*local\s\+function\>.*' , s: end_pat , 'function' ], 138 \[ '^\s*\%(' .. s: attr .. '\)*const\s\+function\>.*' , s: end_pat , 'function' ], 139 \[ '^\s*\%(' .. s: attr .. '\)*\%(export\s\+\)\=type\s\+function\>.*' , s: end_pat , 'function' ], 140 \[ '^\s*class\>\s\+\h.*' , s: end_pat , 'class' ], 141 \[ '^\s*declare\s\+class\>\s\+\h.*' , s: end_pat , 'declare_class' ], 142 \[ '^\s*declare\s\+extern\s\+type\>.\{-}\<with\>' .. s: line_end , s: end_pat , 'block' ], 143 \] 144 145function s: LuauFold () abort 146if b: luau_lasttick == b: changedtick 147return b: luau_foldlists [ v: lnum - 1 ] 148endif 149let b: luau_lasttick = b: changedtick 150 151let b: luau_foldlists = [] 152let foldlist = [] 153let buf = getline ( 1 , "$" ) 154for line in buf 155let open = 0 156let end = 0 157if line !~# s: fold_guard 158call add ( b: luau_foldlists , "" .. len ( foldlist )) 159continue 160endif 161for t in s: patterns 162let tagopen = t [ 0 ] 163let tagend = t [ 1 ] 164if line =~# tagopen 165if t [ 2 ] ==# 'function' && len ( foldlist ) > 0 && foldlist [ -1 ][ 2 ] ==# 'declare_class' 166continue 167endif 168call add ( foldlist , t ) 169let open = 1 170break 171elseif line =~# tagend 172if len ( foldlist ) > 0 && line =~# foldlist [ -1 ][ 1 ] 173call remove ( foldlist , -1 ) 174let end = 1 175else 176let foldlist = [] 177endif 178break 179endif 180endfor 181let prefix = "" 182if open == 1 |let prefix = ">" |endif 183if end == 1 |let prefix = "<" |endif 184call add ( b: luau_foldlists , prefix .. ( len ( foldlist ) + end )) 185endfor 186 187return b: luau_foldlists [ v: lnum - 1 ] 188endfunction 189 190let & cpo = s: cpo_save 191unlet s: cpo_save 192 193" vim: nowrap sw=2 sts=2 ts=8 noet: