1 11 package org.eclipse.jdt.internal.corext.dom; 12 13 import org.eclipse.jdt.core.dom.ASTNode; 14 15 import org.eclipse.jface.text.IRegion; 16 17 import org.eclipse.jdt.internal.corext.Assert; 18 19 public class Selection { 20 21 22 public static final int INTERSECTS= 0; 23 24 25 public static final int BEFORE= 1; 26 27 28 public static final int SELECTED= 2; 29 30 31 public static final int AFTER= 3; 32 33 private int fStart; 34 private int fLength; 35 private int fExclusiveEnd; 36 37 protected Selection() { 38 } 39 40 47 public static Selection createFromStartLength(int s, int l) { 48 Assert.isTrue(s >= 0 && l >= 0); 49 Selection result= new Selection(); 50 result.fStart= s; 51 result.fLength= l; 52 result.fExclusiveEnd= s + l; 53 return result; 54 } 55 56 63 public static Selection createFromStartEnd(int s, int e) { 64 Assert.isTrue(s >= 0 && e >= s); 65 Selection result= new Selection(); 66 result.fStart= s; 67 result.fLength= e - s + 1; 68 result.fExclusiveEnd= result.fStart + result.fLength; 69 return result; 70 } 71 72 public int getOffset() { 73 return fStart; 74 } 75 76 public int getLength() { 77 return fLength; 78 } 79 80 public int getInclusiveEnd() { 81 return fExclusiveEnd - 1; 82 } 83 84 public int getExclusiveEnd() { 85 return fExclusiveEnd; 86 } 87 88 101 public int getVisitSelectionMode(ASTNode node) { 102 int nodeStart= node.getStartPosition(); 103 int nodeEnd= nodeStart + node.getLength(); 104 if (nodeEnd <= fStart) 105 return BEFORE; 106 else if (covers(node)) 107 return SELECTED; 108 else if (fExclusiveEnd <= nodeStart) 109 return AFTER; 110 return INTERSECTS; 111 } 112 113 public int getEndVisitSelectionMode(ASTNode node) { 114 int nodeStart= node.getStartPosition(); 115 int nodeEnd= nodeStart + node.getLength(); 116 if (nodeEnd <= fStart) 117 return BEFORE; 118 else if (covers(node)) 119 return SELECTED; 120 else if (nodeEnd >= fExclusiveEnd) 121 return AFTER; 122 return INTERSECTS; 123 } 124 125 127 public boolean enclosedBy(ASTNode node) { 128 int nodeStart= node.getStartPosition(); 129 return nodeStart < fStart && fExclusiveEnd < nodeStart + node.getLength(); 130 } 131 132 136 138 public boolean covers(int position) { 139 return fStart <= position && position < fStart + fLength; 140 } 141 142 public boolean covers(ASTNode node) { 143 int nodeStart= node.getStartPosition(); 144 return fStart <= nodeStart && nodeStart + node.getLength() <= fExclusiveEnd; 145 } 146 147 151 public boolean coveredBy(ASTNode node) { 152 int nodeStart= node.getStartPosition(); 153 return nodeStart <= fStart && fExclusiveEnd <= nodeStart + node.getLength(); 154 } 155 156 public boolean coveredBy(IRegion region) { 157 int rangeStart= region.getOffset(); 158 return rangeStart <= fStart && fExclusiveEnd <= rangeStart + region.getLength(); 159 } 160 161 public boolean endsIn(ASTNode node) { 166 int nodeStart= node.getStartPosition(); 167 return nodeStart < fExclusiveEnd && fExclusiveEnd < nodeStart + node.getLength(); 168 } 169 170 public boolean liesOutside(ASTNode node) { 171 int nodeStart= node.getStartPosition(); 172 int nodeEnd= nodeStart + node.getLength(); 173 boolean nodeBeforeSelection= nodeEnd < fStart; 174 boolean selectionBeforeNode= fExclusiveEnd < nodeStart; 175 return nodeBeforeSelection || selectionBeforeNode; 176 } 177 178 187 190 public String toString() { 191 return "<start == " + fStart + ", length == " + fLength + "/>"; } 193 } 194 | Popular Tags |