The issue where textDocument/didClose is sent even though the file remains open typically occurs during the initial loading phase of the language server. Visual Studio may send a didOpen followed by a didClose message for the first file as part of its internal process to initialize or refresh the language server state. This can happen due to:
- Editor Rehydration or Reloading: Visual Studio might temporarily unload and reload the document to ensure the language server is properly initialized.
- Multiple Editor Tabs or Views: If the file is opened in multiple views or tabs, closing one might trigger a didClose even though the file remains open elsewhere.
- Language Server Activation Timing: The first file might be used to trigger the server startup, and Visual Studio may reset the document state as part of that process.
Since subsequent files behave as expected (with didOpen, didchange
, etc.), this behavior is likely part of Visual Studio's internal lifecycle management and not an issue with your LSP implementation.
If this behavior causes functional issues in your server, you might consider implementing a debounce or state-check mechanism to ensure that didClose only triggers cleanup when the document is truly closed across all views.