1 11 package org.eclipse.jdt.core.dom; 12 import java.util.Hashtable ; 13 import java.util.List ; 14 import java.util.Map ; 15 16 import org.eclipse.text.edits.MultiTextEdit; 17 import org.eclipse.text.edits.TextEdit; 18 19 import org.eclipse.jface.text.IDocument; 20 import org.eclipse.jface.text.TextUtilities; 21 22 import org.eclipse.jdt.core.dom.SimplePropertyDescriptor; 23 import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor; 24 import org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer; 25 import org.eclipse.jdt.internal.core.dom.rewrite.ASTRewriteAnalyzer; 26 import org.eclipse.jdt.internal.core.dom.rewrite.LineInformation; 27 import org.eclipse.jdt.internal.core.dom.rewrite.ListRewriteEvent; 28 import org.eclipse.jdt.internal.core.dom.rewrite.NodeInfoStore; 29 import org.eclipse.jdt.internal.core.dom.rewrite.NodeRewriteEvent; 30 import org.eclipse.jdt.internal.core.dom.rewrite.RewriteEventStore; 31 import org.eclipse.jdt.internal.core.dom.rewrite.RewriteEventStore.CopySourceInfo; 32 import org.eclipse.jdt.internal.core.dom.rewrite.RewriteEventStore.PropertyLocation; 33 34 38 class InternalASTRewrite extends NodeEventHandler { 39 40 41 private CompilationUnit root; 42 43 protected final RewriteEventStore eventStore; 44 protected final NodeInfoStore nodeStore; 45 protected final Hashtable clonedNodes; 46 47 int cloneDepth = 0; 48 49 53 public InternalASTRewrite(CompilationUnit root) { 54 this.root = root; 55 this.eventStore = new RewriteEventStore(); 56 this.nodeStore = new NodeInfoStore(root.getAST()); 57 this.clonedNodes = new Hashtable (); 58 } 59 60 68 public TextEdit rewriteAST(IDocument document, Map options) { 69 TextEdit result = new MultiTextEdit(); 70 71 final CompilationUnit rootNode = getRootNode(); 72 if (rootNode != null) { 73 TargetSourceRangeComputer xsrComputer = new TargetSourceRangeComputer() { 74 81 public SourceRange computeSourceRange(ASTNode node) { 82 int extendedStartPosition = rootNode.getExtendedStartPosition(node); 83 int extendedLength = rootNode.getExtendedLength(node); 84 return new SourceRange(extendedStartPosition, extendedLength); 85 } 86 }; 87 char[] content= document.get().toCharArray(); 88 LineInformation lineInfo= LineInformation.create(document); 89 String lineDelim= TextUtilities.getDefaultLineDelimiter(document); 90 List comments= rootNode.getCommentList(); 91 92 ASTRewriteAnalyzer visitor = new ASTRewriteAnalyzer(content, lineInfo, lineDelim, result, this.eventStore, this.nodeStore, comments, options, xsrComputer); 93 rootNode.accept(visitor); 94 } 95 return result; 96 } 97 98 private void markAsMoveOrCopyTarget(ASTNode node, ASTNode newChild) { 99 ASTNode source = (ASTNode)this.clonedNodes.get(newChild); 100 if(source != null) { 101 if(this.cloneDepth == 0) { 102 PropertyLocation propertyLocation = this.eventStore.getPropertyLocation(source, RewriteEventStore.ORIGINAL); 103 CopySourceInfo sourceInfo = 104 this.eventStore.markAsCopySource( 105 propertyLocation.getParent(), 106 propertyLocation.getProperty(), 107 source, 108 false); 109 this.nodeStore.markAsCopyTarget(newChild, sourceInfo); 110 } 111 } else if((newChild.getFlags() & ASTNode.ORIGINAL) != 0) { 112 PropertyLocation propertyLocation = this.eventStore.getPropertyLocation(newChild, RewriteEventStore.ORIGINAL); 113 CopySourceInfo sourceInfo = 114 this.eventStore.markAsCopySource( 115 propertyLocation.getParent(), 116 propertyLocation.getProperty(), 117 newChild, 118 true); 119 this.nodeStore.markAsCopyTarget(newChild, sourceInfo); 120 } 121 } 122 123 private CompilationUnit getRootNode() { 124 return this.root; 125 } 126 127 public String toString() { 128 StringBuffer buf = new StringBuffer (); 129 buf.append("Events:\n"); buf.append(this.eventStore.toString()); 131 return buf.toString(); 132 } 133 134 void preValueChangeEvent(ASTNode node, SimplePropertyDescriptor property) { 135 this.getNodeEvent(node, property); 137 } 138 139 void postValueChangeEvent(ASTNode node, SimplePropertyDescriptor property) { 140 NodeRewriteEvent event = this.getNodeEvent(node, property); 141 event.setNewValue(node.getStructuralProperty(property)); 142 } 143 144 void preAddChildEvent(ASTNode node, ASTNode child, StructuralPropertyDescriptor property) { 145 if(property.isChildProperty()) { 146 NodeRewriteEvent event = this.getNodeEvent(node, property); 147 event.setNewValue(child); 148 if(child != null) { 149 this.markAsMoveOrCopyTarget(node, child); 150 } 151 } else if(property.isChildListProperty()) { 152 this.getListEvent(node, property); 154 } 155 } 156 157 void postAddChildEvent(ASTNode node, ASTNode child, StructuralPropertyDescriptor property) { 158 if(property.isChildListProperty()) { 159 160 ListRewriteEvent event = this.getListEvent(node, property); 161 List list = (List )node.getStructuralProperty(property); 162 int i = list.indexOf(child); 163 int s = list.size(); 164 int index; 165 if(i + 1 < s) { 166 ASTNode nextNode = (ASTNode)list.get(i + 1); 167 index = event.getIndex(nextNode, ListRewriteEvent.NEW); 168 } else { 169 index = -1; 170 } 171 event.insert(child, index); 172 if(child != null) { 173 this.markAsMoveOrCopyTarget(node, child); 174 } 175 } 176 } 177 178 void preRemoveChildEvent(ASTNode node, ASTNode child, StructuralPropertyDescriptor property) { 179 if(property.isChildProperty()) { 180 NodeRewriteEvent event = getNodeEvent(node, property); 181 event.setNewValue(null); 182 } else if(property.isChildListProperty()) { 183 ListRewriteEvent event = this.getListEvent(node, property); 184 int i = event.getIndex(child, ListRewriteEvent.NEW); 185 NodeRewriteEvent nodeEvent = (NodeRewriteEvent)event.getChildren()[i]; 186 if(nodeEvent.getOriginalValue() == null) { 187 event.revertChange(nodeEvent); 188 } else { 189 nodeEvent.setNewValue(null); 190 } 191 } 192 } 193 194 void preReplaceChildEvent(ASTNode node, ASTNode child, ASTNode newChild, StructuralPropertyDescriptor property) { 195 if(property.isChildProperty()) { 196 NodeRewriteEvent event = getNodeEvent(node, property); 197 event.setNewValue(newChild); 198 if(newChild != null) { 199 this.markAsMoveOrCopyTarget(node, newChild); 200 } 201 } else if(property.isChildListProperty()) { 202 ListRewriteEvent event = this.getListEvent(node, property); 203 int i = event.getIndex(child, ListRewriteEvent.NEW); 204 NodeRewriteEvent nodeEvent = (NodeRewriteEvent)event.getChildren()[i]; 205 nodeEvent.setNewValue(newChild); 206 if(newChild != null) { 207 this.markAsMoveOrCopyTarget(node, newChild); 208 } 209 } 210 } 211 212 213 void preCloneNodeEvent(ASTNode node) { 214 this.cloneDepth++; 215 } 216 217 218 void postCloneNodeEvent(ASTNode node, ASTNode clone) { 219 if(node.ast == root.ast && clone.ast == root.ast) { 220 if((node.getFlags() & ASTNode.ORIGINAL) != 0) { 221 this.clonedNodes.put(clone, node); 222 } else { 223 Object original = this.clonedNodes.get(node); 225 if(original != null) { 226 this.clonedNodes.put(clone, original); 227 } 228 } 229 } 230 this.cloneDepth--; 231 } 232 233 private NodeRewriteEvent getNodeEvent(ASTNode node, StructuralPropertyDescriptor property) { 234 return this.eventStore.getNodeEvent(node, property, true); 235 } 236 237 private ListRewriteEvent getListEvent(ASTNode node, StructuralPropertyDescriptor property) { 238 return this.eventStore.getListEvent(node, property, true); 239 } 240 } 241 | Popular Tags |