Home > eclipse, java > The Eclipse Reconciler

The Eclipse Reconciler

November 1st, 2007

One aspect of implementing a text editor in Eclipse that I’ve found to be tricky is the reconciler. In this post, I’ll detail some of the lessons I’ve learned. I’m assuming you’ve gotten as far as creating a basic editor. The reconciler is a background process in your editor responsible for updating your model when the user takes a break from typing. The kinds of things it should update are:

  • Elements displayed in the outline view
  • Error markers
  • Folding regions

Some editor programmers don’t seem to know that the reconciler even exists. They usually take one of two approaches: update the entire model on every keystroke or update the model only when the user saves. Neither of these is very satisfying, especially for users familiar with editing Java in Eclipse. The former approach leads to poor performance on large files. The latter requires the user to save constantly to find out whether errors exist or get an up to date outline.

So, how does the reconciler work? The reconciler runs as a background thread in Eclipse. It sets a timer which will trigger a reconcile whenever it expires. Each time the user types a keystroke, the timer is reset. In this way, the user can type a great deal of text without being interrupted by the reconciling process. Once the user pauses, as all humans must, the reconciler’s timer expires and the model is updated.

Creating a Reconciler

Eclipse uses the strategy pattern to separate the timer/keystroke logic from the details of updating your model. The MonoReconciler is the simplest implementation of the timer/keystroke logic. It is configured with an instance of IReconcilingStrategy which is responsible for updating your model, i.e. you implement this interface.

The basic procedure for setting up your reconciler and reconciling strategy are:

  • Implement IReconcilingStrategy. You’ll want to implement the non-incremental version of reconcile(), i.e. reconcile(IRegion partition). More on this below.
  • In your editor’s SourceViewerConfiguration, override getReconciler() to return an instance of MonoReconciler, initialized with an instance of your reconciling strategy and isIncremental set to false

That’s it. Now when your editor is created, your reconciler will be initialized and the reconcile() method will be called whenever the user modifies the document. I usually store a reference to the editor itself in the strategy object.

The Reconciler Runs in Its Own Thread!

It’s important to always remember that the reconciler runs in its own thread, as with most stuff in Eclipse. It’s safe to access the document, but there are few other things you need to keep in mind:

  • All SWT/JFace operations must be executed in the UI thread. So, if you want to update your outline view after a reconcile, you’ll have to use Display.asyncExec().
  • Your model also has to be thread safe. If it’s being update in the reconciler thread and read from in the UI thread to update the outline view, you’re going to need some synchronization.
  • It is safe to add warning and error markers to the file from the reconciler thread

If your model is simple enough, one simple solution to this problem is to have the reconciler construct an entirely new model each time it runs and then pass it on to the editor with Display.asyncExec(). This way there are no shared objects between the threads and thus, no syncrhonization problems.

What if the user doesn’t start typing right away?

After you’ve implemented your reconciler, everything will seem great. Great until you (or a user) decides to just view a file without editing it. They’ll find that their outline view is not updated. Same with folding ranges, etc. The problem is that your reconciling strategy is not run until the document is actually modified. Go figure. If only there was a way to tell the reconciler, “Hey, how about an initial reconcile pass on the document right when it’s opened?”.

Fortunately, a friendly Eclipse programmer also thought of this question. Rather than implementing IReconcilingStrategy, try IReconcilingStrategyExtension. This adds an initialReconcile() method where you can do an … well, you get the idea.

What about the incremental reconciler?

If you poke around in the reconciler interfaces a bit you’ll notice there are actually two modes of operation. The non-incremental mode simply tells you that the document has been modified with little additional information. The incremental mode tells you about what’s changed in the document each time that reconcile() is called. It tells you when text has been added or removed and from where in the document. That sounds pretty useful!

On my last Eclipse editor project, I wanted to take advantage of this. If I knew which part of the document has changed, I could just reparse that section and update the affected model elements. I actually foolishly went through all the trouble of implementing this logic and then gave it a try. Things did not work as I expected. There is a fatal flaw in the incremental mode: By the time the reconciler is called, the document may have been modified further so all the modification information is provides is basically useless!

I believe this is caused by two factors. First, because it is multi-threaded, it’s hard to lock the document and still maintain the “update in the background” semantics of the reconciler. Second, the incremental reconcile() method only provides information about a single modification, i.e. an insert or delete in the document. So if the user does the classic “insert character + down arrow” editing technique to say comment out a bunch of lines, the reconciler will get called for each individual change, but the document will already contain all the modifications when the first reconcile call is made. D’oh.

When I realized it works like this, I did a bunch of searches on Eclipse newsgroups, google code, etc. I came to the conclusion that either no one has ever actually used the incremental reconciler, or they have and then silently slunk back to the non-incremental version when the found this fatal flaw.

If I’m wrong about this, I’d love to hear why!

eclipse, java

  1. January 26th, 2010 at 13:47 | #1

    Hi,
    Thanks a lot for sharing the understanding. It helped me get back in touch with what I had worked on long time ago.

    Eager to check other articles on your blog.

    Swati

  2. November 2nd, 2010 at 21:43 | #2

    Hi thanks for taking the time to post this. I like that you explain reconcilers so thoroughly. Your post has helped me to start to get a handle on why the Groovy-Eclipse plugin is adding error markers all over my groovy files. I thought the builder was the problem but I can see clearly the Reconciler has the true power in the editor… thanks for writing this.

    gMale

  3. February 25th, 2011 at 11:12 | #3

    Thanks for the post – I just went through the trouble of implementing the logic for an incremental reconciler, which appears to work okay (even given the fact that changes can be made during reconciliation). It appears that all calls to the reconciler are queued-up, so the document will eventually be reconciled to match the user’s latest text once they “take a break”.

    Unfortunately, with a 16,000+ line file, I’m finding that the editor locks during a call to ProjectionAnnotationModel.modifyAnnotations(…). Can’t for the life of me figure out why (this method takes 5 seconds with the large file). But I’m glad to see others find success in the sea of inconclusive posts by those facing issues in Eclipse plug-in development!

  4. March 13th, 2014 at 04:00 | #4

    I see that the reconcilers, at least the PresentationReconciler (I have no idea how it compares to the MonoReconciler), rely on the parition types. Therefore, I have created a questions

    http://www.eclipse.org/forums/index.php/t/671696/
    http://www.eclipse.org/forums/index.php/t/671609/

    They both ask for the same thing – why do you need the content (i.e. partition) type of your document to do the repair? It seems to me that the even if you have deal with only one modification in your document, because this change may may change the whole repartitioning of your document, it makes no sense to rely on the premodified parition type. I mean that timing can be not the only problem of incremental reconciliation. It may fail even if you run everything in one thread and reconcile after every individual modification.

  1. No trackbacks yet.