Add auto-goto command/function

This adds g:jedi#goto_command, and uses `<Leader>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
This commit is contained in:
Daniel Hahler
2015-04-29 12:53:24 +02:00
parent 9a13f143a8
commit dd33e38631
5 changed files with 64 additions and 31 deletions

View File

@@ -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 = "<leader>d"
let g:jedi#goto_assignments_command = "<leader>g"
let g:jedi#goto_definitions_command = "<leader>d"
let g:jedi#goto_definitions_command = ""
let g:jedi#documentation_command = "K"
let g:jedi#usages_command = "<leader>n"
let g:jedi#completions_command = "<C-Space>"

View File

@@ -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': "'<leader>d'",
\ 'goto_assignments_command': "'<leader>g'",
\ 'goto_definitions_command': "''",
\ 'completions_command': "'<C-Space>'",
\ 'goto_definitions_command': "'<leader>d'",
\ 'call_signatures_command': "'<leader>n'",
\ 'usages_command': "'<leader>n'",
\ 'rename_command': "'<leader>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(...)

View File

@@ -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 <Tab> 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: <leader>g Go to definition
5.2. `g:jedi#goto_command` *g:jedi#goto_command*
Function: `jedi#goto()`
Default: <leader>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: <leader>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: <leader>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: <K> 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: <leader>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: <leader>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.

View File

@@ -7,6 +7,9 @@ endif
if g:jedi#auto_initialization
" goto / get_definition / usages
if g:jedi#goto_command != ''
execute "nnoremap <buffer> ".g:jedi#goto_command." :call jedi#goto()<CR>"
endif
if g:jedi#goto_assignments_command != ''
execute "nnoremap <buffer> ".g:jedi#goto_assignments_command." :call jedi#goto_assignments()<CR>"
endif

View File

@@ -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.