diff --git a/autoload/jedi.vim b/autoload/jedi.vim index 4062c30c..55fa63df 100644 --- a/autoload/jedi.vim +++ b/autoload/jedi.vim @@ -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 \ } @@ -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 @@ -229,6 +238,7 @@ function! jedi#debug_info() abort echo "\n" endif verb set omnifunc? completeopt? + echo '+timers: '.has('timers') echo '```' if &verbose @@ -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 and complete - if search('\m^\s*from\s\+[A-Za-z0-9._]\{1,50}\%#\s*$', 'bcn', line('.')) - " Enter character and start completion. - return "\import \=jedi#complete_string(1)\" +function! jedi#smart_auto_mappings(...) abort + " Auto put import statement after from module.name 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("\=jedi#complete_string(1)\", 'nt') + endif + endif + return + elseif search('\m^\s*from\s\+[A-Za-z0-9._]\{1,50}\%#\s*$', 'bcn', line('.')) + 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 "\import \=jedi#complete_string(1)\" + else + return "\import " + endif endif return "\" endfunction diff --git a/doc/jedi-vim.txt b/doc/jedi-vim.txt index abd66c5b..c395d884 100644 --- a/doc/jedi-vim.txt +++ b/doc/jedi-vim.txt @@ -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` 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*