Skip to content

Commit 00a00f5

Browse files
brianhusterdkearns
authored andcommitted
runtime(lua): Update lua ftplugin and documentation
Problem: - The doc says the default `g:lua_subversion` is 2, but in fact it is 3 (see `runtime/syntax/lua.vim`) - `includeexpr` doesn't work with module in `init.lua` Solution: - Update documentation - Assign value to option `&include` - Add function `LuaInclude` and assign it to `l:&includeexpr` closes: #16655 Co-authored-by: dkearns <dougkearns@gmail.com> Signed-off-by: brianhuster <phambinhanctb2004@gmail.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent 12b1eb5 commit 00a00f5

5 files changed

Lines changed: 48 additions & 16 deletions

File tree

runtime/doc/filetype.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*filetype.txt* For Vim version 9.1. Last change: 2025 Feb 20
1+
*filetype.txt* For Vim version 9.1. Last change: 2025 Feb 25
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -744,12 +744,21 @@ Add following lines to $HOME/.vim/ftplugin/json.vim: >
744744
import autoload 'dist/json.vim'
745745
setl formatexpr=json.FormatExpr()
746746
747-
LUA *ft-lua-plugin*
747+
LUA *ft-lua-plugin* *g:lua_folding*
748748

749749
You can enable folding of Lua functions using |fold-expr| by: >
750750
751751
let g:lua_folding = 1
752+
< *g:lua_version* *g:lua_subversion*
753+
Lua filetype's 'includeexpr' and |ft-lua-syntax| highlighting use the global
754+
variables "g:lua_version" and "g:lua_subversion" to determine the version of
755+
Lua to use (5.3 is the default)
752756

757+
For example, to use Lua 5.1, set the variables like this: >
758+
759+
let g:lua_version = 5
760+
let g:lua_subversion = 1
761+
<
753762
MAIL *ft-mail-plugin*
754763

755764
Options:

runtime/doc/syntax.txt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*syntax.txt* For Vim version 9.1. Last change: 2025 Feb 23
1+
*syntax.txt* For Vim version 9.1. Last change: 2025 Feb 25
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -2343,13 +2343,9 @@ instead, and the name of your source file should be *.pike
23432343

23442344
LUA *lua.vim* *ft-lua-syntax*
23452345

2346-
The Lua syntax file can be used for versions 4.0, 5.0, 5.1 and 5.2 (5.2 is
2347-
the default). You can select one of these versions using the global variables
2348-
lua_version and lua_subversion. For example, to activate Lua
2349-
5.1 syntax highlighting, set the variables like this: >
2350-
2351-
:let lua_version = 5
2352-
:let lua_subversion = 1
2346+
The Lua syntax file can be used for versions 4.0, 5.0+. You can select one of
2347+
these versions using the global variables |g:lua_version| and
2348+
|g:lua_subversion|.
23532349

23542350

23552351
MAIL *mail.vim* *ft-mail.vim*

runtime/doc/tags

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7625,6 +7625,9 @@ g:html_use_input_for_pc syntax.txt /*g:html_use_input_for_pc*
76257625
g:html_use_xhtml syntax.txt /*g:html_use_xhtml*
76267626
g:html_whole_filler syntax.txt /*g:html_whole_filler*
76277627
g:lf_shell_syntax syntax.txt /*g:lf_shell_syntax*
7628+
g:lua_folding filetype.txt /*g:lua_folding*
7629+
g:lua_subversion filetype.txt /*g:lua_subversion*
7630+
g:lua_version filetype.txt /*g:lua_version*
76287631
g:markdown_fenced_languages syntax.txt /*g:markdown_fenced_languages*
76297632
g:markdown_minlines syntax.txt /*g:markdown_minlines*
76307633
g:markdown_syntax_conceal syntax.txt /*g:markdown_syntax_conceal*

runtime/ftplugin/lua.vim

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,24 @@
55
" Contributor: Dorai Sitaram <ds26@gte.com>
66
" C.D. MacEachern <craig.daniel.maceachern@gmail.com>
77
" Tyler Miller <tmillr@proton.me>
8-
" Last Change: 2024 Dec 03
8+
" Phạm Bình An <phambinhanctb2004@gmail.com>
9+
" Last Change: 2025 Feb 25
910

1011
if exists("b:did_ftplugin")
1112
finish
1213
endif
1314
let b:did_ftplugin = 1
1415

16+
" keep in sync with syntax/lua.vim
17+
if !exists("lua_version")
18+
" Default is lua 5.3
19+
let lua_version = 5
20+
let lua_subversion = 3
21+
elseif !exists("lua_subversion")
22+
" lua_version exists, but lua_subversion doesn't. In this case set it to 0
23+
let lua_subversion = 0
24+
endif
25+
1526
let s:cpo_save = &cpo
1627
set cpo&vim
1728

@@ -21,11 +32,11 @@ setlocal formatoptions-=t formatoptions+=croql
2132

2233
let &l:define = '\<function\|\<local\%(\s\+function\)\='
2334

24-
" TODO: handle init.lua
25-
setlocal includeexpr=tr(v:fname,'.','/')
35+
let &l:include = '\v<((do|load)file|require)[^''"]*[''"]\zs[^''"]+'
36+
setlocal includeexpr=LuaInclude(v:fname)
2637
setlocal suffixesadd=.lua
2738

28-
let b:undo_ftplugin = "setlocal cms< com< def< fo< inex< sua<"
39+
let b:undo_ftplugin = "setlocal cms< com< def< fo< inc< inex< sua<"
2940

3041
if exists("loaded_matchit") && !exists("b:match_words")
3142
let b:match_ignorecase = 0
@@ -75,7 +86,19 @@ let s:patterns = [
7586
\ ['local\s+function\s+.+', 'end'],
7687
\ ]
7788

78-
function! LuaFold(lnum) abort
89+
function LuaInclude(fname) abort
90+
let lua_ver = str2float(printf("%d.%02d", g:lua_version, g:lua_subversion))
91+
let fname = tr(a:fname, '.', '/')
92+
let paths = lua_ver >= 5.03 ? [ fname.'.lua', fname.'/init.lua' ] : [ fname.'.lua' ]
93+
for path in paths
94+
if filereadable(path)
95+
return path
96+
endif
97+
endfor
98+
return fname
99+
endfunction
100+
101+
function LuaFold(lnum) abort
79102
if b:lua_lasttick == b:changedtick
80103
return b:lua_foldlists[a:lnum-1]
81104
endif

runtime/syntax/lua.vim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
" Language: Lua 4.0, Lua 5.0, Lua 5.1, Lua 5.2 and Lua 5.3
33
" Maintainer: Marcus Aurelius Farias <masserahguard-lua 'at' yahoo com>
44
" First Author: Carlos Augusto Teixeira Mendes <cmendes 'at' inf puc-rio br>
5-
" Last Change: 2022 Sep 07
5+
" Last Change: 2025 Feb 25
66
" Options: lua_version = 4 or 5
77
" lua_subversion = 0 (for 4.0 or 5.0)
88
" or 1, 2, 3 (for 5.1, 5.2 or 5.3)
@@ -16,6 +16,7 @@ endif
1616
let s:cpo_save = &cpo
1717
set cpo&vim
1818

19+
" keep in sync with ftplugin/lua.vim
1920
if !exists("lua_version")
2021
" Default is lua 5.3
2122
let lua_version = 5

0 commit comments

Comments
 (0)