ZeroBrane : Register APIs on a per file basis

103 Views Asked by At

I'm writing a ZeroBrane Studio plugin for our Solarus Game Engine and It works like a charm. Autocompletion included.

I'm wondering now if it's do-able to register lua APIs for one file only.

I need this to offer autocompletion/documentation on global symbols that may vary per-script but are deducible from annex files from the engine.

To summary : Is it possible to register an api for a single file? For example in the onEditorLoad() event.

Thanks.

Greg

EDIT:

I tried the following without sucess:

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,

Completion with the solarus api works fine but the on-fly registration of the solarus_map api seem not to be taken in account.

EDIT2:

Silly my, I must have done a typo, because after checking and rewriting some things pretty much as in the code pasted above... it works! Awesome!

The only small gotcha is that when switching to a file where I don't want the solarus_map API... ide:RemoveAPI isn't sufficient. Instead I must do ide:AddAPI('lua','solarus_map',{}) to replace the API with an empty one. Which I can live with.

1

There are 1 best solutions below

0
On BEST ANSWER

To summary, to achieve a custom api which change from file to file:

  • Add the api name to the interpreter
  • In the onEditorFocusSet event, update the API with ide:AddAPI(...), eventually setting it to {} if it needs to be empty/disabled.

Code sample in the editions of my Question.