Skip to content

Commit

Permalink
host: more robust way of finding the script host
Browse files Browse the repository at this point in the history
  • Loading branch information
bfredl committed Mar 20, 2016
1 parent 27dbfce commit 9498b38
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
16 changes: 8 additions & 8 deletions neovim/plugin/host.py
Expand Up @@ -9,6 +9,7 @@

from traceback import format_exc

from . import script_host
from ..api import DecodeHook
from ..compat import IS_PYTHON3, find_module
from ..msgpack_rpc import ErrorResponse
Expand Down Expand Up @@ -105,15 +106,14 @@ def _load(self, plugins):
if path in self._loaded:
error('{0} is already loaded'.format(path))
continue
if path == "script_host.py":
directory = os.path.dirname(__file__)
name = "script_host"
else:
directory, name = os.path.split(os.path.splitext(path)[0])
file, pathname, description = find_module(name, [directory])
handlers = []
try:
module = imp.load_module(name, file, pathname, description)
if path == "script_host.py":
module = script_host
else:
directory, name = os.path.split(os.path.splitext(path)[0])
file, pathname, descr = find_module(name, [directory])
module = imp.load_module(name, file, pathname, descr)
handlers = []
self._discover_classes(module, handlers, path)
self._discover_functions(module, handlers, path)
if not handlers:
Expand Down
18 changes: 8 additions & 10 deletions neovim/plugin/script_host.py
Expand Up @@ -5,7 +5,8 @@
import os
import sys

import neovim
from .decorators import plugin, rpc_export
from ..api import SessionHook

__all__ = ('ScriptHost',)

Expand All @@ -22,7 +23,7 @@
from importlib.machinery import PathFinder


@neovim.plugin
@plugin
class ScriptHost(object):

"""Provides an environment for running python plugins created for Vim."""
Expand Down Expand Up @@ -59,9 +60,6 @@ def setup(self, nvim):

def teardown(self):
"""Restore state modified from the `setup` call."""
for plugin in self.installed_plugins:
if hasattr(plugin, 'on_teardown'):
plugin.teardown()
nvim = self.nvim
info('uninstall import hook/path')
sys.path.remove(nvim.VIM_SPECIAL_PATH)
Expand All @@ -70,21 +68,21 @@ def teardown(self):
sys.stdout = self.saved_stdout
sys.stderr = self.saved_stderr

@neovim.rpc_export('python_execute', sync=True)
@rpc_export('python_execute', sync=True)
def python_execute(self, script, range_start, range_stop):
"""Handle the `python` ex command."""
self._set_current_range(range_start, range_stop)
exec(script, self.module.__dict__)

@neovim.rpc_export('python_execute_file', sync=True)
@rpc_export('python_execute_file', sync=True)
def python_execute_file(self, file_path, range_start, range_stop):
"""Handle the `pyfile` ex command."""
self._set_current_range(range_start, range_stop)
with open(file_path) as f:
script = compile(f.read(), file_path, 'exec')
exec(script, self.module.__dict__)

@neovim.rpc_export('python_do_range', sync=True)
@rpc_export('python_do_range', sync=True)
def python_do_range(self, start, stop, code):
"""Handle the `pydo` ex command."""
self._set_current_range(start, stop)
Expand Down Expand Up @@ -142,7 +140,7 @@ def python_do_range(self, start, stop, code):
# delete the function
del self.module.__dict__[fname]

@neovim.rpc_export('python_eval', sync=True)
@rpc_export('python_eval', sync=True)
def python_eval(self, expr):
"""Handle the `pyeval` vim function."""
return eval(expr, self.module.__dict__)
Expand All @@ -163,7 +161,7 @@ def writelines(self, seq):
self.redirect_handler('\n'.join(seq))


class LegacyEvalHook(neovim.SessionHook):
class LegacyEvalHook(SessionHook):

"""Injects legacy `vim.eval` behavior to a Nvim instance."""

Expand Down

0 comments on commit 9498b38

Please sign in to comment.