Handly 0.3 : Integration with TextFileBuffer-based editors

by Vladimir Piskarev

Besides providing out-of-the-box integration with Xtext editor, Handly can also support other editors. In particular, it allows for integration with TextFileBuffer-based editors. The following notes are intended as forward pointers only; full-blown editor implementation is beyond the scope of this article.

To integrate a TextFileBuffer-based editor with working copy functionality, you’ll need to subclass the editor’s document provider from the class SourceFileDocumentProvider (which extends the TextFileDocumentProvider).

For example:

public class FooFileDocumentProvider
    extends SourceFileDocumentProvider
{
    public FooFileDocumentProvider()
    {
        super(new FooElementForEditorInputFactory());
        // ...
    }

    // ...
}

You’ll also need to implement a Handly-based reconciler for the editor:

public class FooReconciler
    extends HandlyReconciler
{
    public FooReconciler(ITextEditor editor, IWorkingCopyManager manager)
    {
        super(editor, manager);
    }

    @Override
    protected void addElementChangeListener(IElementChangeListener listener)
    {
        FooModelCore.getFooModel().addElementChangeListener(listener);
    }

    @Override
    protected void removeElementChangeListener(IElementChangeListener listener)
    {
        FooModelCore.getFooModel().removeElementChangeListener(listener);
    }
}

The reconciler is connected to the editor via a source viewer configuration:

public class FooSourceViewerConfiguration
    extends TextSourceViewerConfiguration
{
    private final ITextEditor editor;
    private final IWorkingCopyManager manager;

    public FooSourceViewerConfiguration(IPreferenceStore preferenceStore,
        ITextEditor editor, IWorkingCopyManager manager)
    {
        super(preferenceStore);
        this.editor = editor;
        this.manager = manager;
    }

    @Override
    public IReconciler getReconciler(ISourceViewer sourceViewer)
    {
        if (editor != null && editor.isEditable())
        {
            return new FooReconciler(editor, manager);
        }
        return null;
    }
}

Then, you can wire it all together in the editor:

public class FooEditor
    extends AbstractDecoratedTextEditor
{
    @Override
    protected void initializeEditor()
    {
        super.initializeEditor();
        FooFileDocumentProvider provider =
            Activator.getDefault().getFooFileDocumentProvider();
        setDocumentProvider(provider);
        setSourceViewerConfiguration(new FooSourceViewerConfiguration(
            getPreferenceStore(), this, provider));
    }
}

Note that the functionality described in this article is only available since Handly 0.3, which will be released tomorrow (June 24th). The initial API should be considered provisional. Feedback can be directed to the project’s forum or right to the developer mailing list (the list requires subscription).

Advertisements