Skip to content

Commit 584dfd9

Browse files
committed
WIP: improve selection of environments
1 parent 7f4f2db commit 584dfd9

File tree

3 files changed

+86
-18
lines changed

3 files changed

+86
-18
lines changed

autoload/jedi.vim

+17
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ let s:default_settings = {
3434
\ 'popup_select_first': 1,
3535
\ 'quickfix_window_height': 10,
3636
\ 'force_py_version': "'auto'",
37+
\ 'use_environment': "'auto'",
38+
\ 'environment_paths': '[]',
3739
\ 'smart_auto_mappings': 1,
3840
\ 'use_tag_stack': 1
3941
\ }
@@ -275,6 +277,14 @@ call jedi#init_python() " Might throw an error.
275277
" ------------------------------------------------------------------------
276278
" functions that call python code
277279
" ------------------------------------------------------------------------
280+
function! jedi#use_environment(...) abort
281+
PythonJedi jedi_vim.set_environment(*vim.eval('a:000'))
282+
endfunction
283+
284+
function! jedi#use_environment_for_buffer(env) abort
285+
PythonJedi jedi_vim.set_environment(*vim.eval('[env, bufnr("%")]'))
286+
endfunction
287+
278288
function! jedi#goto() abort
279289
PythonJedi jedi_vim.goto(mode="goto")
280290
endfunction
@@ -624,6 +634,13 @@ function! jedi#setup_completion() abort
624634
endif
625635
endfunction
626636

637+
" TODO: fallback to completing files (i.e. return known envs first, then
638+
" files?!).
639+
function! jedi#complete_environments(argl, cmdl, pos) abort
640+
PythonJedi jedi_vim.complete_environments()
641+
EOF
642+
endfunction
643+
627644
"PythonJedi jedi_vim.jedi.set_debug_function(jedi_vim.print_to_stdout, speed=True, warnings=False, notices=False)
628645
"PythonJedi jedi_vim.jedi.set_debug_function(jedi_vim.print_to_stdout)
629646

plugin/jedi.vim

+16
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,22 @@ function! s:jedi_debug_info()
6363
call jedi#debug_info()
6464
endfunction
6565
command! -nargs=0 -bar JediDebugInfo call s:jedi_debug_info()
66+
67+
" Transfer bang into current buffer number.
68+
" TODO: use window instead!?
69+
function! s:use_environment(bang, env) abort
70+
let bufnr = a:bang ? +bufnr('%') : 0
71+
if a:env ==# 'auto'
72+
let env = ''
73+
elseif a:env[-4:] ==# ' (*)'
74+
let env = a:env[:-5]
75+
else
76+
let env = a:env
77+
endif
78+
return jedi#use_environment(env, bufnr)
79+
endfunction
80+
command! -bang -nargs=? -complete=custom,jedi#complete_environments JediUseEnvironment :call s:use_environment(<bang>0, <q-args>)
81+
6682
command! -nargs=0 -bang JediClearCache call jedi#clear_cache(<bang>0)
6783

6884
" vim: set et ts=4:

pythonx/jedi_vim.py

+53-18
Original file line numberDiff line numberDiff line change
@@ -157,49 +157,84 @@ def wrapper(*args, **kwargs):
157157
return func_receiver
158158

159159

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

162163

164+
def set_environment(executable=None, bufnr=None):
165+
global current_environment
166+
167+
# print(repr(executable), repr(bufnr))
168+
169+
if not executable: # "unset"
170+
executable = 'auto'
171+
environment = None
172+
else:
173+
try:
174+
# XXX: create_environment is not deterministic
175+
# (https://github.com/davidhalter/jedi/issues/1155)
176+
# jedi.create_environment(executable, safe=False)
177+
environment = jedi.api.environment.Environment(executable)
178+
except jedi.InvalidPythonEnvironment as exc:
179+
echo_highlight('executable=%s is not supported: %s.' % (
180+
executable, str(exc)))
181+
return
182+
bufnr = int(bufnr)
183+
if bufnr:
184+
vim_command("call setbufvar(%d, 'jedi_use_environment', %r)" % (
185+
bufnr, executable))
186+
else:
187+
vim_command("let g:jedi#use_environment = %r" % executable)
188+
current_environment = (executable, environment)
189+
190+
163191
def get_environment(use_cache=True):
164192
global current_environment
165193

166-
vim_force_python_version = vim_eval("g:jedi#force_py_version")
167-
if use_cache and vim_force_python_version == current_environment[0]:
194+
use_environment = vim_eval(
195+
"get(b:, 'jedi_use_environment', get(g:, 'jedi#use_environment', ''))")
196+
if use_cache and use_environment == current_environment[0]:
168197
return current_environment[1]
169198

170199
environment = None
171-
if vim_force_python_version == "auto":
200+
if use_environment == "auto":
172201
environment = jedi.api.environment.get_cached_default_environment()
173202
else:
174-
force_python_version = vim_force_python_version
175-
if '0000' in force_python_version or '9999' in force_python_version:
176-
# It's probably a float that wasn't shortened.
177-
try:
178-
force_python_version = "{:.1f}".format(float(force_python_version))
179-
except ValueError:
180-
pass
181-
elif isinstance(force_python_version, float):
182-
force_python_version = "{:.1f}".format(force_python_version)
183-
184203
try:
185-
environment = jedi.get_system_environment(force_python_version)
204+
# XXX: create_environment is not deterministic
205+
# (https://github.com/davidhalter/jedi/issues/1155)
206+
# jedi.create_environment(executable, safe=False)
207+
environment = jedi.api.environment.Environment(use_environment)
186208
except jedi.InvalidPythonEnvironment as exc:
187209
environment = jedi.api.environment.get_cached_default_environment()
188210
echo_highlight(
189-
"force_python_version=%s is not supported: %s - using %s." % (
190-
vim_force_python_version, str(exc), str(environment)))
211+
'use_environment=%s is not supported: %s - using %s.' % (
212+
use_environment, str(exc), str(environment)))
191213

192-
current_environment = (vim_force_python_version, environment)
214+
current_environment = (use_environment, environment)
193215
return environment
194216

195217

196218
def get_known_environments():
197219
"""Get known Jedi environments."""
198-
envs = list(jedi.api.environment.find_virtualenvs())
220+
paths = vim_eval("g:jedi#environment_paths")
221+
envs = list(jedi.api.environment.find_virtualenvs(paths=paths, safe=False))
199222
envs.extend(jedi.api.environment.find_system_environments())
200223
return envs
201224

202225

226+
def complete_environments():
227+
envs = get_known_environments()
228+
try:
229+
current_executable = current_environment[1].executable
230+
except AttributeError:
231+
current_executable = None
232+
vim.command("return '%s'" % '\n'.join(
233+
('%s (*)' if env.executable == current_executable else '%s') % (
234+
env.executable
235+
) for env in envs))
236+
237+
203238
@catch_and_print_exceptions
204239
def get_script(source=None, column=None):
205240
jedi.settings.additional_dynamic_modules = [

0 commit comments

Comments
 (0)