So far we've looked at SourceViewer in the context of managing source code annotations.
The SourceViewer is also the central hub for configuring your editor with pluggable behavior such as text hovering and syntax highlighting. For these features, the editor supplies a SourceViewerConfiguration that is used to configure the SourceViewer when it is created. The Java example editor need only to supply a SourceViewerConfiguration appropriate for its needs. The following snippet shows how the JavaTextEditor creates its configuration:
protected void initializeEditor() { super.initializeEditor(); setSourceViewerConfiguration(new JavaSourceViewerConfiguration()); ...
What does the JavaSourceViewerConfiguration do? Much of its behavior is inherited from SourceViewerConfiguration, which defines default strategies for pluggable editor behaviors such as auto indenting, undo behavior, double-click behavior, text hover, syntax highlighting, and formatting. Public methods in SourceViewerConfiguration provide the helper objects that implement these behaviors.
If the default behavior defined in SourceViewerConfiguration does not suit your editor, you should override initializeEditor() as shown above and set your own source viewer configuration into the editor. Your configuration can override methods in SourceViewerConfiguration to supply customized helper objects that implement behavior for your editor. The following snippet shows two of the ways the JavaSourceViewerConfiguration supplies customized helper objects for the Java editor example:
public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) { return new JavaAnnotationHover(); } public IAutoIndentStrategy getAutoIndentStrategy(ISourceViewer sourceViewer, String contentType) { return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? new JavaAutoIndentStrategy() : new DefaultAutoIndentStrategy()); }
In the first method, a customized helper class is provided for implementing annotation hovering. In the second method, the default content type of the document is queried to determine whether a customized auto-indent strategy or the default strategy should be used.
See the API reference for SourceViewerConfiguration for all the ways you can configure a source viewer by overriding methods.