go-mode.el icon indicating copy to clipboard operation
go-mode.el copied to clipboard

imenu-generic-expression does not match all function styles

Open mpolden opened this issue 10 years ago • 2 comments

The current imenu-generic-expression does not match all possible function styles. Screenshot

Maybe using go-func-regexp and go-func-meth-regexp in the imenu-generic-expression would solve it.

mpolden avatar Jul 24 '14 23:07 mpolden

The following regex work:

First displaying the entire line, without multi line data.

"^func\s\\(.+\\)"

... with a little more checking, verifying up to the function name but selecting the entire line.

"^func\s\\(\(\\w\s.\\w+\)\s.*\\|.*\\)"

Next displaying only up to the function name and no further, which is arguably a lot cleaner.

"^func\s\\(\(\\w\s.\\w+\)\s\\w+\\|\\w+\\)"

I'm not sure that it is possible to catch the rest of a multi line function deceleration, if it is it would surely be a little more involved. Requiring perhaps alteration to the elisp that writes the output, for example, to remove the '\n' char so that the use of the output remains reasonable when displayed in a confined space.

8i8 avatar Jan 10 '20 09:01 8i8

I'm using v1.6.0, and I'm leaving a comment in case someone needs a quick way to get which-function-mode to show the beginning of multiline function signatures, which are correct and accepted by go fmt.

The following code in init.el will override the incorrect pattern that is included in go-mode and match the function signature, to the end of the first line. Thanks to the previous commenter:

(add-hook 'go-mode-hook
  (lambda ()
    (add-to-list 'imenu-generic-expression
                 '("func" "^func\s\\((\\w\s.\\w+)\s.*\\|.*\\)" 1))))

I would encourage the maintainers of this project to end the bike-shedding and choose any solution, even if it truncates data uncomfortably. The data generated in imenu--index-alist is incorrect, and causes the wrong information to be displayed ("you are looking at function A", even when you are looking at function B).

sdhoward avatar Nov 02 '22 17:11 sdhoward