def complete(cursor_offset, line):
    """Construct a full list of possibly completions for imports."""
    tokens = line.split()
    if 'from' not in tokens and 'import' not in tokens:
        return None

    result = current_word(cursor_offset, line)
    if result is None:
        return None

    from_import_from = current_from_import_from(cursor_offset, line)
    if from_import_from is not None:
        import_import = current_from_import_import(cursor_offset, line)
        if import_import is not None:
            # `from a import <b|>` completion
            matches = module_matches(import_import[2], from_import_from[2])
            matches.update(attr_matches(import_import[2],
                                        from_import_from[2]))
        else:
            # `from <a|>` completion
            matches = module_attr_matches(from_import_from[2])
            matches.update(module_matches(from_import_from[2]))
        return matches

    cur_import = current_import(cursor_offset, line)
    if cur_import is not None:
        # `import <a|>` completion
        matches = module_matches(cur_import[2])
        matches.update(module_attr_matches(cur_import[2]))
        return matches
    else:
        return None
Exemple #2
0
def complete(cursor_offset, line):
    """Construct a full list of possibly completions for imports."""
    tokens = line.split()
    if 'from' not in tokens and 'import' not in tokens:
        return None

    result = current_word(cursor_offset, line)
    if result is None:
        return None

    from_import_from = current_from_import_from(cursor_offset, line)
    if from_import_from is not None:
        import_import = current_from_import_import(cursor_offset, line)
        if import_import is not None:
            # `from a import <b|>` completion
            matches = module_matches(import_import[2], from_import_from[2])
            matches.update(attr_matches(import_import[2], from_import_from[2]))
        else:
            # `from <a|>` completion
            matches = module_attr_matches(from_import_from[2])
            matches.update(module_matches(from_import_from[2]))
        return matches

    cur_import = current_import(cursor_offset, line)
    if cur_import is not None:
        # `import <a|>` completion
        matches = module_matches(cur_import[2])
        matches.update(module_attr_matches(cur_import[2]))
        return matches
    else:
        return None
Exemple #3
0
def complete(cursor_offset, line):
    """Construct a full list of possibly completions for imports."""
    tokens = line.split()
    if 'from' not in tokens and 'import' not in tokens:
        return None

    result = lineparts.current_word(cursor_offset, line)
    if result is None:
        return None

    if lineparts.current_from_import_from(cursor_offset, line) is not None:
        if lineparts.current_from_import_import(cursor_offset, line) is not None:
            # `from a import <b|>` completion
            return (module_matches(lineparts.current_from_import_import(cursor_offset, line)[2],
                                   lineparts.current_from_import_from(cursor_offset, line)[2]) +
                    attr_matches(lineparts.current_from_import_import(cursor_offset, line)[2],
                                 lineparts.current_from_import_from(cursor_offset, line)[2]))
        else:
            # `from <a|>` completion
            return (module_attr_matches(lineparts.current_from_import_from(cursor_offset, line)[2]) +
                    module_matches(lineparts.current_from_import_from(cursor_offset, line)[2]))
    elif lineparts.current_import(cursor_offset, line):
        # `import <a|>` completion
        return (module_matches(lineparts.current_import(cursor_offset, line)[2]) +
                module_attr_matches(lineparts.current_import(cursor_offset, line)[2]))
    else:
        return None
Exemple #4
0
        def matches(self, cursor_offset, line, **kwargs):
            if 'history' not in kwargs:
                return None
            history = kwargs['history']

            if not lineparts.current_word(cursor_offset, line):
                return None
            history = '\n'.join(history) + '\n' + line
            try:
                script = jedi.Script(history, len(history.splitlines()),
                                     cursor_offset, 'fake.py')
                completions = script.completions()
            except jedi.NotFoundError:
                self._orig_start = None
                return None
            if completions:
                diff = len(completions[0].name) - len(completions[0].complete)
                self._orig_start = cursor_offset - diff
            else:
                self._orig_start = None
                return None

            first_letter = line[self._orig_start:self._orig_start+1]

            matches = [c.name for c in completions]
            if any(not m.lower().startswith(matches[0][0].lower())
                   for m in matches):
                # Too general - giving completions starting with multiple
                # letters
                return None
            else:
                # case-sensitive matches only
                return set(m for m in matches if m.startswith(first_letter))
        def matches(self, cursor_offset, line, **kwargs):
            if 'history' not in kwargs:
                return None
            history = kwargs['history']

            if not lineparts.current_word(cursor_offset, line):
                return None
            history = '\n'.join(history) + '\n' + line
            try:
                script = jedi.Script(history, len(history.splitlines()),
                                     cursor_offset, 'fake.py')
                completions = script.completions()
            except jedi.NotFoundError:
                self._orig_start = None
                return None
            except IndexError:
                # for https://github.com/bpython/bpython/issues/483
                self._orig_start = None
                return None

            if completions:
                diff = len(completions[0].name) - len(completions[0].complete)
                self._orig_start = cursor_offset - diff
            else:
                self._orig_start = None
                return None

            first_letter = line[self._orig_start:self._orig_start+1]

            matches = [c.name for c in completions]
            if any(not m.lower().startswith(matches[0][0].lower())
                   for m in matches):
                # Too general - giving completions starting with multiple
                # letters
                return None
            else:
                # case-sensitive matches only
                return set(m for m in matches if m.startswith(first_letter))
