Beispiel #1
0
    def _simple_complete(self, path, like):
        try:
            scopes = list(self._prepare_goto(path, True))
        except NotFoundError:
            scopes = []
            scope_names_generator = get_names_of_scope(
                self._evaluator, self._parser.user_scope(), self._pos)
            completions = []
            for scope, name_list in scope_names_generator:
                for c in name_list:
                    completions.append((c, scope))
        else:
            completions = []
            debug.dbg('possible completion scopes: %s', scopes)
            for s in scopes:
                if s.isinstance(er.Function):
                    names = s.get_magic_function_names()
                elif isinstance(s, imports.ImportWrapper):
                    under = like + self._user_context.get_path_after_cursor()
                    if under == 'import':
                        current_line = self._user_context.get_position_line()
                        if not current_line.endswith('import import'):
                            continue
                    a = s.import_stmt.alias
                    if a and a.start_pos <= self._pos <= a.end_pos:
                        continue
                    names = s.get_defined_names(on_import_stmt=True)
                else:
                    names = []
                    for _, new_names in s.scope_names_generator():
                        names += new_names

                for c in names:
                    completions.append((c, s))
        return completions
Beispiel #2
0
    def _simple_complete(self, path, like):
        try:
            scopes = list(self._prepare_goto(path, True))
        except NotFoundError:
            scopes = []
            scope_generator = get_names_of_scope(self._evaluator,
                                                 self._parser.user_scope(),
                                                 self._pos)
            completions = []
            for scope, name_list in scope_generator:
                for c in name_list:
                    completions.append((c, scope))
        else:
            completions = []
            debug.dbg('possible completion scopes: %s', scopes)
            for s in scopes:
                if s.isinstance(er.Function):
                    names = s.get_magic_function_names()
                else:
                    if isinstance(s, imports.ImportPath):
                        under = like + self._user_context.get_path_after_cursor()
                        if under == 'import':
                            current_line = self._user_context.get_position_line()
                            if not current_line.endswith('import import'):
                                continue
                        a = s.import_stmt.alias
                        if a and a.start_pos <= self._pos <= a.end_pos:
                            continue
                        names = s.get_defined_names(on_import_stmt=True)
                    else:
                        names = s.get_defined_names()

                for c in names:
                    completions.append((c, s))
        return completions
Beispiel #3
0
def defined_names(evaluator, scope):
    """
    List sub-definitions (e.g., methods in class).

    :type scope: Scope
    :rtype: list of Definition
    """
    pair = next(get_names_of_scope(evaluator, scope, star_search=False,
                                   include_builtin=False), None)
    names = pair[1] if pair else []
    return [Definition(evaluator, d) for d in sorted(names, key=lambda s: s.start_pos)]
Beispiel #4
0
    def get_defined_names(self, on_import_stmt=False):
        names = []
        for scope in self.follow():
            if scope is ImportPath.GlobalNamespace:
                if self._is_relative_import() == 0:
                    names += self._get_module_names()

                if self.file_path is not None:
                    path = os.path.abspath(self.file_path)
                    for i in range(self.import_stmt.relative_count - 1):
                        path = os.path.dirname(path)
                    names += self._get_module_names([path])

                    if self._is_relative_import():
                        rel_path = self._get_relative_path() + '/__init__.py'
                        if os.path.exists(rel_path):
                            m = load_module(rel_path)
                            names += m.get_defined_names()
            else:
                if on_import_stmt and isinstance(scope, pr.Module) \
                        and scope.path.endswith('__init__.py'):
                    pkg_path = os.path.dirname(scope.path)
                    paths = self._namespace_packages(pkg_path,
                                                     self.import_path)
                    names += self._get_module_names([pkg_path] + paths)
                if self.is_just_from:
                    # In the case of an import like `from x.` we don't need to
                    # add all the variables.
                    if [
                            'os'
                    ] == self.import_path and not self._is_relative_import():
                        # os.path is a hardcoded exception, because it's a
                        # ``sys.modules`` modification.
                        p = (0, 0)
                        names.append(
                            pr.Name(self.GlobalNamespace, [('path', p)], p, p,
                                    self.import_stmt))
                    continue
                from jedi.evaluate import finder
                for s, scope_names in finder.get_names_of_scope(
                        self._evaluator, scope, include_builtin=False):
                    for n in scope_names:
                        if self.import_stmt.from_ns is None \
                                or self.is_partial_import:
                            # from_ns must be defined to access module
                            # values plus a partial import means that there
                            # is something after the import, which
                            # automatically implies that there must not be
                            # any non-module scope.
                            continue
                        names.append(n)
        return names
