Subject: Re: bug#34245: perl mode aligns code with comments

From: Mauro Aranda <[email-masked]>
Date: Thu, 20 Oct 2022 08:51:32 -0300

severity 34245 minor
tags 34245 patch
quit

積丹尼 Dan Jacobson <[email-masked]> writes:

 > In perl mode, a TAB on the second line below causes
 >
 > my @genres = (         #["match", "BUT NOT"]
 >                ["Rainbow.*Taiwan|Taiwan.*Rainbow"],
 >
 > But wiser would be like perltidy does:
 >
 > my @genres = (                      #["match", "BUT NOT"]
 >     ["Rainbow.*Taiwan|Taiwan.*Rainbow"],

I don't know why Debbugs marked this as a wishlist, it looks like a bug
to me.

Patch attached.

From a50ef6c2f08b8648f26e5fcf29c084c1449cb093 Mon Sep 17 00:00:00 2001
From: Mauro Aranda <[email-masked]>
Date: Thu, 20 Oct 2022 08:41:42 -0300
Subject: [PATCH] Fix perl-mode indentation after a hanging paren

* lisp/progmodes/perl-mode.el (perl-hanging-paren-p): Allow
comments.  (Bug#34245)
* test/lisp/progmodes/perl-mode-tests.el (perl-test-bug-34245): New
test.
---
 lisp/progmodes/perl-mode.el            |  2 +-
 test/lisp/progmodes/perl-mode-tests.el | 17 +++++++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/lisp/progmodes/perl-mode.el b/lisp/progmodes/perl-mode.el
index c5d5d703fc..ef932f2ad8 100644
--- a/lisp/progmodes/perl-mode.el
+++ b/lisp/progmodes/perl-mode.el
@@ -968,7 +968,7 @@ perl-continuation-line-p
 
 (defun perl-hanging-paren-p ()
   "Non-nil if we are right after a hanging parenthesis-like char."
-  (and (looking-at "[ \t]*$")
+  (and (looking-at "[ \t]*\\(?:#.*\\)?$")
        (save-excursion
 	 (skip-syntax-backward " (") (not (bolp)))))
 
diff --git a/test/lisp/progmodes/perl-mode-tests.el b/test/lisp/progmodes/perl-mode-tests.el
index 91f1db23d6..5fb0ed9d53 100644
--- a/test/lisp/progmodes/perl-mode-tests.el
+++ b/test/lisp/progmodes/perl-mode-tests.el
@@ -28,6 +28,23 @@ perl-test-lock
     (font-lock-ensure (point-min) (point-max))
     (should (equal (get-text-property 4 'face) 'font-lock-variable-name-face))))
 
+(ert-deftest perl-test-bug-34245 ()
+  "Test correct indentation after a hanging paren, with and without comments."
+  (with-temp-buffer
+    (perl-mode)
+    (insert "my @foo = (\n\"bar\",\n\"baz\",\n);")
+    (insert "\n\n")
+    (insert "my @ofoo = (\t\t# A comment.\n\"obar\",\n\"obaz\",\n);")
+    (indent-region (point-min) (point-max))
+    (goto-char (point-min))
+    (forward-line)
+    (skip-chars-forward " \t")
+    (should (equal (current-column) perl-indent-level))
+    (search-forward "# A comment.")
+    (forward-line)
+    (skip-chars-forward " \t")
+    (should (equal (current-column) perl-indent-level))))
+
 ;;;; Re-use cperl-mode tests
 
 (defvar cperl-test-mode)
-- 
2.34.1