Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to diff two regions of the same file in Eclipse

I'm a TDDer and often have a need to refactor out common or similar code. Similar code is not always a result of copy and paste.

I'm not looking for tools to identify the regions or suspected duplications, there are a number of tools to do that. And if the code is exactly the same there is no big problem, Eclipse can almost always do that by itself.

I am looking for tools to visualize differences of sections of code that are radically different, but my human eye can see the structural similarities, and could possibly be made even more similar, so that the common code eventually could be factored out.

It would be very handy if there was a possibility to mark two regions and get Eclipse (or some other tool) to mark the differences. With this information it would be much simpler to iteratively move the regions closer until they are the same and then activate the Extract Method refactoring.

It can be done in Emacs of course, but I'd like to have this readily available from Eclipse. Any pointers?

like image 920
thoni56 Avatar asked May 12 '10 10:05

thoni56


People also ask

How do I compare two EAR files in Eclipse?

To compare two files, highlight them in a view such as the Package Explorer and select Compare With→ Each Other in the view's context menu.

How can I compare two JSON files in Eclipse?

To compare two files in Eclipse, first select them in the Project Explorer / Package Explorer / Navigator with control-click. Now right-click on one of the files, and the following context menu will appear. Select Compare With / Each Other.

How do I open two tabs in Eclipse?

To open multiple Eclipse windows using the same workspace, select Window→ New Window. It's a good idea to use this technique if you want to work in two different perspectives (such as the Java and Debug perspectives) at the same time in different windows.


1 Answers

Seems there was somewhat usable answers in this question, a question articulating the same need. But, again those answers focus on finding duplications, not vizualising it.

Two suggestions that works are KDiff3 and Diffuse. Both allow you to open up the same file twice or paste different sections in the panes. There seems to be no way to use them from Eclipse though.

like image 172
thoni56 Avatar answered Nov 02 '22 00:11

thoni56