13
13
DidOpenTextDocumentParams ,
14
14
DidSaveTextDocumentParams ,
15
15
DocumentUri ,
16
+ FileEvent ,
16
17
TextDocumentContentChangeEvent ,
17
18
TextDocumentContentRangeChangeEvent ,
18
19
TextDocumentContentTextChangeEvent ,
23
24
TextDocumentSyncOptions ,
24
25
TextEdit ,
25
26
VersionedTextDocumentIdentifier ,
27
+ WatchKind ,
26
28
WillSaveTextDocumentParams ,
27
29
)
28
30
from ..text_document import TextDocument
@@ -46,6 +48,26 @@ class TextDocumentProtocolPart(LanguageServerProtocolPart, Mapping[DocumentUri,
46
48
def __init__ (self , parent : LanguageServerProtocol ) -> None :
47
49
super ().__init__ (parent )
48
50
self ._documents : Dict [DocumentUri , TextDocument ] = {}
51
+ self .parent .on_initialized .add (self ._protocol_initialized )
52
+
53
+ async def _protocol_initialized (self , sender : Any ) -> None :
54
+ await self ._update_filewatchers ()
55
+
56
+ async def _update_filewatchers (self ) -> None :
57
+ await self .parent .workspace .add_file_watcher (self ._file_watcher , "**/*" , WatchKind .CHANGE | WatchKind .DELETE )
58
+
59
+ @_logger .call
60
+ async def _file_watcher (self , sender : Any , changes : List [FileEvent ]) -> None :
61
+ self ._logger .debug (f"filewatcher { changes } " )
62
+ to_change : Dict [str , FileEvent ] = {}
63
+ for change in changes :
64
+ to_change [change .uri ] = change
65
+
66
+ for change in to_change .values ():
67
+ document = self ._documents .get (DocumentUri (Uri (change .uri ).normalized ()), None )
68
+ if document is not None and not document .opened_in_editor :
69
+ await self .did_close (self , document )
70
+ await self .close_document (document , True )
49
71
50
72
@async_event
51
73
async def did_open (sender , document : TextDocument ) -> None :
@@ -117,6 +139,7 @@ async def _text_document_did_open(self, text_document: TextDocumentItem, *args:
117
139
else :
118
140
await document .apply_full_change (text_document .version , text_document .text )
119
141
142
+ document .opened_in_editor = True
120
143
document .references .add (self )
121
144
122
145
await self .did_open (self , document )
@@ -129,12 +152,13 @@ async def _text_document_did_close(self, text_document: TextDocumentIdentifier,
129
152
130
153
if document is not None :
131
154
document .references .remove (self )
132
-
155
+ document . opened_in_editor = False
133
156
await self .did_close (self , document )
134
157
await self .close_document (document )
135
158
136
- async def close_document (self , document : TextDocument ) -> None :
137
- if len (document .references ) == 0 :
159
+ @_logger .call
160
+ async def close_document (self , document : TextDocument , ignore_references : bool = False ) -> None :
161
+ if len (document .references ) == 0 or ignore_references :
138
162
self ._documents .pop (str (document .uri ), None )
139
163
140
164
await document .clear ()
0 commit comments