robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

zeertzjqMerge pull request #40317 from zeertzjq/vim-9.2.06730639f6e41a

master
6.2 KiB193 linesraw
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")
7  finish
8endif
9let b:did_ftplugin = 1
10
11let s:cpo_save = &cpo
12set cpo&vim
13
14setlocal comments=:---,:--
15setlocal commentstring=--\ %s
16setlocal formatoptions-=t formatoptions+=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")
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"
43endif
44
45if (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"
53endif
54
55if 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"
60endif
61
62" the rest of the file needs to be sourced only once per Vim session
63if exists("s:loaded_luau") || &cp
64  let &cpo = s:cpo_save
65  unlet s:cpo_save
66  finish
67endif
68let s:loaded_luau = 1
69
70function 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
81endfunction
82
83function s:IsStringOrComment(lnum, col) abort
84  let name = synIDattr(synID(a:lnum, a:col, 1), "name")
85  return name =~# '^luau\%(Comment\|.*String\)'
86endfunction
87
88function 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
98endfunction
99
100function 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\>'
106endfunction
107
108function 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))
115endfunction
116
117function 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", "")
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
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]
188endfunction
189
190let &cpo = s:cpo_save
191unlet s:cpo_save
192
193" vim: nowrap sw=2 sts=2 ts=8 noet: