emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Completion in python mode + IPython


From: Liu Hui
Subject: Re: Completion in python mode + IPython
Date: Tue, 14 Jan 2025 17:35:58 +0800

On Tue, Jan 14, 2025 at 5:06 AM Ergus <spacibba@aol.com> wrote:
>
> Hi Liu:
>
> I totally understand the point.
>
> Whats was surprising for me is that the non-default options works way
> better than the default one for users with Ipython.
>
> I tried already the links you share and sincerely the jedi completion is
> still way worst than the IPython one.
>
> So, for the moment I will just disable the native completion and rely in
> the legacy one.

The native completion has some advantages, e.g. it can work in
multi-line block, and it doesn't lead to discontinuous IPython In/Out
prompts. If you like the IPython completion produced by
'python-shell-completion-setup-code', you can replace the default
readline completer with a custom one.

For example, you may add the following code to the PYTHONSTARTUP file,
which should provide same IPython completions as the legacy one.

──────────────────✀──────────────────

def my_setup_IPythonRL():
    import readline, re, json

    def filter_c(prefix, c):
        if re.match('_+(i?[0-9]+)?$', c):
            return False
        elif c[0] == '%' and not re.match('[%a-zA-Z]+$', prefix):
            return False
        return True

    class IPythonRL:
        def complete(self, text, state):
            if state == 0:
                try:
                    from IPython.core.completer import provisionalcompleter
                    with provisionalcompleter():
                        self.matches = [json.dumps([
                            [c.text, c.start, c.end, c.type or '?',
c.signature or '']
                            for c in
get_ipython().Completer.completions(text, len(text))
                            if filter_c(text, c.text)])]
                except Exception:
                    pass
            try:
                return self.matches[state]
            except IndexError:
                return None

    readline.set_completer(IPythonRL().complete)
    readline.parse_and_bind("tab: complete")
    readline.parse_and_bind("set completion-ignore-case on")
    readline.parse_and_bind("set show-all-if-unmodified")
    readline.parse_and_bind("set show-all-if-ambiguous on")
    readline.parse_and_bind('set colored-completion-prefix off')
    readline.parse_and_bind('set colored-stats off')
    readline.parse_and_bind("set completion-prefix-display-length 2")
    readline.set_completer_delims('')


try:
    __IPYTHON__
    import os
    if os.getenv('INSIDE_EMACS'):
        print('REPL completion using custom IPython completer')
        my_setup_IPythonRL()
except NameError:
    try:
        from jedi.utils import setup_readline
    except ImportError:
        pass
    else:
        setup_readline()

──────────────────✀──────────────────



reply via email to

[Prev in Thread] Current Thread [Next in Thread]