From dd33e38631198bd75ac223a1740de6987fee7f7e Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 29 Apr 2015 12:53:24 +0200 Subject: [PATCH] Add auto-goto command/function This adds g:jedi#goto_command, and uses `d` by default for it - the old default for `g:jedi#goto_definitions_command`, which is now empty. It will try `script.goto_definitions()` first, and falls back to `script.goto_assignments()` for builtins. Closes https://github.com/davidhalter/jedi-vim/pull/407 --- README.rst | 5 +++- autoload/jedi.vim | 14 ++++++---- doc/jedi-vim.txt | 55 +++++++++++++++++++++++++--------------- ftplugin/python/jedi.vim | 3 +++ jedi_vim.py | 18 +++++++++---- 5 files changed, 64 insertions(+), 31 deletions(-) diff --git a/README.rst b/README.rst index ef0db88..5cceeed 100644 --- a/README.rst +++ b/README.rst @@ -164,8 +164,11 @@ get more information. If you set them to ``""``, they are not assigned. .. code-block:: vim + NOTE: subject to change! + + let g:jedi#goto_command = "d" let g:jedi#goto_assignments_command = "g" - let g:jedi#goto_definitions_command = "d" + let g:jedi#goto_definitions_command = "" let g:jedi#documentation_command = "K" let g:jedi#usages_command = "n" let g:jedi#completions_command = "" diff --git a/autoload/jedi.vim b/autoload/jedi.vim index 9a95c21..2dd6b50 100644 --- a/autoload/jedi.vim +++ b/autoload/jedi.vim @@ -5,7 +5,6 @@ scriptencoding utf-8 " ------------------------------------------------------------------------ let s:deprecations = { \ 'get_definition_command': 'goto_definitions_command', - \ 'goto_command': 'goto_assignments_command', \ 'pydoc': 'documentation_command', \ 'related_names_command': 'usages_command', \ 'autocompletion_command': 'completions_command', @@ -17,9 +16,10 @@ let s:default_settings = { \ 'use_splits_not_buffers': 1, \ 'auto_initialization': 1, \ 'auto_vim_configuration': 1, + \ 'goto_command': "'d'", \ 'goto_assignments_command': "'g'", + \ 'goto_definitions_command': "''", \ 'completions_command': "''", - \ 'goto_definitions_command': "'d'", \ 'call_signatures_command': "'n'", \ 'usages_command': "'n'", \ 'rename_command': "'r'", @@ -183,16 +183,20 @@ endif " ------------------------------------------------------------------------ " functions that call python code " ------------------------------------------------------------------------ +function! jedi#goto() + PythonJedi jedi_vim.goto(mode="goto") +endfunction + function! jedi#goto_assignments() - PythonJedi jedi_vim.goto() + PythonJedi jedi_vim.goto(mode="assignment") endfunction function! jedi#goto_definitions() - PythonJedi jedi_vim.goto(is_definition=True) + PythonJedi jedi_vim.goto(mode="definition") endfunction function! jedi#usages() - PythonJedi jedi_vim.goto(is_related_name=True) + PythonJedi jedi_vim.goto(mode="related_name") endfunction function! jedi#rename(...) diff --git a/doc/jedi-vim.txt b/doc/jedi-vim.txt index e450a29..84f49e2 100644 --- a/doc/jedi-vim.txt +++ b/doc/jedi-vim.txt @@ -22,12 +22,13 @@ Contents *jedi-vim-contents* 4. Usage |jedi-vim-usage| 5. Mappings |jedi-vim-keybindings| 5.1. Start completion |g:jedi#completions_command| - 5.2. Go to assignment |g:jedi#goto_assignments_command| - 5.3. Get original definition |g:jedi#goto_definitions_command| - 5.4. Show documentation |g:jedi#documentation_command| - 5.5. Rename variables |g:jedi#rename_command| - 5.6. Show name usages |g:jedi#usages_command| - 5.7. Open module by name |:Pyimport| + 5.2. Go to definition |g:jedi#goto_command| + 5.3. Go to assignment |g:jedi#goto_assignments_command| + 5.4 Go to definition (deprecated) |g:jedi#goto_definitions_command| + 5.5. Show documentation |g:jedi#documentation_command| + 5.6. Rename variables |g:jedi#rename_command| + 5.7. Show name usages |g:jedi#usages_command| + 5.8. Open module by name |:Pyimport| 6. Configuration |jedi-vim-configuration| 6.1. auto_initialization |g:jedi#auto_initialization| 6.2. auto_vim_configuration |g:jedi#auto_vim_configuration| @@ -233,17 +234,15 @@ Note: If you want to use for completion, please install Supertab: https://github.com/ervandew/supertab. ------------------------------------------------------------------------------ -5.2. `g:jedi#goto_assignments_command` *g:jedi#goto_assignments_command* -Function: `jedi#goto_assignments()` -Default: g Go to definition +5.2. `g:jedi#goto_command` *g:jedi#goto_command* +Function: `jedi#goto()` +Default: d Go to definition (or assignment) -This function finds the first definition of the function/class under the -cursor. It produces an error if the definition is not in a Python file. - ------------------------------------------------------------------------------- -5.3. `g:jedi#goto_definitions_command` *g:jedi#goto_definitions_command* -Function: `jedi#goto_definitions()` -Default: d Go to original definition +This function first tries |jedi#goto_definitions|, and falls back to +|jedi#goto_assignments| for builtin modules. It produces an error if nothing +could be found. +NOTE: this implementation is subject to change. +Ref: https://github.com/davidhalter/jedi/issues/570 This command tries to find the original definition of the function/class under the cursor. Just like the `jedi#goto_assignments()` function, it does not work @@ -275,7 +274,23 @@ you all the way to the > line in file3.py. ------------------------------------------------------------------------------ -5.4. `g:jedi#documentation_command` *g:jedi#documentation_command* +5.3. `g:jedi#goto_assignments_command` *g:jedi#goto_assignments_command* +Function: `jedi#goto_assignments()` +Default: g Go to assignment + +This function finds the first definition of the function/class under the +cursor. It produces an error if the definition is not in a Python file. + +------------------------------------------------------------------------------ +5.4. `g:jedi#goto_definitions_command` *g:jedi#goto_definitions_command* +Function: `jedi#goto_definitions()` +Default: - Go to original definition + +NOTE: Deprecated. Use |g:jedi#goto_command| / |jedi#goto()| instead, which +currently uses this internally. + +------------------------------------------------------------------------------ +5.5. `g:jedi#documentation_command` *g:jedi#documentation_command* Function: `jedi#show_documentation()` Default: Show pydoc documentation @@ -284,7 +299,7 @@ The documentation is opened in a horizontally split buffer. The height of this buffer is controlled by `g:jedi#max_doc_height` (set by default to 30). ------------------------------------------------------------------------------ -5.5. `g:jedi#rename_command` *g:jedi#rename_command* +5.6. `g:jedi#rename_command` *g:jedi#rename_command* Function: `jedi#rename()` Default: r Rename variables @@ -295,7 +310,7 @@ with the new one. The number of performed renames is displayed in the command line. ------------------------------------------------------------------------------ -5.6. `g:jedi#usages_command` *g:jedi#usages_command* +5.7. `g:jedi#usages_command` *g:jedi#usages_command* Function: `jedi#usages()` Default: n Show usages of a name. @@ -303,7 +318,7 @@ The quickfix window is populated with a list of all names which point to the definition of the name under the cursor. ------------------------------------------------------------------------------ -5.7. Open module by name *:Pyimport* +5.8. Open module by name *:Pyimport* Function: `jedi#py_import(args)` Default: :Pyimport e.g. `:Pyimport os` shows os.py in VIM. diff --git a/ftplugin/python/jedi.vim b/ftplugin/python/jedi.vim index 9bbcf4b..bd1de9d 100644 --- a/ftplugin/python/jedi.vim +++ b/ftplugin/python/jedi.vim @@ -7,6 +7,9 @@ endif if g:jedi#auto_initialization " goto / get_definition / usages + if g:jedi#goto_command != '' + execute "nnoremap ".g:jedi#goto_command." :call jedi#goto()" + endif if g:jedi#goto_assignments_command != '' execute "nnoremap ".g:jedi#goto_assignments_command." :call jedi#goto_assignments()" endif diff --git a/jedi_vim.py b/jedi_vim.py index a484b6b..d718e21 100644 --- a/jedi_vim.py +++ b/jedi_vim.py @@ -196,15 +196,23 @@ def completions(): @_check_jedi_availability(show_error=True) @catch_and_print_exceptions -def goto(is_definition=False, is_related_name=False, no_output=False): +def goto(mode = "goto", no_output=False): + """ + mode: "related_name", "definition", "assignment", "auto". + """ definitions = [] script = get_script() try: - if is_related_name: + if mode == "goto": + definitions = [x for x in script.goto_definitions() + if not x.in_builtin_module()] + if not definitions: + definitions = script.goto_assignments() + elif mode == "related_name": definitions = script.usages() - elif is_definition: + elif mode == "definition": definitions = script.goto_definitions() - else: + elif mode == "assignment": definitions = script.goto_assignments() except jedi.NotFoundError: echo_highlight("Cannot follow nothing. Put your cursor on a valid name.") @@ -213,7 +221,7 @@ def goto(is_definition=False, is_related_name=False, no_output=False): return definitions if not definitions: echo_highlight("Couldn't find any definitions for this.") - elif len(definitions) == 1 and not is_related_name: + elif len(definitions) == 1 and mode != "related_name": # just add some mark to add the current position to the jumplist. # this is ugly, because it overrides the mark for '`', so if anyone # has a better idea, let me know.