Beispiel #5
0
    def get_defined_names(self, on_import_stmt=False):
        names = []
        for scope in self.follow():
            if scope is ImportPath.GlobalNamespace:
                if self._is_relative_import() == 0:
                    names += self._get_module_names()

                if self.file_path is not None:
                    path = os.path.abspath(self.file_path)
                    for i in range(self.import_stmt.relative_count - 1):
                        path = os.path.dirname(path)
                    names += self._get_module_names([path])

                    if self._is_relative_import():
                        rel_path = self._get_relative_path() + '/__init__.py'
                        if os.path.exists(rel_path):
                            m = load_module(rel_path)
                            names += m.get_defined_names()
            else:
                if on_import_stmt and isinstance(scope, pr.Module) \
                        and scope.path.endswith('__init__.py'):
                    pkg_path = os.path.dirname(scope.path)
                    paths = self._namespace_packages(pkg_path, self.import_path)
                    names += self._get_module_names([pkg_path] + paths)
                if self.is_just_from:
                    # In the case of an import like `from x.` we don't need to
                    # add all the variables.
                    if ['os'] == self.import_path and not self._is_relative_import():
                        # os.path is a hardcoded exception, because it's a
                        # ``sys.modules`` modification.
                        p = (0, 0)
                        names.append(pr.Name(self.GlobalNamespace, [('path', p)],
                                     p, p, self.import_stmt))
                    continue
                from jedi.evaluate import finder
                for s, scope_names in finder.get_names_of_scope(self._evaluator,
                                                                scope, include_builtin=False):
                    for n in scope_names:
                        if self.import_stmt.from_ns is None \
                                or self.is_partial_import:
                                # from_ns must be defined to access module
                                # values plus a partial import means that there
                                # is something after the import, which
                                # automatically implies that there must not be
                                # any non-module scope.
                                continue
                        names.append(n)
        return names
Beispiel #6
0
def defined_names(evaluator, scope):
    """
    List sub-definitions (e.g., methods in class).

    :type scope: Scope
    :rtype: list of Definition
    """
    # Calling get_names_of_scope doesn't make sense always. It might include
    # star imports or inherited stuff. Wanted?
    # TODO discuss!
    if isinstance(scope, pr.Module):
        pair = scope, scope.get_defined_names()
    else:
        pair = next(get_names_of_scope(evaluator, scope, star_search=False,
                                       include_builtin=False), None)
    names = pair[1] if pair else []
    return [Definition(evaluator, d) for d in sorted(names, key=lambda s: s.start_pos)]
Beispiel #7
0
def defined_names(evaluator, scope):
    """
    List sub-definitions (e.g., methods in class).

    :type scope: Scope
    :rtype: list of Definition
    """
    pair = next(
        get_names_of_scope(evaluator,
                           scope,
                           star_search=False,
                           include_builtin=False), None)
    names = pair[1] if pair else []
    names = [n for n in names if isinstance(n, pr.Import) or (len(n) == 1)]
    return [
        Definition(evaluator, d)
        for d in sorted(names, key=lambda s: s.start_pos)
    ]
Beispiel #8
0
def defined_names(evaluator, scope):
    """
    List sub-definitions (e.g., methods in class).

    :type scope: Scope
    :rtype: list of Definition
    """
    # Calling get_names_of_scope doesn't make sense always. It might include
    # star imports or inherited stuff. Wanted?
    # TODO discuss!
    if isinstance(scope, pr.Module):
        pair = scope, scope.get_defined_names()
    else:
        pair = next(get_names_of_scope(evaluator, scope, star_search=False,
                                       include_builtin=False), None)
    names = pair[1] if pair else []
    names = [n for n in names if isinstance(n, pr.Import) or (len(n) == 1)]
    return [Definition(evaluator, d) for d in sorted(names, key=lambda s: s.start_pos)]
Beispiel #9
0
    def get_defined_names(self, on_import_stmt=False):
        names = []
        for scope in self.follow():
            if scope is ImportWrapper.GlobalNamespace:
                if not self._is_relative_import():
                    names += self._get_module_names()

                if self._importer.file_path is not None:
                    path = os.path.abspath(self._importer.file_path)
                    for i in range(self.import_stmt.relative_count - 1):
                        path = os.path.dirname(path)
                    names += self._get_module_names([path])

                    if self._is_relative_import():
                        rel_path = os.path.join(self._importer.get_relative_path(),
                                                '__init__.py')
                        if os.path.exists(rel_path):
                            m = _load_module(rel_path)
                            names += m.get_defined_names()
            else:
                # flask
                if self.import_path == ('flask', 'ext'):
                    # List Flask extensions like ``flask_foo``
                    for mod in self._get_module_names():
                        modname = str(mod)
                        if modname.startswith('flask_'):
                            extname = modname[len('flask_'):]
                            names.append(self._generate_name(extname))
                    # Now the old style: ``flaskext.foo``
                    for dir in self._importer.sys_path_with_modifications():
                        flaskext = os.path.join(dir, 'flaskext')
                        if os.path.isdir(flaskext):
                            names += self._get_module_names([flaskext])

                # namespace packages
                if on_import_stmt and isinstance(scope, pr.Module) \
                        and scope.path.endswith('__init__.py'):
                    pkg_path = os.path.dirname(scope.path)
                    paths = self._importer.namespace_packages(pkg_path,
                                                              self.import_path)
                    names += self._get_module_names([pkg_path] + paths)
                if self.is_just_from:
                    # In the case of an import like `from x.` we don't need to
                    # add all the variables.
                    if ('os',) == self.import_path and not self._is_relative_import():
                        # os.path is a hardcoded exception, because it's a
                        # ``sys.modules`` modification.
                        names.append(self._generate_name('path'))
                    continue

                if not self.import_stmt.from_names or self.is_partial_import:
                        # from_names must be defined to access module
                        # values plus a partial import means that there
                        # is something after the import, which
                        # automatically implies that there must not be
                        # any non-module scope.
                        continue
                from jedi.evaluate import finder
                for s, scope_names in finder.get_names_of_scope(self._evaluator,
                                                                scope, include_builtin=False):
                    for n in scope_names:
                        names.append(n)
        return names