Skip to content

Commit

Permalink
WIP: improve selection of environments
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Jul 10, 2018
1 parent 1773837 commit f36522b
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 26 deletions.
38 changes: 31 additions & 7 deletions autoload/jedi.vim
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ let s:default_settings = {
\ 'popup_select_first': 1,
\ 'quickfix_window_height': 10,
\ 'force_py_version': "'auto'",
\ 'use_environment': "'auto'",
\ 'environment_paths': '[]',
\ 'smart_auto_mappings': 1,
\ 'use_tag_stack': 1
\ }
Expand Down Expand Up @@ -170,7 +172,19 @@ function! jedi#debug_info() abort
endif
endif
echo '#### Jedi-vim debug information'
echo 'Using Python version:' s:python_version
echo "\n"
echo '##### jedi-vim version'
echo "\n"
echo ' - jedi-vim git version: '
echon substitute(system('git -C '.s:script_path.' describe --tags --always --dirty'), '\v\n$', '', '')
echo ' - jedi git submodule status: '
echon substitute(system('git -C '.s:script_path.' submodule status pythonx/jedi'), '\v\n$', '', '')
echo ' - parso git submodule status: '
echon substitute(system('git -C '.s:script_path.' submodule status pythonx/parso'), '\v\n$', '', '')
echo "\n"
echo '##### Global Python'
echo "\n"
echo 'Using Python version '.s:python_version.' to access Jedi.'
let pyeval = s:python_version == 3 ? 'py3eval' : 'pyeval'
let s:pythonjedi_called = 0
PythonJedi import vim; vim.command('let s:pythonjedi_called = 1')
Expand All @@ -185,14 +199,9 @@ function! jedi#debug_info() abort
PythonJedi from jedi_vim_debug import display_debug_info
PythonJedi display_debug_info()
endif
echo ' - jedi-vim git version: '
echon substitute(system('git -C '.s:script_path.' describe --tags --always --dirty'), '\v\n$', '', '')
echo ' - jedi git submodule status: '
echon substitute(system('git -C '.s:script_path.' submodule status pythonx/jedi'), '\v\n$', '', '')
echo ' - parso git submodule status: '
echon substitute(system('git -C '.s:script_path.' submodule status pythonx/parso'), '\v\n$', '', '')
echo "\n"
echo '##### Settings'
echo "\n"
echo '```'
let jedi_settings = items(filter(copy(g:), "v:key =~# '\\v^jedi#'"))
let has_nondefault_settings = 0
Expand Down Expand Up @@ -257,6 +266,14 @@ call jedi#init_python() " Might throw an error.
" ------------------------------------------------------------------------
" functions that call python code
" ------------------------------------------------------------------------
function! jedi#use_environment(...) abort
PythonJedi jedi_vim.set_environment(*vim.eval('a:000'))
endfunction

function! jedi#use_environment_for_buffer(env) abort
PythonJedi jedi_vim.set_environment(*vim.eval('[env, bufnr("%")]'))
endfunction

function! jedi#goto() abort
PythonJedi jedi_vim.goto(mode="goto")
endfunction
Expand Down Expand Up @@ -597,6 +614,13 @@ function! jedi#setup_completion() abort
endif
endfunction

" TODO: fallback to completing files (i.e. return known envs first, then
" files?!).
function! jedi#complete_environments(argl, cmdl, pos) abort
PythonJedi jedi_vim.complete_environments()
EOF
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)

Expand Down
16 changes: 16 additions & 0 deletions plugin/jedi.vim
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ function! s:jedi_debug_info()
call jedi#debug_info()
endfunction
command! -nargs=0 -bar JediDebugInfo call s:jedi_debug_info()

" Transfer bang into current buffer number.
" TODO: use window instead!?
function! s:use_environment(bang, env) abort
let bufnr = a:bang ? +bufnr('%') : 0
if a:env ==# 'auto'
let env = ''
elseif a:env[-4:] ==# ' (*)'
let env = a:env[:-5]
else
let env = a:env
endif
return jedi#use_environment(env, bufnr)
endfunction
command! -bang -nargs=? -complete=custom,jedi#complete_environments JediUseEnvironment :call s:use_environment(<bang>0, <q-args>)

command! -nargs=0 -bang JediClearCache call jedi#clear_cache(<bang>0)

" vim: set et ts=4:
76 changes: 59 additions & 17 deletions pythonx/jedi_vim.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,42 +157,84 @@ def wrapper(*args, **kwargs):
return func_receiver


