mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-10 07:41:51 +08:00
Move the stdlib executions into a plugin
This commit is contained in:
@@ -85,6 +85,18 @@ from jedi.evaluate.syntax_tree import eval_trailer, eval_expr_stmt, \
|
|||||||
eval_node, check_tuple_assignments
|
eval_node, check_tuple_assignments
|
||||||
|
|
||||||
|
|
||||||
|
def execute(context, arguments):
|
||||||
|
try:
|
||||||
|
func = context.py__call__
|
||||||
|
except AttributeError:
|
||||||
|
debug.warning("no execution possible %s", context)
|
||||||
|
return NO_CONTEXTS
|
||||||
|
else:
|
||||||
|
context_set = func(arguments)
|
||||||
|
debug.dbg('execute result: %s in %s', context_set, context)
|
||||||
|
return context_set
|
||||||
|
|
||||||
|
|
||||||
class Evaluator(object):
|
class Evaluator(object):
|
||||||
def __init__(self, project, environment=None, script_path=None):
|
def __init__(self, project, environment=None, script_path=None):
|
||||||
if environment is None:
|
if environment is None:
|
||||||
@@ -109,7 +121,8 @@ class Evaluator(object):
|
|||||||
self.reset_recursion_limitations()
|
self.reset_recursion_limitations()
|
||||||
self.allow_different_encoding = True
|
self.allow_different_encoding = True
|
||||||
from jedi.plugins import plugin_manager
|
from jedi.plugins import plugin_manager
|
||||||
self.plugin_callbacks = plugin_manager.get_callbacks(self)
|
plugin_callbacks = plugin_manager.get_callbacks(self)
|
||||||
|
self.execute = plugin_callbacks.decorate('execute', callback=execute)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@evaluator_function_cache()
|
@evaluator_function_cache()
|
||||||
|
|||||||
@@ -44,26 +44,7 @@ class Context(BaseContext):
|
|||||||
if self.evaluator.is_analysis:
|
if self.evaluator.is_analysis:
|
||||||
arguments.eval_all()
|
arguments.eval_all()
|
||||||
|
|
||||||
debug.dbg('execute: %s %s', self, arguments)
|
return self.evaluator.execute(self, arguments)
|
||||||
from jedi.evaluate import stdlib
|
|
||||||
try:
|
|
||||||
# Some stdlib functions like super(), namedtuple(), etc. have been
|
|
||||||
# hard-coded in Jedi to support them.
|
|
||||||
return stdlib.execute(self.evaluator, self, arguments)
|
|
||||||
except stdlib.NotInStdLib:
|
|
||||||
pass
|
|
||||||
|
|
||||||
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):
|
def execute_evaluated(self, *value_list):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from functools import partial
|
from jedi.plugins.stdlib import StdlibPlugin
|
||||||
|
|
||||||
|
|
||||||
class _PluginManager(object):
|
class _PluginManager(object):
|
||||||
@@ -21,23 +21,15 @@ class _PluginManager(object):
|
|||||||
|
|
||||||
class _PluginCallbacks(object):
|
class _PluginCallbacks(object):
|
||||||
def __init__(self, plugins):
|
def __init__(self, plugins):
|
||||||
plugins = list(plugins)
|
self._plugins = list(plugins)
|
||||||
self.execute = self._wrap(plugins, 'execute')
|
|
||||||
|
|
||||||
def _wrap(self, plugins, name):
|
def decorate(self, name, callback):
|
||||||
if not plugins:
|
for plugin in reversed(self._plugins):
|
||||||
def default_callback(callback, *args, **kwargs):
|
# Need to reverse so the first plugin is run first.
|
||||||
return callback(*args, **kwargs)
|
callback = getattr(plugin, name)(callback)
|
||||||
|
return callback
|
||||||
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()
|
plugin_manager = _PluginManager([
|
||||||
|
StdlibPlugin,
|
||||||
|
])
|
||||||
|
|||||||
@@ -6,5 +6,8 @@ class BasePlugin(object):
|
|||||||
# In __init__ you can do some caching.
|
# In __init__ you can do some caching.
|
||||||
self._evaluator = evaluator
|
self._evaluator = evaluator
|
||||||
|
|
||||||
def execute(self, default_callback, arguments):
|
def execute(self, callback):
|
||||||
return default_callback(arguments)
|
"""
|
||||||
|
Decorates the execute(context, arguments) function.
|
||||||
|
"""
|
||||||
|
return callback
|
||||||
|
|||||||
18
jedi/plugins/stdlib.py
Normal file
18
jedi/plugins/stdlib.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
from jedi import debug
|
||||||
|
from jedi.plugins.base import BasePlugin
|
||||||
|
|
||||||
|
|
||||||
|
class StdlibPlugin(BasePlugin):
|
||||||
|
def execute(self, callback):
|
||||||
|
def wrapper(context, arguments):
|
||||||
|
debug.dbg('execute: %s %s', context, arguments)
|
||||||
|
from jedi.evaluate import stdlib
|
||||||
|
try:
|
||||||
|
# Some stdlib functions like super(), namedtuple(), etc. have been
|
||||||
|
# hard-coded in Jedi to support them.
|
||||||
|
return stdlib.execute(self._evaluator, context, arguments)
|
||||||
|
except stdlib.NotInStdLib:
|
||||||
|
pass
|
||||||
|
return callback(context, arguments)
|
||||||
|
|
||||||
|
return wrapper
|
||||||
Reference in New Issue
Block a user