22
33import ast
44import asyncio
5- import gc
65import weakref
76from collections import OrderedDict
87from concurrent .futures import ProcessPoolExecutor
@@ -220,13 +219,12 @@ def __init__(
220219 self ,
221220 name : str ,
222221 parent : ImportsManager ,
223- get_document_coroutine : Callable [[], Coroutine [Any , Any , Tuple [ TextDocument , bool ] ]],
222+ get_document_coroutine : Callable [[], Coroutine [Any , Any , TextDocument ]],
224223 ) -> None :
225224 super ().__init__ ()
226225 self .name = name
227226 self .parent = parent
228227 self ._get_document_coroutine = get_document_coroutine
229- self ._delete_on_validate_document = False
230228 self .references : weakref .WeakSet [Any ] = weakref .WeakSet ()
231229 self .file_watchers : List [FileWatcherEntry ] = []
232230 self ._document : Optional [TextDocument ] = None
@@ -265,8 +263,16 @@ async def check_file_changed(self, changes: List[FileEvent]) -> Optional[FileCha
265263
266264 return None
267265
266+ def __close_document (self , document : TextDocument ) -> None :
267+ asyncio .run_coroutine_threadsafe (self .parent .parent_protocol .documents .close_document (document ), self ._loop )
268+
268269 async def _update (self ) -> None :
269- self ._document , self ._delete_on_validate_document = await self ._get_document_coroutine ()
270+ self ._document = await self ._get_document_coroutine ()
271+
272+ for r in self .references :
273+ self ._document .references .add (r )
274+
275+ weakref .finalize (r , self .__close_document , self ._document )
270276
271277 if self ._document .version is None :
272278 self .file_watchers .append (
@@ -286,12 +292,6 @@ async def _invalidate(self) -> None:
286292
287293 await self ._remove_file_watcher ()
288294
289- if self ._delete_on_validate_document :
290- if self ._document is not None :
291- await self ._document .clear ()
292- del self ._document
293- gc .collect ()
294-
295295 self ._document = None
296296
297297 async def _remove_file_watcher (self ) -> None :
@@ -376,7 +376,7 @@ async def resource_document_changed(self, sender: Any, document: TextDocument) -
376376 resource_changed : List [LibraryDoc ] = []
377377
378378 async with self ._resources_lock :
379- for r_key , r_entry in self ._resources .items ():
379+ for r_entry in self ._resources .values ():
380380 lib_doc : Optional [LibraryDoc ] = None
381381 try :
382382 if not await r_entry .is_valid ():
@@ -635,7 +635,7 @@ async def find_file(self, name: str, base_dir: str, file_type: str = "Resource")
635635 async def _get_entry_for_resource_import (self , name : str , base_dir : str , sentinel : Any = None ) -> _ResourcesEntry :
636636 source = await self .find_file (name , base_dir , "Resource" )
637637
638- async def _get_document () -> Tuple [ TextDocument , bool ] :
638+ async def _get_document () -> TextDocument :
639639 from robot .utils import FileReader
640640
641641 self ._logger .debug (lambda : f"Load resource { name } from source { source } " )
@@ -648,16 +648,18 @@ async def _get_document() -> Tuple[TextDocument, bool]:
648648 f"Supported extensions are { ', ' .join (repr (s ) for s in RESOURCE_EXTENSIONS )} ."
649649 )
650650
651- source_uri = DocumentUri (Uri .from_path (source_path ))
651+ source_uri = DocumentUri (Uri .from_path (source_path ). normalized () )
652652
653653 result = self .parent_protocol .documents .get (source_uri , None )
654654 if result is not None :
655- return result , False
655+ return result
656656
657657 with FileReader (source_path ) as reader :
658658 text = str (reader .read ())
659659
660- return TextDocument (document_uri = source_uri , language_id = "robot" , version = None , text = text ), True
660+ return self .parent_protocol .documents .append_document (
661+ document_uri = source_uri , language_id = "robotframework" , text = text
662+ )
661663
662664 async with self ._resources_lock :
663665 entry_key = _ResourcesEntryKey (source )
0 commit comments