1
|
|
|
"""Code shared between the API classes.""" |
2
|
6 |
|
import functools |
3
|
|
|
|
4
|
|
|
|
5
|
6 |
|
class Remote(object): |
6
|
|
|
|
7
|
|
|
"""Base class for Nvim objects(buffer/window/tabpage). |
8
|
|
|
|
9
|
|
|
Each type of object has it's own specialized class with API wrappers around |
10
|
|
|
the msgpack-rpc session. This implements equality which takes the remote |
11
|
|
|
object handle into consideration. |
12
|
|
|
""" |
13
|
|
|
|
14
|
6 |
|
def __init__(self, session, code_data): |
15
|
|
|
"""Initialize from session and code_data immutable object. |
16
|
|
|
|
17
|
|
|
The `code_data` contains serialization information required for |
18
|
|
|
msgpack-rpc calls. It must be immutable for Buffer equality to work. |
19
|
|
|
""" |
20
|
6 |
|
self._session = session |
21
|
6 |
|
self.code_data = code_data |
22
|
6 |
|
self.api = RemoteApi(self, self._api_prefix) |
|
|
|
|
23
|
6 |
|
self.vars = RemoteMap(self, self._api_prefix + 'get_var', |
|
|
|
|
24
|
|
|
self._api_prefix + 'set_var') |
|
|
|
|
25
|
6 |
|
self.options = RemoteMap(self, self._api_prefix + 'get_option', |
|
|
|
|
26
|
|
|
self._api_prefix + 'set_option') |
|
|
|
|
27
|
|
|
|
28
|
6 |
|
def __eq__(self, other): |
29
|
|
|
"""Return True if `self` and `other` are the same object.""" |
30
|
6 |
|
return (hasattr(other, 'code_data') and |
31
|
|
|
other.code_data == self.code_data) |
32
|
|
|
|
33
|
6 |
|
def __hash__(self): |
34
|
|
|
"""Return hash based on remote object id.""" |
35
|
6 |
|
return self.code_data.__hash__() |
36
|
|
|
|
37
|
6 |
|
def request(self, name, *args, **kwargs): |
38
|
|
|
"""Wrapper for nvim.request.""" |
39
|
6 |
|
return self._session.request(name, self, *args, **kwargs) |
40
|
|
|
|
41
|
|
|
|
42
|
6 |
|
class RemoteApi(object): |
43
|
|
|
|
44
|
|
|
"""Wrapper to allow api methods to be called like python methods.""" |
45
|
|
|
|
46
|
6 |
|
def __init__(self, obj, api_prefix): |
47
|
|
|
"""Initialize a RemoteApi with object and api prefix.""" |
48
|
6 |
|
self._obj = obj |
49
|
6 |
|
self._api_prefix = api_prefix |
50
|
|
|
|
51
|
6 |
|
def __getattr__(self, name): |
52
|
|
|
"""Return wrapper to named api method.""" |
53
|
6 |
|
return functools.partial(self._obj.request, self._api_prefix + name) |
54
|
|
|
|
55
|
|
|
|
56
|
6 |
|
class RemoteMap(object): |
57
|
|
|
|
58
|
|
|
"""Represents a string->object map stored in Nvim. |
59
|
|
|
|
60
|
|
|
This is the dict counterpart to the `RemoteSequence` class, but it is used |
61
|
|
|
as a generic way of retrieving values from the various map-like data |
62
|
|
|
structures present in Nvim. |
63
|
|
|
|
64
|
|
|
It is used to provide a dict-like API to vim variables and options. |
65
|
|
|
""" |
66
|
|
|
|
67
|
6 |
|
def __init__(self, obj, get_method, set_method=None, self_obj=None): |
|
|
|
|
68
|
|
|
"""Initialize a RemoteMap with session, getter/setter and self_obj.""" |
69
|
6 |
|
self._get = functools.partial(obj.request, get_method) |
70
|
6 |
|
self._set = None |
71
|
6 |
|
if set_method: |
72
|
6 |
|
self._set = functools.partial(obj.request, set_method) |
73
|
|
|
|
74
|
6 |
|
def __getitem__(self, key): |
75
|
|
|
"""Return a map value by key.""" |
76
|
6 |
|
return self._get(key) |
77
|
|
|
|
78
|
6 |
|
def __setitem__(self, key, value): |
79
|
|
|
"""Set a map value by key(if the setter was provided).""" |
80
|
6 |
|
if not self._set: |
81
|
|
|
raise TypeError('This dict is read-only') |
82
|
6 |
|
self._set(key, value) |
83
|
|
|
|
84
|
6 |
|
def __delitem__(self, key): |
85
|
|
|
"""Delete a map value by associating None with the key.""" |
86
|
|
|
if not self._set: |
87
|
|
|
raise TypeError('This dict is read-only') |
88
|
|
|
return self._set(key, None) |
89
|
|
|
|
90
|
6 |
|
def __contains__(self, key): |
91
|
|
|
"""Check if key is present in the map.""" |
92
|
|
|
try: |
93
|
|
|
self._get(key) |
94
|
|
|
return True |
95
|
|
|
except Exception: |
|
|
|
|
96
|
|
|
return False |
97
|
|
|
|
98
|
6 |
|
def get(self, key, default=None): |
99
|
|
|
"""Return value for key if present, else a default value.""" |
100
|
|
|
try: |
101
|
|
|
return self._get(key) |
102
|
|
|
except Exception: |
|
|
|
|
103
|
|
|
return default |
104
|
|
|
|
105
|
|
|
|
106
|
6 |
|
class RemoteSequence(object): |
107
|
|
|
|
108
|
|
|
"""Represents a sequence of objects stored in Nvim. |
109
|
|
|
|
110
|
|
|
This class is used to wrap msgapck-rpc functions that work on Nvim |
111
|
|
|
sequences(of lines, buffers, windows and tabpages) with an API that |
112
|
|
|
is similar to the one provided by the python-vim interface. |
113
|
|
|
|
114
|
|
|
For example, the 'buffers' property of the `Nvim class is a RemoteSequence |
115
|
|
|
sequence instance, and the expression `nvim.buffers[0]` is translated to |
116
|
|
|
session.request('vim_get_buffers')[0]. |
117
|
|
|
|
118
|
|
|
It can also receive an optional self_obj that will be passed as first |
119
|
|
|
argument of the request. For example, `tabpage.windows[0]` is translated |
120
|
|
|
to: session.request('tabpage_get_windows', tabpage_instance)[0]. |
121
|
|
|
|
122
|
|
|
One important detail about this class is that all methods will fetch the |
123
|
|
|
sequence into a list and perform the necessary manipulation |
124
|
|
|
locally(iteration, indexing, counting, etc). |
125
|
|
|
""" |
126
|
|
|
|
127
|
6 |
|
def __init__(self, session, method): |
128
|
|
|
"""Initialize a RemoteSequence with session, method and self_obj.""" |
129
|
6 |
|
self._fetch = functools.partial(session.request, method) |
130
|
|
|
|
131
|
6 |
|
def __len__(self): |
132
|
|
|
"""Return the length of the remote sequence.""" |
133
|
6 |
|
return len(self._fetch()) |
134
|
|
|
|
135
|
6 |
|
def __getitem__(self, idx): |
136
|
|
|
"""Return a sequence item by index.""" |
137
|
6 |
|
if not isinstance(idx, slice): |
138
|
6 |
|
return self._fetch()[idx] |
139
|
|
|
return self._fetch()[idx.start:idx.stop] |
140
|
|
|
|
141
|
6 |
|
def __iter__(self): |
142
|
|
|
"""Return an iterator for the sequence.""" |
143
|
6 |
|
items = self._fetch() |
144
|
6 |
|
for item in items: |
145
|
6 |
|
yield item |
146
|
|
|
|
147
|
6 |
|
def __contains__(self, item): |
148
|
|
|
"""Check if an item is present in the sequence.""" |
149
|
6 |
|
return item in self._fetch() |
150
|
|
|
|
151
|
|
|
|
152
|
6 |
|
def _identity(obj, session, method, kind): |
|
|
|
|
153
|
|
|
return obj |
154
|
|
|
|
155
|
|
|
|
156
|
6 |
|
class DecodeHook(object): |
157
|
|
|
|
158
|
|
|
"""SessionHook subclass that decodes utf-8 strings coming from Nvim. |
159
|
|
|
|
160
|
|
|
This class is useful for python3, where strings are now unicode by |
161
|
|
|
default(byte strings need to be prefixed with "b"). |
162
|
|
|
""" |
163
|
|
|
|
164
|
6 |
|
def __init__(self, encoding='utf-8', encoding_errors='strict'): |
165
|
|
|
"""Initialize with encoding and encoding errors policy.""" |
166
|
3 |
|
self.encoding = encoding |
167
|
3 |
|
self.encoding_errors = encoding_errors |
168
|
|
|
|
169
|
6 |
|
def decode_if_bytes(self, obj): |
170
|
|
|
"""Decode obj if it is bytes.""" |
171
|
3 |
|
if isinstance(obj, bytes): |
172
|
3 |
|
return obj.decode(self.encoding, errors=self.encoding_errors) |
173
|
3 |
|
return obj |
174
|
|
|
|
175
|
6 |
|
def walk(self, obj): |
176
|
|
|
"""Decode bytes found in obj (any msgpack object). |
177
|
|
|
|
178
|
|
|
Uses encoding and policy specified in constructor. |
179
|
|
|
""" |
180
|
3 |
|
return walk(self.decode_if_bytes, obj) |
181
|
|
|
|
182
|
|
|
|
183
|
6 |
|
def walk(fn, obj, *args): |
184
|
|
|
"""Recursively walk an object graph applying `fn`/`args` to objects.""" |
185
|
6 |
|
if type(obj) in [list, tuple]: |
186
|
6 |
|
return list(walk(fn, o, *args) for o in obj) |
187
|
6 |
|
if type(obj) is dict: |
188
|
6 |
|
return dict((walk(fn, k, *args), walk(fn, v, *args)) for k, v in |
189
|
|
|
obj.items()) |
190
|
|
|
return fn(obj, *args) |
191
|
|
|
|
This check looks for calls to members that are non-existent. These calls will fail.
The member could have been renamed or removed.