1010from contextlib import contextmanager
1111from threading import RLock
1212from typing import Callable , Generator , List , Optional
13+ import importlib .metadata
1314
1415import jedi
1516
@@ -392,6 +393,18 @@ def close(self) -> None:
392393
393394
394395class Document :
396+ DO_NOT_PRELOAD_MODULES = ['attrs' , 'backcall' , 'bleach' , 'certifi' , 'chardet' , 'cycler' , 'decorator' , 'defusedxml' ,
397+ 'docopt' , 'entrypoints' , 'idna' , 'importlib-metadata' , 'ipykernel' , 'ipython-genutils' ,
398+ 'ipython' , 'ipywidgets' , 'jedi' , 'jinja2' , 'joblib' , 'jsonschema' , 'jupyter-client' ,
399+ 'jupyter-core' , 'markupsafe' , 'mistune' , 'nbconvert' , 'nbformat' , 'notebook' , 'packaging' ,
400+ 'pandocfilters' , 'parso' , 'pexpect' , 'pickleshare' , 'pip' , 'pipreqs' , 'pluggy' ,
401+ 'prometheus-client' , 'prompt-toolkit' , 'ptyprocess' , 'pygments' , 'pyparsing' ,
402+ 'pyrsistent' , 'python-dateutil' , 'python-jsonrpc-server' , 'python-language-server' ,
403+ 'pytz' , 'pyzmq' , 'send2trash' , 'setuptools' , 'six' , 'terminado' , 'testpath' ,
404+ 'threadpoolctl' , 'tornado' , 'traitlets' , 'ujson' , 'wcwidth' , 'webencodings' , 'wheel' ,
405+ 'widgetsnbextension' , 'yarg' , 'zipp' ]
406+
407+
395408 def __init__ (
396409 self ,
397410 uri ,
@@ -417,6 +430,15 @@ def __init__(
417430 self ._rope_project_builder = rope_project_builder
418431 self ._lock = RLock ()
419432
433+ jedi .settings .cache_directory = '.cache/jedi/'
434+ jedi .settings .use_filesystem_cache = True
435+ jedi .settings .auto_import_modules = self ._get_auto_import_modules ()
436+
437+ def _get_auto_import_modules (self ):
438+ installed_packages_list = [dist .metadata ['Name' ] for dist in importlib .metadata .distributions ()]
439+ auto_import_modules = [pkg for pkg in installed_packages_list if pkg not in self .DO_NOT_PRELOAD_MODULES ]
440+ return auto_import_modules
441+
420442 def __str__ (self ):
421443 return str (self .uri )
422444
@@ -546,12 +568,11 @@ def jedi_script(self, position=None, use_document_path=False):
546568 env_vars = os .environ .copy ()
547569 env_vars .pop ("PYTHONPATH" , None )
548570
549- environment = self .get_enviroment (environment_path , env_vars = env_vars )
550571 sys_path = self .sys_path (
551572 environment_path , env_vars , prioritize_extra_paths , extra_paths
552573 )
553574
554- project_path = self . _workspace . root_path
575+ import __main__
555576
556577 # Extend sys_path with document's path if requested
557578 if use_document_path :
@@ -560,15 +581,14 @@ def jedi_script(self, position=None, use_document_path=False):
560581 kwargs = {
561582 "code" : self .source ,
562583 "path" : self .path ,
563- "environment" : environment if environment_path else None ,
564- "project" : jedi .Project (path = project_path , sys_path = sys_path ),
584+ 'namespaces' : [__main__ .__dict__ ]
565585 }
566586
567587 if position :
568588 # Deprecated by Jedi to use in Script() constructor
569589 kwargs += _utils .position_to_jedi_linecolumn (self , position )
570590
571- return jedi .Script (** kwargs )
591+ return jedi .Interpreter (** kwargs )
572592
573593 def get_enviroment (self , environment_path = None , env_vars = None ):
574594 # TODO(gatesn): #339 - make better use of jedi environments, they seem pretty powerful
0 commit comments