Class: Neovim::Session
- Inherits:
-
Object
- Object
- Neovim::Session
- Includes:
- Logging
- Defined in:
- lib/neovim/session.rb
Overview
Wraps an AsyncSession in a synchronous API using Fibers.
Class Method Summary collapse
-
.child(argv) ⇒ Session
Spawn and connect to a child
nvimprocess. -
.stdio ⇒ Session
Connect to the current process’s standard streams.
-
.tcp(host, port) ⇒ Session
Connect to a TCP socket.
-
.unix(socket_path) ⇒ Session
Connect to a UNIX domain socket.
Instance Method Summary collapse
-
#api ⇒ API
Return the
nvimAPI as described in thevim_get_api_infocall. -
#channel_id ⇒ Fixnum?
Return the channel ID if registered via
vim_get_api_info. -
#discover_api ⇒ API
Discover the
nvimAPI as described in thevim_get_api_infocall. -
#initialize(async_session) ⇒ Session
constructor
A new instance of Session.
-
#notify(method, *args) ⇒ nil
Make an RPC notification.
-
#request(method, *args) ⇒ Object
Make an RPC request and return its response.
-
#run {|Object| ... } ⇒ void
Run the event loop, handling messages in a
Fiber. -
#shutdown ⇒ void
Shut down the event loop.
-
#stop ⇒ void
Stop the event loop.
Methods included from Logging
Constructor Details
#initialize(async_session) ⇒ Session
Returns a new instance of Session.
57 58 59 60 61 62 |
# File 'lib/neovim/session.rb', line 57 def initialize(async_session) @async_session = async_session = [] @main_fiber = Fiber.current @running = false end |
Class Method Details
.child(argv) ⇒ Session
Spawn and connect to a child nvim process.
37 38 39 |
# File 'lib/neovim/session.rb', line 37 def self.child(argv) from_event_loop(EventLoop.child(argv)) end |
.stdio ⇒ Session
Connect to the current process’s standard streams. This is used to promote the current process to a Ruby plugin host.
46 47 48 |
# File 'lib/neovim/session.rb', line 46 def self.stdio from_event_loop(EventLoop.stdio) end |
.tcp(host, port) ⇒ Session
Connect to a TCP socket.
19 20 21 |
# File 'lib/neovim/session.rb', line 19 def self.tcp(host, port) from_event_loop(EventLoop.tcp(host, port)) end |
.unix(socket_path) ⇒ Session
Connect to a UNIX domain socket.
28 29 30 |
# File 'lib/neovim/session.rb', line 28 def self.unix(socket_path) from_event_loop(EventLoop.unix(socket_path)) end |
Instance Method Details
#api ⇒ API
Return the nvim API as described in the vim_get_api_info call. Defaults to empty API information.
69 70 71 |
# File 'lib/neovim/session.rb', line 69 def api @api ||= API.null end |
#channel_id ⇒ Fixnum?
Return the channel ID if registered via vim_get_api_info.
162 163 164 |
# File 'lib/neovim/session.rb', line 162 def channel_id api.channel_id end |
#discover_api ⇒ API
Discover the nvim API as described in the vim_get_api_info call.
77 78 79 |
# File 'lib/neovim/session.rb', line 77 def discover_api @api = API.new(request(:vim_get_api_info)) end |
#notify(method, *args) ⇒ nil
Make an RPC notification.
136 137 138 139 |
# File 'lib/neovim/session.rb', line 136 def notify(method, *args) @async_session.notify(method, *args) nil end |
#request(method, *args) ⇒ Object
Make an RPC request and return its response.
If this method is called inside a callback, we are already inside a Fiber handler. In that case, we write to the stream and yield the Fiber. Once the response is received, resume the Fiber and return the result.
If this method is called outside a callback, write to the stream and run the event loop until a response is received. Messages received in the meantime are enqueued to be handled later.
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/neovim/session.rb', line 119 def request(method, *args) if Fiber.current == @main_fiber debug("handling blocking request") err, res = stopped_request(method, *args) else debug("yielding request to fiber") err, res = running_request(method, *args) end err ? raise(ArgumentError, err) : res end |
#run {|Object| ... } ⇒ void
This method returns an undefined value.
Run the event loop, handling messages in a Fiber.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/neovim/session.rb', line 88 def run @running = true while = .shift Fiber.new { yield if block_given? }.resume end return unless @running @async_session.run(self) do || Fiber.new { yield if block_given? }.resume end ensure stop end |
#shutdown ⇒ void
This method returns an undefined value.
Shut down the event loop.
154 155 156 157 |
# File 'lib/neovim/session.rb', line 154 def shutdown @running = false @async_session.shutdown end |
#stop ⇒ void
This method returns an undefined value.
Stop the event loop.
145 146 147 148 |
# File 'lib/neovim/session.rb', line 145 def stop @running = false @async_session.stop end |