forked from VimPlug/jedi
restructured api_classes, use the same base class
This commit is contained in:
@@ -10,10 +10,12 @@ import keywords
|
|||||||
|
|
||||||
|
|
||||||
class BaseOutput(object):
|
class BaseOutput(object):
|
||||||
def __init__(self, start_pos, definition):
|
def __init__(self, definition, start_pos):
|
||||||
self.module_path = str(definition.get_parent_until().path)
|
|
||||||
self.start_pos = start_pos
|
self.start_pos = start_pos
|
||||||
self.definition = definition
|
self.definition = definition
|
||||||
|
self.is_keyword = isinstance(definition, keywords.Keyword)
|
||||||
|
|
||||||
|
self.module_path = str(definition.get_parent_until().path)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def module_name(self):
|
def module_name(self):
|
||||||
@@ -33,6 +35,32 @@ class BaseOutput(object):
|
|||||||
def column(self):
|
def column(self):
|
||||||
return self.start_pos[1]
|
return self.start_pos[1]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def doc(self):
|
||||||
|
""" Return a document string for this completion object. """
|
||||||
|
try:
|
||||||
|
return self.definition.doc
|
||||||
|
except AttributeError:
|
||||||
|
return self.raw_doc
|
||||||
|
|
||||||
|
@property
|
||||||
|
def raw_doc(self):
|
||||||
|
""" Returns the raw docstring `__doc__` for any object """
|
||||||
|
try:
|
||||||
|
return str(self.definition.docstr)
|
||||||
|
except AttributeError:
|
||||||
|
return ''
|
||||||
|
|
||||||
|
@property
|
||||||
|
def type(self):
|
||||||
|
""" Returns the type of a completion object (e.g. 'Module'/'Class') """
|
||||||
|
if self.name.parent is None:
|
||||||
|
return ''
|
||||||
|
name_type = self.definition
|
||||||
|
if isinstance(name_type, evaluate.InstanceElement):
|
||||||
|
name_type = name_type.var
|
||||||
|
return type(name_type).__name__
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def description(self):
|
def description(self):
|
||||||
raise NotImplementedError('Base Class')
|
raise NotImplementedError('Base Class')
|
||||||
@@ -41,15 +69,17 @@ class BaseOutput(object):
|
|||||||
return "<%s %s>" % (type(self).__name__, self.description)
|
return "<%s %s>" % (type(self).__name__, self.description)
|
||||||
|
|
||||||
|
|
||||||
class Completion(object):
|
class Completion(BaseOutput):
|
||||||
""" `Completion` objects are returned from `Script.complete`. Providing
|
""" `Completion` objects are returned from `Script.complete`. Providing
|
||||||
some useful functions for IDE's. """
|
some useful functions for IDE's. """
|
||||||
def __init__(self, name, needs_dot, like_name_length, base):
|
def __init__(self, name, needs_dot, like_name_length, base):
|
||||||
|
super(Completion, self).__init__(name, name.start_pos)
|
||||||
|
|
||||||
self.name = name
|
self.name = name
|
||||||
self.needs_dot = needs_dot
|
self.needs_dot = needs_dot
|
||||||
self.like_name_length = like_name_length
|
self.like_name_length = like_name_length
|
||||||
self._completion_parent = name.parent() # limit gc
|
|
||||||
self.base = base
|
self.base = base
|
||||||
|
self._parent = name.parent()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def complete(self):
|
def complete(self):
|
||||||
@@ -63,7 +93,7 @@ class Completion(object):
|
|||||||
append = ''
|
append = ''
|
||||||
funcs = (parsing.Function, evaluate.Function)
|
funcs = (parsing.Function, evaluate.Function)
|
||||||
if settings.add_bracket_after_function \
|
if settings.add_bracket_after_function \
|
||||||
and self._completion_parent.isinstance(funcs):
|
and self._parent.isinstance(funcs):
|
||||||
append = '('
|
append = '('
|
||||||
|
|
||||||
if settings.add_dot_after_module:
|
if settings.add_dot_after_module:
|
||||||
@@ -89,32 +119,6 @@ class Completion(object):
|
|||||||
parent = self.name.parent()
|
parent = self.name.parent()
|
||||||
return '' if parent is None else str(parent)
|
return '' if parent is None else str(parent)
|
||||||
|
|
||||||
@property
|
|
||||||
def doc(self):
|
|
||||||
""" Return a document string for this completion object. """
|
|
||||||
try:
|
|
||||||
return self.name.parent().doc
|
|
||||||
except AttributeError:
|
|
||||||
return self.raw_doc
|
|
||||||
|
|
||||||
@property
|
|
||||||
def raw_doc(self):
|
|
||||||
""" Returns the docstring `__doc__` for any object """
|
|
||||||
try:
|
|
||||||
return str(self.name.parent().docstr)
|
|
||||||
except AttributeError :
|
|
||||||
return ''
|
|
||||||
|
|
||||||
@property
|
|
||||||
def type(self):
|
|
||||||
""" Returns the type of a completion object (e.g. Function/Class) """
|
|
||||||
if self.name.parent is None:
|
|
||||||
return ''
|
|
||||||
name_type = self.name.parent()
|
|
||||||
if isinstance(self.name_type, evaluate.InstanceElement):
|
|
||||||
name_type = name_type.var
|
|
||||||
return type(self.name_var).__class__
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<%s: %s>' % (type(self).__name__, self.name)
|
return '<%s: %s>' % (type(self).__name__, self.name)
|
||||||
|
|
||||||
@@ -123,7 +127,7 @@ class Definition(BaseOutput):
|
|||||||
""" These are the objects returned by either `Script.goto` or
|
""" These are the objects returned by either `Script.goto` or
|
||||||
`Script.get_definition`. """
|
`Script.get_definition`. """
|
||||||
def __init__(self, definition):
|
def __init__(self, definition):
|
||||||
super(Definition, self).__init__(definition.start_pos, definition)
|
super(Definition, self).__init__(definition, definition.start_pos)
|
||||||
self._def_parent = definition.parent() # just here to limit gc
|
self._def_parent = definition.parent() # just here to limit gc
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -145,28 +149,12 @@ class Definition(BaseOutput):
|
|||||||
elif isinstance(d, evaluate.parsing.Module):
|
elif isinstance(d, evaluate.parsing.Module):
|
||||||
# only show module name
|
# only show module name
|
||||||
d = 'module %s' % self.module_name
|
d = 'module %s' % self.module_name
|
||||||
elif isinstance(d, keywords.Keyword):
|
elif self.is_keyword:
|
||||||
d = 'keyword %s' % d.name
|
d = 'keyword %s' % d.name
|
||||||
else:
|
else:
|
||||||
d = d.get_code().replace('\n', '')
|
d = d.get_code().replace('\n', '')
|
||||||
return d
|
return d
|
||||||
|
|
||||||
@property
|
|
||||||
def doc(self):
|
|
||||||
""" Returns the docstr, behaves like `Completion.doc`. """
|
|
||||||
try:
|
|
||||||
return self.definition.doc
|
|
||||||
except AttributeError:
|
|
||||||
return self.raw_doc
|
|
||||||
|
|
||||||
@property
|
|
||||||
def raw_doc(self):
|
|
||||||
""" Returns the docstring `__doc__` for any object """
|
|
||||||
try:
|
|
||||||
return str(self.definition.docstr)
|
|
||||||
except AttributeError:
|
|
||||||
return ''
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def desc_with_module(self):
|
def desc_with_module(self):
|
||||||
""" In addition to the Definition, it also returns the module. Don't
|
""" In addition to the Definition, it also returns the module. Don't
|
||||||
@@ -182,6 +170,25 @@ class Definition(BaseOutput):
|
|||||||
return "%s:%s%s" % (self.module_name, self.description, position)
|
return "%s:%s%s" % (self.module_name, self.description, position)
|
||||||
|
|
||||||
|
|
||||||
|
class RelatedName(BaseOutput):
|
||||||
|
def __init__(self, name_part, scope):
|
||||||
|
super(RelatedName, self).__init__(scope, name_part.start_pos)
|
||||||
|
self.name_part = name_part
|
||||||
|
self.text = str(name_part)
|
||||||
|
self.end_pos = name_part.end_pos
|
||||||
|
|
||||||
|
@property
|
||||||
|
def description(self):
|
||||||
|
return "%s@%s,%s" % (self.text, self.start_pos[0], self.start_pos[1])
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
return self.start_pos == other.start_pos \
|
||||||
|
and self.module_path == other.module_path
|
||||||
|
|
||||||
|
def __hash__(self):
|
||||||
|
return hash((self.start_pos, self.module_path))
|
||||||
|
|
||||||
|
|
||||||
class CallDef(object):
|
class CallDef(object):
|
||||||
""" `CallDef` objects is the return value of `Script.get_in_function_call`.
|
""" `CallDef` objects is the return value of `Script.get_in_function_call`.
|
||||||
It knows what functions you are currently in. e.g. `isinstance(` would
|
It knows what functions you are currently in. e.g. `isinstance(` would
|
||||||
@@ -225,21 +232,3 @@ class CallDef(object):
|
|||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<%s: %s index %s>' % (type(self).__name__, self.executable,
|
return '<%s: %s index %s>' % (type(self).__name__, self.executable,
|
||||||
self.index)
|
self.index)
|
||||||
|
|
||||||
|
|
||||||
class RelatedName(BaseOutput):
|
|
||||||
def __init__(self, name_part, scope):
|
|
||||||
super(RelatedName, self).__init__(name_part.start_pos, scope)
|
|
||||||
self.text = str(name_part)
|
|
||||||
self.end_pos = name_part.end_pos
|
|
||||||
|
|
||||||
@property
|
|
||||||
def description(self):
|
|
||||||
return "%s@%s,%s" % (self.text, self.start_pos[0], self.start_pos[1])
|
|
||||||
|
|
||||||
def __eq__(self, other):
|
|
||||||
return self.start_pos == other.start_pos \
|
|
||||||
and self.module_path == other.module_path
|
|
||||||
|
|
||||||
def __hash__(self):
|
|
||||||
return hash((self.start_pos, self.module_path))
|
|
||||||
|
|||||||
@@ -894,6 +894,9 @@ class ArrayElement(object):
|
|||||||
raise AttributeError('Strange access: %s.' % name)
|
raise AttributeError('Strange access: %s.' % name)
|
||||||
return getattr(self.name, name)
|
return getattr(self.name, name)
|
||||||
|
|
||||||
|
def get_parent_until(self):
|
||||||
|
return builtin.Builtin.scope
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<%s of %s>" % (type(self).__name__, self.name)
|
return "<%s of %s>" % (type(self).__name__, self.name)
|
||||||
|
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ class ImportPath(object):
|
|||||||
# This is not an existing Import statement. Therefore, set position to
|
# This is not an existing Import statement. Therefore, set position to
|
||||||
# None.
|
# None.
|
||||||
zero = (None, None)
|
zero = (None, None)
|
||||||
n = parsing.Name(i.namespace.names[1:], zero, zero)
|
n = parsing.Name(i.namespace.names[1:], zero, zero, self.import_stmt)
|
||||||
new = parsing.Import(zero, zero, n)
|
new = parsing.Import(zero, zero, n)
|
||||||
new.parent = weakref.ref(parent)
|
new.parent = weakref.ref(parent)
|
||||||
evaluate.faked_scopes.append(new)
|
evaluate.faked_scopes.append(new)
|
||||||
@@ -125,7 +125,8 @@ class ImportPath(object):
|
|||||||
names = []
|
names = []
|
||||||
for module_loader, name, is_pkg in pkgutil.iter_modules(search_path):
|
for module_loader, name, is_pkg in pkgutil.iter_modules(search_path):
|
||||||
inf_pos = (float('inf'), float('inf'))
|
inf_pos = (float('inf'), float('inf'))
|
||||||
names.append(parsing.Name([(name, inf_pos)], inf_pos, inf_pos))
|
names.append(parsing.Name([(name, inf_pos)], inf_pos, inf_pos,
|
||||||
|
self.import_stmt))
|
||||||
return names
|
return names
|
||||||
|
|
||||||
def sys_path_with_modifications(self):
|
def sys_path_with_modifications(self):
|
||||||
|
|||||||
@@ -35,12 +35,13 @@ def get_operator(string, pos):
|
|||||||
class Keyword(object):
|
class Keyword(object):
|
||||||
def __init__(self, name, pos):
|
def __init__(self, name, pos):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.parent = lambda: None
|
|
||||||
self.start_pos = pos
|
self.start_pos = pos
|
||||||
|
|
||||||
def get_parent_until(self):
|
def parent(self):
|
||||||
return builtin.Builtin.scope
|
return builtin.Builtin.scope
|
||||||
|
|
||||||
|
get_parent_until = parent
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def names(self):
|
def names(self):
|
||||||
""" For a `parsing.Name` like comparision """
|
""" For a `parsing.Name` like comparision """
|
||||||
|
|||||||
Reference in New Issue
Block a user