Exemple #6
0
        def matches(self, cursor_offset, line, history, **kwargs):
            if not lineparts.current_word(cursor_offset, line):
                return None
            history = '\n'.join(history) + '\n' + line
            script = jedi.Script(history, len(history.splitlines()),
                                 cursor_offset, 'fake.py')
            completions = script.completions()
            if completions:
                self._orig_start = cursor_offset - (
                    len(completions[0].name) - len(completions[0].complete))
            else:
                self._orig_start = None
                return None

            first_letter = line[self._orig_start:self._orig_start + 1]

            matches = [c.name for c in completions]
            if any(not m.lower().startswith(matches[0][0].lower())
                   for m in matches):
                return None  # Too general - giving completions starting with multiple letters
            else:
                # case-sensitive matches only
                return set([m for m in matches if m.startswith(first_letter)])
Exemple #7
0
 def locate(self, current_offset, line):
     return lineparts.current_word(current_offset, line)
Exemple #8
0
 def locate(self, current_offset, line):
     return lineparts.current_word(current_offset, line)
Exemple #9
0
def complete(cursor_offset, line):
    """Construct a full list of possibly completions for imports."""
    # TODO if this is done in a thread (as it prob will be in Windows) we'll need this
    # if not fully_loaded:
    #     return []
    tokens = line.split()
    if 'from' not in tokens and 'import' not in tokens:
        return None

    result = lineparts.current_word(cursor_offset, line)
    if result is None:
        return None

    def module_matches(cw, prefix=''):
        """Modules names to replace cw with"""
        full = '%s.%s' % (prefix, cw) if prefix else cw
        matches = [
            name for name in modules
            if (name.startswith(full) and name.find('.', len(full)) == -1)
        ]
        if prefix:
            return [match[len(prefix) + 1:] for match in matches]
        else:
            return matches

    def attr_matches(cw, prefix='', only_modules=False):
        """Attributes to replace name with"""
        full = '%s.%s' % (prefix, cw) if prefix else cw
        module_name, _, name_after_dot = full.rpartition('.')
        if module_name not in sys.modules:
            return []
        module = sys.modules[module_name]
        if only_modules:
            matches = [
                name for name in dir(module)
                if name.startswith(name_after_dot) and '%s.%s' %
                (module_name, name) in sys.modules
            ]
        else:
            matches = [
                name for name in dir(module) if name.startswith(name_after_dot)
            ]
        module_part, _, _ = cw.rpartition('.')
        if module_part:
            return ['%s.%s' % (module_part, m) for m in matches]
        return matches

    def module_attr_matches(name):
        """Only attributes which are modules to replace name with"""
        return attr_matches(name, prefix='', only_modules=True)

    if lineparts.current_from_import_from(cursor_offset, line) is not None:
        if lineparts.current_from_import_import(cursor_offset,
                                                line) is not None:
            # `from a import <b|>` completion
            return (module_matches(
                lineparts.current_from_import_import(cursor_offset, line)[2],
                lineparts.current_from_import_from(cursor_offset, line)[2]) +
                    attr_matches(
                        lineparts.current_from_import_import(
                            cursor_offset, line)[2],
                        lineparts.current_from_import_from(
                            cursor_offset, line)[2]))
        else:
            # `from <a|>` completion
            return (module_attr_matches(
                lineparts.current_from_import_from(cursor_offset, line)[2]) +
                    module_matches(
                        lineparts.current_from_import_from(
                            cursor_offset, line)[2]))
    elif lineparts.current_import(cursor_offset, line):
        # `import <a|>` completion
        return (
            module_matches(lineparts.current_import(cursor_offset, line)[2]) +
            module_attr_matches(
                lineparts.current_import(cursor_offset, line)[2]))
    else:
        return None