Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[handly-dev] Integration of TextFileBuffer-based editors with working copy functionality

Hi,

Some notes on integration of TextFileBuffer-based editors
with working copy functionality in Handly.

Usually, TextFileBuffer-based source editors will have their own document provider
extending TextFileDocumentProvider. To integrate with Handly's working copy
functionality, such document provider would use a subclass of FileInfo
containing a reference to the working copy. The document provider will
be responsible for creating/discarding the working copy. For example:

public class FooDocumentProvider extends TextFileDocumentProvider
{
    protected static class WorkingCopyInfo extends FileInfo
    {
        public SourceFile workingCopy;
    }

	public ISourceFile getWorkingCopy(Object element)
    {
		FileInfo fileInfo = getFileInfo(element);
		if (fileInfo instanceof WorkingCopyInfo)
        {
			return ((WorkingCopyInfo)info).workingCopy;
		}
		return null;
	}

    @Override
    protected FileInfo createEmptyFileInfo()
    {
        return new WorkingCopyInfo();
    }

    @Override
    protected FileInfo createFileInfo(Object element) throws CoreException
    {
        FileInfo info = super.createFileInfo(element);
        SourceFile sourceFile = ... // compute source file corresponding to element
        IWorkingCopyBuffer buffer = new DelegatingWorkingCopyBuffer(
            sourceFile.openBuffer(true, null),
            new WorkingCopyReconciler(sourceFile) // or subclass
        );
        try
        {
            sourceFile.becomeWorkingCopy(buffer, null); // will addRef() the buffer
        }
        finally
        {
            buffer.dispose();
        }
        ((WorkingCopyInfo)info).workingCopy = sourceFile;
        return info;
    }

    @Override
    protected void disposeFileInfo(Object element, FileInfo info)
    {
       if (info instanceof WorkingCopyInfo)
       {
           ((WorkingCopyInfo)info).workingCopy.discardWorkingCopy();
       }
       super.disposeFileInfo();
    }
}

This seems to make a candidate for a base class (SourceFileDocumentProvider),
but there is no such class at the moment.

After that, just call ISourceFile#reconcile from a reconciling strategy
for the editor:

public class FooReconcilingStrategy
    implements IReconcilingStrategy, IReconcilingStrategyExtension
{
    private ITextEditor editor;
    private FooDocumentProvider documentProvider;
    private IProgressMonitor progressMonitor;

    // ...

    private void reconcile(boolean force)
    {
        final ISourceFile workingCopy = documentProvider.getWorkingCopy(
            editor.getEditorInput());
        if (workingCopy != null)
        {
			SafeRunner.run(new ISafeRunnable()
            {
				public void run() throws CoreException
                {
                    workingCopy.reconcile(force, progressMonitor);
				}

				public void handleException(Throwable exception)
                {
				}
			});
        }
    }
}

Hope this helps,
Vladimir




Back to the top