emacs-lsp / lsp-mode

Emacs client/library for the Language Server Protocol
https://emacs-lsp.github.io/lsp-mode
GNU General Public License v3.0
4.73k stars 864 forks source link

LSP mode with imenu does not show Python classes/functions properly #2246

Open dmalyuta opened 3 years ago

dmalyuta commented 3 years ago

(The original question was posted on stack exchange: https://emacs.stackexchange.com/q/61141/13661)

I am using Emacs 27.1 with the latest versions of all packages. My LSP-related setup is:

(use-package lsp-pyright
  :ensure t
  :hook (python-mode . (lambda ()
                         (require 'lsp-pyright)
                         (lsp))))  ; or lsp-deferred

(use-package lsp-mode
  :ensure t
  :init
  (setq lsp-keymap-prefix "C-c l")
  :config
  (require 'lsp-mode)
  (add-hook 'python-mode-hook #'lsp)
  (add-hook 'c-mode-common-hook #'lsp)
  (require 'company-capf)
  (setq lsp-prefer-capf t)
  (push 'company-capf company-backends)
  (push 'company-c-headers company-backends)
  (setq lsp-idle-delay 0.500)
  (setq lsp-diagnostic-package :none)
  (add-hook 'lsp-mode-hook (lambda ()
                 (setq company-minimum-prefix-length 1
                   company-idle-delay 0.0)))
  (setq lsp-enable-semantic-highlighting t)
  (setq lsp-enable-snippet nil)  ;; Enable arguments completion
  (setq lsp-signature-auto-activate nil)
  (setq lsp-progress-via-spinner nil)
  )

(use-package lsp-ui
  :ensure t
  :config
  (add-hook 'lsp-mode-hook
        (lambda ()
          (setq lsp-ui-doc-enable nil ;; disable docs
            ;; lsp-ui-doc-delay 1
            ;; lsp-ui-doc-use-childframe t
            lsp-ui-doc-position 'bottom
            lsp-ui-doc-max-height 20
            lsp-ui-doc-include-signature t

            lsp-ui-sideline-enable t
            lsp-ui-sideline-delay 0.2
            lsp-ui-sideline-show-code-actions nil
            lsp-ui-sideline-show-hover nil

            lsp-ui-flycheck-enable t
            lsp-ui-flycheck-list-position 'right
            lsp-ui-flycheck-live-reporting t

            lsp-ui-peek-enable nil
            ;; lsp-ui-peek-list-width 60
            ;; lsp-ui-peek-peek-height 25

            lsp-ui-imenu-enable nil
            )
          (local-set-key (kbd "C-c l i") 'lsp-ui-imenu)
          ))
  )

I feel like something is wrong with my Imenu setup, because here is what it shows on a simple Python script:

image

It seems to show some variable definitions, but doesn't have separate entries for the function definitions (the def ... lines), or the classes. What I want is an Imenu that shows just the function and class definitions, in the order in which they appear. So, I want to see something like:

Function: foo(x)
Class: Bar
Function: Bar / __init__(s)
Function: Bar / buz(x, y)

Is that possible at all? Semantic is not an option because it does not parse functions that are typed using Python's type annotations (e.g. a function like def foo() -> None: ... does not appear in the list).

nbfalcon commented 3 years ago

Currently it isn't, but I made a PR to fix that.