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