Skip to content

Commit 3ed0e93

Browse files
committed
Refactor legacy vim module to subclass
1 parent 22537a2 commit 3ed0e93

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

neovim/api/nvim.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ class Nvim(object):
3232
from a raw `Session` instance.
3333
3434
Subsequent instances for the same session can be created by calling the
35-
`with_hook` instance method and passing a SessionHook instance. This can
36-
be useful to have multiple `Nvim` objects that behave differently without
37-
one affecting the other.
35+
`with_decodehook` instance method to change the decoding behavior or
36+
`SubClass.from_nvim(nvim)` where `SubClass` is a subclass of `Nvim`, which
37+
is useful for having multiple `Nvim` objects that behave differently
38+
without one affecting the other.
3839
"""
3940

4041
@classmethod
@@ -60,6 +61,12 @@ def from_session(cls, session):
6061

6162
return cls(session, channel_id, metadata, types)
6263

64+
@classmethod
65+
def from_nvim(cls, nvim):
66+
"""Create a new Nvim instance from an existing instance."""
67+
return cls(nvim._session, nvim.channel_id, nvim.metadata,
68+
nvim.types, nvim._decodehook)
69+
6370
def __init__(self, session, channel_id, metadata, types, decodehook=None):
6471
"""Initialize a new Nvim instance. This method is module-private."""
6572
self._session = session

neovim/plugin/script_host.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import sys
77

88
from .decorators import plugin, rpc_export
9-
from ..api import SessionHook
9+
from ..api import Nvim
1010

1111
__all__ = ('ScriptHost',)
1212

@@ -36,7 +36,7 @@ def __init__(self, nvim):
3636
nvim.script_context = self.module
3737
# it seems some plugins assume 'sys' is already imported, so do it now
3838
exec('import sys', self.module.__dict__)
39-
self.legacy_vim = nvim.with_hook(LegacyEvalHook())
39+
self.legacy_vim = LegacyVim.from_nvim(nvim)
4040
sys.modules['vim'] = self.legacy_vim
4141

4242
def setup(self, nvim):
@@ -161,20 +161,14 @@ def writelines(self, seq):
161161
self.redirect_handler('\n'.join(seq))
162162

163163

164-
class LegacyEvalHook(SessionHook):
165-
166-
"""Injects legacy `vim.eval` behavior to a Nvim instance."""
167-
168-
def __init__(self):
169-
super(LegacyEvalHook, self).__init__(from_nvim=self._string_eval)
170-
171-
def _string_eval(self, obj, session, method, kind):
172-
if method == 'vim_eval':
173-
if IS_PYTHON3:
174-
if isinstance(obj, (int, float)):
175-
return str(obj)
176-
elif isinstance(obj, (int, long, float)):
164+
class LegacyVim(Nvim):
165+
def eval(self, expr):
166+
obj = self.request("vim_eval", expr)
167+
if IS_PYTHON3:
168+
if isinstance(obj, (int, float)):
177169
return str(obj)
170+
elif isinstance(obj, (int, long, float)):
171+
return str(obj)
178172
return obj
179173

180174

0 commit comments

Comments
 (0)