mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-19 20:11:12 +08:00
Fix some internal name handling.
This commit is contained in:
@@ -143,7 +143,7 @@ class Script(object):
|
|||||||
@cache.memoize_method
|
@cache.memoize_method
|
||||||
def _get_module(self):
|
def _get_module(self):
|
||||||
module = er.ModuleContext(self._evaluator, self._get_module_node())
|
module = er.ModuleContext(self._evaluator, self._get_module_node())
|
||||||
imports.add_module(self._evaluator, unicode(module.name), module)
|
imports.add_module(self._evaluator, module.name.string_name, module)
|
||||||
return module
|
return module
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -437,17 +437,18 @@ def names(source=None, path=None, encoding='utf-8', all_scopes=False,
|
|||||||
is_def = _def._name.tree_name.is_definition()
|
is_def = _def._name.tree_name.is_definition()
|
||||||
return definitions and is_def or references and not is_def
|
return definitions and is_def or references and not is_def
|
||||||
|
|
||||||
|
def get_definitions():
|
||||||
|
for tree_name in get_module_names(script._get_module_node(), all_scopes):
|
||||||
|
name = TreeNameDefinition(
|
||||||
|
module_context.create_context(tree_name),
|
||||||
|
tree_name
|
||||||
|
)
|
||||||
|
yield classes.Definition(script._evaluator, name)
|
||||||
|
|
||||||
# Set line/column to a random position, because they don't matter.
|
# Set line/column to a random position, because they don't matter.
|
||||||
script = Script(source, line=1, column=0, path=path, encoding=encoding)
|
script = Script(source, line=1, column=0, path=path, encoding=encoding)
|
||||||
module_context = script._get_module()
|
module_context = script._get_module()
|
||||||
defs = [
|
defs = [
|
||||||
classes.Definition(
|
|
||||||
script._evaluator,
|
|
||||||
TreeNameDefinition(
|
|
||||||
module_context.create_context(name),
|
|
||||||
name
|
|
||||||
)
|
|
||||||
) for name in get_module_names(script._get_module_node(), all_scopes)
|
|
||||||
]
|
]
|
||||||
return sorted(filter(def_ref_filter, defs), key=lambda x: (x.line, x.column))
|
return sorted(filter(def_ref_filter, defs), key=lambda x: (x.line, x.column))
|
||||||
|
|
||||||
|
|||||||
@@ -164,28 +164,31 @@ class BaseDefinition(object):
|
|||||||
|
|
||||||
def _path(self):
|
def _path(self):
|
||||||
"""The path to a module/class/function definition."""
|
"""The path to a module/class/function definition."""
|
||||||
path = []
|
def to_reverse():
|
||||||
par = self._definition
|
name = self._name
|
||||||
while par is not None:
|
if name.api_type == 'module':
|
||||||
if isinstance(par, tree.Import):
|
try:
|
||||||
# Not self._name.infer()?
|
name = list(name.infer())[0].name
|
||||||
raise DeprecationWarning
|
except IndexError:
|
||||||
path += imports.ImportWrapper(self._name.context, self._name).import_path
|
pass
|
||||||
break
|
yield name.string_name
|
||||||
try:
|
name.api_type
|
||||||
name = par.name
|
parent_context = name.parent_context
|
||||||
except AttributeError:
|
print(parent_context)
|
||||||
pass
|
while parent_context is not None:
|
||||||
else:
|
try:
|
||||||
if isinstance(par, er.ModuleWrapper):
|
method = parent_context.py__name__
|
||||||
# TODO just make the path dotted from the beginning, we
|
except AttributeError:
|
||||||
# shouldn't really split here.
|
try:
|
||||||
path[0:0] = par.py__name__().split('.')
|
yield parent_context.name.string_name
|
||||||
break
|
except AttributeError:
|
||||||
|
pass
|
||||||
else:
|
else:
|
||||||
path.insert(0, unicode(name))
|
# TODO this main clause seems strange.
|
||||||
par = par.parent
|
for name in (method() or '__main__').split('.'):
|
||||||
return path
|
yield name
|
||||||
|
parent_context = parent_context.parent_context
|
||||||
|
return reversed(list(to_reverse()))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def module_name(self):
|
def module_name(self):
|
||||||
@@ -299,11 +302,12 @@ class BaseDefinition(object):
|
|||||||
>>> print(script.goto_definitions()[0].full_name)
|
>>> print(script.goto_definitions()[0].full_name)
|
||||||
os.path.join
|
os.path.join
|
||||||
|
|
||||||
Notice that it correctly returns ``'os.path.join'`` instead of
|
Notice that it returns ``'os.path.join'`` instead of (for example)
|
||||||
(for example) ``'posixpath.join'``.
|
``'posixpath.join'``. This is not correct, since the modules name would
|
||||||
|
be ``<module 'posixpath' ...>```. However most users find the latter
|
||||||
|
more practical.
|
||||||
"""
|
"""
|
||||||
path = [unicode(p) for p in self._path()]
|
path = list(self._path())
|
||||||
# TODO add further checks, the mapping should only occur on stdlib.
|
# TODO add further checks, the mapping should only occur on stdlib.
|
||||||
if not path:
|
if not path:
|
||||||
return None # for keywords the path is empty
|
return None # for keywords the path is empty
|
||||||
@@ -476,7 +480,7 @@ class Completion(BaseDefinition):
|
|||||||
if t == 'statement' or t == 'import':
|
if t == 'statement' or t == 'import':
|
||||||
desc = self._definition.get_code()
|
desc = self._definition.get_code()
|
||||||
else:
|
else:
|
||||||
desc = '.'.join(unicode(p) for p in self._path())
|
desc = '.'.join(self._path())
|
||||||
|
|
||||||
line = '' if self.in_builtin_module else '@%s' % self.line
|
line = '' if self.in_builtin_module else '@%s' % self.line
|
||||||
return '%s: %s%s' % (t, desc, line)
|
return '%s: %s%s' % (t, desc, line)
|
||||||
|
|||||||
@@ -12,7 +12,8 @@ from jedi import debug
|
|||||||
from jedi.cache import underscore_memoization, memoize_method
|
from jedi.cache import underscore_memoization, memoize_method
|
||||||
from jedi.parser.tree import Param, Operator
|
from jedi.parser.tree import Param, Operator
|
||||||
from jedi.evaluate.helpers import FakeName
|
from jedi.evaluate.helpers import FakeName
|
||||||
from jedi.evaluate.filters import AbstractFilter, AbstractNameDefinition
|
from jedi.evaluate.filters import AbstractFilter, AbstractNameDefinition, \
|
||||||
|
ContextNameMixin
|
||||||
from jedi.evaluate.context import Context, LazyKnownContext
|
from jedi.evaluate.context import Context, LazyKnownContext
|
||||||
from . import fake
|
from . import fake
|
||||||
|
|
||||||
@@ -300,13 +301,11 @@ class UnresolvableParamName(AbstractNameDefinition):
|
|||||||
return set()
|
return set()
|
||||||
|
|
||||||
|
|
||||||
class CompiledContextName(AbstractNameDefinition):
|
class CompiledContextName(ContextNameMixin, AbstractNameDefinition):
|
||||||
def __init__(self, parent_context, name):
|
def __init__(self, context, name):
|
||||||
self.string_name = name
|
self.string_name = name
|
||||||
self.parent_context = parent_context
|
self._context = context
|
||||||
|
self.parent_context = context.parent_context
|
||||||
def infer(self):
|
|
||||||
return [self.parent_context]
|
|
||||||
|
|
||||||
|
|
||||||
class LazyNamesDict(object):
|
class LazyNamesDict(object):
|
||||||
|
|||||||
@@ -52,24 +52,26 @@ class AbstractTreeName(AbstractNameDefinition):
|
|||||||
return self.tree_name.start_pos
|
return self.tree_name.start_pos
|
||||||
|
|
||||||
|
|
||||||
class ContextName(AbstractTreeName):
|
class ContextNameMixin(object):
|
||||||
def __init__(self, context, tree_name):
|
|
||||||
super(ContextName, self).__init__(context.parent_context, tree_name)
|
|
||||||
self._context = context
|
|
||||||
|
|
||||||
def infer(self):
|
def infer(self):
|
||||||
return [self._context]
|
return set([self._context])
|
||||||
|
|
||||||
def get_root_context(self):
|
def get_root_context(self):
|
||||||
if self.parent_context is None:
|
if self.parent_context is None:
|
||||||
return self._context
|
return self._context
|
||||||
return super(ContextName, self).get_root_context()
|
return super(ContextNameMixin, self).get_root_context()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def api_type(self):
|
def api_type(self):
|
||||||
return self._context.api_type
|
return self._context.api_type
|
||||||
|
|
||||||
|
|
||||||
|
class ContextName(ContextNameMixin, AbstractTreeName):
|
||||||
|
def __init__(self, context, tree_name):
|
||||||
|
super(ContextName, self).__init__(context.parent_context, tree_name)
|
||||||
|
self._context = context
|
||||||
|
|
||||||
|
|
||||||
class TreeNameDefinition(AbstractTreeName):
|
class TreeNameDefinition(AbstractTreeName):
|
||||||
def get_parent_flow_context(self):
|
def get_parent_flow_context(self):
|
||||||
return self.parent_context
|
return self.parent_context
|
||||||
|
|||||||
@@ -216,7 +216,7 @@ class ClassContext(use_metaclass(CachedMetaClass, context.TreeContext)):
|
|||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
return method()[:-1]
|
return list(method())[1:]
|
||||||
return []
|
return []
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ class TestFullDefinedName(TestCase):
|
|||||||
self.check("""
|
self.check("""
|
||||||
def f(): pass
|
def f(): pass
|
||||||
class C: pass
|
class C: pass
|
||||||
""", ['f', 'C'])
|
""", ['__main__.f', '__main__.C'])
|
||||||
|
|
||||||
def test_imports(self):
|
def test_imports(self):
|
||||||
self.check("""
|
self.check("""
|
||||||
|
|||||||
Reference in New Issue
Block a user