runtime(lua): add optional lua function folding
Commit:
https://github.com/vim/vim/commit/fdfcce56a6b8fe64ae155c822e3b920db97cde0d
Author: Enno <[email protected]>
Date: Tue Dec 3 22:23:48 2024 +0100
runtime(lua): add optional lua function folding
closes: https://github.com/vim/vim/issues/16151
Signed-off-by: Konfekt <[email protected]>
Signed-off-by: Christian Brabandt <[email protected]>
diff --git a/runtime/doc/filetype.txt b/runtime/doc/filetype.txt
index 06fc82923..8d8502a73 100644
--- a/runtime/doc/filetype.txt
+++ b/runtime/doc/filetype.txt
@@ -1,4 +1,4 @@
-*filetype.txt* For Vim version 9.1. Last change: 2024 Nov 14
+*filetype.txt* For Vim version 9.1. Last change: 2024 Dec 03
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -733,7 +733,6 @@ To enable the recognition of Markdown comments each time
after removing
re-source "javaformat.vim" for Vim versions greater than `8.2.1397`: >
runtime autoload/javaformat.vim
<
-
JSON-FORMAT *ft-json-plugin*
JSON filetype can be extended to use 'formatexpr' and "json.FormatExpr()"
@@ -745,6 +744,11 @@ Add following lines to $HOME/.vim/ftplugin/json.vim: >
import autoload 'dist/json.vim'
setl formatexpr=json.FormatExpr()
+LUA *ft-lua-plugin*
+
+You can enable folding of lua functions using |fold-expr| by: >
+
+ let g:lua_folding = 1
MAIL *ft-mail-plugin*
diff --git a/runtime/doc/tags b/runtime/doc/tags
index 5e57f2539..ad870060d 100644
--- a/runtime/doc/tags
+++ b/runtime/doc/tags
@@ -7358,6 +7358,7 @@ ft-lifelines-syntax syntax.txt
/*ft-lifelines-syntax*
ft-lisp-syntax syntax.txt /*ft-lisp-syntax*
ft-lite-syntax syntax.txt /*ft-lite-syntax*
ft-lpc-syntax syntax.txt /*ft-lpc-syntax*
+ft-lua-plugin filetype.txt /*ft-lua-plugin*
ft-lua-syntax syntax.txt /*ft-lua-syntax*
ft-mail-plugin filetype.txt /*ft-mail-plugin*
ft-mail.vim syntax.txt /*ft-mail.vim*
diff --git a/runtime/ftplugin/lua.vim b/runtime/ftplugin/lua.vim
index 80cbba78a..22b998627 100644
--- a/runtime/ftplugin/lua.vim
+++ b/runtime/ftplugin/lua.vim
@@ -5,7 +5,7 @@
" Contributor: Dorai Sitaram <[email protected]>
" C.D. MacEachern <[email protected]>
" Tyler Miller <[email protected]>
-" Last Change: 2024 Jan 14
+" Last Change: 2024 Dec 03
if exists("b:did_ftplugin")
finish
@@ -48,6 +48,64 @@ if (has("gui_win32") || has("gui_gtk")) &&
!exists("b:browsefilter")
let b:undo_ftplugin ..= " | unlet! b:browsefilter"
endif
+if has("folding") && get(g:, "lua_folding", 0)
+ setlocal foldmethod=expr
+ setlocal foldexpr=LuaFold(v:lnum)
+ let b:lua_lasttick = -1
+ 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
+ let &cpo = s:cpo_save
+ unlet s:cpo_save
+ finish
+endif
+let s:loaded_lua = 1
+
+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! LuaFold(lnum) abort
+ if b:lua_lasttick == b:changedtick
+ return b:lua_foldlists[a:lnum-1]
+ endif
+ let b:lua_lasttick = b:changedtick
+
+ let b:lua_foldlists = []
+ let foldlist = []
+ 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*$'
+ 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
+ endif
+ endfor
+ call add(b:lua_foldlists, len(foldlist))
+ endfor
+
+ return lua_foldlists[a:lnum-1]
+endfunction
+
let &cpo = s:cpo_save
unlet 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 [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/vim_dev/E1tIaT5-006IXJ-WB%40256bit.org.