@@ -18,9 +18,15 @@ module Language.LSP.VFS (
18
18
VFS (.. ),
19
19
vfsMap ,
20
20
VirtualFile (.. ),
21
+ VirtualFileGhost (.. ),
22
+ VirtualFileEntry (.. ),
21
23
lsp_version ,
22
24
file_version ,
23
25
file_text ,
26
+ language_id ,
27
+ ghost_language_id ,
28
+ _Open ,
29
+ _Closed ,
24
30
virtualFileText ,
25
31
virtualFileVersion ,
26
32
virtualFileLanguageKind ,
@@ -102,13 +108,36 @@ data VirtualFile = VirtualFile
102
108
}
103
109
deriving (Show )
104
110
111
+ {- | Represents a closed file in the VFS
112
+ We are keeping track of this in order to be able to get information
113
+ on virtual files after they were closed.
114
+ -}
115
+ data VirtualFileGhost = VirtualFileGhost
116
+ { _ghost_language_id :: ! (Maybe J. LanguageKind )
117
+ -- ^ The text document's language identifier
118
+ -- This is a Maybe, since when we use the VFS as a client
119
+ -- we don't have this information, since server sends WorkspaceEdit
120
+ -- notifications without a language kind.
121
+ -- When using the VFS in a server, this should always be Just.
122
+ }
123
+ deriving (Show )
124
+
125
+ ghostify :: VirtualFile -> VirtualFileGhost
126
+ ghostify vf =
127
+ VirtualFileGhost
128
+ { _ghost_language_id = _language_id vf
129
+ }
130
+ data VirtualFileEntry = Open VirtualFile | Closed VirtualFileGhost
131
+ deriving (Show )
132
+
105
133
data VFS = VFS
106
- { _vfsMap :: ! (Map. Map J. NormalizedUri VirtualFile )
134
+ { _vfsMap :: ! (Map. Map J. NormalizedUri VirtualFileEntry )
107
135
}
108
136
deriving (Show )
109
137
110
138
data VfsLog
111
139
= SplitInsideCodePoint Utf16. Position Rope
140
+ | ClosedVirtualFile
112
141
| URINotFound J. NormalizedUri
113
142
| Opening J. NormalizedUri
114
143
| Closing J. NormalizedUri
@@ -120,6 +149,7 @@ data VfsLog
120
149
instance Pretty VfsLog where
121
150
pretty (SplitInsideCodePoint pos r) =
122
151
" VFS: asked to make change inside code point. Position" <+> viaShow pos <+> " in" <+> viaShow r
152
+ pretty ClosedVirtualFile = " VFS: trying to handle closed virtual file"
123
153
pretty (URINotFound uri) = " VFS: don't know about URI" <+> pretty uri
124
154
pretty (Opening uri) = " VFS: opening" <+> pretty uri
125
155
pretty (Closing uri) = " VFS: closing" <+> pretty uri
@@ -129,7 +159,9 @@ instance Pretty VfsLog where
129
159
pretty (DeleteNonExistent uri) = " VFS: asked to delete non-existent file" <+> pretty uri
130
160
131
161
makeFieldsNoPrefix ''VirtualFile
162
+ makeFieldsNoPrefix ''VirtualFileGhost
132
163
makeFieldsNoPrefix ''VFS
164
+ makePrisms ''VirtualFileEntry
133
165
134
166
---
135
167
@@ -155,7 +187,7 @@ openVFS logger msg = do
155
187
let J. TextDocumentItem (J. toNormalizedUri -> uri) languageId version text = msg ^. J. params . J. textDocument
156
188
vfile = VirtualFile version 0 (Rope. fromText text) (Just languageId)
157
189
logger <& Opening uri `WithSeverity ` Debug
158
- vfsMap . at uri .= Just vfile
190
+ vfsMap . at uri .= ( Just $ Open vfile)
159
191
160
192
-- ---------------------------------------------------------------------
161
193
@@ -168,9 +200,10 @@ changeFromClientVFS logger msg = do
168
200
J. VersionedTextDocumentIdentifier (J. toNormalizedUri -> uri) version = vid
169
201
vfs <- get
170
202
case vfs ^. vfsMap . at uri of
171
- Just (VirtualFile _ file_ver contents kind) -> do
203
+ Just (Open ( VirtualFile _ file_ver contents kind) ) -> do
172
204
contents' <- applyChanges logger contents changes
173
- vfsMap . at uri .= Just (VirtualFile version (file_ver + 1 ) contents' kind)
205
+ vfsMap . at uri .= Just (Open (VirtualFile version (file_ver + 1 ) contents' kind))
206
+ Just (Closed (VirtualFileGhost _)) -> logger <& ClosedVirtualFile `WithSeverity ` Warning
174
207
Nothing -> logger <& URINotFound uri `WithSeverity ` Warning
175
208
176
209
-- ---------------------------------------------------------------------
@@ -181,7 +214,7 @@ applyCreateFile (J.CreateFile _ann _kind (J.toNormalizedUri -> uri) options) =
181
214
%= Map. insertWith
182
215
(\ new old -> if shouldOverwrite then new else old)
183
216
uri
184
- (VirtualFile 0 0 mempty Nothing )
217
+ (Open ( VirtualFile 0 0 mempty Nothing ) )
185
218
where
186
219
shouldOverwrite :: Bool
187
220
shouldOverwrite = case options of
@@ -308,7 +341,8 @@ persistFileVFS :: (MonadIO m) => LogAction m (WithSeverity VfsLog) -> FilePath -
308
341
persistFileVFS logger dir vfs uri =
309
342
case vfs ^. vfsMap . at uri of
310
343
Nothing -> Nothing
311
- Just vf ->
344
+ (Just (Closed _)) -> Nothing
345
+ (Just (Open vf)) ->
312
346
let tfn = virtualFileName dir uri vf
313
347
action = do
314
348
exists <- liftIO $ doesFileExist tfn
@@ -329,7 +363,12 @@ closeVFS :: (MonadState VFS m) => LogAction m (WithSeverity VfsLog) -> J.TMessag
329
363
closeVFS logger msg = do
330
364
let J. DidCloseTextDocumentParams (J. TextDocumentIdentifier (J. toNormalizedUri -> uri)) = msg ^. J. params
331
365
logger <& Closing uri `WithSeverity ` Debug
332
- vfsMap . at uri .= Nothing
366
+ vfsMap . at uri
367
+ %= ( \ mf ->
368
+ case mf of
369
+ Just (Open f) -> Just $ Closed $ ghostify f
370
+ _ -> Nothing
371
+ )
333
372
334
373
-- ---------------------------------------------------------------------
335
374
0 commit comments