Class: Neovim::EventLoop

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/neovim/event_loop.rb

Overview

The lowest level interface to reading from and writing to nvim.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rd, wr = rd) ⇒ EventLoop

Returns a new instance of EventLoop.



48
49
50
51
# File 'lib/neovim/event_loop.rb', line 48

def initialize(rd, wr=rd)
  @rd, @wr = rd, wr
  @running = false
end

Class Method Details

.child(argv) ⇒ EventLoop

Spawn and connect to a child nvim process.

Parameters:

  • argv (Array)

    The arguments to pass to the spawned process

Returns:



34
35
36
37
38
# File 'lib/neovim/event_loop.rb', line 34

def self.child(argv)
  argv = [ENV.fetch("NVIM_EXECUTABLE", "nvim"), "--embed"] | argv
  io = IO.popen(argv, "rb+")
  new(io)
end

.stdioEventLoop

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

Returns:



44
45
46
# File 'lib/neovim/event_loop.rb', line 44

def self.stdio
  new(STDIN, STDOUT)
end

.tcp(host, port) ⇒ EventLoop

Connect to a TCP socket.

Parameters:

  • host (String)

    The hostname or IP address

  • port (Fixnum)

    The port

Returns:



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

def self.tcp(host, port)
  socket = TCPSocket.new(host, port)
  new(socket)
end

.unix(path) ⇒ EventLoop

Connect to a UNIX domain socket.

Parameters:

  • path (String)

    The socket path

Returns:



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

def self.unix(path)
  socket = UNIXSocket.new(path)
  new(socket)
end

Instance Method Details

#run {|String| ... } ⇒ void

This method returns an undefined value.

Run the event loop, reading from the underlying IO and yielding received messages to the block.

Yields:

  • (String)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/neovim/event_loop.rb', line 79

def run
  @running = true

  loop do
    break unless @running
    message = @rd.readpartial(1024 * 16)
    debug("received #{message.inspect}")
    yield message if block_given?
  end
rescue EOFError
  warn("got EOFError")
rescue => e
  fatal("got unexpected error #{e}")
  debug(e.backtrace.join("\n"))
end

#shutdownvoid

This method returns an undefined value.

Stop the event loop and close underlying IOs.



105
106
107
108
109
# File 'lib/neovim/event_loop.rb', line 105

def shutdown
  stop
  [@rd, @wr].each(&:close)
rescue IOError
end

#stopvoid

This method returns an undefined value.

Stop the event loop.



98
99
100
# File 'lib/neovim/event_loop.rb', line 98

def stop
  @running = false
end

#write(data) ⇒ self

Write data to the underlying IO. This will block until all the data has been written.

Parameters:

  • data (String)

    The data to write (typically message-packed)

Returns:

  • (self)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/neovim/event_loop.rb', line 58

def write(data)
  start = 0
  size = data.size
  debug("writing #{data.inspect}")

  begin
    while start < size
      start += @wr.write_nonblock(data[start..-1])
    end
    self
  rescue IO::WaitWritable
    IO.select(nil, [@wr], nil, 1)
    retry
  end
end