org.jrubyparser.util.diff
Class SequenceMatcher

java.lang.Object
  extended by org.jrubyparser.util.diff.SequenceMatcher

public class SequenceMatcher
extends Object

The SequenceMatcher class is used to produce a list of matching nodes. It does most of the work in the diffing process.

See Also:
NodeDiff

Field Summary
protected  List<Change> diffNodes
           
protected  IsJunk isJunk
           
protected  Node newNode
           
protected  Node oldNode
           
 
Constructor Summary
SequenceMatcher(Node newNode, Node oldNode)
          Create a SequenceMatcher object without a function for sorting out junk.
SequenceMatcher(Node newNode, Node oldNode, IsJunk isJunk)
          SequenceMatcher compares two nodes for matching nodes.
 
Method Summary
 int calcComplexity(Node node)
          Returns an integer representing how many levels of nesting of children the node has.
protected  void checkDiffForMoves()
          If a node has been moved, our initial diff will contain two changes: one insertion and one deletion.
protected  boolean checkForJunk(Node node)
          If an isJunk object provided, check the passed in node to see if it should be skipped.
protected  void deletedNode(Node node)
          A Node in the old AST is no longer present in the new one.
protected  void findChanges(Node newNode, Node oldNode)
          This method does the diffing by iterating through the nodes and calling itself recursively.
 List<Change> getDiffNodes()
          If we already have a diff, return that, otherwise create one.
 Node getNewNode()
           
 Node getOldNode()
           
protected  void handleBlockNodes(Node childNew, Node childOld)
          BlockNodes are problematic because they are often mismatched in source code where nodes have been moved around.
protected  void handleMismatchedNodes(Node childNew, Node childOld)
          Here we deal with the case where two nodes are mismatched.
protected  void insertedNode(Node node)
          A Node was inserted into the new AST.
protected  void modifiedNode(Node newNode, Node oldNode)
          The node in the new AST has been changed.
 void setNewNode(Node newNode)
           
 void setOldNode(Node oldNode)
           
 void setSequences(Node newNode, Node oldNode)
           
protected  void sortNodesIntoDiff(Node childNew, Node childOld)
          Decides what to do with nodes being diffed.
protected  Node stripOutNewlines(Node node)
          Newlines throw off the diffing algorithm's so we skip those.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

isJunk

protected IsJunk isJunk

newNode

protected Node newNode

oldNode

protected Node oldNode

diffNodes

protected List<Change> diffNodes
Constructor Detail

SequenceMatcher

public SequenceMatcher(Node newNode,
                       Node oldNode)
Create a SequenceMatcher object without a function for sorting out junk.

Parameters:
newNode - The current version of the node.
oldNode - The original version of the node being checked.

SequenceMatcher

public SequenceMatcher(Node newNode,
                       Node oldNode,
                       IsJunk isJunk)
SequenceMatcher compares two nodes for matching nodes.

isJunk is an object which implements the IsJunk interface and the #checkJunk() method. checkJunk is a method which takes a Node and determines whether or not it should be compared against the other node.

We pass in two nodes. Later, we can use the #setSequence, #setNewNode and #setOldNode methods to change out one or both of the nodes and create a new set of matches.

Parameters:
isJunk - A callback used to let users choose nodes not to be checked in diff.
newNode - The current version of the node.
oldNode - The node in the old version.
See Also:
IsJunk
Method Detail

setSequences

public final void setSequences(Node newNode,
                               Node oldNode)

setNewNode

public void setNewNode(Node newNode)

setOldNode

public void setOldNode(Node oldNode)

getNewNode

public Node getNewNode()

getOldNode

public Node getOldNode()

sortNodesIntoDiff

protected void sortNodesIntoDiff(Node childNew,
                                 Node childOld)
Decides what to do with nodes being diffed.

Parameters:
childNew - The current version of the node being diffed.
childOld - The original version of the node being diffed.

handleMismatchedNodes

protected void handleMismatchedNodes(Node childNew,
                                     Node childOld)
Here we deal with the case where two nodes are mismatched. This can be due to the insertion or deletion of a node in the new source. We make an initial attempt to match them back up here.

Parameters:
childNew - The current version of the node being diffed.
childOld - The original version of the node being diffed.

insertedNode

protected void insertedNode(Node node)
A Node was inserted into the new AST.

It is possible that it was just mismatched, but we will sort that out later.

Parameters:
node - recieves a Node.

deletedNode

protected void deletedNode(Node node)
A Node in the old AST is no longer present in the new one.

We assume it was deleted, but we will check for mismatches later.

Parameters:
node - Receives a Node.

modifiedNode

protected void modifiedNode(Node newNode,
                            Node oldNode)
The node in the new AST has been changed.

Parameters:
newNode - The Node from the new AST which was modified.
oldNode - The original version of the Node, from the old AST.

findChanges

protected void findChanges(Node newNode,
                           Node oldNode)
This method does the diffing by iterating through the nodes and calling itself recursively.

Parameters:
newNode - The node in the new version.
oldNode - The node in the old version.

handleBlockNodes

protected void handleBlockNodes(Node childNew,
                                Node childOld)
BlockNodes are problematic because they are often mismatched in source code where nodes have been moved around. However, they can't just be skipped like the NewLineNode, because they often have more than one child. This is how we deal with them.

Parameters:
childNew - The current version of the node.
childOld - The original version of the node.

checkForJunk

protected boolean checkForJunk(Node node)
If an isJunk object provided, check the passed in node to see if it should be skipped.

Parameters:
node - A Node to be checked against provided callback.
Returns:
Returns a boolean indicating whether or not Node should be skipped.

stripOutNewlines

protected Node stripOutNewlines(Node node)
Newlines throw off the diffing algorithm's so we skip those.

Parameters:
node - node is a Node that needs checked to see if it is a Newline.
Returns:
returns a Node.

checkDiffForMoves

protected void checkDiffForMoves()
If a node has been moved, our initial diff will contain two changes: one insertion and one deletion. Here we match those up, then send their children back in to check for changes internal to the moved node. This is how we can handle a method which has been both moved and modified, something impossible for most text-based diff tools.


getDiffNodes

public List<Change> getDiffNodes()
If we already have a diff, return that, otherwise create one.

Returns:
Returns an ArrayList of Change objects, the diff.

calcComplexity

public int calcComplexity(Node node)
Returns an integer representing how many levels of nesting of children the node has.

Returns:
Returns an int


Copyright © 2013. All Rights Reserved.