Mercurial > vim
comparison src/testdir/test_lua.vim @ 21006:ae185f35e256 v8.2.1054
patch 8.2.1054: not so easy to pass a lua function to Vim
Commit: https://github.com/vim/vim/commit/801ab069341c8652680d63c174530fd4feb2911e
Author: Bram Moolenaar <Bram@vim.org>
Date: Thu Jun 25 19:27:56 2020 +0200
patch 8.2.1054: not so easy to pass a lua function to Vim
Problem: Not so easy to pass a lua function to Vim.
Solution: Convert a Lua function and closure to a Vim funcref. (Prabir
Shrestha, closes #6246)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Thu, 25 Jun 2020 19:30:21 +0200 |
parents | 054ba681412d |
children | 08e284594211 |
comparison
equal
deleted
inserted
replaced
21005:3f0abea9bed2 | 21006:ae185f35e256 |
---|---|
537 endfunc | 537 endfunc |
538 | 538 |
539 func Test_update_package_paths() | 539 func Test_update_package_paths() |
540 set runtimepath+=./testluaplugin | 540 set runtimepath+=./testluaplugin |
541 call assert_equal("hello from lua", luaeval("require('testluaplugin').hello()")) | 541 call assert_equal("hello from lua", luaeval("require('testluaplugin').hello()")) |
542 endfunc | |
543 | |
544 func Vim_func_call_lua_callback(Concat, Cb) | |
545 let l:message = a:Concat("hello", "vim") | |
546 call a:Cb(l:message) | |
547 endfunc | |
548 | |
549 func Test_pass_lua_callback_to_vim_from_lua() | |
550 lua pass_lua_callback_to_vim_from_lua_result = "" | |
551 call assert_equal("", luaeval("pass_lua_callback_to_vim_from_lua_result")) | |
552 lua <<EOF | |
553 vim.funcref('Vim_func_call_lua_callback')( | |
554 function(greeting, message) | |
555 return greeting .. " " .. message | |
556 end, | |
557 function(message) | |
558 pass_lua_callback_to_vim_from_lua_result = message | |
559 end) | |
560 EOF | |
561 call assert_equal("hello vim", luaeval("pass_lua_callback_to_vim_from_lua_result")) | |
562 endfunc | |
563 | |
564 func Vim_func_call_metatable_lua_callback(Greet) | |
565 return a:Greet("world") | |
566 endfunc | |
567 | |
568 func Test_pass_lua_metatable_callback_to_vim_from_lua() | |
569 let result = luaeval("vim.funcref('Vim_func_call_metatable_lua_callback')(setmetatable({ space = ' '}, { __call = function(tbl, msg) return 'hello' .. tbl.space .. msg end }) )") | |
570 call assert_equal("hello world", result) | |
542 endfunc | 571 endfunc |
543 | 572 |
544 " Test vim.line() | 573 " Test vim.line() |
545 func Test_lua_line() | 574 func Test_lua_line() |
546 new | 575 new |