TextMergeViewer with custom diff-generator, or reuse its visual parts [message #334999] |
Sat, 14 March 2009 19:13 |
Jarod McKnight Messages: 2 Registered: July 2009 |
Junior Member |
|
|
Hi,
I am working on an eclipse plugin to visualize copied source code. In my
use-case I have two files, with some lines of code duplicated across them.
Now I want a MergeViewer displaying the two files next to each other and
mark these copied lines of code, the same way, the normal TextMergeViewer
shows differences across two files. So some could say, its a sort of
reverted-diff, showing what is the same, and not what is different across
files.
Since it is nearly the same as the TextMergeViewer, I tried to inherit
from TextMergeViewer and change the code building the diffs. But as far as
I understand it, the 'private DocumentMerger fMerger;' is responsibly for
that.
My next idea was to inherit from ContentMergeViewer and copy the
visualizing code from TextMergeViwer, since its perfectly suitable for my
needs. But that wouldn't work either, because a lot of the used classes
are located in 'org.eclipse.compare.internal.*', which I couldn't use
outside of the original eclipse .jar-files.
So my question is, if there is an easier way of reusing the visual
components of TextMergeViewer (the linedrawing between the two shown files
for instance), or to simply switch the DocumentMerger with my own
diff-generating class.
Since the TextMergeViewer is nearly perfect for my needs, I would hate it
to copy its code, and all the stuff thats inside
'org.eclipse.compare.internal.*', just because the reuse of the
TextMergeViewer with another source of lines-to-mark wasn't foreseen.
~ Jarod
|
|
|
Re: TextMergeViewer with custom diff-generator, or reuse its visual parts [message #1386071 is a reply to message #334999] |
Fri, 13 June 2014 10:38 |
Adam Fiksen Messages: 1 Registered: June 2014 |
Junior Member |
|
|
I have a very similar use case and unfortunately haven't found a more elegant solution than copying the entire TextMergeViewer class and modifying it to my needs. Unfortunately its a HUGE monolithic class so was very tricky navigating and understanding what it was doing. Fortunately for my case the plugin I was developing was merely a proof of concept and so doesn't need to look beautiful.
For others who may come across this going down a similar path, I essentially copied TextMergeViewer into my own class, extending ContentMergeViewer, copied DocumentMerger similarly. In MyDocumentMerger I removed the token comparator portion of the doDiff() method as I didn't need it, and replaced the
result[0]= RangeDifferencer.findRanges(monitor, sa, sl, sr);
line in the Runnable's run function (defined in doDiff()) with MyOtherObject.analyse(lDoc, rDoc))
In MyOtherObject, I defined an inner class: MyRangeDifference extends RangeDifference which didn't add any functionality except add a public constructor. My analyse function calculated the lines that I wanted to highlight, and I returned an array of MyRangeDifference, which holds the line each section starts at and the length (number of lines) it covers.
In the constructor of MyTextMergeViewer, I replaced fMerger = new DocumentMerger(new IDocumentMergerInput(){...}; with fMerger = new MyDocumentMerger(new IDocumentMergerInput(){...};
To Summarise:
- Copy TextMergeViewer and DocumentMerger to own classes
- Fix compile errors with calls to getX() or removing the line (!)
- Extend RangeDifference to provide public constructor
- Replace DocumentMerger construction with MyDocumentMerger in MyTextMergerViewer constructor
- In doDiff method of MyDocumentMerger call own analysis/diff function
- Return a RangeDifference[], specifying you're own diff ranges.
- Optional but probably necessary: modify UI code in MyTextMergeViewer to display as you wish
[Updated on: Fri, 13 June 2014 20:23] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.03170 seconds