mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-08 06:44:46 +08:00
Remove classes from plugins and use decorators instead
This commit is contained in:
@@ -121,7 +121,7 @@ class Evaluator(object):
|
|||||||
|
|
||||||
# Plugin API
|
# Plugin API
|
||||||
from jedi.plugins import plugin_manager
|
from jedi.plugins import plugin_manager
|
||||||
plugin_callbacks = plugin_manager.get_callbacks(self)
|
plugin_callbacks = plugin_manager.get_callbacks()
|
||||||
self.execute = plugin_callbacks.decorate('execute', callback=_execute)
|
self.execute = plugin_callbacks.decorate('execute', callback=_execute)
|
||||||
self._import_module = partial(
|
self._import_module = partial(
|
||||||
plugin_callbacks.decorate(
|
plugin_callbacks.decorate(
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
from jedi.plugins.stdlib import StdlibPlugin
|
from jedi.plugins import stdlib
|
||||||
from jedi.plugins.flask import FlaskPlugin
|
from jedi.plugins import flask
|
||||||
|
|
||||||
|
|
||||||
class _PluginManager(object):
|
class _PluginManager(object):
|
||||||
@@ -12,12 +12,12 @@ class _PluginManager(object):
|
|||||||
"""
|
"""
|
||||||
self._registered_plugins.append(plugin_class)
|
self._registered_plugins.append(plugin_class)
|
||||||
|
|
||||||
def _build_chain(self, evaluator):
|
def _build_chain(self):
|
||||||
for plugin_class in self._registered_plugin_classes:
|
for plugin_class in self._registered_plugin_classes:
|
||||||
yield plugin_class(evaluator)
|
yield plugin_class
|
||||||
|
|
||||||
def get_callbacks(self, evaluator):
|
def get_callbacks(self):
|
||||||
return _PluginCallbacks(self._build_chain(evaluator))
|
return _PluginCallbacks(self._build_chain())
|
||||||
|
|
||||||
|
|
||||||
class _PluginCallbacks(object):
|
class _PluginCallbacks(object):
|
||||||
@@ -27,11 +27,13 @@ class _PluginCallbacks(object):
|
|||||||
def decorate(self, name, callback):
|
def decorate(self, name, callback):
|
||||||
for plugin in reversed(self._plugins):
|
for plugin in reversed(self._plugins):
|
||||||
# Need to reverse so the first plugin is run first.
|
# Need to reverse so the first plugin is run first.
|
||||||
callback = getattr(plugin, name)(callback)
|
try:
|
||||||
|
func = getattr(plugin, name)
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
callback = func(callback)
|
||||||
return callback
|
return callback
|
||||||
|
|
||||||
|
|
||||||
plugin_manager = _PluginManager([
|
plugin_manager = _PluginManager([stdlib, flask])
|
||||||
StdlibPlugin,
|
|
||||||
FlaskPlugin,
|
|
||||||
])
|
|
||||||
|
|||||||
@@ -1,21 +0,0 @@
|
|||||||
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, callback):
|
|
||||||
"""
|
|
||||||
Decorates the execute(context, arguments) function.
|
|
||||||
"""
|
|
||||||
return callback
|
|
||||||
|
|
||||||
def import_module(self, callback):
|
|
||||||
"""
|
|
||||||
Decorates the
|
|
||||||
import_module(evaluator, import_path, sys_path, add_error_callback)
|
|
||||||
function.
|
|
||||||
"""
|
|
||||||
return callback
|
|
||||||
@@ -1,8 +1,4 @@
|
|||||||
from jedi.plugins.base import BasePlugin
|
def import_module(callback):
|
||||||
|
|
||||||
|
|
||||||
class FlaskPlugin(BasePlugin):
|
|
||||||
def import_module(self, callback):
|
|
||||||
"""
|
"""
|
||||||
Handle "magic" Flask extension imports:
|
Handle "magic" Flask extension imports:
|
||||||
``flask.ext.foo`` is really ``flask_foo`` or ``flaskext.foo``.
|
``flask.ext.foo`` is really ``flask_foo`` or ``flaskext.foo``.
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ compiled module that returns the types for C-builtins.
|
|||||||
import parso
|
import parso
|
||||||
|
|
||||||
from jedi._compatibility import force_unicode
|
from jedi._compatibility import force_unicode
|
||||||
from jedi.plugins.base import BasePlugin
|
|
||||||
from jedi import debug
|
from jedi import debug
|
||||||
from jedi.evaluate.helpers import get_str_or_none
|
from jedi.evaluate.helpers import get_str_or_none
|
||||||
from jedi.evaluate.arguments import ValuesArguments, \
|
from jedi.evaluate.arguments import ValuesArguments, \
|
||||||
@@ -98,15 +97,14 @@ _NAMEDTUPLE_FIELD_TEMPLATE = '''\
|
|||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
||||||
class StdlibPlugin(BasePlugin):
|
def execute(callback):
|
||||||
def execute(self, callback):
|
|
||||||
def wrapper(context, arguments):
|
def wrapper(context, arguments):
|
||||||
try:
|
try:
|
||||||
obj_name = context.name.string_name
|
obj_name = context.name.string_name
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
if context.parent_context == self._evaluator.builtins_module:
|
if context.parent_context == context.evaluator.builtins_module:
|
||||||
module_name = 'builtins'
|
module_name = 'builtins'
|
||||||
elif context.parent_context is not None and context.parent_context.is_module():
|
elif context.parent_context is not None and context.parent_context.is_module():
|
||||||
module_name = context.parent_context.py__name__()
|
module_name = context.parent_context.py__name__()
|
||||||
|
|||||||
Reference in New Issue
Block a user