@@ -21,13 +21,17 @@ class Workspace(object):
21
21
M_APPLY_EDIT = 'workspace/applyEdit'
22
22
M_SHOW_MESSAGE = 'window/showMessage'
23
23
24
- def __init__ (self , root_uri , endpoint ):
24
+ def __init__ (self , root_uri , endpoint , config = None ):
25
+ self ._config = config
25
26
self ._root_uri = root_uri
26
27
self ._endpoint = endpoint
27
28
self ._root_uri_scheme = uris .urlparse (self ._root_uri )[0 ]
28
29
self ._root_path = uris .to_fs_path (self ._root_uri )
29
30
self ._docs = {}
30
31
32
+ # Cache jedi environments
33
+ self ._environments = {}
34
+
31
35
# Whilst incubating, keep rope private
32
36
self .__rope = None
33
37
self .__rope_config = None
@@ -77,6 +81,11 @@ def update_document(self, doc_uri, change, version=None):
77
81
self ._docs [doc_uri ].apply_change (change )
78
82
self ._docs [doc_uri ].version = version
79
83
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
+
80
89
def apply_edit (self , edit ):
81
90
return self ._endpoint .request (self .M_APPLY_EDIT , {'edit' : edit })
82
91
@@ -97,17 +106,21 @@ def _create_document(self, doc_uri, source=None, version=None):
97
106
doc_uri , source = source , version = version ,
98
107
extra_sys_path = self .source_roots (path ),
99
108
rope_project_builder = self ._rope_project_builder ,
109
+ config = self ._config , workspace = self ,
100
110
)
101
111
102
112
103
113
class Document (object ):
104
114
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 ):
106
117
self .uri = uri
107
118
self .version = version
108
119
self .path = uris .to_fs_path (uri )
109
120
self .filename = os .path .basename (self .path )
110
121
122
+ self ._config = config
123
+ self ._workspace = workspace
111
124
self ._local = local
112
125
self ._source = source
113
126
self ._extra_sys_path = extra_sys_path or []
@@ -131,6 +144,9 @@ def source(self):
131
144
return f .read ()
132
145
return self ._source
133
146
147
+ def update_config (self , config ):
148
+ self ._config = config
149
+
134
150
def apply_change (self , change ):
135
151
"""Apply a change to the document."""
136
152
text = change ['text' ]
@@ -197,28 +213,58 @@ def word_at_position(self, position):
197
213
return m_start [0 ] + m_end [- 1 ]
198
214
199
215
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
+
200
222
return jedi .api .names (
201
223
source = self .source , path = self .path , all_scopes = all_scopes ,
202
- definitions = definitions , references = references
224
+ definitions = definitions , references = references , environment = environment ,
203
225
)
204
226
205
227
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
+
206
239
kwargs = {
207
240
'source' : self .source ,
208
241
'path' : self .path ,
209
- 'sys_path' : self .sys_path ()
242
+ 'sys_path' : sys_path ,
243
+ 'environment' : environment ,
210
244
}
245
+
211
246
if position :
212
247
kwargs ['line' ] = position ['line' ] + 1
213
248
kwargs ['column' ] = _utils .clip_column (position ['character' ], self .lines , position ['line' ])
249
+
214
250
return jedi .Script (** kwargs )
215
251
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 ):
217
266
# Copy our extra sys path
218
267
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 )
222
269
path .extend (environment .get_sys_path ())
223
-
224
270
return path
0 commit comments