@@ -21,13 +21,17 @@ class Workspace(object):
2121 M_APPLY_EDIT = 'workspace/applyEdit'
2222 M_SHOW_MESSAGE = 'window/showMessage'
2323
24- def __init__ (self , root_uri , endpoint ):
24+ def __init__ (self , root_uri , endpoint , config = None ):
25+ self ._config = config
2526 self ._root_uri = root_uri
2627 self ._endpoint = endpoint
2728 self ._root_uri_scheme = uris .urlparse (self ._root_uri )[0 ]
2829 self ._root_path = uris .to_fs_path (self ._root_uri )
2930 self ._docs = {}
3031
32+ # Cache jedi environments
33+ self ._environments = {}
34+
3135 # Whilst incubating, keep rope private
3236 self .__rope = None
3337 self .__rope_config = None
@@ -77,6 +81,11 @@ def update_document(self, doc_uri, change, version=None):
7781 self ._docs [doc_uri ].apply_change (change )
7882 self ._docs [doc_uri ].version = version
7983
84+ def update_config (self , config ):
85+ self ._config = config
86+ for doc_uri in self .documents :
87+ self .get_document (doc_uri ).update_config (config )
88+
8089 def apply_edit (self , edit ):
8190 return self ._endpoint .request (self .M_APPLY_EDIT , {'edit' : edit })
8291
@@ -97,17 +106,21 @@ def _create_document(self, doc_uri, source=None, version=None):
97106 doc_uri , source = source , version = version ,
98107 extra_sys_path = self .source_roots (path ),
99108 rope_project_builder = self ._rope_project_builder ,
109+ config = self ._config , workspace = self ,
100110 )
101111
102112
103113class Document (object ):
104114
105- def __init__ (self , uri , source = None , version = None , local = True , extra_sys_path = None , rope_project_builder = None ):
115+ def __init__ (self , uri , source = None , version = None , local = True , extra_sys_path = None , rope_project_builder = None ,
116+ config = None , workspace = None ):
106117 self .uri = uri
107118 self .version = version
108119 self .path = uris .to_fs_path (uri )
109120 self .filename = os .path .basename (self .path )
110121
122+ self ._config = config
123+ self ._workspace = workspace
111124 self ._local = local
112125 self ._source = source
113126 self ._extra_sys_path = extra_sys_path or []
@@ -131,6 +144,9 @@ def source(self):
131144 return f .read ()
132145 return self ._source
133146
147+ def update_config (self , config ):
148+ self ._config = config
149+
134150 def apply_change (self , change ):
135151 """Apply a change to the document."""
136152 text = change ['text' ]
@@ -197,28 +213,58 @@ def word_at_position(self, position):
197213 return m_start [0 ] + m_end [- 1 ]
198214
199215 def jedi_names (self , all_scopes = False , definitions = True , references = False ):
216+ environment_path = None
217+ if self ._config :
218+ jedi_settings = self ._config .plugin_settings ('jedi' , document_path = self .path )
219+ environment_path = jedi_settings .get ('environment' )
220+ environment = self .get_enviroment (environment_path ) if environment_path else None
221+
200222 return jedi .api .names (
201223 source = self .source , path = self .path , all_scopes = all_scopes ,
202- definitions = definitions , references = references
224+ definitions = definitions , references = references , environment = environment ,
203225 )
204226
205227 def jedi_script (self , position = None ):
228+ extra_paths = []
229+ environment_path = None
230+
231+ if self ._config :
232+ jedi_settings = self ._config .plugin_settings ('jedi' , document_path = self .path )
233+ environment_path = jedi_settings .get ('environment' )
234+ extra_paths = jedi_settings .get ('extra_paths' ) or []
235+
236+ sys_path = self .sys_path (environment_path ) + extra_paths
237+ environment = self .get_enviroment (environment_path ) if environment_path else None
238+
206239 kwargs = {
207240 'source' : self .source ,
208241 'path' : self .path ,
209- 'sys_path' : self .sys_path ()
242+ 'sys_path' : sys_path ,
243+ 'environment' : environment ,
210244 }
245+
211246 if position :
212247 kwargs ['line' ] = position ['line' ] + 1
213248 kwargs ['column' ] = _utils .clip_column (position ['character' ], self .lines , position ['line' ])
249+
214250 return jedi .Script (** kwargs )
215251
216- def sys_path (self ):
252+ def get_enviroment (self , environment_path = None ):
253+ # TODO(gatesn): #339 - make better use of jedi environments, they seem pretty powerful
254+ if environment_path is None :
255+ environment = jedi .api .environment .get_cached_default_environment ()
256+ else :
257+ if environment_path in self ._workspace ._environments :
258+ environment = self ._workspace ._environments [environment_path ]
259+ else :
260+ environment = jedi .api .environment .create_environment (path = environment_path , safe = False )
261+ self ._workspace ._environments [environment_path ] = environment
262+
263+ return environment
264+
265+ def sys_path (self , environment_path = None ):
217266 # Copy our extra sys path
218267 path = list (self ._extra_sys_path )
219-
220- # TODO(gatesn): #339 - make better use of jedi environments, they seem pretty powerful
221- environment = jedi .api .environment .get_cached_default_environment ()
268+ environment = self .get_enviroment (environment_path = environment_path )
222269 path .extend (environment .get_sys_path ())
223-
224270 return path
0 commit comments