runtime(lua): Improve 'include' and make '*expr' functions script-local Commit: https://github.com/vim/vim/commit/0b8205484b703b4a5a569cd1b0ed876bbb13901f Author: Doug Kearns <dougkea...@gmail.com> Date: Thu Feb 27 19:17:33 2025 +0100
runtime(lua): Improve 'include' and make '*expr' functions script-local - Prevent 'include' from matching variable assignments as calls to require() and others. - Use script-local functions for 'includeexpr' and 'foldexpr'. - Formatting fixes. closes: #16746 Signed-off-by: Doug Kearns <dougkea...@gmail.com> Signed-off-by: Christian Brabandt <c...@256bit.org> diff --git a/runtime/ftplugin/lua.vim b/runtime/ftplugin/lua.vim index 9687d55e9..cc042c5dc 100644 --- a/runtime/ftplugin/lua.vim +++ b/runtime/ftplugin/lua.vim @@ -6,7 +6,7 @@ " C.D. MacEachern <craig.daniel.maceach...@gmail.com> " Tyler Miller <tmi...@proton.me> " Ph廕《 B穫nh An <phambinhanctb2...@gmail.com> -" Last Change: 2025 Feb 25 +" Last Change: 2025 Feb 27 if exists("b:did_ftplugin") finish @@ -32,11 +32,11 @@ setlocal formatoptions-=t formatoptions+=croql let &l:define = '\<function\|\<local\%(\s\+function\)\=' -let &l:include = ' <((do|load)file|require)[^''"]*[''"]\zs[^''"]+' -setlocal includeexpr=LuaInclude(v:fname) +let &l:include = '\<\%(\%(do\|load\)file\|require\)\s*(' +setlocal includeexpr=s:LuaInclude(v:fname) setlocal suffixesadd=.lua -let b:undo_ftplugin = "setlocal cms< com< def< fo< inc< inex< sua<" +let b:undo_ftplugin = "setl cms< com< def< fo< inc< inex< sua<" if exists("loaded_matchit") && !exists("b:match_words") let b:match_ignorecase = 0 @@ -61,20 +61,31 @@ endif if has("folding") && get(g:, "lua_folding", 0) setlocal foldmethod=expr - setlocal foldexpr=LuaFold(v:lnum) + setlocal foldexpr=s:LuaFold(v:lnum) let b:lua_lasttick = -1 - let b:undo_ftplugin ..= "|setl foldexpr< foldmethod< | unlet! b:lua_lasttick b:lua_foldlists" + let b:undo_ftplugin ..= " | setl foldexpr< foldmethod< | unlet! b:lua_lasttick b:lua_foldlists" endif - " The rest of the file needs to be :sourced only once per Vim session -if exists('s:loaded_lua') || &cp +if exists("s:loaded_lua") || &cp let &cpo = s:cpo_save unlet s:cpo_save finish endif let s:loaded_lua = 1 +function s:LuaInclude(fname) abort + let lua_ver = str2float(printf("%d.%02d", g:lua_version, g:lua_subversion)) + let fname = tr(a:fname, '.', '/') + let paths = lua_ver >= 5.03 ? [fname .. ".lua", fname .. "/init.lua"] : [fname .. ".lua"] + for path in paths + if filereadable(path) + return path + endif + endfor + return fname +endfunction + let s:patterns = [ \ ['do', 'end'], \ ['if\s+.+\s+then', 'end'], @@ -86,47 +97,35 @@ let s:patterns = [ \ ['local\s+function\s+.+', 'end'], \ ] -function LuaInclude(fname) abort - let lua_ver = str2float(printf("%d.%02d", g:lua_version, g:lua_subversion)) - let fname = tr(a:fname, '.', '/') - let paths = lua_ver >= 5.03 ? [ fname.'.lua', fname.'/init.lua' ] : [ fname.'.lua' ] - for path in paths - if filereadable(path) - return path - endif - endfor - return fname -endfunction - -function LuaFold(lnum) abort +function s:LuaFold(lnum) abort if b:lua_lasttick == b:changedtick - return b:lua_foldlists[a:lnum-1] + return b:lua_foldlists[a:lnum - 1] endif let b:lua_lasttick = b:changedtick let b:lua_foldlists = [] let foldlist = [] - let buf = getline(1, '$') + let buf = getline(1, "$") for line in buf for t in s:patterns - let tagopen = ' ^\s*'..t[0]..'\s*$' - let tagclose = ' ^\s*'..t[1]..'\s*$' + let tagopen = ' ^\s*' .. t[0] ..'\s*$' + let tagclose = ' ^\s*' .. t[1] ..'\s*$' if line =~# tagopen - call add(foldlist, t) - break + call add(foldlist, t) + break elseif line =~# tagclose - if len(foldlist) > 0 && line =~# foldlist[-1][1] - call remove(foldlist, -1) - else - let foldlist = [] - endif - break + if len(foldlist) > 0 && line =~# foldlist[-1][1] + call remove(foldlist, -1) + else + let foldlist = [] + endif + break endif endfor call add(b:lua_foldlists, len(foldlist)) endfor - return lua_foldlists[a:lnum-1] + return lua_foldlists[a:lnum - 1] endfunction let &cpo = s:cpo_save -- -- You received this message from the "vim_dev" maillist. Do not top-post! Type your reply below the text you are replying to. For more information, visit http://www.vim.org/maillist.php --- You received this message because you are subscribed to the Google Groups "vim_dev" group. To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscr...@googlegroups.com. To view this discussion visit https://groups.google.com/d/msgid/vim_dev/E1tnie4-00GbcJ-Vb%40256bit.org.