1 package no.uio.ifi.refaktor.utils;
3 import static no.uio.ifi.refaktor.RefaktorAssert.assertThat;
4 import static org.hamcrest.CoreMatchers.instanceOf;
6 import org.eclipse.core.resources.IProject;
7 import org.eclipse.jdt.core.ICompilationUnit;
8 import org.eclipse.jdt.core.dom.ASTNode;
9 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
10 import org.eclipse.jdt.core.dom.CompilationUnit;
11 import org.eclipse.jdt.core.dom.NodeFinder;
12 import org.eclipse.jface.text.IDocument;
13 import org.eclipse.jface.text.ITextSelection;
14 import org.eclipse.jface.text.TextSelection;
17 * A custom TextSelection that enforces the presence of
18 * a compilation unit to back the selection.
20 public class CompilationUnitTextSelection extends TextSelection implements ITextSelection {
22 private class NodeFinderCache {
23 private NodeFinder nodeFinder;
25 private NodeFinder getNodeFinder() {
26 if (nodeFinder == null)
27 nodeFinder = ParseUtils.createNodeFinder(ParseUtils.parse(compilationUnit), CompilationUnitTextSelection.this);
31 public void dropCachedData() {
36 private ICompilationUnit compilationUnit;
37 private final NodeFinderCache nodeFinderCache;
39 public CompilationUnitTextSelection(ICompilationUnit compilationUnit, int offset, int length) {
40 super(offset, length);
41 if (compilationUnit == null)
42 throw new NullPointerException(this.getClass().getCanonicalName() + ": given compilation unit cannot be null");
43 this.compilationUnit = compilationUnit;
44 nodeFinderCache = new NodeFinderCache();
47 public CompilationUnitTextSelection(ICompilationUnit compilationUnit, ITextSelection selection) {
48 this(compilationUnit, selection.getOffset(), selection.getLength());
52 public IDocument getDocument() {
53 return DocumentUtils.getDocumentFromCompilationUnit(compilationUnit);
57 return getOffset() + getLength();
60 public IProject getProject() {
61 return compilationUnit.getJavaProject().getProject();
64 public ICompilationUnit getCompilationUnit() {
65 return compilationUnit;
68 public ASTNode getCoveringNode() {
69 return getNodeFinder().getCoveringNode();
72 public ASTNode getCoveredNode() {
73 return getNodeFinder().getCoveredNode();
76 private NodeFinder getNodeFinder() {
77 return nodeFinderCache.getNodeFinder();
80 public boolean surroundsNode(ASTNode node) {
81 return getOffset() <= node.getStartPosition() && endOfNode(node) <= getEnd();
84 private int endOfNode(ASTNode node) {
85 return node.getStartPosition() + node.getLength();
88 public String getPackageName() {
89 return getConcreteCompilationUnit().getPackage().getName().getFullyQualifiedName();
92 public String getNameOfSurroundingType() {
93 for (Object type: getConcreteCompilationUnit().types()) {
94 assertThat(type, instanceOf(AbstractTypeDeclaration.class));
95 AbstractTypeDeclaration absTypeDecl = (AbstractTypeDeclaration) type;
96 if (isSurroundedBy(absTypeDecl))
97 return absTypeDecl.getName().getFullyQualifiedName();
102 private boolean isSurroundedBy(AbstractTypeDeclaration absTypeDecl) {
103 return absTypeDecl.getStartPosition() <= getOffset() && getLength() <= absTypeDecl.getLength();
106 private CompilationUnit getConcreteCompilationUnit() {
107 ASTNode root = getCoveringNode().getRoot();
108 assertThat(root, instanceOf(CompilationUnit.class));
109 return (CompilationUnit) root;
113 public boolean equals(Object obj) {
114 if (obj instanceof CompilationUnitTextSelection)
115 return super.equals(obj) && compilationUnit.equals(((CompilationUnitTextSelection)obj).compilationUnit);
120 public int hashCode() {
121 return (getOffset() << 24) | (getLength() << 16) | compilationUnit.hashCode();
125 public String toString() {
126 return "Offset: " + getOffset() + "; Length: " + getLength() + "; End: " + getEnd() + "; CU handle identifier: " + compilationUnit.getHandleIdentifier();
130 public int getStartLine() {
131 return createTextSelectionWithDocument().getStartLine();
135 public int getEndLine() {
136 return createTextSelectionWithDocument().getEndLine();
140 public String getText() {
141 return createTextSelectionWithDocument().getText();
144 private TextSelection createTextSelectionWithDocument() {
145 return new TextSelection(getDocument(), getOffset(), getLength());
148 public void dropCachedData() {
149 nodeFinderCache.dropCachedData();