This Javascript code implements a VS Code extension that relays text document changes over WebSockets. Here's a breakdown:
Core Functionality:
- Websocket Communication: The extension establishes a WebSocket connection to a server (defined in
server.js).
- Real-time Text Synchronization: It uses the
vscode-languageclient library to synchronize text document changes between VS Code and the server.
- Selective Synchronization: Only changes to files specified by the server are synchronized.
How it Works:
-
Activation (activate function):
- Logs an activation message.
- Reads configuration settings for the WebSocket server (command, arguments, network access permissions).
- Creates a
LanguageClient instance to manage the WebSocket connection and communication with the server.
- Starts the client to establish the connection.
- Sets up event listeners for when text documents are opened or closed in VS Code.
- Sends an initial notification to the server with a list of currently open files.
-
Handling Open/Close Events:
- When a text document is opened or closed, the
updateOpenFiles function is triggered.
- This function collects the file paths of all currently open documents in VS Code.
- It sends a notification (
wtr/update-open-files) to the server with the updated list of open files.
-
Server-Controlled Synchronization:
- The server determines which files should have their changes synchronized.
- The server sends a notification (
wtr/update-active-files) to the extension with a list of "active" files.
- The extension listens for this notification and calls the
updateRegistrations function.
-
Updating Synchronized Files (updateRegistrations function):
- This function updates the
LanguageClient's registrations to only synchronize changes for the specified "active" files.
- It first unregisters any previous registrations for the
textDocument/didChange feature.
- Then, it registers the feature again with a document selector that only includes the "active" files.
-
Deactivation (deactivate function):
- Stops the
LanguageClient to close the WebSocket connection when the extension is deactivated.
In essence: This extension acts as a bridge, allowing a server to selectively receive and potentially modify real-time text edits made in VS Code. The server controls which files are synchronized and can implement custom logic based on the received changes.