anaconda-mode icon indicating copy to clipboard operation
anaconda-mode copied to clipboard

Better goto definition

Open valignatev opened this issue 7 years ago • 2 comments

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:

  1. Falling back to goto_assignments if goto_definitions fails. 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
  2. A new function, something like anaconda-mode-find-definitions-dwim which will use jedi's assignments with import following. Pros: intuitive Cons: might cause user's confusion, has to be documented
  3. Replace goto_definition call with goto_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?

valignatev avatar Jul 08 '18 15:07 valignatev

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_definitions and goto_assignments keybindings (M-. and M-=)
  • goto_assignments called with follow imports by default.
  • With C-u goto_assignments called without follow imports.

I suggest to implement it this way:

  • script_method and process_definitions decorators should pass **kwargs to the function call.
  • goto_assignments should accept follow imports argument.
  • anaconda-mode-jsonrpc-request-data should 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!

proofit404 avatar Jul 08 '18 16:07 proofit404

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.

valignatev avatar Jul 11 '18 03:07 valignatev