bug-gnu-emacs
[Top][All Lists]
Advanced

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

bug#68559: [PATCH] Improve Python shell completion


From: Liu Hui
Subject: bug#68559: [PATCH] Improve Python shell completion
Date: Mon, 5 Feb 2024 23:03:19 +0800

On Sun, Feb 4, 2024 at 10:35 PM kobarity <kobarity@gmail.com> wrote:

> > It is more a limitation of readline completer than a problem with
> > jedi, as we cannot provide proper completion context for jedi. We may
> > define a custom completer to combine jedi and rlcompleter, e.g.
> >
> > (setq python-shell-readline-completer "
> > def __PYTHON_EL_setup_readline_completer():
> >     import readline, rlcompleter
> >     import re, sys, os, __main__
> >     from jedi import Interpreter
> >
> >     class MyJediRL:
> >         def __init__(self):
> >             self.rlcompleter = rlcompleter.Completer()
> >             self.rldelim = readline.get_completer_delims()
> >
> >         def complete(self, text, state):
> >             if state == 0:
> >                 sys.path.insert(0, os.getcwd())
> >                 try:
> >                     interpreter = Interpreter(text, [__main__.__dict__])
> >                     completions = interpreter.complete(fuzzy=False)
> >                     self.matches = [
> >                         text[:len(text) - c._like_name_length] + 
> > c.name_with_symbols
> >                         for c in completions
> >                     ]
> >
> >                     # try rlcompleter
> >                     sub = re.split('[' + re.escape(self.rldelim) + ']', 
> > text)[-1]
> >                     i = 0
> >                     while True:
> >                         completion = self.rlcompleter.complete(sub, i)
> >                         if not completion:
> >                             break
> >                         i += 1
> >                         completion = text[:len(text)-len(sub)] + 
> > completion.rstrip(' ()')
> >                         if completion not in self.matches:
> >                             self.matches.append(completion)
> >                 except:
> >                     raise
> >                 finally:
> >                     sys.path.pop(0)
> >             try:
> >                 return self.matches[state]
> >             except IndexError:
> >                 return None
> >
> >     readline.set_completer(MyJediRL().complete)
> >     readline.set_completer_delims('')")
>
> Thank you for the detailed explanation and the workaround.  I
> confirmed that the problem is solved by the above workaround.  Just to
> confirm, are you of the opinion that this workaround should not be the
> default?

I'm not sure if we should add more Python code in the form of strings
to python.el, which increases maintenance burden IMO. Maybe they could
be distributed separately at ELPA/Git forges.

Actually, I'm considering simplifying this patch to mainly fix the bug
that python shell completion doesn't respect the delimiter of readline
completer. The new patch has been attached. It should support
the completer defined in the PYTHONSTARTUP file, e.g., jedi or a
custom completer like the above one. WDYT?

Attachment: 0001-Respect-the-delimiter-of-completer-in-Python-shell-c.patch
Description: Text Data


reply via email to

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