blob: 22b99862741811683c9cb3a26081fe7f6f0b49ee [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim filetype plugin file.
Bram Moolenaar71b6d332022-09-10 13:13:14 +01002" Language: Lua
Bram Moolenaar519cc552021-11-16 19:18:26 +00003" Maintainer: Doug Kearns <dougkearns@gmail.com>
4" Previous Maintainer: Max Ischenko <mfi@ukr.net>
Bram Moolenaar71b6d332022-09-10 13:13:14 +01005" Contributor: Dorai Sitaram <ds26@gte.com>
Bram Moolenaar3c053a12022-10-16 13:11:12 +01006" C.D. MacEachern <craig.daniel.maceachern@gmail.com>
Bram Moolenaar71badf92023-04-22 22:40:14 +01007" Tyler Miller <tmillr@proton.me>
Ennofdfcce52024-12-03 22:23:48 +01008" Last Change: 2024 Dec 03
Bram Moolenaar071d4272004-06-13 20:20:40 +00009
Bram Moolenaar071d4272004-06-13 20:20:40 +000010if exists("b:did_ftplugin")
11 finish
12endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013let b:did_ftplugin = 1
14
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010015let s:cpo_save = &cpo
16set cpo&vim
17
Bram Moolenaar71badf92023-04-22 22:40:14 +010018setlocal comments=:---,:--
Bram Moolenaar71b6d332022-09-10 13:13:14 +010019setlocal commentstring=--\ %s
Bram Moolenaar519cc552021-11-16 19:18:26 +000020setlocal formatoptions-=t formatoptions+=croql
Bram Moolenaar071d4272004-06-13 20:20:40 +000021
Bram Moolenaar71b6d332022-09-10 13:13:14 +010022let &l:define = '\<function\|\<local\%(\s\+function\)\='
23
Bram Moolenaar3c053a12022-10-16 13:11:12 +010024" TODO: handle init.lua
Bram Moolenaarb59ae592022-11-23 23:46:31 +000025setlocal includeexpr=tr(v:fname,'.','/')
Bram Moolenaar071d4272004-06-13 20:20:40 +000026setlocal suffixesadd=.lua
27
Bram Moolenaar3c053a12022-10-16 13:11:12 +010028let b:undo_ftplugin = "setlocal cms< com< def< fo< inex< sua<"
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar519cc552021-11-16 19:18:26 +000030if exists("loaded_matchit") && !exists("b:match_words")
Bram Moolenaar071d4272004-06-13 20:20:40 +000031 let b:match_ignorecase = 0
32 let b:match_words =
Bram Moolenaar71b6d332022-09-10 13:13:14 +010033 \ '\<\%(do\|function\|if\)\>:' ..
34 \ '\<\%(return\|else\|elseif\)\>:' ..
35 \ '\<end\>,' ..
36 \ '\<repeat\>:\<until\>,' ..
37 \ '\%(--\)\=\[\(=*\)\[:]\1]'
38 let b:undo_ftplugin ..= " | unlet! b:match_words b:match_ignorecase"
Bram Moolenaar519cc552021-11-16 19:18:26 +000039endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000040
Bram Moolenaar519cc552021-11-16 19:18:26 +000041if (has("gui_win32") || has("gui_gtk")) && !exists("b:browsefilter")
Doug Kearns93197fd2024-01-14 20:59:02 +010042 let b:browsefilter = "Lua Source Files (*.lua)\t*.lua\n"
43 if has("win32")
44 let b:browsefilter ..= "All Files (*.*)\t*\n"
45 else
46 let b:browsefilter ..= "All Files (*)\t*\n"
47 endif
Bram Moolenaar71b6d332022-09-10 13:13:14 +010048 let b:undo_ftplugin ..= " | unlet! b:browsefilter"
Bram Moolenaar519cc552021-11-16 19:18:26 +000049endif
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010050
Ennofdfcce52024-12-03 22:23:48 +010051if has("folding") && get(g:, "lua_folding", 0)
52 setlocal foldmethod=expr
53 setlocal foldexpr=LuaFold(v:lnum)
54 let b:lua_lasttick = -1
55 let b:undo_ftplugin ..= "|setl foldexpr< foldmethod< | unlet! b:lua_lasttick b:lua_foldlists"
56endif
57
58
59" The rest of the file needs to be :sourced only once per Vim session
60if exists('s:loaded_lua') || &cp
61 let &cpo = s:cpo_save
62 unlet s:cpo_save
63 finish
64endif
65let s:loaded_lua = 1
66
67let s:patterns = [
68 \ ['do', 'end'],
69 \ ['if\s+.+\s+then', 'end'],
70 \ ['repeat', 'until\s+.+'],
71 \ ['for\s+.+\s+do', 'end'],
72 \ ['while\s+.+\s+do', 'end'],
73 \ ['function.+', 'end'],
74 \ ['return\s+function.+', 'end'],
75 \ ['local\s+function\s+.+', 'end'],
76 \ ]
77
78function! LuaFold(lnum) abort
79 if b:lua_lasttick == b:changedtick
80 return b:lua_foldlists[a:lnum-1]
81 endif
82 let b:lua_lasttick = b:changedtick
83
84 let b:lua_foldlists = []
85 let foldlist = []
86 let buf = getline(1, '$')
87 for line in buf
88 for t in s:patterns
89 let tagopen = '\v^\s*'..t[0]..'\s*$'
90 let tagclose = '\v^\s*'..t[1]..'\s*$'
91 if line =~# tagopen
92 call add(foldlist, t)
93 break
94 elseif line =~# tagclose
95 if len(foldlist) > 0 && line =~# foldlist[-1][1]
96 call remove(foldlist, -1)
97 else
98 let foldlist = []
99 endif
100 break
101 endif
102 endfor
103 call add(b:lua_foldlists, len(foldlist))
104 endfor
105
106 return lua_foldlists[a:lnum-1]
107endfunction
108
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100109let &cpo = s:cpo_save
110unlet s:cpo_save
Bram Moolenaar71b6d332022-09-10 13:13:14 +0100111
112" vim: nowrap sw=2 sts=2 ts=8 noet: