The other day we posted about how syntax languages are changing to use the service locator design pattern. This design pattern allows us to generically make available new features for syntax languages, and for language designers to easily implement them. It’s a win-win for everyone.
While we’ve been working on adding support for languages to respond to user input and show completion lists, etc., we changed the internal model we use for handling IntelliPrompt input to be based on the service locator design pattern as well.
Which “services” are available?
Right now there are a handful of services available for use by syntax languages. These include:
- IExampleTextProvider
- ILexicalParser
- ILineCommenter
- ITextStatisticsFactory
- ITokenClassifierFactory
- IWordBreakFinder
- IActiveEditorViewChangeEventSink
- IEditorDocumentTextChangeEventSink
- IEditorViewKeyInputEventSink
- IEditorViewMouseInputEventSink
- IEditorViewTextInputEventSink
- IEditorViewSelectionChangeEventSink
The first six items in that list are feature-related, specifically for syntax language usage.
The "event sink” items are interfaces that can be implemented on objects that wish to receive event notifications. These can be registered on both syntax languages and IntelliPrompt sessions.
More about event sink interfaces
Let’s talk more about how the event sink interfaces work.
Say your language wishes to be notified when any document that uses it has its text changed. In this case, you could have your language implement the IEditorDocumentTextChangeEventSink interface and register itself as a IEditorDocumentTextChangeEventSink service for the language. Alternatively you could have some other object implement the interface and register that object as a IEditorDocumentTextChangeEventSink service for the language.
The IEditorDocumentTextChangeEventSink interface looks like this:
1: public interface IEditorDocumentTextChangeEventSink {
2:
3: void NotifyDocumentTextChanged(SyntaxEditor editor,
4: EditorSnapshotChangedEventArgs e);
5:
6: void NotifyDocumentTextChanging(SyntaxEditor editor,
7: EditorSnapshotChangingEventArgs e);
8:
9: }
When a SyntaxEditor is edited and a document text change occurs, the IEditorDocumentTextChangeEventSink service registered with the related language, if any, is notified of the text changing and changed events. It can choose to handle those events or allow them to continue on.
Likewise the other event sink interfaces help with trapping and/or responding to active view changes, key/mouse/text input, and view selection changes.
IntelliPrompt sessions become service locators too
One other major change we’re making with the next build is that we’ve removed input processors. In the first public beta, input processors were objects that had some mouse/key/text input methods on them and you could attach one to an IntelliPrompt session to filter input before the view received it. We’ve removed input processors and have instead made IntelliPrompt sessions service locators.
This change allows us to generically add support for new features down the road. We still support all the input filtering capabilities as before however now they are implemented using the event sink interfaces described above.
Note that this change will most likely not affect your code at all since this part of the IntelliPrompt implementation was done internally before anyhow.
Event sink priorities
Event sinks can be registered on any syntax language or IntelliPrompt session. When an event sink notification is to be performed, a prioritized list of available event sinks is built. The list contains the open IntelliPrompt sessions (newest to oldest) that have the related event sink registered, followed by the syntax language if it has the event sink registered. Each event sync is notified in that particular order and any handler can mark the event as handled along the way.
Summary
The service locator design pattern has proved to be a welcome addition to the SyntaxEditor for WPF design, as it implements a generic methodology for providing features and relaying events notifications.