首页 > 解决方案 > ZeroBrane:基于每个文件注册 API

问题描述

我正在为我们的Solarus 游戏引擎编写 ZeroBrane Studio 插件,它就像一个魅力。包括自动完成。

我现在想知道是否可以仅为一个文件注册 lua API。

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

总结:是否可以为单个文件注册 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,

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

编辑2:

愚蠢的我,我一定是打错了,因为在检查和重写了一些几乎就像上面粘贴的代码一样的东西之后......它起作用了!惊人的!

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

标签: zerobrane

解决方案


总而言之,要实现一个从文件到文件的自定义api:

  • 将 api 名称添加到解释器
  • 在这种情况onEditorFocusSet下,使用 更新 API ide:AddAPI(...),最终将其设置为{}是否需要为空/禁用。

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


推荐阅读