diff --git a/jedi/api_classes.py b/jedi/api_classes.py index 177ebd51..58f7037a 100644 --- a/jedi/api_classes.py +++ b/jedi/api_classes.py @@ -15,7 +15,22 @@ class BaseOutput(object): self.definition = definition self.is_keyword = isinstance(definition, keywords.Keyword) + # generate the type + self.stripped_definition = self.definition + if isinstance(self.definition, evaluate.InstanceElement): + self.stripped_definition = self.definition.var + self.type = type(self.stripped_definition).__name__ + + # generate a path to the definition self.module_path = str(definition.get_parent_until().path) + self.path = [] + par = definition + while par is not None: + if not isinstance(self.stripped_definition, + (parsing.Flow, parsing.Statement, parsing.Import, + evaluate.Array, parsing.Name)): + self.path.insert(0, par.name) + par = par.parent() @property def module_name(self): @@ -51,16 +66,6 @@ class BaseOutput(object): 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 def description(self): raise NotImplementedError('Base Class') @@ -90,9 +95,8 @@ class Completion(BaseOutput): """ dot = '.' if self.needs_dot else '' append = '' - funcs = (parsing.Function, evaluate.Function) if settings.add_bracket_after_function \ - and self.definition.isinstance(funcs): + and self.type == 'Function': append = '(' if settings.add_dot_after_module: @@ -119,12 +123,13 @@ class Completion(BaseOutput): if parent is None: return '' t = self.type - desc = self.definition.get_code(False) if t == 'Statement' \ - else str(self.name.names[-1]) + if t == 'Statement': + desc = self.definition.get_code(False) + else: + desc = '.'.join(str(p) for p in self.path) + line_nr = '' if self.in_builtin_module else '@%s' % self.line_nr - temp = '%s: %s%s' % (t, desc, line_nr) - print temp - return temp + return '%s: %s%s' % (t, desc, line_nr) def __repr__(self): return '<%s: %s>' % (type(self).__name__, self.name) diff --git a/jedi/parsing.py b/jedi/parsing.py index 6a365baf..570643bc 100644 --- a/jedi/parsing.py +++ b/jedi/parsing.py @@ -249,12 +249,12 @@ class Scope(Simple): def __repr__(self): try: - name = self.name + name = self.path except AttributeError: try: - name = self.command + name = self.name except AttributeError: - name = self.path + name = self.command return "<%s: %s@%s-%s>" % (type(self).__name__, name, self.start_pos[0], self.end_pos[0]) @@ -289,16 +289,22 @@ class Module(Scope): n += self.global_vars return n - def get_name(self): + @property + def name(self): """ This is used for the goto function. """ - sep = (os.path.sep,) * 2 - r = re.search(r'([^%s]+?)(%s__init__)?(\.py)?$' % sep, self.path) - string = r.group(1) + if self._name is not None: + return self._name + if self.path is None: + string = '' # no path -> empty name + else: + sep = (os.path.sep,) * 2 + r = re.search(r'([^%s]+?)(%s__init__)?(\.py)?$' % sep, self.path) + string = r.group(1) names = [(string, (0, 0))] - if not self._name: - self._name = Name(names, self.start_pos, self.end_pos, self) + self._name = Name(names, self.start_pos, self.end_pos, self) return self._name + def is_builtin(self): return not (self.path is None or self.path.endswith('.py'))