From 3ad63c9458cf47f12117dcefa8522e4633ffade3 Mon Sep 17 00:00:00 2001 From: Asa Jay Date: Fri, 13 Dec 2013 20:51:24 +0100 Subject: [PATCH 1/2] Implemented: force python 2 or 3 with ability to switch during runtime Simultaneous python2 and python3 support, switchable during runtime calling a function as well as setting a variable in .vimrc! Closes issues #200 and #210 (which gave the idea for this commit, thank you @Shtucer). --- AUTHORS.txt | 1 + autoload/jedi.vim | 82 +++++++++++++++++++++++++++-------------------- doc/jedi-vim.txt | 21 +++++++++++- initialize.py | 25 +++++++++++++++ 4 files changed, 94 insertions(+), 35 deletions(-) create mode 100644 initialize.py diff --git a/AUTHORS.txt b/AUTHORS.txt index d7b6da5..18e97b0 100644 --- a/AUTHORS.txt +++ b/AUTHORS.txt @@ -35,6 +35,7 @@ Stephen J. Fuhry (@fuhrysteve) Sheng Yun (@ShengYun) Yann Thomas-Gérard (@inside) Colin Su (@littleq0903) +Arthur Jaron (@eyetracker) @something are github user names. diff --git a/autoload/jedi.vim b/autoload/jedi.vim index 201a8e3..9d33745 100644 --- a/autoload/jedi.vim +++ b/autoload/jedi.vim @@ -1,4 +1,4 @@ -" ------------------------------------------------------------------------ +" ------------------------------------------------------------------------ " functions that call python code " ------------------------------------------------------------------------ function! jedi#goto_assignments() @@ -193,6 +193,23 @@ function! jedi#complete_opened() endfunction +function! jedi#force_pycmd(pycmd) + let g:jedi#force_pycmd = a:pycmd + exec("command! -nargs=1 Python ".a:pycmd." ") +endfunction + + +function! jedi#force_pycmd_switch() + if g:jedi#force_pycmd == 'python' + call jedi#force_pycmd('python3') + elseif g:jedi#force_pycmd == 'python3' + call jedi#force_pycmd('python') + else + " to be able switch from custom pycmd's + call jedi#force_pycmd('python') + endif +endfunction + " ------------------------------------------------------------------------ " deprecations @@ -207,18 +224,9 @@ let s:deprecations = { \ 'show_function_definition': 'show_call_signatures', \ } -for [key, val] in items(s:deprecations) - if exists('g:jedi#'.key) - echom "'g:jedi#".key."' is deprecated. Please use 'g:jedi#".val."' instead. Sorry for the inconvenience." - exe 'let g:jedi#'.val.' = g:jedi#'.key - end -endfor - - " ------------------------------------------------------------------------ " defaults for jedi-vim " ------------------------------------------------------------------------ - let s:settings = { \ 'use_tabs_not_buffers': 1, \ 'use_splits_not_buffers': 1, @@ -237,15 +245,29 @@ let s:settings = { \ 'auto_close_doc': 1, \ 'popup_select_first': 1, \ 'quickfix_window_height': 10, - \ 'completions_enabled': 1 + \ 'completions_enabled': 1, + \ 'force_pycmd': "'python'" \ } -for [key, val] in items(s:settings) - if !exists('g:jedi#'.key) - exe 'let g:jedi#'.key.' = '.val - endif -endfor +function! s:init() + for [key, val] in items(s:deprecations) + if exists('g:jedi#'.key) + echom "'g:jedi#".key."' is deprecated. Please use 'g:jedi#".val."' instead. Sorry for the inconvenience." + exe 'let g:jedi#'.val.' = g:jedi#'.key + end + endfor + + + for [key, val] in items(s:settings) + if !exists('g:jedi#'.key) + exe 'let g:jedi#'.key.' = '.val + endif + endfor +endfunction + + +call s:init() " ------------------------------------------------------------------------ " Python initialization @@ -262,27 +284,19 @@ else finish end -Python << PYTHONEOF -""" here we initialize the jedi stuff """ -import vim +if has('python') && has('python3') + call jedi#force_pycmd(g:jedi#force_pycmd) +endif -# update the system path, to include the jedi path -import sys -import os -sys.path.insert(0, os.path.join(vim.eval('expand(":p:h:h")'), 'jedi')) +" jedi_vim.py has to be imported for 'python' and 'python3' each +if has('python') + execute 'pyfile '.fnameescape(expand(':p:h:h')).'/initialize.py' +endif -# to display errors correctly -import traceback +if has('python3') + execute 'py3file '.fnameescape(expand(':p:h:h')).'/initialize.py' +endif -# update the sys path to include the jedi_vim script -sys.path.insert(1, vim.eval('expand(":p:h:h")')) -try: - import jedi_vim -except ImportError: - vim.command('echoerr "Please install Jedi if you want to use jedi_vim."') -sys.path.pop(1) - -PYTHONEOF "Python jedi_vim.jedi.set_debug_function(jedi_vim.print_to_stdout, speed=True, warnings=False, notices=False) "Python jedi_vim.jedi.set_debug_function(jedi_vim.print_to_stdout) diff --git a/doc/jedi-vim.txt b/doc/jedi-vim.txt index 20c1140..feac2db 100644 --- a/doc/jedi-vim.txt +++ b/doc/jedi-vim.txt @@ -38,11 +38,11 @@ Contents *jedi-vim-contents* 6.8. squelch_py_warning |g:jedi#squelch_py_warning| 6.9. completions_enable |g:jedi#completions_enable| 6.10. use_splits_not_buffers |g:jedi#use_splits_not_buffers| + 6.11. force_pycmd |g:jedi#force_pycmd| 7. Testing |jedi-vim-testing| 8. Contributing |jedi-vim-contributing| 9. License |jedi-vim-license| - ============================================================================== 1. Introduction *jedi-vim-introduction* @@ -404,6 +404,25 @@ direction which you want to open a split with. Options: top, left, right or bottom Default: "" (not enabled by default) +------------------------------------------------------------------------------ +6.11. `g:jedi#force_pycmd` *g:jedi#force_pycmd* + +If you have installed both python 2 and python 3, you can force which one jedi +should use by setting this variable. It forces the internal Vim command, which +will be used for every jedi call to the respective python interpreter. +The variable can be set in the .vimrc like this: + +let g:jedi#force_pycmd = 'python3' + +This variable can be switched during runtime using the following function: +Function: `jedi#force_pycmd_switch()` + +or set directly using this function, which has the same name as the variable: +Function: `jedi#force_pycmd(pycmd)` + +Options: 'python' or 'python3' +Default: 'python' + ============================================================================== 7. Testing *jedi-vim-testing* diff --git a/initialize.py b/initialize.py new file mode 100644 index 0000000..04e7ce3 --- /dev/null +++ b/initialize.py @@ -0,0 +1,25 @@ +''' ------------------------------------------------------------------------ +Python initialization +--------------------------------------------------------------------------- +here we initialize the jedi stuff ''' + +import vim + +# update the system path, to include the jedi path +import sys +import os + +# vim.command('echom expand(":p:h:h")') +sys.path.insert(0, os.path.join(vim.eval('expand(":p:h:h")'), 'jedi')) + +# to display errors correctly +import traceback + +# update the sys path to include the jedi_vim script +sys.path.insert(1, vim.eval('expand(":p:h:h")')) +try: + import jedi_vim +except ImportError: + vim.command('echoerr "Please install Jedi if you want to use jedi_vim."') +sys.path.pop(1) + From b1205ce7794b352b3a8aaf5aedb57d95c0167f89 Mon Sep 17 00:00:00 2001 From: Asa Jay Date: Sun, 15 Dec 2013 17:01:28 +0100 Subject: [PATCH 2/2] Load only default/forced py_version, changed names and variable type 1. Changed pycmd: renamed to py_version, changed type, resulting in ...#force_py_version = (2 or 3) 2. Changed structure of initialization, changes broke functionality ( behaviour changes inside a function) 3. Cleaned up. --- autoload/jedi.vim | 47 ++++++++++++++++++++++------------------------- doc/jedi-vim.txt | 16 ++++++++-------- initialize.py | 7 ++++--- 3 files changed, 34 insertions(+), 36 deletions(-) diff --git a/autoload/jedi.vim b/autoload/jedi.vim index 9d33745..c052ed1 100644 --- a/autoload/jedi.vim +++ b/autoload/jedi.vim @@ -193,20 +193,23 @@ function! jedi#complete_opened() endfunction -function! jedi#force_pycmd(pycmd) - let g:jedi#force_pycmd = a:pycmd - exec("command! -nargs=1 Python ".a:pycmd." ") +function! jedi#force_py_version(py_version) + let g:jedi#force_py_version = a:py_version + if g:jedi#force_py_version == 2 + command! -nargs=1 Python python + execute 'pyfile '.s:script_path.'/initialize.py' + elseif g:jedi#force_py_version == 3 + command! -nargs=1 Python python3 + execute 'py3file '.s:script_path.'/initialize.py' + endif endfunction -function! jedi#force_pycmd_switch() - if g:jedi#force_pycmd == 'python' - call jedi#force_pycmd('python3') - elseif g:jedi#force_pycmd == 'python3' - call jedi#force_pycmd('python') - else - " to be able switch from custom pycmd's - call jedi#force_pycmd('python') +function! jedi#force_py_version_switch() + if g:jedi#force_py_version == 2 + call jedi#force_py_version(3) + elseif g:jedi#force_py_version == 3 + call jedi#force_py_version(2) endif endfunction @@ -246,7 +249,7 @@ let s:settings = { \ 'popup_select_first': 1, \ 'quickfix_window_height': 10, \ 'completions_enabled': 1, - \ 'force_pycmd': "'python'" + \ 'force_py_version': 2 \ } @@ -273,10 +276,16 @@ call s:init() " Python initialization " ------------------------------------------------------------------------ -if has('python') +let s:script_path = fnameescape(expand(':p:h:h')) + +if has('python') && has('python3') + call jedi#force_py_version(g:jedi#force_py_version) +elseif has('python') command! -nargs=1 Python python + execute 'pyfile '.s:script_path.'/initialize.py' elseif has('python3') command! -nargs=1 Python python3 + execute 'py3file '.s:script_path.'/initialize.py' else if !exists("g:jedi#squelch_py_warning") echomsg "Error: jedi-vim requires vim compiled with +python" @@ -284,18 +293,6 @@ else finish end -if has('python') && has('python3') - call jedi#force_pycmd(g:jedi#force_pycmd) -endif - -" jedi_vim.py has to be imported for 'python' and 'python3' each -if has('python') - execute 'pyfile '.fnameescape(expand(':p:h:h')).'/initialize.py' -endif - -if has('python3') - execute 'py3file '.fnameescape(expand(':p:h:h')).'/initialize.py' -endif "Python jedi_vim.jedi.set_debug_function(jedi_vim.print_to_stdout, speed=True, warnings=False, notices=False) "Python jedi_vim.jedi.set_debug_function(jedi_vim.print_to_stdout) diff --git a/doc/jedi-vim.txt b/doc/jedi-vim.txt index feac2db..ee69544 100644 --- a/doc/jedi-vim.txt +++ b/doc/jedi-vim.txt @@ -38,7 +38,7 @@ Contents *jedi-vim-contents* 6.8. squelch_py_warning |g:jedi#squelch_py_warning| 6.9. completions_enable |g:jedi#completions_enable| 6.10. use_splits_not_buffers |g:jedi#use_splits_not_buffers| - 6.11. force_pycmd |g:jedi#force_pycmd| + 6.11. force_py_version |g:jedi#force_py_version| 7. Testing |jedi-vim-testing| 8. Contributing |jedi-vim-contributing| 9. License |jedi-vim-license| @@ -405,23 +405,23 @@ Options: top, left, right or bottom Default: "" (not enabled by default) ------------------------------------------------------------------------------ -6.11. `g:jedi#force_pycmd` *g:jedi#force_pycmd* +6.11. `g:jedi#force_py_version` *g:jedi#force_py_version* If you have installed both python 2 and python 3, you can force which one jedi should use by setting this variable. It forces the internal Vim command, which will be used for every jedi call to the respective python interpreter. -The variable can be set in the .vimrc like this: +The variable can be set in the .vimrc like this to force python 3: -let g:jedi#force_pycmd = 'python3' +let g:jedi#force_py_version = 3 This variable can be switched during runtime using the following function: -Function: `jedi#force_pycmd_switch()` +Function: `jedi#force_py_version_switch()` or set directly using this function, which has the same name as the variable: -Function: `jedi#force_pycmd(pycmd)` +Function: `jedi#force_py_version(py_version)` -Options: 'python' or 'python3' -Default: 'python' +Options: 2 or 3 +Default: 2 ============================================================================== 7. Testing *jedi-vim-testing* diff --git a/initialize.py b/initialize.py index 04e7ce3..11283bb 100644 --- a/initialize.py +++ b/initialize.py @@ -9,14 +9,15 @@ import vim import sys import os -# vim.command('echom expand(":p:h:h")') -sys.path.insert(0, os.path.join(vim.eval('expand(":p:h:h")'), 'jedi')) +# vim.command('echom expand(":p:h:h")') # broken, inside function +# sys.path.insert(0, os.path.join(vim.eval('expand(":p:h:h")'), 'jedi')) +sys.path.insert(0, os.path.join(vim.eval('s:script_path'), 'jedi')) # to display errors correctly import traceback # update the sys path to include the jedi_vim script -sys.path.insert(1, vim.eval('expand(":p:h:h")')) +sys.path.insert(0, vim.eval('s:script_path')) try: import jedi_vim except ImportError: