@@ -129,6 +129,8 @@ def __init__(self, protocol: LanguageServerProtocol) -> None:
129
129
130
130
self .parent .on_initialized .add (self .initialized )
131
131
132
+ self .parent .documents .did_close .add (self .on_did_close )
133
+
132
134
self .in_get_workspace_diagnostics = Event (True )
133
135
134
136
async def initialized (self , sender : Any ) -> None :
@@ -239,6 +241,36 @@ async def force_refresh_document(self, document: TextDocument, refresh: bool = T
239
241
if refresh and document .opened_in_editor :
240
242
await self .refresh ()
241
243
244
+ @_logger .call
245
+ async def on_did_close (self , sender : Any , document : TextDocument ) -> None :
246
+ if await self .get_diagnostics_mode (document .uri ) == DiagnosticsMode .WORKSPACE :
247
+ return
248
+
249
+ try :
250
+ data = self .get_diagnostics_data (document )
251
+ if data .task is not None and not data .task .done ():
252
+ self ._logger .debug (lambda : f"try to cancel diagnostics for { document } " )
253
+
254
+ e = threading .Event ()
255
+
256
+ def done (t : asyncio .Task [Any ]) -> None :
257
+ e .set ()
258
+
259
+ data .task .add_done_callback (done )
260
+ data .task .get_loop ().call_soon_threadsafe (data .task .cancel )
261
+
262
+ e .wait (120 )
263
+
264
+ finally :
265
+ self .parent .send_notification (
266
+ "textDocument/publishDiagnostics" ,
267
+ PublishDiagnosticsParams (
268
+ uri = document .document_uri ,
269
+ version = document ._version ,
270
+ diagnostics = [],
271
+ ),
272
+ )
273
+
242
274
@_logger .call
243
275
async def _get_diagnostics_for_document (self , document : TextDocument , data : DiagnosticsData ) -> None :
244
276
self ._logger .debug (lambda : f"Get diagnostics for { document } " )
0 commit comments