ZeroBrane :在每个文件的基础上注册 API

ZeroBrane : Register APIs on a per file basis

我正在为我们的 Solarus Game Engine 编写一个 ZeroBrane Studio 插件,它非常有用。包括自动完成功能。

我想知道是否可以只为一个文件注册 lua APIs。

我需要它来提供 autocompletion/documentation 全局符号,这些符号可能因脚本而异,但可以从引擎的附件文件中推断出来。

总结:是否可以为单个文件注册 api?例如在 onEditorLoad() 事件中。

谢谢。

格雷格

编辑:

我尝试了以下但没有成功:

local function switch_editor(editor)

  if current_editor == editor then
    ide:Print("same editor")
    return
  end
  current_editor = editor
  if not editor then
    ide:Print("null ed")
    return
  end
  lua_file_path = ide:GetDocument(editor).filePath
  if lua_file_path:match('/data/maps/') then
    ide:Print("map file!",type(editor))
    local map_api = make_map_api(lua_file_path)
    current_api = map_api
    ide:AddAPI('lua','solarus_map',map_api)
  else
    ide:Print('other file')
    if current_api then
      ide:RemoveAPI('lua','solarus_map')
      current_api = nil
    end
  end
end

api = {"baselib", "solarus", "solarus_map"}, --in interpreter table

... -- in the plugin table :
onEditorFocusSet = function(self,editor)
    switch_editor(editor)
end,

完成 solarus api 工作正常,但 solarus_map api 的即时注册似乎没有被考虑在内。

编辑 2:

愚蠢的我,我一定是打错了,因为在检查并重写了一些与上面粘贴的代码非常相似的东西之后......它有效!太棒了!

唯一的小问题是,当切换到我不想要的文件时 solarus_map API... ide:RemoveAPI 是不够的。相反,我必须执行 ide:AddAPI('lua','solarus_map',{}) 以将 API 替换为空的。我可以忍受。

总而言之,实现自定义 api 文件之间的变化:

  • 将api名称添加到解释器
  • onEditorFocusSet 事件中,将 API 更新为 ide:AddAPI(...),如果需要 empty/disabled,最终将其设置为 {}

我的问题版本中的代码示例。