runtime(lua): improve foldexpr, add vim9 script version
Commit:
https://github.com/vim/vim/commit/00b927b295c11e61942d34f7e1c384f1c6af9513
Author: Konfekt <[email protected]>
Date: Sun Apr 6 17:40:43 2025 +0200
runtime(lua): improve foldexpr, add vim9 script version
closes: https://github.com/vim/vim/issues/17049
Signed-off-by: Konfekt <[email protected]>
Signed-off-by: Christian Brabandt <[email protected]>
diff --git a/runtime/ftplugin/lua.vim b/runtime/ftplugin/lua.vim
index cc042c5dc..87f54ddb6 100644
--- a/runtime/ftplugin/lua.vim
+++ b/runtime/ftplugin/lua.vim
@@ -1,12 +1,13 @@
" Vim filetype plugin file.
-" Language: Lua
-" Maintainer: Doug Kearns <[email protected]>
-" Previous Maintainer: Max Ischenko <[email protected]>
-" Contributor: Dorai Sitaram <[email protected]>
-" C.D. MacEachern <[email protected]>
-" Tyler Miller <[email protected]>
-" Phạm Bình An <[email protected]>
-" Last Change: 2025 Feb 27
+" Language: Lua
+" Maintainer: Doug Kearns <[email protected]>
+" Previous Maintainer: Max Ischenko <[email protected]>
+" Contributor: Dorai Sitaram <[email protected]>
+" C.D. MacEachern <[email protected]>
+" Tyler Miller <[email protected]>
+" Phạm Bình An <[email protected]>
+" @konfekt
+" Last Change: 2025 Apr 04
if exists("b:did_ftplugin")
finish
@@ -41,11 +42,11 @@ 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
let b:match_words =
- \ '\<\%(do\|function\|if\)\>:' ..
- \ '\<\%(return\|else\|elseif\)\>:' ..
- \ '\<end\>,' ..
- \ '\<repeat\>:\<until\>,' ..
- \ '\%(--\)\=\[\(=*\)\[:] ]'
+ \ '\<\%(do\|function\|if\)\>:' ..
+ \ '\<\%(return\|else\|elseif\)\>:' ..
+ \ '\<end\>,' ..
+ \ '\<repeat\>:\<until\>,' ..
+ \ '\%(--\)\=\[\(=*\)\[:] ]'
let b:undo_ftplugin ..= " | unlet! b:match_words b:match_ignorecase"
endif
@@ -61,7 +62,7 @@ endif
if has("folding") && get(g:, "lua_folding", 0)
setlocal foldmethod=expr
- setlocal foldexpr=s:LuaFold(v:lnum)
+ setlocal foldexpr=s:LuaFold()
let b:lua_lasttick = -1
let b:undo_ftplugin ..= " | setl foldexpr< foldmethod< | unlet!
b:lua_lasttick b:lua_foldlists"
endif
@@ -87,19 +88,19 @@ function s:LuaInclude(fname) abort
endfunction
let s:patterns = [
- \ ['do', 'end'],
- \ ['if\s+.+\s+then', 'end'],
- \ ['repeat', 'until\s+.+'],
- \ ['for\s+.+\s+do', 'end'],
- \ ['while\s+.+\s+do', 'end'],
- \ ['function.+', 'end'],
- \ ['return\s+function.+', 'end'],
- \ ['local\s+function\s+.+', 'end'],
- \ ]
-
-function s:LuaFold(lnum) abort
+ \ ['do', 'end'],
+ \ ['if\s+.+\s+then', 'end'],
+ \ ['repeat', 'until\s+.+'],
+ \ ['for\s+.+\s+do', 'end'],
+ \ ['while\s+.+\s+do', 'end'],
+ \ ['function.+', 'end'],
+ \ ['return\s+function.+', 'end'],
+ \ ['local\s+function\s+.+', 'end'],
+ \ ]
+
+function s:LuaFold() abort
if b:lua_lasttick == b:changedtick
- return b:lua_foldlists[a:lnum - 1]
+ return b:lua_foldlists[v:lnum - 1]
endif
let b:lua_lasttick = b:changedtick
@@ -108,27 +109,78 @@ function s:LuaFold(lnum) abort
let buf = getline(1, "$")
for line in buf
for t in s:patterns
+ let open = 0
+ let end = 0
let tagopen = ' ^\s*' .. t[0] ..'\s*$'
- let tagclose = ' ^\s*' .. t[1] ..'\s*$'
+ let tagend = ' ^\s*' .. t[1] ..'\s*$'
if line =~# tagopen
- 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
+ call add(foldlist, t)
+ let open = 1
+ break
+ elseif line =~# tagend
+ if len(foldlist) > 0 && line =~# foldlist[-1][1]
+ call remove(foldlist, -1)
+ let end = 1
+ else
+ let foldlist = []
+ endif
+ break
endif
endfor
- call add(b:lua_foldlists, len(foldlist))
+ let prefix = ""
+ if open == 1 | let prefix = ">" | endif
+ if end == 1 | let prefix = "<" | endif
+ let b:lua_foldlists += [prefix..(len(foldlist) + end)]
endfor
- return lua_foldlists[a:lnum - 1]
+ return b:lua_foldlists[v:lnum - 1]
endfunction
+if !has('vim9script')
+ let &cpo = s:cpo_save
+ unlet s:cpo_save
+
+ finish
+endif
+
+delfunction! s:LuaFold
+def s:LuaFold(): string
+ if b:lua_lasttick == b:changedtick
+ return b:lua_foldlists[v:lnum - 1]
+ endif
+ b:lua_lasttick = b:changedtick
+
+ b:lua_foldlists = []
+ var foldlist = []
+ var buf = getline(1, "$")
+ for line in buf
+ var open = 0
+ var end = 0
+ for t in patterns
+ var tagopen = ' ^\s*' .. t[0] .. '\s*$'
+ var tagend = ' ^\s*' .. t[1] .. '\s*$'
+ if line =~# tagopen
+ add(foldlist, t)
+ open = 1
+ break
+ elseif line =~# tagend
+ if len(foldlist) > 0 && line =~# foldlist[-1][1]
+ end = 1
+ remove(foldlist, -1)
+ else
+ foldlist = []
+ endif
+ break
+ endif
+ endfor
+ var prefix = ""
+ if open == 1 | prefix = ">" | endif
+ if end == 1 | prefix = "<" | endif
+ b:lua_foldlists += [prefix .. (len(foldlist) + end)]
+ endfor
+ return b:lua_foldlists[v:lnum - 1]
+enddef
+
let &cpo = s:cpo_save
unlet s:cpo_save
-
" vim: nowrap sw=2 sts=2 ts=8 noet:
--
--
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 [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/vim_dev/E1u1SBG-004E7Y-U7%40256bit.org.