Better goto definition
Hi there! As David Halter said,
"goto_definitions is an important function, because it gives you access to the type inference. But it's probably not the goto function IDE's want"
What I want to propose is using jedi's goto_assignment with follow_imports=True for definition function either as a fallback, or as a full replacement. Consider this project layout:
# project root
.
- foo.py
- bar.py
foo.py:
a = (1, 2, 3, 4)
bar.py
from foo import a
b = a[0]
If in bar.py I'll try to go to a definition, anaconda-mode will say "tuple is defined in compiled module" which is expected considering how anaconda-mode uses Jedi, but it's definitely not what user wants and expects. Related issues in this repo: #205, #214.
Related discussions in jedi/jedi-vim repo: https://github.com/davidhalter/jedi/issues/570, https://github.com/davidhalter/jedi-vim/issues/348.
I played a bit with anaconda-mode sources and found out, that goto_assignments with import following solves this problem. Paths we might choose:
- Falling back to
goto_assignmentsifgoto_definitionsfails. Pros: don't have to change user-facing API Cons: potential speed reduction since two operations are performed one after another in case of builtin type - A new function, something like
anaconda-mode-find-definitions-dwimwhich will use jedi's assignments with import following. Pros: intuitive Cons: might cause user's confusion, has to be documented - Replace
goto_definitioncall withgoto_assignments(follow_imports=True)call. Pros: same as in 1) and 2) Cons: inconsistent with Jedi. Is it an issue?
jedi-vim switched from somewhere in between 1) and 2) to 2) and 3), check this PR.
I'll open a PR with a fallback option to continue the discussion. What do you think? Are you generally in favor of such change? If so, which option do you prefer?
Hi, thanks for the investigation!
I think it is reasonable to solve this problem in a convenient way. I don't want to lose goto assignments as it is today. I use it to go to the import statement and edit import itself.
In my opinion, the most convenient way to do this:
- Swap
goto_definitionsandgoto_assignmentskeybindings (M-. and M-=) goto_assignmentscalled with follow imports by default.- With C-u
goto_assignmentscalled without follow imports.
I suggest to implement it this way:
script_methodandprocess_definitionsdecorators should pass**kwargsto the function call.goto_assignmentsshould accept follow imports argument.anaconda-mode-jsonrpc-request-datashould accept optional alist and expand it with,@at the end of params section.- the same should do
anaconda-mode-call anaconda-mode-find-assignments, commands for other window and frame should check prefix argument with an interactive directive and set follow imports flag for the current command.- related changes in the keymap and readme.
Thanks for your time you're spending on hacking anaconda-mode!
Thanks for the fast response!
I don't want to lose goto assignments as it is today. I use it to go to the import statement and edit import itself.
I don't want to do anything with current goto_assignments as well, all I'm saying is for current goto_definition.
Regarding your suggestion, I think this might be confusing for users. While technically it makes sense, users expect action to be called "goto definition". Also, C-u is one additional keystroke (yeah, user might rebind that, but it's one extra hassle).
What downsides do you see in replacing current goto_definition implementation with goto_assignments with following imports? This way, anaconda's public interface won't be changed.