@@ -211,49 +211,84 @@ def wrapper(*args, **kwargs):
211
211
return func_receiver
212
212
213
213
214
+ # XXX: might use lru cache, given that you can configure it per buffer?!
214
215
current_environment = (None , None )
215
216
216
217
218
+ def set_environment (executable = None , bufnr = None ):
219
+ global current_environment
220
+
221
+ # print(repr(executable), repr(bufnr))
222
+
223
+ if not executable : # "unset"
224
+ executable = 'auto'
225
+ environment = None
226
+ else :
227
+ try :
228
+ # XXX: create_environment is not deterministic
229
+ # (https://github.com/davidhalter/jedi/issues/1155)
230
+ # jedi.create_environment(executable, safe=False)
231
+ environment = jedi .api .environment .Environment (executable )
232
+ except jedi .InvalidPythonEnvironment as exc :
233
+ echo_highlight ('executable=%s is not supported: %s.' % (
234
+ executable , str (exc )))
235
+ return
236
+ bufnr = int (bufnr )
237
+ if bufnr :
238
+ vim_command ("call setbufvar(%d, 'jedi_use_environment', %r)" % (
239
+ bufnr , executable ))
240
+ else :
241
+ vim_command ("let g:jedi#use_environment = %r" % executable )
242
+ current_environment = (executable , environment )
243
+
244
+
217
245
def get_environment (use_cache = True ):
218
246
global current_environment
219
247
220
- vim_force_python_version = vim_eval ("g:jedi#force_py_version" )
221
- if use_cache and vim_force_python_version == current_environment [0 ]:
248
+ use_environment = vim_eval (
249
+ "get(b:, 'jedi_use_environment', get(g:, 'jedi#use_environment', ''))" )
250
+ if use_cache and use_environment == current_environment [0 ]:
222
251
return current_environment [1 ]
223
252
224
253
environment = None
225
- if vim_force_python_version == "auto" :
254
+ if use_environment == "auto" :
226
255
environment = jedi .api .environment .get_cached_default_environment ()
227
256
else :
228
- force_python_version = vim_force_python_version
229
- if '0000' in force_python_version or '9999' in force_python_version :
230
- # It's probably a float that wasn't shortened.
231
- try :
232
- force_python_version = "{:.1f}" .format (float (force_python_version ))
233
- except ValueError :
234
- pass
235
- elif isinstance (force_python_version , float ):
236
- force_python_version = "{:.1f}" .format (force_python_version )
237
-
238
257
try :
239
- environment = jedi .get_system_environment (force_python_version )
258
+ # XXX: create_environment is not deterministic
259
+ # (https://github.com/davidhalter/jedi/issues/1155)
260
+ # jedi.create_environment(executable, safe=False)
261
+ environment = jedi .api .environment .Environment (use_environment )
240
262
except jedi .InvalidPythonEnvironment as exc :
241
263
environment = jedi .api .environment .get_cached_default_environment ()
242
264
echo_highlight (
243
- "force_python_version =%s is not supported: %s - using %s." % (
244
- vim_force_python_version , str (exc ), str (environment )))
265
+ 'use_environment =%s is not supported: %s - using %s.' % (
266
+ use_environment , str (exc ), str (environment )))
245
267
246
- current_environment = (vim_force_python_version , environment )
268
+ current_environment = (use_environment , environment )
247
269
return environment
248
270
249
271
250
272
def get_known_environments ():
251
273
"""Get known Jedi environments."""
252
- envs = list (jedi .api .environment .find_virtualenvs ())
274
+ paths = vim_eval ("g:jedi#environment_paths" )
275
+ envs = list (jedi .api .environment .find_virtualenvs (paths = paths , safe = False ))
253
276
envs .extend (jedi .api .environment .find_system_environments ())
254
277
return envs
255
278
256
279
280
+ def complete_environments ():
281
+ envs = get_known_environments ()
282
+ try :
283
+ current_executable = current_environment [1 ].executable
284
+ except AttributeError :
285
+ current_executable = None
286
+ vim .command ("return '%s'" % '\n ' .join (
287
+ ('%s (*)' if env .executable == current_executable else '%s' ) % (
288
+ env .executable
289
+ ) for env in envs ))
290
+
291
+
257
292
@catch_and_print_exceptions
258
293
def get_script (source = None , column = None ):
259
294
jedi .settings .additional_dynamic_modules = [
0 commit comments