From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: kobarity Newsgroups: gmane.emacs.bugs Subject: bug#68559: [PATCH] Improve Python shell completion Date: Sun, 04 Feb 2024 23:35:00 +0900 Message-ID: References: <83zfx39mcy.fsf@gnu.org> <87a5og8m7t.fsf@gmail.com> Mime-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="33327"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?Q?Goj=C5=8D?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/30.0.50 (x86_64-pc-linux-gnu) MULE/6.0 (HANACHIRUSATO) Cc: Eli Zaretskii , 68559@debbugs.gnu.org To: Liu Hui Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Feb 04 15:36:13 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rWdbQ-0008Ym-Sw for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 04 Feb 2024 15:36:12 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rWdbD-0004ca-Ms; Sun, 04 Feb 2024 09:35:59 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rWdbB-0004be-TP for bug-gnu-emacs@gnu.org; Sun, 04 Feb 2024 09:35:58 -0500 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rWdb3-0003Yq-VZ for bug-gnu-emacs@gnu.org; Sun, 04 Feb 2024 09:35:56 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rWdbF-0001XA-Mn for bug-gnu-emacs@gnu.org; Sun, 04 Feb 2024 09:36:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: kobarity Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 04 Feb 2024 14:36:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 68559 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 68559-submit@debbugs.gnu.org id=B68559.17070573255850 (code B ref 68559); Sun, 04 Feb 2024 14:36:01 +0000 Original-Received: (at 68559) by debbugs.gnu.org; 4 Feb 2024 14:35:25 +0000 Original-Received: from localhost ([127.0.0.1]:48484 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rWdaf-0001WI-1Y for submit@debbugs.gnu.org; Sun, 04 Feb 2024 09:35:25 -0500 Original-Received: from mail-pg1-x52a.google.com ([2607:f8b0:4864:20::52a]:59761) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rWdaZ-0001Vx-I2 for 68559@debbugs.gnu.org; Sun, 04 Feb 2024 09:35:23 -0500 Original-Received: by mail-pg1-x52a.google.com with SMTP id 41be03b00d2f7-5d3907ff128so3138628a12.3 for <68559@debbugs.gnu.org>; Sun, 04 Feb 2024 06:35:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707057301; x=1707662101; darn=debbugs.gnu.org; h=mime-version:user-agent:references:in-reply-to:subject:cc:to:from :message-id:date:from:to:cc:subject:date:message-id:reply-to; bh=k/xrsysBgmaK0ocKi6r7mOD8lv/+d+O6765cA17sR5o=; b=lcCDCeD24tllGs4hmPOhV2+Wr9CmO4hfUGVJ5ec9O5XtFcJZpdzLglL7UgVzCob5wK tb2I4TYpHhhSL9MTWNOajSrYIw7gfmfNxzft2j8RCPTxzS2CGRjY4cQTa1Wm0Rusr/Uu EEdPl6mRYK2Xzh6hLzUrsq2K6a2XwsjKsJoXhPMlw8S7niYJ9KcgXYos17eFWtpQsGiI isJ1Wk4pIvLL7g5VR4z5K+/0JgP7+ZwoA4z9DS/G/xJQQShO7nY+dJKKhhICxSA59Xp0 gkxhGuyHeoCZnuS8v/E8Y3P1TRIg3WTTA596Urz8g9203H8Ev8ggPe7wVpq6zwAz+p46 MdHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707057301; x=1707662101; h=mime-version:user-agent:references:in-reply-to:subject:cc:to:from :message-id:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=k/xrsysBgmaK0ocKi6r7mOD8lv/+d+O6765cA17sR5o=; b=jT0Hzee14X1Zwe5Lz4YJbIsr1CWOXMDCCAsQDUikdzRMOotnnctSII4+Q5mZb/heuH vaZyObOW0GaJV2EhhTrBzmexQGq+Q9bA77dbFK4A8LH/NjYAtsHnXPtDZ9Oy2YlwAUzN b+YGXuwL/739IE/tsxEmG9kGCRgmKFhgI6P6aDalGhGaYPeEGuhbrCfNOsmIRosUnHf5 5d3iUsV+QJdJFFqA8uujPXHwJeJOrqrS+k3NEx87XYytOPp/Y15jhCiRFlBHkPZXh6Sa X9ojS7kSg1Zc7UZjuRvurNdRnFu+Iwn6b3kB5wFBXmLa/Yt21D5wsVpBkvXbxQ7uiT9e 0jGQ== X-Gm-Message-State: AOJu0Ywwun4DwD7jVXoRo0tqj6dhVO2d0ZQEIqrnFtkiSPgck5VWrvyY qPFCvIHVZJXyE1Q6eFoAMtAriSCuiSZaWzXVqBSXygfkM5maLbOM X-Google-Smtp-Source: AGHT+IGTRE43Dd2eTj64xLoE7V+6wGZA/87I+5JxKHnGxvTWEZbBjb4YHrF6aTqz3Ogfsn+/xRViZA== X-Received: by 2002:a05:6a21:31c8:b0:19c:7ec8:467c with SMTP id zb8-20020a056a2131c800b0019c7ec8467cmr17876531pzb.23.1707057301552; Sun, 04 Feb 2024 06:35:01 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCU8+tRoOZlraslY0oUhLTHh7vmQL0eQx+/L5swbuTWhAfl7p0mcZwbtZe0NKqmnMcDlaN4hdN+H3MzLYEhD9awOkgp2o+4= Original-Received: from localhost (58x12x133x161.ap58.ftth.ucom.ne.jp. [58.12.133.161]) by smtp.gmail.com with ESMTPSA id s17-20020a632151000000b005cd8044c6fesm5103840pgm.23.2024.02.04.06.35.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Feb 2024 06:35:01 -0800 (PST) In-Reply-To: <87a5og8m7t.fsf@gmail.com> X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:279416 Archived-At: Liu Hui wrote: > kobarity 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.