vim-patch:9.0.0370: cleaning up afterwards can make a function messy

Problem:    Cleaning up afterwards can make a function messy.
Solution:   Add the :defer command.

1d84f7608f

Omit EX_EXPR_ARG: Vim9 script only.
Make :def throw E319 to avoid confusing behavior.

Co-authored-by: Bram Moolenaar <Bram@vim.org>
This commit is contained in:
zeertzjq
2023-04-16 07:50:18 +08:00
parent 54dab9ed9e
commit b75634e55e
7 changed files with 275 additions and 73 deletions

View File

@@ -532,4 +532,36 @@ func Test_funcdef_alloc_failure()
bw!
endfunc
func AddDefer(arg)
call extend(g:deferred, [a:arg])
endfunc
func WithDeferTwo()
call extend(g:deferred, ['in Two'])
for nr in range(3)
defer AddDefer('Two' .. nr)
endfor
call extend(g:deferred, ['end Two'])
endfunc
func WithDeferOne()
call extend(g:deferred, ['in One'])
call writefile(['text'], 'Xfuncdefer')
defer delete('Xfuncdefer')
defer AddDefer('One')
call WithDeferTwo()
call extend(g:deferred, ['end One'])
endfunc
func Test_defer()
let g:deferred = []
call WithDeferOne()
call assert_equal(['in One', 'in Two', 'end Two', 'Two2', 'Two1', 'Two0', 'end One', 'One'], g:deferred)
unlet g:deferred
call assert_equal('', glob('Xfuncdefer'))
endfunc
" vim: shiftwidth=2 sts=2 expandtab