unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: kobarity <kobarity@gmail.com>
To: Liu Hui <liuhui1610@gmail.com>
Cc: Eli Zaretskii <eliz@gnu.org>, 68559@debbugs.gnu.org
Subject: bug#68559: [PATCH] Improve Python shell completion
Date: Sun, 04 Feb 2024 23:35:00 +0900	[thread overview]
Message-ID: <eke71q9s70x7.wl-kobarity@gmail.com> (raw)
In-Reply-To: <87a5og8m7t.fsf@gmail.com>

Liu Hui wrote:
> kobarity <kobarity@gmail.com> writes:
> > I'm experiencing strange behavior regarding completion of import
> > statement in a block in Python buffer.  If I try to type the following
> > lines and then try to complete it, it will fail.
> >
> > #+begin_src python
> > try:
> >     im
> > #+end_src
> 
> The problem can be reproduced by running python with jedi completer in
> a terminal. The reason is that readline completer can only see the
> text in the current line, i.e. "    im"; in this case, jedi does not
> complete the text to "    import".
> 
> Similarly, the jedi completer cannot complete "except" after ex:
> 
> try:
>     pass
> ex|
> 
> > However, when I try to complete at the beginning of the second line:
> >
> > #+begin_src python
> > try:
> >
> > #+end_src
> >
> >
> > "import" keyword also appears as a candidate.  If I cancel the
> > candidates and type "im" and try to complete it, it will succeed.
> 
> It is because jedi produces completions including "    import" for
> blank string "    ". Due to the same prefix between "    " and
> "    im", completion cache is reused for the latter. Then "    import"
> can be completed.
> 
> 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?

> > Another thing I noticed is the multi-line import statement.  If the
> > import statement is one-line, each items (IGNORECASE and MULTILINE in
> > the example below) can be completed.
> >
> > #+begin_src python
> > from re import IGNORECASE, MULTILINE
> > #+end_src
> >
> >
> > However, they cannot be completed if the import statement spans
> > multi-line.
> >
> > #+begin_src python
> > from re import (
> >     IGN
> > #+end_src
> >
> > This happens in both Python buffer and Python Shell buffer.  Perhaps
> > this is a limitation of Jedi completer?
> 
> Yes. Because readline completer cannot see cross-line context, I added
> the function "python-shell-completion-extra-context" in previous patch
> to address the case of multi-line function call. I have updated the
> attached patch to handle multi-line import statement.

Thank you very much.  I confirmed that the new patch allows completion
of multi-line import statements.





  reply	other threads:[~2024-02-04 14:35 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-18  4:48 bug#68559: [PATCH] Improve Python shell completion Liu Hui
2024-01-18  6:39 ` Eli Zaretskii
2024-01-21  9:34   ` kobarity
2024-01-23 11:31     ` Liu Hui
2024-01-23 14:15       ` kobarity
2024-01-24 10:07         ` Liu Hui
2024-01-25 15:38           ` kobarity
2024-01-26 10:12             ` Liu Hui
2024-01-28 13:22               ` kobarity
2024-01-29 13:15                 ` kobarity
2024-02-01  9:52                   ` Eli Zaretskii
2024-02-01 14:39                     ` kobarity
2024-02-01 15:02                       ` Liu Hui
2024-02-04 12:09                 ` Liu Hui
2024-02-04 14:35                   ` kobarity [this message]
2024-02-05 15:03                     ` Liu Hui
2024-02-06  1:25                       ` Liu Hui
2024-02-06 15:12                         ` kobarity
2024-02-07 13:22                           ` Liu Hui
2024-02-07 15:19                             ` kobarity
2024-02-08 12:13                               ` Eli Zaretskii
2024-02-08 13:33                                 ` Liu Hui
2024-02-08 13:46                                   ` Eli Zaretskii
2024-02-08 14:16                                     ` Liu Hui
2024-02-08 16:43                                       ` Eli Zaretskii
2024-02-15 14:43 ` Mattias Engdegård
2024-02-15 16:37   ` Eli Zaretskii
2024-02-15 16:48     ` Eli Zaretskii
2024-02-15 17:21       ` Mattias Engdegård
2024-02-19 13:18       ` Basil L. Contovounesios
2024-02-20  4:46         ` Liu Hui
2024-02-20 13:15           ` Basil L. Contovounesios
2024-02-21 10:00             ` Liu Hui
2024-02-21 14:55               ` Basil L. Contovounesios
2024-02-22 10:31                 ` Liu Hui
2024-02-22 13:56                   ` Basil L. Contovounesios
2024-02-23 13:07                     ` Liu Hui
2024-02-28 14:47                       ` Basil L. Contovounesios
2024-02-16  4:06     ` Liu Hui
2024-02-16  7:41       ` Eli Zaretskii
2024-02-16 12:51         ` Eli Zaretskii
2024-02-16  3:24   ` Liu Hui
2024-02-16  9:34     ` kobarity
2024-02-16 11:45       ` Mattias Engdegård
2024-02-16 15:24         ` kobarity
2024-02-16 15:52           ` Eli Zaretskii
2024-02-16 20:10           ` Mattias Engdegård
2024-02-17 13:33             ` kobarity
2024-02-20 10:16               ` Mattias Engdegård
2024-02-21 13:13                 ` kobarity
2024-02-21 18:20                   ` Mattias Engdegård
2024-02-22 16:15                     ` kobarity
2024-02-23 11:00                       ` Mattias Engdegård
2024-02-23 14:39                         ` kobarity
2024-02-26 11:06                           ` Liu Hui
2024-02-26 12:16                             ` Mattias Engdegård
2024-02-26 15:08                               ` kobarity
2024-02-28 14:49                             ` Basil L. Contovounesios
2024-03-06 10:14                               ` Liu Hui
2024-03-08 15:44                                 ` Basil L. Contovounesios
2024-03-11 11:35                                   ` Liu Hui
2024-03-11 16:02                                     ` Basil L. Contovounesios
2024-03-13 10:21                                       ` Liu Hui
2024-03-14 14:24                                         ` Basil L. Contovounesios
2024-03-16  6:49                                           ` Liu Hui
2024-03-16  8:27                                             ` Eli Zaretskii
2024-02-17  4:36           ` Liu Hui
2024-02-17 13:20             ` Mattias Engdegård

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=eke71q9s70x7.wl-kobarity@gmail.com \
    --to=kobarity@gmail.com \
    --cc=68559@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=liuhui1610@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).