Eric's Blog 时光荏苒,岁月如梭

Neovim lua bindeval 解决方案

2023-04-03
王世东

最近在改写插件 tagbar 的日志系统,采用 SpaceVim 内置的日志插件。但是在调用 debug 函数时发现日志一直无法写入。

代码逻辑非常简单,lua 文件逻辑:

lua/spacevim/logger.lua

local M = {}


function M.test()
  local derive = {
    _debug_mode = false,
  }

  function derive.debug(msg)
    if derive._debug_mode then
      print(msg)
    end
  end
  return derive
end

return M

vim script 这边只定义了一个函数:

autoload/testl.vim

function! testl#test() abort
  return luaeval('require("testluaeval").test()')
endfunction

测试步骤:

let tlog = testl#test()
let tlog._debug_mode = v:true
call tlog.debug('hello')

运行到此处发现,消息并没有被打印。于是在 neovim 仓库提交了issue。得到的回复是,目前 neovim 还不支持 bindeval,但是支持 closures。

因此逻辑上做了如下改动:

lua/spacevim/logger.lua

local M = {}


function M.test()
  local derive = {
    _debug_mode = false,
  }

  function derive.debug(msg)
    if derive._debug_mode then
      print(msg)
    end
  end
  function derive.start_debug()
    derive._debug_mode = true
  end
  function derive.stop_debug()
    derive._debug_mode = false
  end
  return derive
end

return M

vim script 这边只定义了一个函数:

autoload/testl.vim

function! testl#test() abort
  return luaeval('require("testluaeval").test()')
endfunction

测试步骤:

let tlog = testl#test()
call tlog.start_debug()
call tlog.debug('hello')

通过以上的改动,测试步骤可以达到预期效果。


分享到:

评论

目前只支持使用邮件参与评论。

目录