1 11 12 package org.eclipse.jdt.internal.corext.refactoring.util; 13 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.jdt.core.dom.ASTNode; 19 import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor; 20 import org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer; 21 22 public class TightSourceRangeComputer extends TargetSourceRangeComputer { 23 private HashSet fTightSourceRangeNodes= new HashSet (); 24 25 public void addTightSourceNode(ASTNode reference) { 26 fTightSourceRangeNodes.add(reference); 27 28 List properties= reference.structuralPropertiesForType(); 29 for (Iterator iterator= properties.iterator(); iterator.hasNext();) { 30 StructuralPropertyDescriptor descriptor= (StructuralPropertyDescriptor)iterator.next(); 31 if (descriptor.isChildProperty()) { 32 ASTNode child= (ASTNode)reference.getStructuralProperty(descriptor); 33 if (isExtending(child, reference)) { 34 addTightSourceNode(child); 35 } 36 } else if (descriptor.isChildListProperty()) { 37 List childs= (List )reference.getStructuralProperty(descriptor); 38 for (Iterator iterator2= childs.iterator(); iterator2.hasNext();) { 39 ASTNode child= (ASTNode)iterator2.next(); 40 if (isExtending(child, reference)) { 41 addTightSourceNode(child); 42 } 43 } 44 } 45 } 46 } 47 48 public SourceRange computeSourceRange(ASTNode node) { 49 if (fTightSourceRangeNodes.contains(node)) { 50 return new TargetSourceRangeComputer.SourceRange(node.getStartPosition(), node.getLength()); 51 } else { 52 return super.computeSourceRange(node); } 54 } 55 56 private boolean isExtending(ASTNode child, ASTNode parent) { 57 SourceRange extendedRange= super.computeSourceRange(child); 58 59 int parentStart= parent.getStartPosition(); 60 int extendedStart= extendedRange.getStartPosition(); 61 if (parentStart > extendedStart) 62 return true; 63 64 int parentEnd= parentStart + parent.getLength(); 65 int extendedEnd= extendedStart + extendedRange.getLength(); 66 if (parentEnd < extendedEnd) 67 return true; 68 69 return false; 70 } 71 } 72 | Popular Tags |