Subject: Imenu in `python-mode' does not display function name for async def

From: Dmitry Lazurkin <[email-masked]>
Date: Sat, 29 Oct 2016 17:34:53 +0300

Package: emacs
Version: 25
Tags: patch

 From 9429522de96c27980f61bc107c4801216994d80b Mon Sep 17 00:00:00 2001
From: Dmitry Lazurkin <[email-masked]>
Date: Sat, 29 Oct 2016 16:51:40 +0300
Subject: [PATCH] Fix extracting async def type and name in python mode imenu

* lisp/progmodes/python.el (python-imenu--get-defun-type-name):
New function.
(python-imenu--build-tree): Use python-imenu--get-defun-type-name for
extract async or simple def type and name at current position.
* test/automated/python-tests.el (python-imenu-create-index-1,
python-imenu-create-flat-index-1): Add async def's.
---
  lisp/progmodes/python.el       | 16 +++++++++++-----
  test/automated/python-tests.el | 12 ++++++++++--
  2 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 49f7bcf..60bd4dc 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -4389,6 +4389,14 @@ python-imenu-format-parent-item-jump-label
        "*class definition*"
      "*function definition*"))
  
+(defun python-imenu--get-defun-type-name ()
+  "Return defun type and name at current position."
+  (when (looking-at python-nav-beginning-of-defun-regexp)
+    (let ((split (split-string (match-string-no-properties 0))))
+      (if (= (length split) 2)
+          split
+        (cdr split)))))
+
  (defun python-imenu--put-parent (type name pos tree)
    "Add the parent with TYPE, NAME and POS to TREE."
    (let ((label
@@ -4406,11 +4414,9 @@ python-imenu--build-tree
    (setq min-indent (or min-indent 0)
          prev-indent (or prev-indent python-indent-offset))
    (let* ((pos (python-nav-backward-defun))
-         (type)
-         (name (when (and pos (looking-at python-nav-beginning-of-defun-regexp))
-                 (let ((split (split-string (match-string-no-properties 0))))
-                   (setq type (car split))
-                   (cadr split))))
+         (defun-type-name (and pos (python-imenu--get-defun-type-name)))
+         (type (car defun-type-name))
+         (name (cadr defun-type-name))
           (label (when name
                    (funcall python-imenu-format-item-label-function type name)))
           (indent (current-indentation))
diff --git a/test/automated/python-tests.el b/test/automated/python-tests.el
index 54ed922..3cdabf8 100644
--- a/test/automated/python-tests.el
+++ b/test/automated/python-tests.el
@@ -3552,6 +3552,9 @@ python-tests-shell-interpreter
  
          def c(self):
              pass
+
+        async def d(self):
+            pass
  "
     (goto-char (point-max))
     (should (equal
@@ -3573,7 +3576,8 @@ python-tests-shell-interpreter
                (list
                 "Frob (class)"
                 (cons "*class definition*" (copy-marker 601))
-               (cons "c (def)" (copy-marker 626)))))
+               (cons "c (def)" (copy-marker 626))
+               (cons "d (def)" (copy-marker 665)))))
              (python-imenu-create-index)))))
  
  (ert-deftest python-imenu-create-index-2 ()
@@ -3695,6 +3699,9 @@ python-tests-shell-interpreter
  
          def c(self):
              pass
+
+        async def d(self):
+            pass
  "
     (goto-char (point-max))
     (should (equal
@@ -3707,7 +3714,8 @@ python-tests-shell-interpreter
                    (cons "Baz.a" (copy-marker 539))
                    (cons "Baz.b" (copy-marker 570))
                    (cons "Baz.Frob" (copy-marker 601))
-                  (cons "Baz.Frob.c" (copy-marker 626)))
+                  (cons "Baz.Frob.c" (copy-marker 626))
+                  (cons "Baz.Frob.d" (copy-marker 665)))
              (python-imenu-create-flat-index)))))
  
  (ert-deftest python-imenu-create-flat-index-2 ()
-- 
2.7.4