forked from VimPlug/jedi
Add a plugin infrastructure
This commit is contained in:
@@ -108,6 +108,8 @@ class Evaluator(object):
|
||||
|
||||
self.reset_recursion_limitations()
|
||||
self.allow_different_encoding = True
|
||||
from jedi.plugins import plugin_manager
|
||||
self.plugin_callbacks = plugin_manager.get_callbacks(self)
|
||||
|
||||
@property
|
||||
@evaluator_function_cache()
|
||||
|
||||
@@ -53,17 +53,17 @@ class Context(BaseContext):
|
||||
except stdlib.NotInStdLib:
|
||||
pass
|
||||
|
||||
try:
|
||||
func = self.py__call__
|
||||
except AttributeError:
|
||||
debug.warning("no execution possible %s", self)
|
||||
return NO_CONTEXTS
|
||||
else:
|
||||
context_set = func(arguments)
|
||||
debug.dbg('execute result: %s in %s', context_set, self)
|
||||
return context_set
|
||||
|
||||
return self.evaluator.execute(self, arguments)
|
||||
def default(arguments):
|
||||
try:
|
||||
func = self.py__call__
|
||||
except AttributeError:
|
||||
debug.warning("no execution possible %s", self)
|
||||
return NO_CONTEXTS
|
||||
else:
|
||||
context_set = func(arguments)
|
||||
debug.dbg('execute result: %s in %s', context_set, self)
|
||||
return context_set
|
||||
return self.evaluator.plugin_callbacks.execute(default, arguments)
|
||||
|
||||
def execute_evaluated(self, *value_list):
|
||||
"""
|
||||
|
||||
43
jedi/plugins/__init__.py
Normal file
43
jedi/plugins/__init__.py
Normal file
@@ -0,0 +1,43 @@
|
||||
from functools import partial
|
||||
|
||||
|
||||
class _PluginManager(object):
|
||||
def __init__(self, registered_plugin_classes=()):
|
||||
self._registered_plugin_classes = list(registered_plugin_classes)
|
||||
|
||||
def register(self, plugin_class):
|
||||
"""
|
||||
Makes it possible to register your plugin.
|
||||
"""
|
||||
self._registered_plugins.append(plugin_class)
|
||||
|
||||
def _build_chain(self, evaluator):
|
||||
for plugin_class in self._registered_plugin_classes:
|
||||
yield plugin_class(evaluator)
|
||||
|
||||
def get_callbacks(self, evaluator):
|
||||
return _PluginCallbacks(self._build_chain(evaluator))
|
||||
|
||||
|
||||
class _PluginCallbacks(object):
|
||||
def __init__(self, plugins):
|
||||
plugins = list(plugins)
|
||||
self.execute = self._wrap(plugins, 'execute')
|
||||
|
||||
def _wrap(self, plugins, name):
|
||||
if not plugins:
|
||||
def default_callback(callback, *args, **kwargs):
|
||||
return callback(*args, **kwargs)
|
||||
|
||||
return default_callback
|
||||
|
||||
func = None
|
||||
for plugin in plugins:
|
||||
if func is None:
|
||||
func = getattr(plugin, name)
|
||||
else:
|
||||
func = partial(getattr(plugin, name), func)
|
||||
return func
|
||||
|
||||
|
||||
plugin_manager = _PluginManager()
|
||||
10
jedi/plugins/base.py
Normal file
10
jedi/plugins/base.py
Normal file
@@ -0,0 +1,10 @@
|
||||
class BasePlugin(object):
|
||||
"""
|
||||
Plugins are created each time an evaluator is created.
|
||||
"""
|
||||
def __init__(self, evaluator):
|
||||
# In __init__ you can do some caching.
|
||||
self._evaluator = evaluator
|
||||
|
||||
def execute(self, default_callback, arguments):
|
||||
return default_callback(arguments)
|
||||
Reference in New Issue
Block a user