2
2
import inspect
3
3
import io
4
4
import weakref
5
+ from contextlib import contextmanager
5
6
from typing import (
6
7
Any ,
7
8
Callable ,
8
9
Dict ,
9
10
Final ,
11
+ Iterator ,
10
12
List ,
11
13
Optional ,
12
14
TypeVar ,
@@ -143,25 +145,23 @@ def revert(self, version: Optional[int]) -> bool:
143
145
144
146
@_logger .call
145
147
def apply_none_change (self ) -> None :
146
- with self ._lock :
148
+ with self ._cache_invalidating () :
147
149
self ._lines = None
148
- self ._invalidate_cache ()
149
150
150
151
@_logger .call
151
152
def apply_full_change (self , version : Optional [int ], text : Optional [str ], * , save : bool = False ) -> None :
152
- with self ._lock :
153
+ with self ._cache_invalidating () :
153
154
if version is not None :
154
155
self ._version = version
155
156
if text is not None :
156
157
self ._text = text
157
158
self ._lines = None
158
159
if save :
159
160
self ._orig_text = self ._text
160
- self ._invalidate_cache ()
161
161
162
162
@_logger .call
163
163
def apply_incremental_change (self , version : Optional [int ], range : Range , text : str ) -> None :
164
- with self ._lock :
164
+ with self ._cache_invalidating () :
165
165
try :
166
166
if version is not None :
167
167
self ._version = version
@@ -193,7 +193,6 @@ def apply_incremental_change(self, version: Optional[int], range: Range, text: s
193
193
self ._text = new_text .getvalue ()
194
194
finally :
195
195
self ._lines = None
196
- self ._invalidate_cache ()
197
196
198
197
def __get_lines (self ) -> List [str ]:
199
198
if self ._lines is None :
@@ -214,13 +213,21 @@ def cache_invalidate(sender) -> None: ...
214
213
@event
215
214
def cache_invalidated (sender ) -> None : ...
216
215
216
+ @contextmanager
217
+ def _cache_invalidating (self ) -> Iterator [None ]:
218
+ self .cache_invalidate ()
219
+ try :
220
+ with self ._lock :
221
+ yield
222
+ finally :
223
+ self ._invalidate_cache ()
224
+ self .cache_invalidated (self )
225
+
217
226
def _invalidate_cache (self ) -> None :
218
- self .cache_invalidate (self )
219
227
self ._cache .clear ()
220
- self .cache_invalidated (self )
221
228
222
229
def invalidate_cache (self ) -> None :
223
- with self ._lock :
230
+ with self ._cache_invalidating () :
224
231
self ._invalidate_cache ()
225
232
226
233
def _invalidate_data (self ) -> None :
@@ -288,12 +295,11 @@ def get_data(self, key: Any, default: Optional[Any] = None) -> Any:
288
295
289
296
def _clear (self ) -> None :
290
297
self ._lines = None
291
- self ._invalidate_cache ()
292
298
self ._invalidate_data ()
293
299
294
300
@_logger .call
295
301
def clear (self ) -> None :
296
- with self ._lock :
302
+ with self ._cache_invalidating () :
297
303
self ._clear ()
298
304
299
305
def position_from_utf16 (self , position : Position ) -> Position :
0 commit comments