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

Methods included from Logging

included

Constructor Details

#initialize(rd, wr = rd) ⇒ EventLoop

Returns a new instance of EventLoop.



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

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
# File 'lib/neovim/event_loop.rb', line 34

def self.child(argv)
  io = IO.popen(argv | ["--embed"], "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:



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

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)


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

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.



104
105
106
107
108
109
110
111
112
113
# File 'lib/neovim/event_loop.rb', line 104

def shutdown
  stop

  [@rd, @wr].each do |io|
    begin
      io.close
    rescue IOError
    end
  end
end

#stopvoid

This method returns an undefined value.

Stop the event loop.



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

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)


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

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