1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
module Neovim
# @api private
class API
attr_reader :channel_id
def initialize(payload)
@channel_id, @api_info = payload
end
# Return all functions defined by the API.
def functions
@functions ||= @api_info.fetch("functions").inject({}) do |acc, func|
function = Function.new(func)
acc.merge(function.name => function)
end
end
# Return information about +nvim+ types. Used for registering MessagePack
# +ext+ types.
def types
@types ||= @api_info.fetch("types")
end
def function_for_object_method(obj, method_name)
functions[function_name(obj, method_name)]
end
def functions_for_object(obj)
pattern = function_pattern(obj)
functions.values.select { |func| func.name =~ pattern }
end
# Truncate the output of inspect so console sessions are more pleasant.
def inspect
format("#<#{self.class}:0x%x @channel_id=#{@channel_id.inspect}>", object_id << 1)
end
private
def function_name(obj, method_name)
case obj
when Client
"nvim_#{method_name}"
when Buffer
"nvim_buf_#{method_name}"
when Window
"nvim_win_#{method_name}"
when Tabpage
"nvim_tabpage_#{method_name}"
else
raise "Unknown object #{obj.inspect}"
end
end
def function_pattern(obj)
case obj
when Client
/^nvim_(?!(buf|win|tabpage)_)/
when Buffer
/^nvim_buf_/
when Window
/^nvim_win_/
when Tabpage
/^nvim_tabpage_/
else
raise "Unknown object #{obj.inspect}"
end
end
# @api private
class Function
attr_reader :name
def initialize(attributes)
@name = attributes.fetch("name")
end
def method_name
@name.sub(/^nvim_(win_|buf_|tabpage_)?/, "").to_sym
end
# Apply this function to a running RPC session.
def call(session, *args)
session.request(name, *args)
end
end
end
end
|