Pages

Friday, March 30, 2012

eclipse structure compare implementation tips

Eclipse platform is a powerful platform. Its plugin architecture enables people to develop more tools based on it without starting from scratch. This also encouraged me to write a structural comparison editor tool for a special text format using its compare support framework.

What I wanted to implement is to show the structure differences like the following image as comparing two different versions of java file.

Structure Compare Example
Definitely the eclipse platform plugin development guide - Implementing a structure viewer is the starting point to gain an initial understanding before getting into the hacking. We have our special own special format text file. It happens that we can parse it into a a tree-like structure. So all I need is to extend  org.eclipse.compare.structureCreators.So we define following in the plugin.xml.

<extension point="org.eclipse.compare.structureCreators">

<structureCreator

  class="com.mytechtip.model.StructureCreator"

  extensions="mtt"

  id="com.mytechtip.model.StructureCreator">

</structureCreator>

</extension>


Two main methods need to be implemented:
public String getContents(Object node, boolean ignoreWhitespace)  {
  ...
}
protected IStructureComparator createStructureComparator(Object element, IDocument document, ISharedDocumentAdapter sharedDocumentAdapter,
IProgressMonitor monitor) throws CoreException {
  ...
}

For the second method, although it only requires to return IStructureComparator, I did end up extending DocumentRangeNode and also implementing ITypedElement. Also make sure you do implement "hashCode()" and "equal()" correctly in the extended DocumentRangeNode class. This is because the traverse code in Differencer.traverse(boolean, Node, IProgressMonitor, Object, Object, Object) in package org.eclipse.compare.structuremergeviewer uses the HashSet to hold all the nodes within one tree level. If the hashCode() is not implemented correctly, even the equal() is implemented correctly, you will get a lot of unwanted differences when comparing.

No comments:

Post a Comment