Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Request] Context definition #211

Closed
NuriYuri opened this issue Jul 20, 2019 · 2 comments
Closed

[Request] Context definition #211

NuriYuri opened this issue Jul 20, 2019 · 2 comments

Comments

@NuriYuri
Copy link

Hi, I'm working on a project that will allow the definitions of Events (in RPG Maker a Game) using Ruby Script. The problem is that those events are loaded in runtime (when needed) and I'd like to be able to tel SolarGraph that the ruby file (that will not contain class or def) is executed in a specific context : like if it was a "method" of the Interpreter class.

Basically, I'll have this kind of file :

# @context Interpreter.instance
choice = message("Hello there.", 1, "General Kenoby?", "Hello")
case choice
when 0 # Choice was General Kenoby?
  start_battle("battles/Grievous")
when 1 # Hello
  # ...
end

I want to be able to have the hints when I write the various event commands.

If you wonder this event will be loaded this way (not the actual way but that's the command that will be used) :

class Interpreter
  def load_event(filename)
    event_proc = "proc do\n#{File.read(filename)}end"
    block = eval(event_proc)
    @fiber = Fiber.new do
      instance_exec(&block)
      @fiber = nil
    end
  end
end

When the interpreter updates it'll call @fiber.resume if fiber is not nil.

If you think that's useless you can directly close the issue ^^' I just want to make it easier for the one who will use the Ruby Events.


Note 1 : The game will use the eval stuff only in debug. When the game will be compiled, all the events will also be compiled to RubyVM::InstructionSequence with the right decoration (proc & cache management).
Note 2 : I use a Fiber because the message method will give the control back to the interpreter before returning the value. It'll be the same for the start_battle function.
Note 3 : The @context tag is subject to discussion, I think it as an helper for solargraph to give the right hints ^^

@castwide
Copy link
Owner

There's a @!domain directive that works something like what you're describing, although it's experimental and might change. You can use it to tell Solargraph to add a class or module's public methods to the global namespace. Example:

# interpreter.rb
module Interpreter
  def message; end
end
# script.rb
# @!domain [Interpreter]
m # <- `message` is one of the completion items

@NuriYuri
Copy link
Author

Great !
Thanks for the answer. I'm looking forward for it to be stable :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants