1 11 package org.eclipse.jdt.internal.ui.javaeditor; 12 13 import org.eclipse.jface.text.IDocument; 14 import org.eclipse.jface.text.TextSelection; 15 16 import org.eclipse.jdt.core.ICompilationUnit; 17 import org.eclipse.jdt.core.IJavaElement; 18 import org.eclipse.jdt.core.JavaModelException; 19 20 import org.eclipse.jdt.core.dom.ASTNode; 21 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; 22 import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration; 23 import org.eclipse.jdt.core.dom.BodyDeclaration; 24 import org.eclipse.jdt.core.dom.CompilationUnit; 25 import org.eclipse.jdt.core.dom.SingleVariableDeclaration; 26 import org.eclipse.jdt.core.dom.VariableDeclarationFragment; 27 28 import org.eclipse.jdt.internal.corext.dom.Selection; 29 import org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer; 30 import org.eclipse.jdt.internal.ui.JavaPlugin; 31 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 32 33 37 public class JavaTextSelection extends TextSelection { 38 39 private IJavaElement fElement; 40 private IJavaElement[] fResolvedElements; 41 42 private boolean fEnclosingElementRequested; 43 private IJavaElement fEnclosingElement; 44 45 private boolean fPartialASTRequested; 46 private CompilationUnit fPartialAST; 47 48 private boolean fNodesRequested; 49 private ASTNode[] fSelectedNodes; 50 private ASTNode fCoveringNode; 51 52 private boolean fInMethodBodyRequested; 53 private boolean fInMethodBody; 54 55 private boolean fInClassInitializerRequested; 56 private boolean fInClassInitializer; 57 58 private boolean fInVariableInitializerRequested; 59 private boolean fInVariableInitializer; 60 61 64 public JavaTextSelection(IJavaElement element, IDocument document, int offset, int length) { 65 super(document, offset, length); 66 fElement= element; 67 } 68 69 77 public IJavaElement[] resolveElementAtOffset() throws JavaModelException { 78 if (fResolvedElements != null) 79 return fResolvedElements; 80 fResolvedElements= SelectionConverter.codeResolve(fElement, this); 82 return fResolvedElements; 84 } 85 86 public IJavaElement resolveEnclosingElement() throws JavaModelException { 87 if (fEnclosingElementRequested) 88 return fEnclosingElement; 89 fEnclosingElementRequested= true; 90 fEnclosingElement= SelectionConverter.resolveEnclosingElement(fElement, this); 91 return fEnclosingElement; 92 } 93 94 public CompilationUnit resolvePartialAstAtOffset() { 95 if (fPartialASTRequested) 96 return fPartialAST; 97 fPartialASTRequested= true; 98 if (! (fElement instanceof ICompilationUnit)) 99 return null; 100 fPartialAST= JavaPlugin.getDefault().getASTProvider().getAST(fElement, ASTProvider.WAIT_YES, null); 102 return fPartialAST; 104 } 105 106 public ASTNode[] resolveSelectedNodes() { 107 if (fNodesRequested) 108 return fSelectedNodes; 109 fNodesRequested= true; 110 CompilationUnit root= resolvePartialAstAtOffset(); 111 if (root == null) 112 return null; 113 Selection ds= Selection.createFromStartLength(getOffset(), getLength()); 114 SelectionAnalyzer analyzer= new SelectionAnalyzer(ds, false); 115 root.accept(analyzer); 116 fSelectedNodes= analyzer.getSelectedNodes(); 117 fCoveringNode= analyzer.getLastCoveringNode(); 118 return fSelectedNodes; 119 } 120 121 public ASTNode resolveCoveringNode() { 122 if (fNodesRequested) 123 return fCoveringNode; 124 resolveSelectedNodes(); 125 return fCoveringNode; 126 } 127 128 public boolean resolveInMethodBody() { 129 if (fInMethodBodyRequested) 130 return fInMethodBody; 131 fInMethodBodyRequested= true; 132 resolveSelectedNodes(); 133 ASTNode node= getStartNode(); 134 if (node == null) { 135 fInMethodBody= true; 136 } else { 137 while (node != null) { 138 int nodeType= node.getNodeType(); 139 if (nodeType == ASTNode.BLOCK && node.getParent() instanceof BodyDeclaration) { 140 fInMethodBody= node.getParent().getNodeType() == ASTNode.METHOD_DECLARATION; 141 break; 142 } else if (nodeType == ASTNode.ANONYMOUS_CLASS_DECLARATION) { 143 fInMethodBody= false; 144 break; 145 } 146 node= node.getParent(); 147 } 148 } 149 return fInMethodBody; 150 } 151 152 public boolean resolveInClassInitializer() { 153 if (fInClassInitializerRequested) 154 return fInClassInitializer; 155 fInClassInitializerRequested= true; 156 resolveSelectedNodes(); 157 ASTNode node= getStartNode(); 158 if (node == null) { 159 fInClassInitializer= true; 160 } else { 161 while (node != null) { 162 int nodeType= node.getNodeType(); 163 if (node instanceof AbstractTypeDeclaration) { 164 fInClassInitializer= false; 165 break; 166 } else if (nodeType == ASTNode.ANONYMOUS_CLASS_DECLARATION) { 167 fInClassInitializer= false; 168 break; 169 } else if (nodeType == ASTNode.INITIALIZER) { 170 fInClassInitializer= true; 171 break; 172 } 173 node= node.getParent(); 174 } 175 } 176 return fInClassInitializer; 177 } 178 179 public boolean resolveInVariableInitializer() { 180 if (fInVariableInitializerRequested) 181 return fInVariableInitializer; 182 fInVariableInitializerRequested= true; 183 resolveSelectedNodes(); 184 ASTNode node= getStartNode(); 185 ASTNode last= null; 186 while (node != null) { 187 int nodeType= node.getNodeType(); 188 if (node instanceof AbstractTypeDeclaration) { 189 fInVariableInitializer= false; 190 break; 191 } else if (nodeType == ASTNode.ANONYMOUS_CLASS_DECLARATION) { 192 fInVariableInitializer= false; 193 break; 194 } else if (nodeType == ASTNode.VARIABLE_DECLARATION_FRAGMENT && 195 ((VariableDeclarationFragment)node).getInitializer() == last) { 196 fInVariableInitializer= true; 197 break; 198 } else if (nodeType == ASTNode.SINGLE_VARIABLE_DECLARATION && 199 ((SingleVariableDeclaration)node).getInitializer() == last) { 200 fInVariableInitializer= true; 201 break; 202 } else if (nodeType == ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION && 203 ((AnnotationTypeMemberDeclaration)node).getDefault() == last) { 204 fInVariableInitializer= true; 205 break; 206 } 207 last= node; 208 node= node.getParent(); 209 } 210 return fInVariableInitializer; 211 } 212 213 private ASTNode getStartNode() { 214 if (fSelectedNodes != null && fSelectedNodes.length > 0) 215 return fSelectedNodes[0]; 216 else 217 return fCoveringNode; 218 } 219 } 220 | Popular Tags |