neovim 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +5 -2
- data/CHANGELOG.md +11 -0
- data/README.md +15 -4
- data/Rakefile +64 -4
- data/bin/j2mp +8 -0
- data/bin/mp2j +8 -0
- data/lib/neovim.rb +100 -17
- data/lib/neovim/api.rb +84 -0
- data/lib/neovim/async_session.rb +74 -21
- data/lib/neovim/buffer.rb +111 -4
- data/lib/neovim/client.rb +175 -4
- data/lib/neovim/current.rb +19 -8
- data/lib/neovim/event_loop.rb +55 -10
- data/lib/neovim/host.rb +21 -8
- data/lib/neovim/line_range.rb +55 -15
- data/lib/neovim/logging.rb +4 -0
- data/lib/neovim/manifest.rb +11 -3
- data/lib/neovim/msgpack_stream.rb +53 -19
- data/lib/neovim/notification.rb +2 -0
- data/lib/neovim/plugin.rb +12 -87
- data/lib/neovim/plugin/dsl.rb +81 -0
- data/lib/neovim/plugin/handler.rb +43 -0
- data/lib/neovim/remote_object.rb +64 -0
- data/lib/neovim/request.rb +4 -2
- data/lib/neovim/session.rb +167 -16
- data/lib/neovim/tabpage.rb +24 -2
- data/lib/neovim/version.rb +1 -1
- data/lib/neovim/window.rb +92 -2
- data/neovim.gemspec +1 -1
- data/spec/acceptance/neovim-ruby-host_spec.rb +16 -8
- data/spec/helper.rb +11 -3
- data/spec/neovim/api_spec.rb +40 -0
- data/spec/neovim/async_session_spec.rb +19 -25
- data/spec/neovim/current_spec.rb +64 -4
- data/spec/neovim/event_loop_spec.rb +18 -27
- data/spec/neovim/host_spec.rb +20 -1
- data/spec/neovim/manifest_spec.rb +0 -2
- data/spec/neovim/msgpack_stream_spec.rb +5 -6
- data/spec/neovim/{object_spec.rb → remote_object_spec.rb} +1 -4
- data/spec/neovim/session_spec.rb +18 -26
- data/spec/neovim_spec.rb +2 -3
- data/spec/support.rb +5 -5
- metadata +13 -6
- data/lib/neovim/api_info.rb +0 -27
- data/lib/neovim/object.rb +0 -40
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7e20e3f4df52ff80bb9e0aa9826d5c6c723d2cc6
|
4
|
+
data.tar.gz: 04019ea3e3e7bb33d231d8a2160a2320f62ca365
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d3a46053cf6e54b97ef541cf314062ee3ed597a61b642e1d3be7355c60d1b568fe35e0dd76b8b22dd9f5e82cbcdd981ca83d09a596dbbaacbb7221530169d510
|
7
|
+
data.tar.gz: ae0fc91672a39bda8c0a2c756c0a27db7360a8428cb5ab7cdfe9423a25a97d3f3d968d51c0fd42ac986582f32a44eb511a75d6d58213c9384b76483a47b655da
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
# 0.0.6
|
2
|
+
- Update Session with improved Fiber coordination
|
3
|
+
- Documentation
|
4
|
+
- Rename APIInfo -> API
|
5
|
+
- Rename Object -> RemoteObject
|
6
|
+
|
7
|
+
# 0.0.5
|
8
|
+
- Various fixes for Ruby remote plugins
|
9
|
+
- Move Current#range and #range= methods to Buffer
|
10
|
+
- Add better logging
|
11
|
+
|
1
12
|
# 0.0.4
|
2
13
|
- Add support for loading Ruby remote plugins from nvim
|
3
14
|
- Add Current#range to return a LineRange enumerable object
|
data/README.md
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/neovim.svg)](https://badge.fury.io/rb/neovim)
|
4
4
|
[![Travis](https://travis-ci.org/alexgenco/neovim-ruby.svg?branch=master)](https://travis-ci.org/alexgenco/neovim-ruby)
|
5
5
|
[![Coverage Status](https://coveralls.io/repos/alexgenco/neovim-ruby/badge.png)](https://coveralls.io/r/alexgenco/neovim-ruby)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/alexgenco/neovim-ruby/badges/gpa.svg)](https://codeclimate.com/github/alexgenco/neovim-ruby)
|
6
7
|
|
7
8
|
Ruby bindings for [Neovim](https://github.com/neovim/neovim).
|
8
9
|
|
@@ -37,7 +38,9 @@ require "neovim"
|
|
37
38
|
client = Neovim.attach_unix("/tmp/nvim.sock")
|
38
39
|
```
|
39
40
|
|
40
|
-
The client's interface is generated at runtime from the `vim_get_api_info` RPC call.
|
41
|
+
The client's interface is generated at runtime from the `vim_get_api_info` RPC call. Refer to the [docs](http://www.rubydoc.info/github/alexgenco/neovim-ruby/master/Neovim/Client) for details.
|
42
|
+
|
43
|
+
### Plugins
|
41
44
|
|
42
45
|
The `neovim-ruby-host` executable can be used to spawn Ruby plugins via the `rpcstart` command. A plugin can be defined like this:
|
43
46
|
|
@@ -52,9 +55,8 @@ Neovim.plugin do |plug|
|
|
52
55
|
nvim.current.line = str
|
53
56
|
end
|
54
57
|
|
55
|
-
# Define a function called "Sum" which
|
56
|
-
#
|
57
|
-
# nvim.
|
58
|
+
# Define a function called "Sum" which adds two numbers. This function is
|
59
|
+
# executed synchronously, so the result of the block will be returned to nvim.
|
58
60
|
plug.function(:Sum, :nargs => 2, :sync => true) do |nvim, x, y|
|
59
61
|
x + y
|
60
62
|
end
|
@@ -68,6 +70,15 @@ end
|
|
68
70
|
|
69
71
|
After a call to `:UpdateRemotePlugins`, plugins will be auto-loaded from the `$VIMRUNTIME/rplugin/ruby` directory.
|
70
72
|
|
73
|
+
## Links
|
74
|
+
|
75
|
+
* Source: <http://github.com/alexgenco/neovim-ruby>
|
76
|
+
* Bugs: <http://github.com/alexgenco/neovim-ruby/issues>
|
77
|
+
* CI: <http://travis-ci.org/alexgenco/neovim-ruby>
|
78
|
+
* Documentation:
|
79
|
+
* Latest Gem: <http://rubydoc.info/gems/neovim>
|
80
|
+
* Master: <http://rubydoc.info/github/alexgenco/neovim-ruby/master/frames>
|
81
|
+
|
71
82
|
## Contributing
|
72
83
|
|
73
84
|
1. Fork it (http://github.com/alexgenco/neovim-ruby/fork)
|
data/Rakefile
CHANGED
@@ -2,20 +2,24 @@ require "bundler/gem_tasks"
|
|
2
2
|
require "rspec/core/rake_task"
|
3
3
|
|
4
4
|
RSpec::Core::RakeTask.new(:spec)
|
5
|
-
|
5
|
+
|
6
|
+
namespace :spec do
|
7
|
+
desc "Build Neovim and run specs on CI"
|
8
|
+
task :ci => ["neovim:build", :spec]
|
9
|
+
end
|
6
10
|
|
7
11
|
namespace :neovim do
|
8
12
|
vendor = File.expand_path("../vendor/neovim", __FILE__)
|
9
13
|
|
10
|
-
desc "
|
11
|
-
task :
|
14
|
+
desc "Build Neovim"
|
15
|
+
task :build do
|
12
16
|
sh "git submodule update --init && " +
|
13
17
|
"cd #{vendor} && " +
|
14
18
|
"make distclean && " +
|
15
19
|
"make"
|
16
20
|
end
|
17
21
|
|
18
|
-
desc "Update Neovim
|
22
|
+
desc "Update vendored Neovim revision"
|
19
23
|
task :update do
|
20
24
|
sh "git submodule update --init && " +
|
21
25
|
"cd #{vendor} && " +
|
@@ -23,4 +27,60 @@ namespace :neovim do
|
|
23
27
|
"git pull origin master && " +
|
24
28
|
"make"
|
25
29
|
end
|
30
|
+
|
31
|
+
desc "Generate Neovim remote API docs"
|
32
|
+
task :generate_docs do
|
33
|
+
require "neovim"
|
34
|
+
require "pathname"
|
35
|
+
|
36
|
+
vim_docs = []
|
37
|
+
buffer_docs = []
|
38
|
+
window_docs = []
|
39
|
+
tabpage_docs = []
|
40
|
+
session = Neovim::Session.child(%w(-u NONE -n -N))
|
41
|
+
|
42
|
+
session.request(:vim_get_api_info)[1]["functions"].each do |func|
|
43
|
+
prefix, method_name = func["name"].split("_", 2)
|
44
|
+
return_type = func["return_type"]
|
45
|
+
params = func["parameters"]
|
46
|
+
params.shift unless prefix == "vim"
|
47
|
+
param_names = params.map(&:last)
|
48
|
+
param_str = params.empty? ? "" : "(#{param_names.join(", ")})"
|
49
|
+
method_decl = "@method #{method_name}#{param_str}"
|
50
|
+
param_docs = params.map do |type, name|
|
51
|
+
" @param [#{type}] #{name}"
|
52
|
+
end
|
53
|
+
return_doc = " @return [#{return_type}]\n"
|
54
|
+
method_doc = [method_decl, *param_docs, return_doc].join("\n")
|
55
|
+
method_doc.gsub!(/ArrayOf\((\w+)[^)]*\)/, 'Array<\1>')
|
56
|
+
method_doc.gsub!(/Integer/, "Fixnum")
|
57
|
+
|
58
|
+
case prefix
|
59
|
+
when "vim"
|
60
|
+
vim_docs << method_doc
|
61
|
+
when "buffer"
|
62
|
+
buffer_docs << method_doc
|
63
|
+
when "tabpage"
|
64
|
+
tabpage_docs << method_doc
|
65
|
+
when "window"
|
66
|
+
window_docs << method_doc
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
lib_dir = Pathname.new(File.expand_path("../lib/neovim", __FILE__))
|
71
|
+
{
|
72
|
+
"client.rb" => vim_docs,
|
73
|
+
"buffer.rb" => buffer_docs,
|
74
|
+
"tabpage.rb" => tabpage_docs,
|
75
|
+
"window.rb" => window_docs,
|
76
|
+
}.each do |filename, docs|
|
77
|
+
path = lib_dir.join(filename)
|
78
|
+
contents = File.read(path)
|
79
|
+
doc_str = ["=begin", *docs, "=end"].join("\n")
|
80
|
+
|
81
|
+
File.write(path, contents.sub(/=begin.+=end/m, doc_str))
|
82
|
+
end
|
83
|
+
end
|
26
84
|
end
|
85
|
+
|
86
|
+
task :default => :spec
|
data/bin/j2mp
ADDED
data/bin/mp2j
ADDED
data/lib/neovim.rb
CHANGED
@@ -6,27 +6,99 @@ require "neovim/msgpack_stream"
|
|
6
6
|
require "neovim/session"
|
7
7
|
require "neovim/plugin"
|
8
8
|
|
9
|
+
# The main entrypoint to the +Neovim+ gem. It allows you to connect to a
|
10
|
+
# running +nvim+ instance programmatically or define a remote plugin to be
|
11
|
+
# autoloaded by +nvim+.
|
12
|
+
#
|
13
|
+
# You can connect to a running +nvim+ instance by setting or inspecting the
|
14
|
+
# +NVIM_LISTEN_ADDRESS+ environment variable and connecting via the appropriate
|
15
|
+
# +attach_+ method. This is currently supported for both UNIX domain sockets
|
16
|
+
# and TCP. You can also spawn and connect to an +nvim+ subprocess via
|
17
|
+
# +Neovim.attach_child(argv)+.
|
18
|
+
#
|
19
|
+
# You can define a remote plugin using the +Neovim.plugin+ DSL, which allows
|
20
|
+
# you to register commands, functions, and autocmds. Plugins are autoloaded by
|
21
|
+
# +nvim+ from the +rplugin/ruby+ directory in your +nvim+ runtime path.
|
22
|
+
#
|
23
|
+
# @example Connect over a TCP socket
|
24
|
+
# Neovim.attach_tcp("0.0.0.0", 3333) # => Neovim::Client
|
25
|
+
#
|
26
|
+
# @example Connect over a UNIX domain socket
|
27
|
+
# Neovim.attach_unix("/tmp/nvim.sock") # => Neovim::Client
|
28
|
+
#
|
29
|
+
# @example Spawn and connect to a child +nvim+ process
|
30
|
+
# Neovim.attach_child(["-u", "NONE"]) # => Neovim::Client
|
31
|
+
#
|
32
|
+
# @example Define a Ruby plugin
|
33
|
+
# # ~/.config/nvim/rplugin/ruby/plugin.rb
|
34
|
+
#
|
35
|
+
# Neovim.plugin do |plug|
|
36
|
+
# # Define a command called "SetLine" which sets the contents of the
|
37
|
+
# # current line. This command is executed asynchronously, so the return
|
38
|
+
# # value is ignored.
|
39
|
+
# plug.command(:SetLine, :nargs => 1) do |nvim, str|
|
40
|
+
# nvim.current.line = str
|
41
|
+
# end
|
42
|
+
#
|
43
|
+
# # Define a function called "Sum" which adds two numbers. This function is
|
44
|
+
# # executed synchronously, so the result of the block will be returned to
|
45
|
+
# # nvim.
|
46
|
+
# plug.function(:Sum, :nargs => 2, :sync => true) do |nvim, x, y|
|
47
|
+
# x + y
|
48
|
+
# end
|
49
|
+
#
|
50
|
+
# # Define an autocmd for the BufEnter event on Ruby files.
|
51
|
+
# plug.autocmd(:BufEnter, :pattern => "*.rb") do |nvim|
|
52
|
+
# nvim.command("echom 'Ruby file, eh?'")
|
53
|
+
# end
|
54
|
+
# end
|
55
|
+
#
|
56
|
+
# @see Client
|
57
|
+
# @see Plugin::DSL
|
9
58
|
module Neovim
|
10
59
|
class << self
|
11
|
-
|
60
|
+
# @api private
|
61
|
+
# @return [Manifest, nil]
|
62
|
+
attr_accessor :__configured_plugin_manifest
|
63
|
+
|
64
|
+
# @api private
|
65
|
+
# @return [String, nil]
|
66
|
+
attr_accessor :__configured_plugin_path
|
12
67
|
end
|
13
68
|
|
69
|
+
# Connect to a running +nvim+ instance over TCP.
|
70
|
+
#
|
71
|
+
# @param host [String] The hostname or IP address
|
72
|
+
# @param port [Fixnum] The port
|
73
|
+
# @return [Client]
|
74
|
+
# @see Session.tcp
|
14
75
|
def self.attach_tcp(host, port)
|
15
|
-
|
76
|
+
Client.new(Session.tcp(host, port).discover_api)
|
16
77
|
end
|
17
78
|
|
79
|
+
# Connect to a running +nvim+ instance over a UNIX domain socket.
|
80
|
+
#
|
81
|
+
# @param socket_path [String] The socket path
|
82
|
+
# @return [Client]
|
83
|
+
# @see Session.unix
|
18
84
|
def self.attach_unix(socket_path)
|
19
|
-
|
85
|
+
Client.new(Session.unix(socket_path).discover_api)
|
20
86
|
end
|
21
87
|
|
88
|
+
# Spawn and connect to a child +nvim+ process.
|
89
|
+
#
|
90
|
+
# @param argv [Array] The arguments to pass to the spawned process
|
91
|
+
# @return [Client]
|
92
|
+
# @see Session.child
|
22
93
|
def self.attach_child(argv=[])
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
def self.start_host(rplugin_paths)
|
27
|
-
Host.load_from_files(rplugin_paths).run
|
94
|
+
Client.new(Session.child(argv).discover_api)
|
28
95
|
end
|
29
96
|
|
97
|
+
# Define an +nvim+ remote plugin using the plugin DSL.
|
98
|
+
#
|
99
|
+
# @yield [Plugin::DSL]
|
100
|
+
# @return [Plugin]
|
101
|
+
# @see Plugin::DSL
|
30
102
|
def self.plugin(&block)
|
31
103
|
Plugin.from_config_block(__configured_plugin_path, &block).tap do |plugin|
|
32
104
|
if __configured_plugin_manifest.respond_to?(:register)
|
@@ -35,20 +107,31 @@ module Neovim
|
|
35
107
|
end
|
36
108
|
end
|
37
109
|
|
110
|
+
# Start a plugin host. This is called by the +nvim-ruby-host+ executable,
|
111
|
+
# which is spawned by +nvim+ to discover and run Ruby plugins, and acts as
|
112
|
+
# the bridge between +nvim+ and the plugin.
|
113
|
+
#
|
114
|
+
# @param rplugin_paths [Array<String>] The paths to remote plugin files
|
115
|
+
# @return [void]
|
116
|
+
# @see Host
|
117
|
+
def self.start_host(rplugin_paths)
|
118
|
+
Host.load_from_files(rplugin_paths).run
|
119
|
+
end
|
120
|
+
|
121
|
+
# Set the Neovim global logger.
|
122
|
+
#
|
123
|
+
# @param logger [Logger] The target logger
|
124
|
+
# @return [Logger]
|
125
|
+
# @see Logging
|
38
126
|
def self.logger=(logger)
|
39
127
|
Logging.logger = logger
|
40
128
|
end
|
41
129
|
|
130
|
+
# The Neovim global logger.
|
131
|
+
#
|
132
|
+
# @return [Logger]
|
133
|
+
# @see Logging
|
42
134
|
def self.logger
|
43
135
|
Logging.logger
|
44
136
|
end
|
45
|
-
|
46
|
-
def self.attach_event_loop(event_loop)
|
47
|
-
msgpack_stream = MsgpackStream.new(event_loop)
|
48
|
-
async_session = AsyncSession.new(msgpack_stream)
|
49
|
-
session = Session.new(async_session)
|
50
|
-
|
51
|
-
Client.new(session)
|
52
|
-
end
|
53
|
-
private_class_method :attach_event_loop
|
54
137
|
end
|
data/lib/neovim/api.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
module Neovim
|
2
|
+
class API
|
3
|
+
# Represents an unknown API. Used as a stand-in when the API hasn't been
|
4
|
+
# discovered yet via the +vim_get_api_info+ RPC call.
|
5
|
+
#
|
6
|
+
# @return [API]
|
7
|
+
def self.null
|
8
|
+
new([nil, {"functions" => [], "types" => []}])
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(data)
|
12
|
+
@data = data
|
13
|
+
end
|
14
|
+
|
15
|
+
# Return all functions defined by the API, as +Function+ objects.
|
16
|
+
#
|
17
|
+
# @return [Array<Function>]
|
18
|
+
# @see Function
|
19
|
+
def functions
|
20
|
+
@functions ||= @data.fetch(1).fetch("functions").map do |func|
|
21
|
+
Function.new(func["name"], func["async"])
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Return information about +nvim+ types. Used for registering MessagePack
|
26
|
+
# +ext+ types.
|
27
|
+
#
|
28
|
+
# @return [Hash]
|
29
|
+
def types
|
30
|
+
@types ||= @data.fetch(1).fetch("types")
|
31
|
+
end
|
32
|
+
|
33
|
+
# Return the channel ID of the current RPC session.
|
34
|
+
#
|
35
|
+
# @return [Fixnum, nil]
|
36
|
+
def channel_id
|
37
|
+
@channel_id ||= @data.fetch(0)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Return a list of functions with the given name prefix.
|
41
|
+
#
|
42
|
+
# @param prefix [String] The function prefix
|
43
|
+
# @return [Array<Function>]
|
44
|
+
def functions_with_prefix(prefix)
|
45
|
+
functions.select do |function|
|
46
|
+
function.name =~ /\A#{prefix}/
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# Find a function with the given name.
|
51
|
+
#
|
52
|
+
# @param name [String] The name of the function
|
53
|
+
# @return [Function, nil]
|
54
|
+
def function(name)
|
55
|
+
functions.find do |func|
|
56
|
+
func.name == name.to_s
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Truncate the output of inspect so console sessions are more pleasant.
|
61
|
+
#
|
62
|
+
# @return [String]
|
63
|
+
def inspect
|
64
|
+
"#<#{self.class}:0x%x @types={...} @functions={...}>" % (object_id << 1)
|
65
|
+
end
|
66
|
+
|
67
|
+
# Encapsulate an RPC function.
|
68
|
+
class Function < Struct.new(:name, :async)
|
69
|
+
# Apply this function to a running RPC session. Sends either a request if
|
70
|
+
# +async+ is +false+ or a notification if +async+ is +true+.
|
71
|
+
#
|
72
|
+
# @param session [Session] The session to apply the function to.
|
73
|
+
# @param *args [Array] Arguments to the function.
|
74
|
+
# @return [Object, nil]
|
75
|
+
def call(session, *args)
|
76
|
+
if async
|
77
|
+
session.notify(name, *args)
|
78
|
+
else
|
79
|
+
session.request(name, *args)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
data/lib/neovim/async_session.rb
CHANGED
@@ -3,6 +3,9 @@ require "neovim/request"
|
|
3
3
|
require "neovim/notification"
|
4
4
|
|
5
5
|
module Neovim
|
6
|
+
# Handles formatting RPC requests and writing them to the
|
7
|
+
# +MsgpackStream+. This exposes an asynchronous API, in which responses
|
8
|
+
# are handled in callbacks.
|
6
9
|
class AsyncSession
|
7
10
|
include Logging
|
8
11
|
|
@@ -12,49 +15,99 @@ module Neovim
|
|
12
15
|
@pending_requests = {}
|
13
16
|
end
|
14
17
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
18
|
+
# Send an RPC request and enqueue it's callback to be called when a
|
19
|
+
# response is received.
|
20
|
+
#
|
21
|
+
# @param method [Symbol, String] The RPC method name
|
22
|
+
# @param *args [Array] The arguments to the RPC method
|
23
|
+
# @return [self]
|
24
|
+
# @example
|
25
|
+
# async_session.request(:vim_strwidth, "foobar") do |response|
|
26
|
+
# $stderr.puts("Got a response #{response}")
|
27
|
+
# end
|
19
28
|
def request(method, *args, &response_cb)
|
20
29
|
reqid = @request_id
|
21
30
|
@request_id += 1
|
22
31
|
|
23
|
-
@msgpack_stream.
|
32
|
+
@msgpack_stream.write([0, reqid, method, args])
|
24
33
|
@pending_requests[reqid] = response_cb || Proc.new {}
|
25
34
|
self
|
26
35
|
end
|
27
36
|
|
37
|
+
# Send an RPC notification. Notifications don't receive a response
|
38
|
+
# from +nvim+.
|
39
|
+
#
|
40
|
+
# @param method [Symbol, String] The RPC method name
|
41
|
+
# @param *args [Array] The arguments to the RPC method
|
42
|
+
# @return [self]
|
43
|
+
# @example
|
44
|
+
# async_session.notify(:vim_input, "jk")
|
28
45
|
def notify(method, *args)
|
29
|
-
@msgpack_stream.
|
46
|
+
@msgpack_stream.write([2, method, args])
|
30
47
|
self
|
31
48
|
end
|
32
49
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
50
|
+
# Run the event loop, yielding received RPC messages to the block. RPC
|
51
|
+
# requests and notifications from +nvim+ will be wrapped in +Request+
|
52
|
+
# and +Notification+ objects, respectively, and responses will be
|
53
|
+
# passed to their callbacks with optional errors.
|
54
|
+
#
|
55
|
+
# @param session [Session] The current session
|
56
|
+
# @yield [Object]
|
57
|
+
# @return [void]
|
58
|
+
# @see MsgpackStream#run
|
59
|
+
# @see EventLoop#run
|
60
|
+
def run(session=nil, &callback)
|
61
|
+
@msgpack_stream.run(session) do |msg|
|
62
|
+
kind, *payload = msg
|
40
63
|
|
41
64
|
case kind
|
42
65
|
when 0
|
43
|
-
|
44
|
-
request_cb.call(Request.new(method, args, @msgpack_stream, reqid))
|
66
|
+
handle_request(payload, callback)
|
45
67
|
when 1
|
46
|
-
|
47
|
-
@pending_requests.fetch(reqid).call(error, result)
|
68
|
+
handle_response(payload)
|
48
69
|
when 2
|
49
|
-
|
50
|
-
notification_cb.call(Notification.new(method, args))
|
70
|
+
handle_notification(payload, callback)
|
51
71
|
end
|
52
72
|
end
|
53
|
-
|
54
|
-
@msgpack_stream.run(msg_cb, setup_cb)
|
55
73
|
rescue => e
|
56
74
|
fatal("got unexpected error #{e}")
|
57
75
|
debug(e.backtrace.join("\n"))
|
58
76
|
end
|
77
|
+
|
78
|
+
# Stop the event loop.
|
79
|
+
#
|
80
|
+
# @return [void]
|
81
|
+
# @see EventLoop#stop
|
82
|
+
def stop
|
83
|
+
@msgpack_stream.stop
|
84
|
+
end
|
85
|
+
|
86
|
+
# Shut down the event loop.
|
87
|
+
#
|
88
|
+
# @return [void]
|
89
|
+
# @see EventLoop#shutdown
|
90
|
+
def shutdown
|
91
|
+
@msgpack_stream.shutdown
|
92
|
+
end
|
93
|
+
|
94
|
+
private
|
95
|
+
|
96
|
+
def handle_request(payload, callback)
|
97
|
+
callback ||= Proc.new {}
|
98
|
+
reqid, method, args = payload
|
99
|
+
callback.call(Request.new(method, args, @msgpack_stream, reqid))
|
100
|
+
end
|
101
|
+
|
102
|
+
def handle_response(payload)
|
103
|
+
reqid, (_, error), result = payload
|
104
|
+
@pending_requests.delete(reqid).call(error, result)
|
105
|
+
end
|
106
|
+
|
107
|
+
def handle_notification(payload, callback)
|
108
|
+
callback ||= Proc.new {}
|
109
|
+
method, args = payload
|
110
|
+
callback.call(Notification.new(method, args))
|
111
|
+
end
|
59
112
|
end
|
60
113
|
end
|