From 6155b48b0b82ab5e7998b317d1b589f46fac3e69 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 19 May 2017 17:08:30 +0200 Subject: [PATCH 1/6] Import smart_auto_mappings: use a timer if available Fixes https://github.com/davidhalter/jedi-vim/issues/568. --- autoload/jedi.vim | 31 ++++++++++++++++++++++++------- doc/jedi-vim.txt | 10 ++++++++-- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/autoload/jedi.vim b/autoload/jedi.vim index 4062c30c..4cea7589 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': 200, \ 'use_tag_stack': 1 \ } @@ -597,12 +598,28 @@ 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. + if b:changedtick == s:import_timer[1] + 1 + " Enter characters and start completion. + call feedkeys("import \=jedi#complete_string(1)\", 'n') + 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] + else + " Enter characters and start completion. + return "\import \=jedi#complete_string(1)\" + endif endif return "\" endfunction diff --git a/doc/jedi-vim.txt b/doc/jedi-vim.txt index abd66c5b..2ca2ba09 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 100). +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* From fefb6b5f665a35a0d48ce361b7a761a36e33fcfb Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 19 May 2017 17:27:30 +0200 Subject: [PATCH 2/6] fixup! Import smart_auto_mappings: use a timer if available --- autoload/jedi.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoload/jedi.vim b/autoload/jedi.vim index 4cea7589..fa0da090 100644 --- a/autoload/jedi.vim +++ b/autoload/jedi.vim @@ -604,7 +604,7 @@ function! jedi#smart_auto_mappings(...) abort " Callback from timer. if b:changedtick == s:import_timer[1] + 1 " Enter characters and start completion. - call feedkeys("import \=jedi#complete_string(1)\", 'n') + call feedkeys("import \=jedi#complete_string(1)\", 'nt') endif return elseif search('\m^\s*from\s\+[A-Za-z0-9._]\{1,50}\%#\s*$', 'bcn', line('.')) From 74a76f831672d4e0dee2609a44f04b396c3ef201 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 24 May 2017 20:44:49 +0200 Subject: [PATCH 3/6] fixup! fixup! Import smart_auto_mappings: use a timer if available --- autoload/jedi.vim | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/autoload/jedi.vim b/autoload/jedi.vim index fa0da090..7e569bbb 100644 --- a/autoload/jedi.vim +++ b/autoload/jedi.vim @@ -602,7 +602,10 @@ 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 \=jedi#complete_string(1)\", 'nt') endif @@ -615,7 +618,7 @@ function! jedi#smart_auto_mappings(...) abort endif let s:import_timer = [ \ timer_start(g:jedi#smart_auto_mappings_delay, function('jedi#smart_auto_mappings')), - \ b:changedtick] + \ b:changedtick, getpos('.')] else " Enter characters and start completion. return "\import \=jedi#complete_string(1)\" From 1581e4a8e128307219f32b0c92b826c7b5830b17 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 24 May 2017 20:45:43 +0200 Subject: [PATCH 4/6] squash! fixup! fixup! Import smart_auto_mappings: use a timer if available Only start completion with `g:jedi#completions_enabled`. TODO: doc --- autoload/jedi.vim | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/autoload/jedi.vim b/autoload/jedi.vim index 7e569bbb..9832120e 100644 --- a/autoload/jedi.vim +++ b/autoload/jedi.vim @@ -607,7 +607,10 @@ function! jedi#smart_auto_mappings(...) abort \ && mode() ==# 'i' \ && s:import_timer[2][1:2] == [pos[1], pos[2]-1] " Enter characters and start completion. - call feedkeys("import \=jedi#complete_string(1)\", 'nt') + 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('.')) From 659bc2977d8273a577b7bd997d47710f5e4684c0 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 8 Aug 2017 17:10:42 +0200 Subject: [PATCH 5/6] Handle/lock g:jedi#smart_auto_mappings_delay without timers --- autoload/jedi.vim | 11 ++++++++++- doc/jedi-vim.txt | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/autoload/jedi.vim b/autoload/jedi.vim index 9832120e..20bf899e 100644 --- a/autoload/jedi.vim +++ b/autoload/jedi.vim @@ -35,7 +35,7 @@ let s:default_settings = { \ 'quickfix_window_height': 10, \ 'force_py_version': "'auto'", \ 'smart_auto_mappings': has('timers'), - \ 'smart_auto_mappings_delay': 200, + \ 'smart_auto_mappings_delay': has('timers') ? 200 : 0, \ 'use_tag_stack': 1 \ } @@ -52,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 @@ -230,6 +238,7 @@ function! jedi#debug_info() abort echo "\n" endif verb set omnifunc? completeopt? + echo '+timers: '.has('timers') echo '```' if &verbose diff --git a/doc/jedi-vim.txt b/doc/jedi-vim.txt index 2ca2ba09..c395d884 100644 --- a/doc/jedi-vim.txt +++ b/doc/jedi-vim.txt @@ -502,7 +502,7 @@ When you start typing `from module.name` jedi-vim automatically adds the "import" statement and displays the autocomplete popup. When the |+timers| feature is available it gets triggered after -`g:jedi#smart_auto_mappings_delay` milliseconds (which defaults to 100). +`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: From 997bb1655639285217348c30f3cbe7b892a9b76f Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 8 Aug 2017 17:13:14 +0200 Subject: [PATCH 6/6] jedi#smart_auto_mappings: handle disabled completions with no timers --- autoload/jedi.vim | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/autoload/jedi.vim b/autoload/jedi.vim index 20bf899e..55fa63df 100644 --- a/autoload/jedi.vim +++ b/autoload/jedi.vim @@ -631,9 +631,10 @@ function! jedi#smart_auto_mappings(...) abort let s:import_timer = [ \ timer_start(g:jedi#smart_auto_mappings_delay, function('jedi#smart_auto_mappings')), \ b:changedtick, getpos('.')] - else - " Enter characters and start completion. + elseif g:jedi#completions_enabled return "\import \=jedi#complete_string(1)\" + else + return "\import " endif endif return "\"