Skip to content

Latest commit

 

History

History
44 lines (32 loc) · 2.86 KB

4-README.md

File metadata and controls

44 lines (32 loc) · 2.86 KB

Back to the table of contents

04 - Sending Notifications

Learning goal: Learn how to send notifications to the client without a related request.

Messages from the server do not only have to be sent when requested by the client, the server can also send messages when it needs to notify the client of a change. The example we are using in this exercise is that of validation. Once the document is updated, we will validate that its content is correct and tell the client to display warnings/errors otherwise. We are beginning this sequence of calls when the server receives a didChange or didOpen notification, but using this technique, you can send the client notifications at any point.

EclipseConTextDocumentService.java

When a file is opened or edited didOpen and didChange notifications are sent to the server respectfully. These are different from the other endpoints, such as the completion endpoint used in the second exercise, as they do not send back a response, instead the notification is used to update the responses that the other endpoints will send.

Acquire The Client

To send notifications to the client, you first need a reference to the client. This is done with EclipseConLanguageServer's setRemoteProxy(LanguageClient) function which is called by the client with a reference to itself and saves the client as the client variable in the EclipseConLanguageServer instance.

Send the Notification

In both didOpen and didChange functions, we will take the model generated and send it to the validate function. This will return any necessary Diagnostics as a List which will be used to create a new PublishDiagnosticsParams which is then published to the client:

CompletableFuture.runAsync(() ->
	eclipseConLanguageServer.client.publishDiagnostics(
		new PublishDiagnosticsParams(params.getTextDocument().getUri(), validate(model))
	)
);

The server is able to tell the client to show a message, log a messages, and update its list of the server capabilities this way. To show to yourself that this works, we will create an irritating addition that will have the server tell the client to display a message each time a change is made to a file:

CompletableFuture.runAsync(() ->
	eclipseConLanguageServer.client.showMessage(new MessageParams(MessageType.Info, "THE FILE HAS BEEN EDITED"))
);

Server in Action

With our new message being sent on updates, let's see the language server in action:

  • Right clicking on the org.eclipsecon.languageserverplugin project, use Run as > Eclipse Application to launch
  • Create a new file called exer4.txt
  • Open this file with the Generic Editor
  • Begin typing in the Editor
  • Observe the notification being displayed

Congratulations! You have sent a notification from a Language Server in Java to its client!

Back to the table of contents