From ac1615c647da766534759ece8b253e3a6dd543ee Mon Sep 17 00:00:00 2001 From: Ali Aliyev Date: Mon, 17 Aug 2015 08:09:39 +0500 Subject: [PATCH 1/5] Smart completion after "from foo": add "import" and complete This adds a new setting `jedi#smart_auto_mappings` (default 1). --- autoload/jedi.vim | 13 ++++++++++++- doc/jedi-vim.txt | 13 +++++++++++++ ftplugin/python/jedi.vim | 4 ++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/autoload/jedi.vim b/autoload/jedi.vim index 0f7413a..affc590 100644 --- a/autoload/jedi.vim +++ b/autoload/jedi.vim @@ -32,7 +32,8 @@ let s:default_settings = { \ 'popup_select_first': 1, \ 'quickfix_window_height': 10, \ 'completions_enabled': 1, - \ 'force_py_version': "'auto'" + \ 'force_py_version': "'auto'", + \ 'smart_auto_mappings': 1 \ } for [key, val] in items(s:deprecations) @@ -422,6 +423,16 @@ function! jedi#complete_opened(is_popup_on_dot) endfunction +function! jedi#smart_auto_mappings(is_popup_on_dot) + " Auto put import statement after from module.name and complete + if search('\import \\\=jedi#complete_opened(".a:is_popup_on_dot.")\" + endif + return "\" +endfunction + + "PythonJedi jedi_vim.jedi.set_debug_function(jedi_vim.print_to_stdout, speed=True, warnings=False, notices=False) "PythonJedi jedi_vim.jedi.set_debug_function(jedi_vim.print_to_stdout) diff --git a/doc/jedi-vim.txt b/doc/jedi-vim.txt index 21d70f6..be17a35 100644 --- a/doc/jedi-vim.txt +++ b/doc/jedi-vim.txt @@ -41,6 +41,7 @@ Contents *jedi-vim-contents* 6.9. completions_enabled |g:jedi#completions_enabled| 6.10. use_splits_not_buffers |g:jedi#use_splits_not_buffers| 6.11. force_py_version |g:jedi#force_py_version| + 6.12. smart_auto_mappings |g:jedi#smart_auto_mappings| 7. Testing |jedi-vim-testing| 8. Contributing |jedi-vim-contributing| 9. License |jedi-vim-license| @@ -481,6 +482,18 @@ Function: `jedi#force_py_version(py_version)` Options: 2 or 3 Default: "auto" (will use sys.version_info from "python" in your $PATH) +------------------------------------------------------------------------------ +6.12. `g:jedi#smart_auto_mappings` *g:jedi#smart_auto_mappings* + +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: + +`let g:jedi#smart_auto_mappings = 0` + +Options: 0 or 1 +Default: 1 (enabled by default) ============================================================================== 7. Testing *jedi-vim-testing* diff --git a/ftplugin/python/jedi.vim b/ftplugin/python/jedi.vim index bd1de9d..4cc811a 100644 --- a/ftplugin/python/jedi.vim +++ b/ftplugin/python/jedi.vim @@ -37,6 +37,10 @@ if g:jedi#auto_initialization inoremap . .=jedi#complete_string(1) endif + if g:jedi#smart_auto_mappings == 1 + inoremap jedi#smart_auto_mappings(g:jedi#popup_on_dot) + end + if g:jedi#auto_close_doc " close preview if its still open after insert autocmd InsertLeave if pumvisible() == 0|pclose|endif From 889feae117f1f32428477065df9188c7db2168b2 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 18 Aug 2015 10:28:27 +0200 Subject: [PATCH 2/5] jedi#smart_auto_mappings: always 'popup_on_dot' `popup_on_dot=1` means that it should not auto-select the first entry, which is the case for the new smart-import-mapping. --- autoload/jedi.vim | 4 ++-- ftplugin/python/jedi.vim | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/autoload/jedi.vim b/autoload/jedi.vim index affc590..db750ab 100644 --- a/autoload/jedi.vim +++ b/autoload/jedi.vim @@ -423,11 +423,11 @@ function! jedi#complete_opened(is_popup_on_dot) endfunction -function! jedi#smart_auto_mappings(is_popup_on_dot) +function! jedi#smart_auto_mappings() " Auto put import statement after from module.name and complete if search('\import \\\=jedi#complete_opened(".a:is_popup_on_dot.")\" + return "\import \\\=jedi#complete_opened(1)\" endif return "\" endfunction diff --git a/ftplugin/python/jedi.vim b/ftplugin/python/jedi.vim index 4cc811a..26f26ee 100644 --- a/ftplugin/python/jedi.vim +++ b/ftplugin/python/jedi.vim @@ -38,7 +38,7 @@ if g:jedi#auto_initialization endif if g:jedi#smart_auto_mappings == 1 - inoremap jedi#smart_auto_mappings(g:jedi#popup_on_dot) + inoremap jedi#smart_auto_mappings() end if g:jedi#auto_close_doc From 6dedbf339ec80053aef7e0ed606bef23739d48fd Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 18 Aug 2015 10:28:59 +0200 Subject: [PATCH 3/5] smart_auto_mappings: improve pattern --- autoload/jedi.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoload/jedi.vim b/autoload/jedi.vim index db750ab..eff65e0 100644 --- a/autoload/jedi.vim +++ b/autoload/jedi.vim @@ -425,7 +425,7 @@ endfunction function! jedi#smart_auto_mappings() " Auto put import statement after from module.name and complete - if search('\import \\\=jedi#complete_opened(1)\" endif From 07034941888c0787ab956a0ecc5a17247070bf7d Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 19 Aug 2015 22:30:24 +0200 Subject: [PATCH 4/5] smart_auto_mappings: use C-r instead of expr-mapping This makes it work in tests. Using `` instead will not trigger the expansion of the mapping. This is likely a bug in vim-vspec; I have tried 1.4.1 (currently pinned) and 1.6.1. --- ftplugin/python/jedi.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ftplugin/python/jedi.vim b/ftplugin/python/jedi.vim index 26f26ee..f1885ec 100644 --- a/ftplugin/python/jedi.vim +++ b/ftplugin/python/jedi.vim @@ -38,7 +38,7 @@ if g:jedi#auto_initialization endif if g:jedi#smart_auto_mappings == 1 - inoremap jedi#smart_auto_mappings() + inoremap =jedi#smart_auto_mappings() end if g:jedi#auto_close_doc From 91c2a031dfb8193fcc204633d0cb005ee1b60cae Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 19 Aug 2015 22:32:25 +0200 Subject: [PATCH 5/5] Add tests for smart_auto_mappings --- test/completions.vim | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/completions.vim b/test/completions.vim index cd57da9..60cafbe 100644 --- a/test/completions.vim +++ b/test/completions.vim @@ -11,6 +11,17 @@ describe 'completions' bd! end + it 'smart import' + exec "normal ifrom os " + Expect getline('.') == 'from os import ' + end + + it 'no smart import after space' + exec "normal! ifrom os " + exec "normal a " + Expect getline('.') == 'from os ' + end + it 'import' " X is the completion command normal oimporX