org.eclipse.emf.codegen.merge.java.facade.ast
Class ASTJCompilationUnit.NodeCommenter

java.lang.Object
  extended by org.eclipse.emf.codegen.merge.java.facade.ast.ASTJCompilationUnit.AbstractRewriter
      extended by org.eclipse.emf.codegen.merge.java.facade.ast.ASTJCompilationUnit.NodeCommenter
Enclosing class:
ASTJCompilationUnit

protected class ASTJCompilationUnit.NodeCommenter
extends ASTJCompilationUnit.AbstractRewriter


Nested Class Summary
protected  class ASTJCompilationUnit.NodeCommenter.LineBreakInserter
          Class that inserts extra line breaks between nodes when nodes are being commented out.
 
Field Summary
protected  java.util.Map<java.lang.Integer,InsertEdit> addedInsertEdits
          Map of insert offsets to InsertEdit objects created.
protected  java.util.Map<org.eclipse.jdt.core.dom.ASTNode,org.eclipse.jdt.core.dom.rewrite.ITrackedNodePosition> commentedOutPositions
          Map of commented out nodes to their tracked positions
protected  IDocument doc
          Document after call to ASTRewrite.rewriteAST() or ASTRewrite.rewriteAST(IDocument, Map).
protected static java.lang.String EMPTY_STRING
           
protected static java.lang.String LINE_COMMENT_STRING
          String to be inserted at the beginning of lines to indicate the line comment
protected  ASTJCompilationUnit.NodeCommenter.LineBreakInserter lineBreakInserter
          Responsible for inserting line breaks at the beginning and the end of the commented out nodes
protected  java.util.List<TextEdit> textEditsToRevert
          List of currently added text edits that have to be reverted (removed) in case of an exception.
 
Constructor Summary
ASTJCompilationUnit.NodeCommenter()
          Enables tracking for all commented out nodes.
 
Method Summary
protected  TextEdit addEdits(TextEdit existingEdits, IDocument doc)
          Adds a TextEdit that comments out required nodes.
protected  void addLineBreakAfterNode(TextEdit existingEdits, org.eclipse.jdt.core.dom.rewrite.ITrackedNodePosition nodePosition, org.eclipse.jdt.core.dom.ASTNode node)
          If there is anything after the node, inserts the line break to prevent commenting out extra content.
protected  int addLineBreakBeforeNode(TextEdit existingEdits, org.eclipse.jdt.core.dom.rewrite.ITrackedNodePosition nodePosition, org.eclipse.jdt.core.dom.ASTNode node)
          Add line break before the node if needed.
protected  java.lang.String commentOutEachLine(java.lang.String text)
          Comments out each line but the first one in the given text, and returns resulting new text.
protected  void commentOutLines(TextEdit existingEdits, int firstLine, int lastLine)
          Creates and adds InsertEdits that comment out all lines between firstLine and lastLine inclusively.
protected  ReplaceEdit commentOutReplaceEdit(ReplaceEdit replaceEdit)
          Replaces given ReplaceEdit by new ReplaceEdit with each line commented out.
protected  void findAndCommentOutReplaceEdit(TextEdit existingEdits, org.eclipse.jdt.core.dom.rewrite.ITrackedNodePosition nodePosition)
          Finds first ReplaceEdit in existing edits that covers node position range, and comments out each line in it.
 
Methods inherited from class org.eclipse.emf.codegen.merge.java.facade.ast.ASTJCompilationUnit.AbstractRewriter
createEdits
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LINE_COMMENT_STRING

protected static final java.lang.String LINE_COMMENT_STRING
String to be inserted at the beginning of lines to indicate the line comment

See Also:
Constant Field Values

EMPTY_STRING

protected static final java.lang.String EMPTY_STRING
See Also:
Constant Field Values

commentedOutPositions

protected java.util.Map<org.eclipse.jdt.core.dom.ASTNode,org.eclipse.jdt.core.dom.rewrite.ITrackedNodePosition> commentedOutPositions
Map of commented out nodes to their tracked positions


lineBreakInserter

protected ASTJCompilationUnit.NodeCommenter.LineBreakInserter lineBreakInserter
Responsible for inserting line breaks at the beginning and the end of the commented out nodes


addedInsertEdits

protected java.util.Map<java.lang.Integer,InsertEdit> addedInsertEdits
Map of insert offsets to InsertEdit objects created. Used to prevent adding different InsertEdit objects at the same offset.


textEditsToRevert

protected java.util.List<TextEdit> textEditsToRevert
List of currently added text edits that have to be reverted (removed) in case of an exception.


