def cmdline_tab_complete(w, size, key):
    try:
        from jedi import Interpreter
    except ImportError:
        self.add_cmdline_content('Tab completion requires jedi to be installed. ', 'command line error')
        return
    import jedi
    from distutils.version import LooseVersion
    if (LooseVersion(jedi.__version__) < LooseVersion('0.16.0')):
        self.add_cmdline_content('jedi 0.16.0 is required for Tab completion', 'command line error')
    text = self.cmdline_edit.edit_text
    pos = self.cmdline_edit.edit_pos
    chopped_text = text[:pos]
    suffix = text[pos:]
    try:
        completions = Interpreter(chopped_text, [cmdline_get_namespace()]).complete()
    except Exception as e:
        self.add_cmdline_content(("Could not tab complete (Jedi error: '%s')" % e), 'command line error')
        return
    full_completions = [i.name_with_symbols for i in completions]
    chopped_completions = [i.complete for i in completions]

    def common_prefix(a, b):
        for (i, (a_i, b_i)) in enumerate(zip(a, b)):
            if (a_i != b_i):
                return a[:i]
        return a[:max(len(a), len(b))]
    common_compl_prefix = None
    for completion in chopped_completions:
        if (common_compl_prefix is None):
            common_compl_prefix = completion
        else:
            common_compl_prefix = common_prefix(common_compl_prefix, completion)
    completed_chopped_text = common_compl_prefix
    if (completed_chopped_text is None):
        return
    if ((len(completed_chopped_text) == 0) and (len(completions) > 1)):
        self.add_cmdline_content('   '.join(full_completions), 'command line output')
        return
    self.cmdline_edit.edit_text = ((chopped_text + completed_chopped_text) + suffix)
    self.cmdline_edit.edit_pos = (len(chopped_text) + len(completed_chopped_text))
def cmdline_tab_complete(w, size, key):
    try:
        from jedi import Interpreter
    except ImportError:
        self.add_cmdline_content('Tab completion requires jedi to be installed. ', 'command line error')
        return
    import jedi
    from distutils.version import LooseVersion
    if (LooseVersion(jedi.__version__) < LooseVersion('0.16.0')):
        self.add_cmdline_content('jedi 0.16.0 is required for Tab completion', 'command line error')
    text = self.cmdline_edit.edit_text
    pos = self.cmdline_edit.edit_pos
    chopped_text = text[:pos]
    suffix = text[pos:]
    try:
        completions = Interpreter(chopped_text, [cmdline_get_namespace()]).complete()
    except Exception as e:
        self.add_cmdline_content(("Could not tab complete (Jedi error: '%s')" % e), 'command line error')
        return
    full_completions = [i.name_with_symbols for i in completions]
    chopped_completions = [i.complete for i in completions]

    def common_prefix(a, b):
        for (i, (a_i, b_i)) in enumerate(zip(a, b)):
            if (a_i != b_i):
                return a[:i]
        return a[:max(len(a), len(b))]
    common_compl_prefix = None
    for completion in chopped_completions:
        if (common_compl_prefix is None):
            common_compl_prefix = completion
        else:
            common_compl_prefix = common_prefix(common_compl_prefix, completion)
    completed_chopped_text = common_compl_prefix
    if (completed_chopped_text is None):
        return
    if ((len(completed_chopped_text) == 0) and (len(completions) > 1)):
        self.add_cmdline_content('   '.join(full_completions), 'command line output')
        return
    self.cmdline_edit.edit_text = ((chopped_text + completed_chopped_text) + suffix)
    self.cmdline_edit.edit_pos = (len(chopped_text) + len(completed_chopped_text))