Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions autoload/jedi.vim
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ let s:default_settings = {
\ 'popup_select_first': 1,
\ 'quickfix_window_height': 10,
\ 'force_py_version': "'auto'",
\ 'smart_auto_mappings': 1,
\ 'smart_auto_mappings': has('timers'),
\ 'smart_auto_mappings_delay': has('timers') ? 200 : 0,
\ 'use_tag_stack': 1
\ }

Expand All @@ -51,6 +52,14 @@ for [s:key, s:val] in items(s:default_settings)
endif
endfor

if !has('timers')
if g:jedi#smart_auto_mappings != 0
echom 'jedi-vim: using g:jedi#smart_auto_mappings_delay requires the timers feature.'
let g:jedi#smart_auto_mappings_delay = 0
endif
lockvar g:jedi#smart_auto_mappings_delay
endif


" ------------------------------------------------------------------------
" Python initialization
Expand Down Expand Up @@ -229,6 +238,7 @@ function! jedi#debug_info() abort
echo "\n"
endif
verb set omnifunc? completeopt?
echo '+timers: '.has('timers')
echo '```'

if &verbose
Expand Down Expand Up @@ -597,12 +607,35 @@ function! jedi#complete_opened(autocomplete) abort
return ''
endfunction


function! jedi#smart_auto_mappings() abort
" Auto put import statement after from module.name<space> and complete
if search('\m^\s*from\s\+[A-Za-z0-9._]\{1,50}\%#\s*$', 'bcn', line('.'))
" Enter character and start completion.
return "\<space>import \<C-r>=jedi#complete_string(1)\<CR>"
function! jedi#smart_auto_mappings(...) abort
" Auto put import statement after from module.name<space> and complete.
if a:0
" Callback from timer.
let pos = getpos('.')
if b:changedtick == s:import_timer[1] + 1
\ && mode() ==# 'i'
\ && s:import_timer[2][1:2] == [pos[1], pos[2]-1]
" Enter characters and start completion.
call feedkeys('import ', 'nt')
if g:jedi#completions_enabled
call feedkeys("\<C-r>=jedi#complete_string(1)\<CR>", 'nt')
endif
endif
return
elseif search('\m^\s*from\s\+[A-Za-z0-9._]\{1,50}\%#\s*$', 'bcn', line('.'))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The search should be done only in the timer callback (more lazy).

if g:jedi#smart_auto_mappings_delay > 0
if exists('s:import_timer')
call timer_stop(s:import_timer[0])
unlet s:import_timer
endif
let s:import_timer = [
\ timer_start(g:jedi#smart_auto_mappings_delay, function('jedi#smart_auto_mappings')),
\ b:changedtick, getpos('.')]
elseif g:jedi#completions_enabled
return "\<space>import \<C-r>=jedi#complete_string(1)\<CR>"
else
return "\<space>import "
endif
endif
return "\<space>"
endfunction
Expand Down
10 changes: 8 additions & 2 deletions doc/jedi-vim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -496,16 +496,22 @@ Options: 2, 2.7, 3, 3.5, 3.6, ...
Default: "auto"
------------------------------------------------------------------------------
6.13. `g:jedi#smart_auto_mappings` *g:jedi#smart_auto_mappings*
*g:jedi#smart_auto_mappings_delay*

When you start typing `from module.name<space>` jedi-vim automatically
adds the "import" statement and displays the autocomplete popup.

This option can be disabled in the .vimrc:
When the |+timers| feature is available it gets triggered after
`g:jedi#smart_auto_mappings_delay` milliseconds (which defaults to 200).
If the buffer changed until the timer times out, this gets skipped.

You can disable or configure this option in the .vimrc:

`let g:jedi#smart_auto_mappings = 0`
`let g:jedi#smart_auto_mappings_delay = 0`

Options: 0 or 1
Default: 1 (enabled by default)
Default: 1 (if |+timers| are available, 0 otherwise)

------------------------------------------------------------------------------
6.14. `g:jedi#use_tag_stack` *g:jedi#use_tag_stack*
Expand Down