Timesavers: Eclipse Quick Diff

Friday, April 24, 2009

This is part of an ongoing series on Timesavers. The goal is simple: short, easily-digestible posts designed to help developers get faster and more productive

Have you ever been editing your code, and sometime down the road -- maybe 5 minutes, maybe 2 hours, maybe after lunch -- you break something. It used to work, and now it doesn't. You're thinking, "what change, of all the beautiful edits I've made, broke my fortress of code?" Being an experienced Eclipse user, you smile and say to yourself, "I know, I'll compare to my local history!". Or maybe you decide to simply compare to the latest SVN Version.

So off you go, right-clicking on the file, selecting "compare", and then choosing a comparison target. Often, this is a perfectly reasonable decision. Well done, good sir.

There is, however, a quicker way to easily see the changes you've made to your code in an editing session, or even across sessions: Quick Diff

On the left side of your Eclipse editor is the "ruler" bar. It's the thing with the line numbers. And if you have Quick Diff enabled, it's also the thing with the pretty colors showing you what lines have been added, changed, and deleted. Here's what Quick Diff looks like when I change a line of code and then hover over the ruler bar color decorator:


Notice there's an "F2 for focus" line at the bottom of the popup. Here's what it looks like when you do that:


What's cool is that you can select this text from the popup. So if you see something in your Quick Diff that you think "OOOH! That's the code I monkeyed with. Let me grab that and drop it back in and see if that fixes my problem", you can simply copy it from the popup and off you go.

"Added" lines of code will merely result in a "+" for each added line in the popup; Changes will show the full lines that were changed (though they won't give you the nifty "Compare Editor", which is one of the compelling reasons for using the full-on "Compare With" stuff). Deleted lines will give you a small line in the ruler bar. For example, I deleted a function, hovered over the black line, and this is what I got:


To enable Quick Diff, simply open the Eclipse preferences (ctrl-shift-L, ctrl-shift-L), filter on "Quick", select the "Quick Diff" label, and have at it. Here you can also choose the Reference Source and change colors. It looks like this:


I use "Pristine SVN Copy" as the source for projects connected to SVN, and "Version on Disk" for non-SVN projects.

Bottom line: Enable Quick Diff, then hover in the gutter for popups showing you what’s changed. F2 to focus so you can select code


Anuj Gakhar said...

Thanks Marc, that is really useful. I didnt know about this noe before.

Scot said...

So this only works until you save the current file? Maybe I don't have something setup properly? The pretty colors go away when i save the file and then I can only compare against local history to see the differences.

Marc Esher said...

I'm not having that problem. When I set it up to do a Quick Diff between the Pristine SVN Copy, it works as I'd expect it to work. Then, when I commit the file, the diffs go away

Meteor said...
This comment has been removed by a blog administrator.
Wyatt said...

The problem I'm having is that when I commit, the diffs don't go away.

Marc Esher said...

Wyatt, I've never understood that behavior either. You'd think that even just "refresh"ing the file would cause the gutter notations to go away, but they stay there till you close and reopen the file.

I wonder if this is a bug, an oversight, or expected behavior