Event sinks are interfaces that allow a language to be notified of events that occur to a Syntax
Event sinks use the service locator architecture and thus are managed as services. This design has several benefits:
- Event notifications can be prioritized (IntelliPrompt sessions receive events before languages, etc.)
- Custom event sink handlers can be externally swapped in without inheriting a language class
- There are no extra ties between a
SyntaxEditor
,IEditorDocument
, orISyntaxLanguage
meaning less chance for memory leaks - Languages continue to use basic .NET 2.0-based techniques for event handling, keeping their code platform independent
Note
The Service Locator Architecture topic contains a lot of information that is necessary to understand when working with event sinks.
Built-In Event Sink Service Types
This table lists the built-in event sink service types that can be used with ISyntaxType
keys.
Service Type | Description |
---|---|
IActive |
An object that can be notified of Syntax |
ICode |
An object that can be notified of when an ICode |
ICode |
An object that can be notified of when attached ICode |
ICode |
An object that can be notified when various events occur that are related to a code snippet template session. See the Code Snippets documentation for more information. |
IEditor |
An object that can be notified of IEditor |
IEditor |
An object that can be notified of IEditor |
IEditor |
An object that can be notified of IEditor |
IEditor |
An object that can be notified of IEditor |
IEditor |
An object that can be notified of IEditor |
IText |
An object that can be notified of when an IText |
Sample: Implementing an Event Sink for Listening to Text Changes
For this sample, assume that you are implementing an HTML language and wish to watch for <
being typed so that you know when to show a completion list of HTML tags. To do this, we'll work with the IEditor<
characters.
Registering the Event Sink
As mentioned above, event sinks use the service locator architecture. In this sample, we'll have our language class itself implement the IEditor
This code registers the language itself as an IEditor
Note
See the Service Locator Architecture topic for details on registering and retrieving various service object instances.
Implementing the IEditorDocumentTextChangeEventSink Interface
Next, we implement the interface on the language. Again, we could have implemented this interface on any other object and registered that instead but for simplicity, we have chosen to implement it on the language itself.
This code shows the two methods on the interface, added into the language class:
Note that in the text changed handler, we look to see if text that was typed was the <
character. If that scenario occurred, we'd call code to display a completion list.
Of course, we may also wish to add code to ensure we didn't type within a comment, etc. This logic could be added by getting an IText