@@ -18,12 +18,19 @@ module Language.LSP.VFS (
18
18
VFS (.. ),
19
19
vfsMap ,
20
20
VirtualFile (.. ),
21
+ ClosedVirtualFile (.. ),
22
+ VirtualFileEntry (.. ),
21
23
lsp_version ,
22
24
file_version ,
23
25
file_text ,
26
+ language_id ,
27
+ _Open ,
28
+ _Closed ,
24
29
virtualFileText ,
25
30
virtualFileVersion ,
26
31
virtualFileLanguageKind ,
32
+ closedVirtualFileLanguageKind ,
33
+ virtualFileEntryLanguageKind ,
27
34
VfsLog (.. ),
28
35
29
36
-- * Managing the VFS
@@ -102,13 +109,27 @@ data VirtualFile = VirtualFile
102
109
}
103
110
deriving (Show )
104
111
112
+ {- | Represents a closed file in the VFS
113
+ We are keeping track of this in order to be able to get information
114
+ on virtual files after they were closed.
115
+ -}
116
+ data ClosedVirtualFile = ClosedVirtualFile
117
+ { _language_id :: ! (Maybe J. LanguageKind )
118
+ -- ^ see 'VirtualFile._language_id'
119
+ }
120
+ deriving (Show )
121
+
122
+ data VirtualFileEntry = Open VirtualFile | Closed ClosedVirtualFile
123
+ deriving (Show )
124
+
105
125
data VFS = VFS
106
- { _vfsMap :: ! (Map. Map J. NormalizedUri VirtualFile )
126
+ { _vfsMap :: ! (Map. Map J. NormalizedUri VirtualFileEntry )
107
127
}
108
128
deriving (Show )
109
129
110
130
data VfsLog
111
131
= SplitInsideCodePoint Utf16. Position Rope
132
+ | ApplyChangeToClosedFile J. NormalizedUri
112
133
| URINotFound J. NormalizedUri
113
134
| Opening J. NormalizedUri
114
135
| Closing J. NormalizedUri
@@ -120,6 +141,7 @@ data VfsLog
120
141
instance Pretty VfsLog where
121
142
pretty (SplitInsideCodePoint pos r) =
122
143
" VFS: asked to make change inside code point. Position" <+> viaShow pos <+> " in" <+> viaShow r
144
+ pretty (ApplyChangeToClosedFile uri) = " VFS: trying to apply a change to a closed file" <+> pretty uri
123
145
pretty (URINotFound uri) = " VFS: don't know about URI" <+> pretty uri
124
146
pretty (Opening uri) = " VFS: opening" <+> pretty uri
125
147
pretty (Closing uri) = " VFS: closing" <+> pretty uri
@@ -129,7 +151,9 @@ instance Pretty VfsLog where
129
151
pretty (DeleteNonExistent uri) = " VFS: asked to delete non-existent file" <+> pretty uri
130
152
131
153
makeFieldsNoPrefix ''VirtualFile
154
+ makeFieldsNoPrefix ''ClosedVirtualFile
132
155
makeFieldsNoPrefix ''VFS
156
+ makePrisms ''VirtualFileEntry
133
157
134
158
---
135
159
@@ -140,7 +164,20 @@ virtualFileVersion :: VirtualFile -> Int32
140
164
virtualFileVersion vf = _lsp_version vf
141
165
142
166
virtualFileLanguageKind :: VirtualFile -> Maybe J. LanguageKind
143
- virtualFileLanguageKind vf = _language_id vf
167
+ virtualFileLanguageKind vf = vf ^. language_id
168
+
169
+ closedVirtualFileLanguageKind :: ClosedVirtualFile -> Maybe J. LanguageKind
170
+ closedVirtualFileLanguageKind vf = vf ^. language_id
171
+
172
+ virtualFileEntryLanguageKind :: VirtualFileEntry -> Maybe J. LanguageKind
173
+ virtualFileEntryLanguageKind (Open vf) = virtualFileLanguageKind vf
174
+ virtualFileEntryLanguageKind (Closed vf) = closedVirtualFileLanguageKind vf
175
+
176
+ toClosedVirtualFile :: VirtualFile -> ClosedVirtualFile
177
+ toClosedVirtualFile vf =
178
+ ClosedVirtualFile
179
+ { _language_id = virtualFileLanguageKind vf
180
+ }
144
181
145
182
---
146
183
@@ -155,7 +192,7 @@ openVFS logger msg = do
155
192
let J. TextDocumentItem (J. toNormalizedUri -> uri) languageId version text = msg ^. J. params . J. textDocument
156
193
vfile = VirtualFile version 0 (Rope. fromText text) (Just languageId)
157
194
logger <& Opening uri `WithSeverity ` Debug
158
- vfsMap . at uri .= Just vfile
195
+ vfsMap . at uri .= ( Just $ Open vfile)
159
196
160
197
-- ---------------------------------------------------------------------
161
198
@@ -168,9 +205,10 @@ changeFromClientVFS logger msg = do
168
205
J. VersionedTextDocumentIdentifier (J. toNormalizedUri -> uri) version = vid
169
206
vfs <- get
170
207
case vfs ^. vfsMap . at uri of
171
- Just (VirtualFile _ file_ver contents kind) -> do
208
+ Just (Open ( VirtualFile _ file_ver contents kind) ) -> do
172
209
contents' <- applyChanges logger contents changes
173
- vfsMap . at uri .= Just (VirtualFile version (file_ver + 1 ) contents' kind)
210
+ vfsMap . at uri .= Just (Open (VirtualFile version (file_ver + 1 ) contents' kind))
211
+ Just (Closed (ClosedVirtualFile _)) -> logger <& ApplyChangeToClosedFile uri `WithSeverity ` Warning
174
212
Nothing -> logger <& URINotFound uri `WithSeverity ` Warning
175
213
176
214
-- ---------------------------------------------------------------------
@@ -181,7 +219,7 @@ applyCreateFile (J.CreateFile _ann _kind (J.toNormalizedUri -> uri) options) =
181
219
%= Map. insertWith
182
220
(\ new old -> if shouldOverwrite then new else old)
183
221
uri
184
- (VirtualFile 0 0 mempty Nothing )
222
+ (Open ( VirtualFile 0 0 mempty Nothing ) )
185
223
where
186
224
shouldOverwrite :: Bool
187
225
shouldOverwrite = case options of
@@ -308,7 +346,8 @@ persistFileVFS :: (MonadIO m) => LogAction m (WithSeverity VfsLog) -> FilePath -
308
346
persistFileVFS logger dir vfs uri =
309
347
case vfs ^. vfsMap . at uri of
310
348
Nothing -> Nothing
311
- Just vf ->
349
+ (Just (Closed _)) -> Nothing
350
+ (Just (Open vf)) ->
312
351
let tfn = virtualFileName dir uri vf
313
352
action = do
314
353
exists <- liftIO $ doesFileExist tfn
@@ -329,7 +368,12 @@ closeVFS :: (MonadState VFS m) => LogAction m (WithSeverity VfsLog) -> J.TMessag
329
368
closeVFS logger msg = do
330
369
let J. DidCloseTextDocumentParams (J. TextDocumentIdentifier (J. toNormalizedUri -> uri)) = msg ^. J. params
331
370
logger <& Closing uri `WithSeverity ` Debug
332
- vfsMap . at uri .= Nothing
371
+ vfsMap . ix uri
372
+ %= ( \ mf ->
373
+ case mf of
374
+ Open f -> Closed $ toClosedVirtualFile f
375
+ Closed f -> Closed f
376
+ )
333
377
334
378
-- ---------------------------------------------------------------------
335
379
0 commit comments