From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Dima Kogan Newsgroups: gmane.emacs.bugs Subject: bug#56635: 29.0.50; [PATCH] hide-show in python-mode supports ONLY function and class blocks Date: Mon, 18 Jul 2022 15:20:30 -0700 Message-ID: <87v8ruysep.fsf@secretsauce.net> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="19611"; mail-complaints-to="usenet@ciao.gmane.io" To: 56635@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Jul 19 00:21:16 2022 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 1oDZ74-0004r1-1C for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 19 Jul 2022 00:21:15 +0200 Original-Received: from localhost ([::1]:47116 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oDZ72-0000l5-F6 for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 18 Jul 2022 18:21:12 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:33886) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oDZ6t-0000kv-92 for bug-gnu-emacs@gnu.org; Mon, 18 Jul 2022 18:21:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:54634) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oDZ6r-0007or-VU for bug-gnu-emacs@gnu.org; Mon, 18 Jul 2022 18:21:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oDZ6r-00076x-Ly for bug-gnu-emacs@gnu.org; Mon, 18 Jul 2022 18:21:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Dima Kogan Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 18 Jul 2022 22:21:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 56635 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.165818284527303 (code B ref -1); Mon, 18 Jul 2022 22:21:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 18 Jul 2022 22:20:45 +0000 Original-Received: from localhost ([127.0.0.1]:52393 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oDZ6a-00076J-RF for submit@debbugs.gnu.org; Mon, 18 Jul 2022 18:20:45 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:34372) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oDZ6Y-00076A-6t for submit@debbugs.gnu.org; Mon, 18 Jul 2022 18:20:43 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:33812) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oDZ6X-0000ik-R2 for bug-gnu-emacs@gnu.org; Mon, 18 Jul 2022 18:20:42 -0400 Original-Received: from wout2-smtp.messagingengine.com ([64.147.123.25]:60497) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oDZ6U-0007jo-Pt for bug-gnu-emacs@gnu.org; Mon, 18 Jul 2022 18:20:41 -0400 Original-Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.west.internal (Postfix) with ESMTP id BE40A32007D7 for ; Mon, 18 Jul 2022 18:20:35 -0400 (EDT) Original-Received: from mailfrontend2 ([10.202.2.163]) by compute1.internal (MEProxy); Mon, 18 Jul 2022 18:20:35 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secretsauce.net; h=cc:content-type:date:date:from:from:in-reply-to:message-id :mime-version:reply-to:sender:subject:subject:to:to; s=fm2; t= 1658182834; x=1658269234; bh=AHP82qI2EpLLjSzvIdvMLawEJNAt3ECV0NH P4o16ycw=; b=POU8pk349naQTUZ/WH5APxyZWUXC+WWLOvktzinZRZ+tTIPF2Xa FpHV6GS6/BMleMZ84FAfBLpxW0cli/pOvH9F3h4Pd2jiyELdfCJXDCMrXWUD83MZ pnk1krtB4fK0EUtxIYi5irUHDC/nb2j1INsluDNYnFr38D6V7hfA3ykV5nMvKfZw lQ3jbuyhMgzallp8vl4HZX44W5cKu/LQ353q/IA45ijaDxaV9DaWPTFHKX4MoUQT 33ccBc+o0w4op6eVy6HynXpx1iT3nJEh/q93YWIbYUOnzsvF1Q9HRhBCzo3zCUXL wXg0m0BL2bI8nxJQKWoxWiVDMqBlgIFmlxA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:date:feedback-id :feedback-id:from:from:in-reply-to:message-id:mime-version :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1658182834; x= 1658269234; bh=AHP82qI2EpLLjSzvIdvMLawEJNAt3ECV0NHP4o16ycw=; b=t 5oJ1wIq9gUkFXr43VY/MI5TeYxFmh4aFyR7ehJmlITj9dEHCgmewnIYZi1TEafEQ 4heRFVm2ONPxxrgIO0UXVcJE90KlunobX1WNjInBqm9fphBkxVPNf0jtDNr//yKR lg0mpguGBwfp3TLZlgQk9mcBVj6VBNHn+/iefuQZ4XhHz/zkozXlI6v8cepfE7IO UYA6w8l42QZyR00qo2qc9/3QD1DEc2g2KLNCIMZEzJmqs+vBapEZbNIVjf0qijKP CU5LpdvBx35CEKAqodj6u4aXjZo62O/Z93lgpoH2nNXqzOc2oAQ/gI+OcO7F6Hwg hZORSF0XQwO20HrgTxmfQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedrudekledgtdelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefhvffufffkgggtsehttdertddttd dtnecuhfhrohhmpeffihhmrgcumfhoghgrnhcuoeguihhmrgesshgvtghrvghtshgruhgt vgdrnhgvtheqnecuggftrfgrthhtvghrnhepffdvffduhfekfffhieefhfffvddtgfefje evuedvfedujeejudevfedtgeeguedvnecuvehluhhsthgvrhfuihiivgeptdenucfrrghr rghmpehmrghilhhfrhhomhepughimhgrsehsvggtrhgvthhsrghutggvrdhnvght X-ME-Proxy: Feedback-ID: i3e8042a0:Fastmail Original-Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Mon, 18 Jul 2022 18:20:34 -0400 (EDT) Received-SPF: pass client-ip=64.147.123.25; envelope-from=dima@secretsauce.net; helo=wout2-smtp.messagingengine.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action 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" Xref: news.gmane.io gmane.emacs.bugs:237388 Archived-At: Hi. I'm using hide-show mode to visually collapse blocks while coding. This is very helpful, and works great in C-like modes. hide-show mode currently supports Python in a limited fashion: only def: and class: blocks are hidden. Other kinds of blocks (if, else, for, while, ...) are not supported. A simple patch that mostly adds support for all other kinds of blocks is: diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 1c99937c4b9..b63d2bc0690 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -4819,7 +4819,7 @@ python-describe-at-point (defun python-hideshow-forward-sexp-function (_arg) "Python specific `forward-sexp' function for `hs-minor-mode'. Argument ARG is ignored." - (python-nav-end-of-defun) + (python-nav-end-of-block) (unless (python-info-current-line-empty-p) (backward-char))) @@ -5766,8 +5766,8 @@ python-mode (add-to-list 'hs-special-modes-alist - '(python-mode - "\\s-*\\_<\\(?:def\\|class\\)\\_>" + `(python-mode + ,(python-rx block-start) ;; Use the empty string as end regexp so it doesn't default to ;; "\\s)". This way parens at end of defun are properly hidden. "" I say "mostly" because this isn't doable with regular expressions, as is required by hide-show mode. So this patch can confuse the parser: some bits of code will look like they begin a block, while in reality they do not. hide-show would then collapse lines that it should ignore. Some Python code that shows this failure: if 0: aaa l = [ i for i in range(5) \ if i < 3 ] ccc abc = o.match(1,2,3) ddd Here the "for" and "if" in the list comprehension both trigger the block hiding. And the "match" function call triggers it too. python-mode has functions to move between blocks (python-nav-...), but they're not completely regex-based, so they can't be baked into hide-show. Suggestions? Obvious options: 1. Leave the code as it is, don't support other blocks 2. Take this patch, with the understanding that it'll collapse some stuff that it shouldn't 3. Patch hide-show to potentially accept non-regex logic I've been dogfooding the patch for a few days now, and it's behaving 99% properly for me, so I'm inclined to suggest option 2. Making hide-show smarter (option 3) doesn't feel worth the effort to fix THIS problem. Thanks!