0

I want a list of functions, classes, and methods, without imports or variables, that matches the ordering in the current file. Since I use helm-swoop, I would like to be able to do this with helm. I used to be able to.

However, I just switched to lsp-mode. Prior to lsp being enabled, helm-imenu works as I would like:

enter image description here

Once lsp is enabled, though, helm-imenu gives modules, classes, methods, fields, and variables ordered by group:

enter image description here

It's not clear to me how to return to the pre-lsp-mode behavior.

In true Wisdom of the Ancients fashion, I found an old Reddit post where a person (FreshCakes) achieved precisely what I want....but didn't post their code.

The code they modified is:

(require 'helm-imenu)

(defun my-helm-imenu-transformer (cands)
  (with-helm-current-buffer
    (save-excursion
      (cl-loop for (func-name . mrkr) in cands
               collect
               (cons (format "Line %4d: %s"
                             (line-number-at-pos mrkr)
                             (progn (goto-char mrkr)
                                    (buffer-substring mrkr (line-end-position))))
                     (cons func-name mrkr))))))

(defvar my-helm-imenu-source  (helm-make-source "Imenu" 'helm-imenu-source
                                :candidate-transformer
                                'my-helm-imenu-transformer))
(defun my-helm-imenu ()
  (interactive)
  (let ((imenu-auto-rescan t)
        (str (thing-at-point 'symbol))
        (helm-execute-action-at-once-if-one
         helm-imenu-execute-action-at-once-if-one))
    (helm :sources 'my-helm-imenu-source
          :preselect str
          :buffer "*helm imenu*")))

I can modify format to calculate the number of spaces between the marker and the beginning of the line so that items are indented properly:

(defun my-helm-imenu-transformer (cands)
(with-helm-current-buffer
    (save-excursion
    (cl-loop for (func-name . mrkr) in cands
        collect
        (cons (format "%s %s"
                (progn (goto-char mrkr)
                    (make-string (- (marker-position mrkr) (line-beginning-position)) ?\s))
                (progn (goto-char mrkr)
                    (buffer-substring mrkr (line-end-position))))
            (cons func-name mrkr))))))

Unfortunately, items are still ordered by group and includes imports and variables. Changing imenu-sort-functions to imenu--sort-by-position doesn't appear to do anything for the grouping.

How can I return to the pre-lsp-mode behavior?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.