Rewrite SpaceVim built-in plugin using Lua

Original link: https://wsdjeg.spacevim.org/rewrite-spacevim-builtin-plugin-using-lua/

If you’ve been following the Neovim community lately, you’ll find more and more plugins developed using Lua.

Neovim supports luajit by default. Earlier, I did a speed comparison between luajit and vim script.

The following is a test Vim Script script.

 function ! Fibo ( N ) abort let t = a:N let b = 0 while t > 0 let t = t - 1 let a = 1 let b = 1 let c = 73 while c > 0 let c = c - 1 let tmp = a + b let a = b let b = tmp endwhile endwhile echo b endfunction function ! LuaFibo ( N ) abort lua << EOF local a , b , r , c , t t = vim . api . nvim_eval ( "str2nr(a:N)" ) while t > 0 do t = t - 1 a = 1 b = 1 c = 73 while c > 0 do c = c - 1 r = a + b a = b b = r end end print ( string . format ( "%d" , b )) EOF endfunction function ! s:test (...) abort " for func in a:000 let start = reltime () call call ( a:1 ,[ a:2 ]) let sec = reltimefloat ( reltime ( start )) echom printf ( '%s(%d): %.6g sec' , a:1 , a:2 , sec ) " endfor endfunction command ! - nargs =+ TestFunc call s:test (< f - args >)

After loading the following script, execute the following command to see the test results:

 :TestFunc Fibo 1000 :TestFunc Fibo 10000000 :TestFunc LuaFibo 1000 :TestFunc LuaFibo 10000000

Local test results:

 Fibo(1000): 0.410364 sec Fibo(10000000): 1470.280914 sec LuaFibo(1000): 9.052000e-4 sec LuaFibo(10000000): 1.235385 sec

Through the above simple test, it can be clearly seen that the speed of luajit is much faster than that of Vim script.
With Neovim’s esteem for Lua, there have been many plugins developed using Lua.
At the same time, some well-known Vim plugins have also been rewritten in Lua.

There are many built-in plugins in SpaceVim, such as flygrep , iedit , task-manager and so on.
These plugins originally used Vim scripts for compatibility with Vim and Neovim.

In the latest version of SpaceVim, some plugins have been rewritten using lua. Of course, the version written by the original Vim script is also kept for compatibility with Vim.

Plugins that have been rewritten so far include:

  • flygrep : Real-time asynchronous code retrieval, according to the input content, real-time display of code search results.
  • iedit : Similar to iedit-mode of emacs, it provides support for multi-cursor editing.
  • todomanager : project to-do management

This article is reprinted from: https://wsdjeg.spacevim.org/rewrite-spacevim-builtin-plugin-using-lua/
This site is for inclusion only, and the copyright belongs to the original author.