Class: Neovim::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/neovim/session.rb

Overview

Wraps an AsyncSession in a synchronous API using Fibers.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(async_session) ⇒ Session

Returns a new instance of Session.



54
55
56
57
58
59
# File 'lib/neovim/session.rb', line 54

def initialize(async_session)
  @async_session = async_session
  @pending_messages = []
  @in_handler = false
  @running = false
end

Class Method Details

.child(argv) ⇒ Session

Spawn and connect to a child nvim process.

Parameters:

  • argv (Array)

    The arguments to pass to the spawned process

Returns:

See Also:



34
35
36
# File 'lib/neovim/session.rb', line 34

def self.child(argv)
  from_event_loop(EventLoop.child(argv))
end

.stdioSession

Connect to the current process’s standard streams. This is used to promote the current process to a Ruby plugin host.

Returns:

See Also:



43
44
45
# File 'lib/neovim/session.rb', line 43

def self.stdio
  from_event_loop(EventLoop.stdio)
end

.tcp(host, port) ⇒ Session

Connect to a TCP socket.

Parameters:

  • host (String)

    The hostname or IP address

  • port (Fixnum)

    The port

Returns:

See Also:



16
17
18
# File 'lib/neovim/session.rb', line 16

def self.tcp(host, port)
  from_event_loop(EventLoop.tcp(host, port))
end

.unix(socket_path) ⇒ Session

Connect to a UNIX domain socket.

Parameters:

  • socket_path (String)

    The socket path

Returns:

See Also:



25
26
27
# File 'lib/neovim/session.rb', line 25

def self.unix(socket_path)
  from_event_loop(EventLoop.unix(socket_path))
end

Instance Method Details

#apiAPI

Return the nvim API as described in the vim_get_api_info call. Defaults to empty API information.

Returns:

See Also:



66
67
68
# File 'lib/neovim/session.rb', line 66

def api
  @api ||= API.null
end

#channel_idFixnum?

Return the channel ID if registered via vim_get_api_info.

Returns:

  • (Fixnum, nil)


157
158
159
# File 'lib/neovim/session.rb', line 157

def channel_id
  api.channel_id
end

#discover_apiAPI

Discover the nvim API as described in the vim_get_api_info call.

Returns:

See Also:



74
75
76
# File 'lib/neovim/session.rb', line 74

def discover_api
  @api = API.new(request(:vim_get_api_info))
end

#notify(method, *args) ⇒ nil

Make an RPC notification.

Parameters:

  • method (String, Symbol)

    The RPC method name

  • *args (Array)

    The RPC method arguments

Returns:

  • (nil)


131
132
133
134
# File 'lib/neovim/session.rb', line 131

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.

Parameters:

  • method (String, Symbol)

    The RPC method name

  • *args (Array)

    The RPC method arguments

Returns:

  • (Object)

    The response from the RPC call

Raises:

  • (ArgumentError)

    An error returned from nvim



116
117
118
119
120
121
122
123
124
# File 'lib/neovim/session.rb', line 116

def request(method, *args)
  if @in_handler
    err, res = running_request(method, *args)
  else
    err, res = stopped_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.

Yields:

  • (Object)

See Also:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/neovim/session.rb', line 85

def run
  @running = true

  while message = @pending_messages.shift
    in_handler_fiber { yield message if block_given? }
  end

  return unless @running

  @async_session.run(self) do |message|
    in_handler_fiber { yield message if block_given? }
  end
ensure
  stop
end

#shutdownvoid

This method returns an undefined value.

Shut down the event loop.

See Also:



149
150
151
152
# File 'lib/neovim/session.rb', line 149

def shutdown
  @running = false
  @async_session.shutdown
end

#stopvoid

This method returns an undefined value.

Stop the event loop.

See Also:



140
141
142
143
# File 'lib/neovim/session.rb', line 140

def stop
  @running = false
  @async_session.stop
end