@@ -112,7 +112,8 @@ def get_document(self, doc_uri):
112
112
def put_document (self , doc_uri , content , version = None ):
113
113
path = uris .to_fs_path (doc_uri )
114
114
self ._docs [doc_uri ] = Document (
115
- doc_uri , content , sys_path = self .syspath_for_path (path ), version = version , rope = self ._rope
115
+ doc_uri , content ,
116
+ extra_sys_path = self .source_roots (path ), version = version , rope = self ._rope
116
117
)
117
118
118
119
def rm_document (self , doc_uri ):
@@ -136,35 +137,23 @@ def show_message(self, message, msg_type=lsp.MessageType.Info):
136
137
params = {'type' : msg_type , 'message' : message }
137
138
self ._lang_server .notify (self .M_SHOW_MESSAGE , params )
138
139
139
- def syspath_for_path (self , path ):
140
- """Construct a sensible sys path to use for the given file path.
141
-
142
- Since the workspace root may not be the root of the Python project we instead
143
- append the closest parent directory containing a setup.py file.
144
- """
145
- files = _utils .find_parents (self ._root_path , path , ['setup.py' ]) or []
146
- path = [os .path .dirname (setup_py ) for setup_py in files ]
147
-
148
- # Check to see if we're in a virtualenv
149
- if 'VIRTUAL_ENV' in os .environ :
150
- log .info ("Using virtualenv %s" , os .environ ['VIRTUAL_ENV' ])
151
- path .extend (jedi .evaluate .sys_path .get_venv_path (os .environ ['VIRTUAL_ENV' ]))
152
- else :
153
- path .extend (sys .path )
154
- return path
140
+ def source_roots (self , document_path ):
141
+ """Return the source roots for the given document."""
142
+ files = _utils .find_parents (self ._root_path , document_path , ['setup.py' ]) or []
143
+ return [os .path .dirname (setup_py ) for setup_py in files ]
155
144
156
145
157
146
class Document (object ):
158
147
159
- def __init__ (self , uri , source = None , version = None , local = True , sys_path = None , rope = None ):
148
+ def __init__ (self , uri , source = None , version = None , local = True , extra_sys_path = None , rope = None ):
160
149
self .uri = uri
161
150
self .version = version
162
151
self .path = uris .to_fs_path (uri )
163
152
self .filename = os .path .basename (self .path )
164
153
165
154
self ._local = local
166
155
self ._source = source
167
- self ._sys_path = sys_path or sys . path
156
+ self ._extra_sys_path = extra_sys_path or []
168
157
self ._rope_project = rope
169
158
170
159
def __str__ (self ):
@@ -260,9 +249,22 @@ def jedi_script(self, position=None):
260
249
kwargs = {
261
250
'source' : self .source ,
262
251
'path' : self .path ,
263
- 'sys_path' : self ._sys_path
252
+ 'sys_path' : self .sys_path ()
264
253
}
265
254
if position :
266
255
kwargs ['line' ] = position ['line' ] + 1
267
256
kwargs ['column' ] = _utils .clip_column (position ['character' ], self .lines , position ['line' ])
268
257
return jedi .Script (** kwargs )
258
+
259
+ def sys_path (self ):
260
+ # Copy our extra sys path
261
+ path = list (self ._extra_sys_path )
262
+
263
+ # Check to see if we're in a virtualenv
264
+ if 'VIRTUAL_ENV' in os .environ :
265
+ log .info ("Using virtualenv %s" , os .environ ['VIRTUAL_ENV' ])
266
+ path .extend (jedi .evaluate .sys_path .get_venv_path (os .environ ['VIRTUAL_ENV' ]))
267
+ else :
268
+ path .extend (sys .path )
269
+
270
+ return path
0 commit comments