Safely Manipulating Files in Eclipse

Thanks to Kai-Uwe Maetzel for the code and the presentation.

Here are the useful nuggets that I got:

Use the new FileBuffers API from within org.eclipse.core.filebuffers. This was derived from the Text widget.

Most of the API is concerned with ensuring that only one editor is changing one text file at a time and updating the UI appropriately. It also handles undo for you, packaging up a file edit (which can consist of many changes to the file) as one undoable action. It works for both IFiles in a Workspace and external Files.

Use ITextFileBufferManager and ITextFileBuffer. I had to import org.eclipse.core.filebuffers to get the classes to show up up.

Do the actual edits in a subclass (that you create) of TextFileBufferOperation. This saves 80% of the code that you would normally write. Implement the computeTextEdit() and getDocumentRewriteSessionType() methods.

Then execute the operation inside of a Job.

Future: File Buffer operations will be merged with refactoring infrastructure. Allow contribution of concrete file types.

There isn’t any support for non-text files currently, but I’m going to get together with Ryan and submit the AudioMan2 use cases, which deal with digital music files like MP3.

One Reply to “Safely Manipulating Files in Eclipse”

Comments are closed.