robin/laufey

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

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

master
4.5 KiB153 linesraw
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")
8  finish
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")
26  let &cpo = s:cpo_save
27  unlet s:cpo_save
28  finish
29endif
30
31function GetLuauIndent() abort
32  let ignorecase_save = &ignorecase
33  try
34    let &ignorecase = 0
35    return s:GetLuauIndentIntern()
36  finally
37    let &ignorecase = ignorecase_save
38  endtry
39endfunction
40
41function s:InDeclareClass(lnum) abort
42  let save_cursor = getcurpos()
43  call cursor(a:lnum - 1, 1)
44  let lnum = search('^\s*\%(end\>\|declare\s\+class\>\)', 'bcnW')
45  call setpos('.', save_cursor)
46  return lnum > 0 && getline(lnum) =~# '^\s*declare\s\+class\>'
47endfunction
48
49function s:IsStringOrComment(lnum, col) abort
50  let name = synIDattr(synID(a:lnum, a:col, 1), "name")
51  return name =~# '^luau\%(Comment\|.*String\)'
52endfunction
53
54function s:LineCommentStart(lnum) abort
55  let line = getline(a:lnum)
56  let midx = stridx(line, '--')
57  while midx != -1
58    if !s:IsStringOrComment(a:lnum, midx + 1)
59      return midx
60    endif
61    let midx = stridx(line, '--', midx + 2)
62  endwhile
63  return -1
64endfunction
65
66function s:IsCode(lnum, col) abort
67  let comment = s:LineCommentStart(a:lnum)
68  return (comment == -1 || a:col <= comment) && !s:IsStringOrComment(a:lnum, a:col)
69endfunction
70
71function s:HasBlockCloser(lnum) abort
72  let line = getline(a:lnum)
73  let midx = match(line, '\<\%(end\|until\)\>')
74  while midx != -1
75    if s:IsCode(a:lnum, midx + 1)
76      return 1
77    endif
78    let midx = match(line, '\<\%(end\|until\)\>', midx + 1)
79  endwhile
80  return 0
81endfunction
82
83function s:GetLuauIndentIntern() abort
84  " find a non-blank line above the current line
85  let prevlnum = prevnonblank(v:lnum - 1)
86
87  " hit the start of the file, use zero indent
88  if prevlnum == 0
89    return 0
90  endif
91
92  " add a 'shiftwidth' after lines that start a block
93  let ind = indent(prevlnum)
94  let prevline = getline(prevlnum)
95  let attr = '@\%(\h\w*\|\[[^]]*\]\)\s*'
96  let stmt = 'if\>\|for\>\|while\>\|repeat\>\|else\>\|elseif\>\|do\>\|then\>'
97  let class = 'class\>\s\+\h\|declare\s\+class\>\s\+\h'
98  let func = '\%(\%(public\s\+\)\=function\|local\s\+function\|const\s\+function\|type\s\+function\|return\s\+function\)'
99  let declare_func = '^\s*\%(' .. attr .. '\)*declare\s\+function\>'
100  let midx = -1
101  if prevline =~# '^\s*\%(@\|\%(if\|for\|while\|repeat\|else\|elseif\|do\|then\|class\)\>\|declare\s\+class\>\)'
102    let midx = match(prevline, '^\s*\%(' .. attr .. '\)*\%(' .. stmt .. '\|' .. class .. '\)')
103  endif
104  if midx == -1
105    if prevline =~# '^\s*\%(' .. attr .. '\)*declare\s\+extern\s\+type\>'
106      let midx = match(prevline, '\<with\>\s*\%(--.*\)\=$')
107    endif
108    if midx == -1
109      let midx = match(prevline, '\%({\|(\|\[\)\s*\%(--\%([^[].*\)\?\)\?$')
110      if midx == -1 && stridx(prevline, 'function') != -1 && prevline !~# declare_func
111        let midx = match(prevline, '\<' .. func .. '\>\s*\%(\k\|[.:]\)\{-}\s*\%(<[^>]*>\s*\)\=(')
112      endif
113    endif
114  endif
115
116  if midx == -1 && prevline =~ '^\s*)\s*\%(:.\+\)\=\s*\%(--.*\)\=$'
117    let save_cursor = getcurpos()
118    call cursor(prevlnum, match(prevline, ')') + 1)
119    let [par_lnum, par_col] = searchpairpos('(', '', ')', 'bnW')
120    call setpos('.', save_cursor)
121    if par_lnum > 0
122      let funline = getline(par_lnum)
123      let funidx = match(funline, '\<' .. func .. '\>')
124      if funidx != -1 && funidx < par_col && funline !~# declare_func
125        let midx = match(prevline, ')')
126      endif
127    endif
128  endif
129
130  if 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
133    if s:IsCode(prevlnum, midx + 1) && !s:HasBlockCloser(prevlnum)
134          \ && !(prevline =~# '^\s*\%(public\s\+\)\=function\>' && s:InDeclareClass(prevlnum))
135      let ind = ind + shiftwidth()
136    endif
137  endif
138
139  " subtract a 'shiftwidth' on end, else, elseif, until, '}', ')' and ']'
140  let midx = match(getline(v:lnum), '^\s*\%(end\>\|else\>\|elseif\>\|until\>\|}\|)\|\]\)')
141  if midx != -1
142    if s:IsCode(v:lnum, midx + 1)
143      let ind = ind - shiftwidth()
144    endif
145  endif
146
147  return ind
148endfunction
149
150let &cpo = s:cpo_save
151unlet s:cpo_save
152
153" vim: nowrap sw=2 sts=2 ts=8 noet: