3
3
import logging
4
4
import os
5
5
import re
6
+ import functools
7
+ from threading import RLock
6
8
7
9
import jedi
8
10
15
17
RE_END_WORD = re .compile ('^[A-Za-z_0-9]*' )
16
18
17
19
20
+ def lock (method ):
21
+ """Define an atomic region over a method."""
22
+ @functools .wraps (method )
23
+ def wrapper (self , * args , ** kwargs ):
24
+ with self ._lock :
25
+ return method (self , * args , ** kwargs )
26
+ return wrapper
27
+
28
+
18
29
class Workspace (object ):
19
30
20
31
M_PUBLISH_DIAGNOSTICS = 'textDocument/publishDiagnostics'
@@ -131,6 +142,7 @@ def __init__(self, uri, workspace, source=None, version=None, local=True, extra_
131
142
self ._source = source
132
143
self ._extra_sys_path = extra_sys_path or []
133
144
self ._rope_project_builder = rope_project_builder
145
+ self ._lock = RLock ()
134
146
135
147
def __str__ (self ):
136
148
return str (self .uri )
@@ -140,10 +152,12 @@ def _rope_resource(self, rope_config):
140
152
return libutils .path_to_resource (self ._rope_project_builder (rope_config ), self .path )
141
153
142
154
@property
155
+ @lock
143
156
def lines (self ):
144
157
return self .source .splitlines (True )
145
158
146
159
@property
160
+ @lock
147
161
def source (self ):
148
162
if self ._source is None :
149
163
with io .open (self .path , 'r' , encoding = 'utf-8' ) as f :
@@ -153,6 +167,7 @@ def source(self):
153
167
def update_config (self , settings ):
154
168
self ._config .update ((settings or {}).get ('pyls' , {}))
155
169
170
+ @lock
156
171
def apply_change (self , change ):
157
172
"""Apply a change to the document."""
158
173
text = change ['text' ]
@@ -218,11 +233,13 @@ def word_at_position(self, position):
218
233
219
234
return m_start [0 ] + m_end [- 1 ]
220
235
236
+ @lock
221
237
def jedi_names (self , all_scopes = False , definitions = True , references = False ):
222
238
script = self .jedi_script ()
223
239
return script .get_names (all_scopes = all_scopes , definitions = definitions ,
224
240
references = references )
225
241
242
+ @lock
226
243
def jedi_script (self , position = None ):
227
244
extra_paths = []
228
245
environment_path = None
0 commit comments