Class: Neovim::Remote

Inherits:
Object show all
Extended by:
Logging
Includes:
Logging
Defined in:
lib/neovim/remote.rb

Direct Known Subclasses

Provider

Defined Under Namespace

Classes: Disconnected, Message, ResponseError

Constant Summary

Constants included from Logging

Logging::DEFAULT_LEVEL, Logging::LEVELS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

put

Constructor Details

#initialize(plugins, conn) ⇒ Remote

Returns a new instance of Remote.



143
144
145
146
147
148
149
# File 'lib/neovim/remote.rb', line 143

def initialize plugins, conn
  @conn = conn
  @request_id = 0
  @responses = {}
  @plugins = {}
  @plugins.update plugins if plugins
end

Class Method Details

.open(conntype, *args, **kwargs) ⇒ Object



116
117
118
119
120
121
# File 'lib/neovim/remote.rb', line 116

def open conntype, *args, **kwargs
  open_conn conntype, *args, **kwargs do |conn|
    i = new nil, conn
    yield i
  end
end

.open_conn(conntype, *args, **kwargs) ⇒ Object



108
109
110
111
112
# File 'lib/neovim/remote.rb', line 108

def open_conn conntype, *args, **kwargs
  conntype.open_files *args, **kwargs do |conn|
    yield conn
  end
end

.start(plugins, *args) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/neovim/remote.rb', line 123

def start plugins, *args
  open_logfile do
    log :info, "Starting", args: $*
    open_conn *args do |conn|
      i = new plugins, conn
      yield i
    end
  ensure
    log :info, "Leaving"
  end
end

.start_client(*args) ⇒ Object



135
136
137
138
139
# File 'lib/neovim/remote.rb', line 135

def start_client *args
  start nil, *args do |i|
    yield i.start
  end
end

Instance Method Details

#client_methodsObject



166
167
168
169
170
171
172
173
# File 'lib/neovim/remote.rb', line 166

def client_methods
  l = @plugins.values.reject { |p| p.type }
  if l.notempty? then
    r = {}
    l.each { |p| p.options { |name,opts| r[ name] = opts } }
    r
  end
end

#client_nameObject



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/neovim/remote.rb', line 151

def client_name
  l = @plugins.values.select { |p| p.type }
  if l.notempty? then
    l.map! { |p| p.type }
    l.uniq!
    name = l.join "-"
    log :info, "Client Name", name: name
    "ruby-#{name}-host"
  else
    "ruby-client"
  end
end

#client_typeObject



164
# File 'lib/neovim/remote.rb', line 164

def client_type ; self.class.plain_name.downcase ; end

#notify(method, *args) ⇒ Object



240
241
242
# File 'lib/neovim/remote.rb', line 240

def notify method, *args
  put Message::Notification[ method, args]
end

#request(method, *args) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/neovim/remote.rb', line 226

def request method, *args
  rid = @request_id = @request_id.succ
  put Message::Request[ rid, method, args]
  @responses[ rid] = nil
  run rid
  r = @responses.delete rid
  if r.error then
    t, e = *r.error
    t = @conn.error t
    raise ResponseError, "#{t}: #{e}"
  end
  r.value
end

#run(until_id = nil) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/neovim/remote.rb', line 181

def run until_id = nil
  loop do
    if @deferred and @conn.client then
      d, @deferred = @deferred, nil
      d.each { |p| p.call }
    end
    message = get
    case message
    when Message::Response then
      if @responses.key? message.request_id then
        @responses[ message.request_id] = message
      else
        log :warning, "Dropped response", message.request_id
      end
    when Message::Request, Message::Notification then
      h = find_handler message.method_name
      if h then
        p = proc do
          begin
            log :debug1, "Calling handler", name: message.method_name, args: message.arguments
            r = h.execute @conn.client, *message.arguments
            log :debug1, "Handler result", result: r
          rescue
            e = [ 0, $!.to_s]
            log_exception :error
          end
          put Message::Response[ message.request_id, e, r] if message.respond_to? :request_id
        end
        if @conn.client or not h.needs_client? then
          p.call
        else
          log :info, "Deferred handler for", name: message.method_name
          @deferred ||= []
          @deferred.push p
        end
      else
        if message.respond_to? :request_id then
          put Message::Response[ message.request_id, [0, "No handler #{message.method_name}."], nil]
        end
      end
    end
    break if until_id and @responses[ until_id]
  end
end

#startObject



176
177
178
179
# File 'lib/neovim/remote.rb', line 176

def start
  @conn.start self
  @conn.client
end