doc

protected IDocument doc
Document after call to ASTRewrite.rewriteAST() or ASTRewrite.rewriteAST(IDocument, Map).

Constructor Detail

ASTJCompilationUnit.NodeCommenter

public ASTJCompilationUnit.NodeCommenter()
Enables tracking for all commented out nodes.

This constructor must be called before call to ASTRewrite.rewriteAST() or ASTRewrite.rewriteAST(IDocument, Map).

See Also:
ASTRewrite.track(ASTNode)
Method Detail

addEdits

protected TextEdit addEdits(TextEdit existingEdits,
                            IDocument doc)
Adds a TextEdit that comments out required nodes.

Specified by:
addEdits in class ASTJCompilationUnit.AbstractRewriter
Parameters:
existingEdits - existing edits on
doc - document after call to ASTRewrite.rewriteAST() or ASTRewrite.rewriteAST(IDocument, Map).
Returns:
existingEdits with new edits added
See Also:
ASTJCompilationUnit.NodeContentsReplacer.trackedNodePositionsMap

addLineBreakBeforeNode

protected int addLineBreakBeforeNode(TextEdit existingEdits,
                                     org.eclipse.jdt.core.dom.rewrite.ITrackedNodePosition nodePosition,
                                     org.eclipse.jdt.core.dom.ASTNode node)
                              throws BadLocationException
Add line break before the node if needed. If there was a line break added at this position (e.g. after the previous node), replace it. Added line break will contain the marker to comment out the first line of the node. If InsertEdit has been created, the returned line number is the line after the line break, i.e. the next line that has to be commented out. If no changes are made, returned line number is the first line of the node.

Parameters:
existingEdits -
nodePosition -
node -
Returns:
line number of the next line that has to be commented out
Throws:
BadLocationException
See Also:
ASTJCompilationUnit.NodeCommenter.LineBreakInserter.createLineBreakBeforeNode(ITrackedNodePosition, ASTNode)

commentOutLines

protected void commentOutLines(TextEdit existingEdits,
                               int firstLine,
                               int lastLine)
                        throws BadLocationException
Creates and adds InsertEdits that comment out all lines between firstLine and lastLine inclusively.

If there is a ReplaceEdit that covers positions where InsertEdits are inserted, then ReplaceEdit is replaced by another ReplaceEdit with modified text with all lines commented out.

If there is any other problem adding new InsertEdits to existing edits, original exception is re-thrown.

Parameters:
existingEdits -
firstLine -
lastLine -
Throws:
BadLocationException

commentOutReplaceEdit

protected ReplaceEdit commentOutReplaceEdit(ReplaceEdit replaceEdit)
Replaces given ReplaceEdit by new ReplaceEdit with each line commented out.

New ReplaceEdit has the same offset and length as the given ReplaceEdit. Text of new ReplaceEdit has each line but the first one commented out. Given ReplaceEdit is removed from its parent, and new ReplaceEdit is inserted in its place.

Parameters:
replaceEdit -
Returns:
new ReplaceEdit

findAndCommentOutReplaceEdit

protected void findAndCommentOutReplaceEdit(TextEdit existingEdits,
                                            org.eclipse.jdt.core.dom.rewrite.ITrackedNodePosition nodePosition)
Finds first ReplaceEdit in existing edits that covers node position range, and comments out each line in it.

This method is used in the case when existing replace edit covers only a part of one line, but the contents that it replaces is longer than 1 line. In this case, such replace edit will be found and its contents changed by this method.

Parameters:
existingEdits -
nodePosition - range of existing node that has a corresponding ReplaceEdit for node's range
See Also:
commentOutReplaceEdit(ReplaceEdit)

commentOutEachLine

protected java.lang.String commentOutEachLine(java.lang.String text)
Comments out each line but the first one in the given text, and returns resulting new text.

Parameters:
text -
Returns:
new text with each line but the first one commented out

addLineBreakAfterNode

protected void addLineBreakAfterNode(TextEdit existingEdits,
                                     org.eclipse.jdt.core.dom.rewrite.ITrackedNodePosition nodePosition,
                                     org.eclipse.jdt.core.dom.ASTNode node)
                              throws BadLocationException
If there is anything after the node, inserts the line break to prevent commenting out extra content.

Parameters:
existingEdits -
nodePosition -
node -
Throws:
BadLocationException
See Also:
ASTJCompilationUnit.NodeCommenter.LineBreakInserter.createLineBreakAfterNode(ITrackedNodePosition, ASTNode)

Copyright 2001-2012 IBM Corporation and others.
All Rights Reserved.