Skip to content

Commit 945b595

Browse files
committed
WIP: improve selection of environments
1 parent 08f13af commit 945b595

File tree

3 files changed

+86
-18
lines changed

3 files changed

+86
-18
lines changed

autoload/jedi.vim

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ let s:default_settings = {
3636
\ 'quickfix_window_height': 10,
3737
\ 'force_py_version': "'auto'",
3838
\ 'smart_auto_mappings': 0,
39+
\ 'use_environment': "'auto'",
40+
\ 'environment_paths': '[]',
3941
\ 'use_tag_stack': 1
4042
\ }
4143

@@ -289,6 +291,14 @@ call jedi#init_python() " Might throw an error.
289291
" ------------------------------------------------------------------------
290292
" functions that call python code
291293
" ------------------------------------------------------------------------
294+
function! jedi#use_environment(...) abort
295+
PythonJedi jedi_vim.set_environment(*vim.eval('a:000'))
296+
endfunction
297+
298+
function! jedi#use_environment_for_buffer(env) abort
299+
PythonJedi jedi_vim.set_environment(*vim.eval('[env, bufnr("%")]'))
300+
endfunction
301+
292302
function! jedi#goto() abort
293303
PythonJedi jedi_vim.goto(mode="goto")
294304
endfunction
@@ -752,6 +762,13 @@ function! jedi#setup_completion() abort
752762
endif
753763
endfunction
754764

765+
" TODO: fallback to completing files (i.e. return known envs first, then
766+
" files?!).
767+
function! jedi#complete_environments(argl, cmdl, pos) abort
768+
PythonJedi jedi_vim.complete_environments()
769+
EOF
770+
endfunction
771+
755772
"PythonJedi jedi_vim.jedi.set_debug_function(jedi_vim.print_to_stdout, speed=True, warnings=False, notices=False)
756773
"PythonJedi jedi_vim.jedi.set_debug_function(jedi_vim.print_to_stdout)
757774

plugin/jedi.vim

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,22 @@ function! s:jedi_debug_info()
6868
call jedi#debug_info()
6969
endfunction
7070
command! -nargs=0 -bar JediDebugInfo call s:jedi_debug_info()
71+
72+
" Transfer bang into current buffer number.
73+
" TODO: use window instead!?
74+
function! s:use_environment(bang, env) abort
75+
let bufnr = a:bang ? +bufnr('%') : 0
76+
if a:env ==# 'auto'
77+
let env = ''
78+
elseif a:env[-4:] ==# ' (*)'
79+
let env = a:env[:-5]
80+
else
81+
let env = a:env
82+
endif
83+
return jedi#use_environment(env, bufnr)
84+
endfunction
85+
command! -bang -nargs=? -complete=custom,jedi#complete_environments JediUseEnvironment :call s:use_environment(<bang>0, <q-args>)
86+
7187
command! -nargs=0 -bang JediClearCache call jedi#clear_cache(<bang>0)
7288

7389
" vim: set et ts=4:

pythonx/jedi_vim.py

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -209,49 +209,84 @@ def wrapper(*args, **kwargs):
209209
return func_receiver
210210

211211

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

214215

216+
def set_environment(executable=None, bufnr=None):
217+
global current_environment
218+
219+
# print(repr(executable), repr(bufnr))
220+
221+
if not executable: # "unset"
222+
executable = 'auto'
223+
environment = None
224+
else:
225+
try:
226+
# XXX: create_environment is not deterministic
227+
# (https://github.com/davidhalter/jedi/issues/1155)
228+
# jedi.create_environment(executable, safe=False)
229+
environment = jedi.api.environment.Environment(executable)
230+
except jedi.InvalidPythonEnvironment as exc:
231+
echo_highlight('executable=%s is not supported: %s.' % (
232+
executable, str(exc)))
233+
return
234+
bufnr = int(bufnr)
235+
if bufnr:
236+
vim_command("call setbufvar(%d, 'jedi_use_environment', %r)" % (
237+
bufnr, executable))
238+
else:
239+
vim_command("let g:jedi#use_environment = %r" % executable)
240+
current_environment = (executable, environment)
241+
242+
215243
def get_environment(use_cache=True):
216244
global current_environment
217245

218-
vim_force_python_version = vim_eval("g:jedi#force_py_version")
219-
if use_cache and vim_force_python_version == current_environment[0]:
246+
use_environment = vim_eval(
247+
"get(b:, 'jedi_use_environment', get(g:, 'jedi#use_environment', ''))")
248+
if use_cache and use_environment == current_environment[0]:
220249
return current_environment[1]
221250

222251
environment = None
223-
if vim_force_python_version == "auto":
252+
if use_environment == "auto":
224253
environment = jedi.api.environment.get_cached_default_environment()
225254
else:
226-
force_python_version = vim_force_python_version
227-
if '0000' in force_python_version or '9999' in force_python_version:
228-
# It's probably a float that wasn't shortened.
229-
try:
230-
force_python_version = "{:.1f}".format(float(force_python_version))
231-
except ValueError:
232-
pass
233-
elif isinstance(force_python_version, float):
234-
force_python_version = "{:.1f}".format(force_python_version)
235-
236255
try:
237-
environment = jedi.get_system_environment(force_python_version)
256+
# XXX: create_environment is not deterministic
257+
# (https://github.com/davidhalter/jedi/issues/1155)
258+
# jedi.create_environment(executable, safe=False)
259+
environment = jedi.api.environment.Environment(use_environment)
238260
except jedi.InvalidPythonEnvironment as exc:
239261
environment = jedi.api.environment.get_cached_default_environment()
240262
echo_highlight(
241-
"force_python_version=%s is not supported: %s - using %s." % (
242-
vim_force_python_version, str(exc), str(environment)))
263+
'use_environment=%s is not supported: %s - using %s.' % (
264+
use_environment, str(exc), str(environment)))
243265

244-
current_environment = (vim_force_python_version, environment)
266+
current_environment = (use_environment, environment)
245267
return environment
246268

247269

248270
def get_known_environments():
249271
"""Get known Jedi environments."""
250-
envs = list(jedi.api.environment.find_virtualenvs())
272+
paths = vim_eval("g:jedi#environment_paths")
273+
envs = list(jedi.api.environment.find_virtualenvs(paths=paths, safe=False))
251274
envs.extend(jedi.api.environment.find_system_environments())
252275
return envs
253276

254277

278+
def complete_environments():
279+
envs = get_known_environments()
280+
try:
281+
current_executable = current_environment[1].executable
282+
except AttributeError:
283+
current_executable = None
284+
vim.command("return '%s'" % '\n'.join(
285+
('%s (*)' if env.executable == current_executable else '%s') % (
286+
env.executable
287+
) for env in envs))
288+
289+
255290
@catch_and_print_exceptions
256291
def get_script(source=None, column=None):
257292
jedi.settings.additional_dynamic_modules = [

0 commit comments

Comments
 (0)