# XXX: might use lru cache, given that you can configure it per buffer?!
current_environment = (None, None)


def set_environment(executable=None, bufnr=None):
global current_environment

# print(repr(executable), repr(bufnr))

if not executable: # "unset"
executable = 'auto'
environment = None
else:
try:
# XXX: create_environment is not deterministic
# (https://github.com/davidhalter/jedi/issues/1155)
# jedi.create_environment(executable, safe=False)
environment = jedi.api.environment.Environment(executable)
except jedi.InvalidPythonEnvironment as exc:
echo_highlight('executable=%s is not supported: %s.' % (
executable, str(exc)))
return
bufnr = int(bufnr)
if bufnr:
vim_command("call setbufvar(%d, 'jedi_use_environment', %r)" % (
bufnr, executable))
else:
vim_command("let g:jedi#use_environment = %r" % executable)
current_environment = (executable, environment)


def get_environment(use_cache=True):
global current_environment

vim_force_python_version = vim_eval("g:jedi#force_py_version")
if use_cache and vim_force_python_version == current_environment[0]:
use_environment = vim_eval(
"get(b:, 'jedi_use_environment', get(g:, 'jedi#use_environment', ''))")
if use_cache and use_environment == current_environment[0]:
return current_environment[1]

environment = None
if vim_force_python_version == "auto":
if use_environment == "auto":
environment = jedi.api.environment.get_cached_default_environment()
else:
force_python_version = vim_force_python_version
if '0000' in force_python_version or '9999' in force_python_version:
# It's probably a float that wasn't shortened.
try:
force_python_version = "{:.1f}".format(float(force_python_version))
except ValueError:
pass
elif isinstance(force_python_version, float):
force_python_version = "{:.1f}".format(force_python_version)

try:
environment = jedi.get_system_environment(force_python_version)
# XXX: create_environment is not deterministic
# (https://github.com/davidhalter/jedi/issues/1155)
# jedi.create_environment(executable, safe=False)
environment = jedi.api.environment.Environment(use_environment)
except jedi.InvalidPythonEnvironment as exc:
environment = jedi.api.environment.get_cached_default_environment()
echo_highlight(
"force_python_version=%s is not supported: %s - using %s." % (
vim_force_python_version, str(exc), str(environment)))
'use_environment=%s is not supported: %s - using %s.' % (
use_environment, str(exc), str(environment)))

current_environment = (vim_force_python_version, environment)
current_environment = (use_environment, environment)
return environment


def get_known_environments():
"""Get known Jedi environments."""
paths = vim_eval("g:jedi#environment_paths")
envs = list(jedi.api.environment.find_virtualenvs(paths=paths, safe=False))
envs.extend(jedi.api.environment.find_system_environments())
return envs


def complete_environments():
envs = get_known_environments()
try:
current_executable = current_environment[1].executable
except AttributeError:
current_executable = None
vim.command("return '%s'" % '\n'.join(
('%s (*)' if env.executable == current_executable else '%s') % (
env.executable
) for env in envs))


@catch_and_print_exceptions
def get_script(source=None, column=None):
jedi.settings.additional_dynamic_modules = [
Expand Down
13 changes: 11 additions & 2 deletions pythonx/jedi_vim_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def display_debug_info():
echo_error('ERROR: could not import the "jedi" Python module: {0}'.format(
error_msg))
else:
echo('Jedi path: `{0}`'.format(jedi_vim.jedi.__file__))
echo('\n##### Jedi\n\n - path: `{0}`'.format(jedi_vim.jedi.__file__))
echo(' - version: {0}'.format(jedi_vim.jedi.__version__))

try:
Expand All @@ -65,7 +65,8 @@ def display_debug_info():
except AttributeError:
sys_path = script_evaluator.sys_path
else:
echo(' - environment: `{0}`'.format(environment))
echo('\n##### Jedi environment: {0}\n\n'.format(environment))
echo(' - executable: {0}'.format(environment.executable))
try:
sys_path = environment.get_sys_path()
except Exception:
Expand All @@ -76,3 +77,11 @@ def display_debug_info():
echo(' - sys_path:')
for p in sys_path:
echo(' - `{0}`'.format(p))

if environment:
echo('\n##### Known environments\n\n')
for environment in jedi_vim.get_known_environments():
echo(' - {0} ({1})\n'.format(
environment,
environment.executable,
))

0 comments on commit f36522b

Please sign in to comment.