go-mode.el
go-mode.el copied to clipboard
imenu-generic-expression does not match all function styles
The current imenu-generic-expression
does not match all possible function styles.
Maybe using go-func-regexp
and go-func-meth-regexp
in the imenu-generic-expression
would solve it.
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.
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).