0
0
mirror of https://github.com/vim/vim.git synced 2025-09-23 03:43:49 -04:00

runtime(lua): improve foldexpr, add vim9 script version

closes: #17049

Signed-off-by: Konfekt <Konfekt@users.noreply.github.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
Konfekt
2025-04-06 17:40:43 +02:00
committed by Christian Brabandt
parent 2afdb3a65b
commit 00b927b295

View File

@@ -1,12 +1,13 @@
" Vim filetype plugin file. " Vim filetype plugin file.
" Language: Lua " Language: Lua
" Maintainer: Doug Kearns <dougkearns@gmail.com> " Maintainer: Doug Kearns <dougkearns@gmail.com>
" Previous Maintainer: Max Ischenko <mfi@ukr.net> " Previous Maintainer: Max Ischenko <mfi@ukr.net>
" Contributor: Dorai Sitaram <ds26@gte.com> " Contributor: Dorai Sitaram <ds26@gte.com>
" C.D. MacEachern <craig.daniel.maceachern@gmail.com> " C.D. MacEachern <craig.daniel.maceachern@gmail.com>
" Tyler Miller <tmillr@proton.me> " Tyler Miller <tmillr@proton.me>
" Phạm Bình An <phambinhanctb2004@gmail.com> " Phạm Bình An <phambinhanctb2004@gmail.com>
" Last Change: 2025 Feb 27 " @konfekt
" Last Change: 2025 Apr 04
if exists("b:did_ftplugin") if exists("b:did_ftplugin")
finish 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") if exists("loaded_matchit") && !exists("b:match_words")
let b:match_ignorecase = 0 let b:match_ignorecase = 0
let b:match_words = let b:match_words =
\ '\<\%(do\|function\|if\)\>:' .. \ '\<\%(do\|function\|if\)\>:' ..
\ '\<\%(return\|else\|elseif\)\>:' .. \ '\<\%(return\|else\|elseif\)\>:' ..
\ '\<end\>,' .. \ '\<end\>,' ..
\ '\<repeat\>:\<until\>,' .. \ '\<repeat\>:\<until\>,' ..
\ '\%(--\)\=\[\(=*\)\[:]\1]' \ '\%(--\)\=\[\(=*\)\[:]\1]'
let b:undo_ftplugin ..= " | unlet! b:match_words b:match_ignorecase" let b:undo_ftplugin ..= " | unlet! b:match_words b:match_ignorecase"
endif endif
@@ -61,7 +62,7 @@ endif
if has("folding") && get(g:, "lua_folding", 0) if has("folding") && get(g:, "lua_folding", 0)
setlocal foldmethod=expr setlocal foldmethod=expr
setlocal foldexpr=s:LuaFold(v:lnum) setlocal foldexpr=s:LuaFold()
let b:lua_lasttick = -1 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 endif
@@ -87,19 +88,19 @@ function s:LuaInclude(fname) abort
endfunction endfunction
let s:patterns = [ let s:patterns = [
\ ['do', 'end'], \ ['do', 'end'],
\ ['if\s+.+\s+then', 'end'], \ ['if\s+.+\s+then', 'end'],
\ ['repeat', 'until\s+.+'], \ ['repeat', 'until\s+.+'],
\ ['for\s+.+\s+do', 'end'], \ ['for\s+.+\s+do', 'end'],
\ ['while\s+.+\s+do', 'end'], \ ['while\s+.+\s+do', 'end'],
\ ['function.+', 'end'], \ ['function.+', 'end'],
\ ['return\s+function.+', 'end'], \ ['return\s+function.+', 'end'],
\ ['local\s+function\s+.+', 'end'], \ ['local\s+function\s+.+', 'end'],
\ ] \ ]
function s:LuaFold(lnum) abort function s:LuaFold() abort
if b:lua_lasttick == b:changedtick if b:lua_lasttick == b:changedtick
return b:lua_foldlists[a:lnum - 1] return b:lua_foldlists[v:lnum - 1]
endif endif
let b:lua_lasttick = b:changedtick let b:lua_lasttick = b:changedtick
@@ -108,27 +109,78 @@ function s:LuaFold(lnum) abort
let buf = getline(1, "$") let buf = getline(1, "$")
for line in buf for line in buf
for t in s:patterns for t in s:patterns
let open = 0
let end = 0
let tagopen = '\v^\s*' .. t[0] ..'\s*$' let tagopen = '\v^\s*' .. t[0] ..'\s*$'
let tagclose = '\v^\s*' .. t[1] ..'\s*$' let tagend = '\v^\s*' .. t[1] ..'\s*$'
if line =~# tagopen if line =~# tagopen
call add(foldlist, t) call add(foldlist, t)
break let open = 1
elseif line =~# tagclose break
if len(foldlist) > 0 && line =~# foldlist[-1][1] elseif line =~# tagend
call remove(foldlist, -1) if len(foldlist) > 0 && line =~# foldlist[-1][1]
else call remove(foldlist, -1)
let foldlist = [] let end = 1
endif else
break let foldlist = []
endif
break
endif endif
endfor 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 endfor
return lua_foldlists[a:lnum - 1] return b:lua_foldlists[v:lnum - 1]
endfunction 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 = '\v^\s*' .. t[0] .. '\s*$'
var tagend = '\v^\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 let &cpo = s:cpo_save
unlet s:cpo_save unlet s:cpo_save
" vim: nowrap sw=2 sts=2 ts=8 noet: " vim: nowrap sw=2 sts=2 ts=8 noet: