How to use helm-semantic-or-imenu for code navigation with type annotated python code

666 views Asked by At

I would like to use the helm-semantic-or-imenu command to navigate components of type annotated Python code, but whatever code analyzer is used to dentify the components doesn't seem to recognize the type annotated python code. Functions with the return type annotation doesn't get recognized at all and functions with annotated arguments show the type instead of the arguments names in the signatures see screenshot

The main problem I have is that I do not properly understand the components that is involved in making this work (when it does work). Obviously it might help to somehow update the code analyzer, but in which project do I find that? helm? semantic? imenu? or as someone mentioned somewhere else with regards to code analysis python.el? I could really use some help getting started to solve this. If the code analyzer is found in python.el, can I then try to modify and make emacs use a local version preferentially over the installed one?

EDIT: After making the initial post I finally made a break through in trying to figure out where the components come from. I searched for python*.el in all of the file systemsystem and discovered these:

./usr/share/emacs/26.2/lisp/cedet/semantic/wisent/python.elc ./usr/share/emacs/26.2/lisp/cedet/semantic/wisent/python-wy.elc

I found the source for emacs 26.2 and discovered that indeed it seems python-el is responsible for parsing python files for semantic. It also internally uses the python-wy for recognizing a large portion of the language components. But unfortunately that is where I hit a brick wall. I was hoping to be able to monkey patch the function that recognizes a function definition via an re or something, but semantic actually solves the problem the right way. So python-wy seems to be auto-generated from a formal grammar definition file (in emacs git admin/grammars/python.wy) and figuring out how to modify that it is unfortunately much beyond my abilities.

1

There are 1 answers

0
Rorschach On BEST ANSWER

The semantic python backend doesn't appear to parse type annotations correctly (and there hasn't been much recent development on those libraries as far as I can tell). Since helm-semantic-or-imenu favors semantic when it is active, you can disable semantic altogether for python buffers unless you use its other features (personally I only use it for C/C++).

When the semantic mode-specific libraries are loaded they set imenu-create-default-create-index and imenu-default-goto-function, causing imenu to use semantic instead of python.el's imenu function.

To disable semantic support for your python files you can customize the semantic-new-buffer-setup-functions, only adding entries for modes you want semantic support for, eg. in your semantic hook (or alternatively with the customize UI),

(setq semantic-new-buffer-setup-functions
      '((c-mode                . semantic-default-c-setup)
        (c++-mode              . semantic-default-c-setup)
        (srecode-template-mode . srecode-template-setup-parser)
        (texinfo-mode          . semantic-default-texi-setup)
        ;; etc.
        ;; (makefile-automake-mode . semantic-default-make-setup)
        ;; (makefile-mode         . semantic-default-make-setup)
        ;; (makefile-gmake-mode   . semantic-default-make-setup)
        ))