use of org.eclipse.jdt.core.ISourceReference in project che by eclipse.
the class SelectionConverter method resolveEnclosingElement.
// public static IJavaElement getElementAtOffset(ITypeRoot input, ITextSelection selection) throws JavaModelException {
// if (input instanceof ICompilationUnit) {
// JavaModelUtil.reconcile((ICompilationUnit)input);
// }
// IJavaElement ref = input.getElementAt(selection.getOffset());
// if (ref == null)
// return input;
// return ref;
// }
//
// public static IJavaElement resolveEnclosingElement(JavaEditor editor, ITextSelection selection) throws JavaModelException {
// ITypeRoot input = getInput(editor);
// if (input != null)
// return resolveEnclosingElement(input, selection);
// return null;
// }
//
public static IJavaElement resolveEnclosingElement(IJavaElement input, ITextSelection selection) throws JavaModelException {
IJavaElement atOffset = null;
if (input instanceof ICompilationUnit) {
ICompilationUnit cunit = (ICompilationUnit) input;
JavaModelUtil.reconcile(cunit);
atOffset = cunit.getElementAt(selection.getOffset());
} else if (input instanceof IClassFile) {
IClassFile cfile = (IClassFile) input;
atOffset = cfile.getElementAt(selection.getOffset());
} else {
return null;
}
if (atOffset == null) {
return input;
} else {
int selectionEnd = selection.getOffset() + selection.getLength();
IJavaElement result = atOffset;
if (atOffset instanceof ISourceReference) {
ISourceRange range = ((ISourceReference) atOffset).getSourceRange();
while (range.getOffset() + range.getLength() < selectionEnd) {
result = result.getParent();
if (!(result instanceof ISourceReference)) {
result = input;
break;
}
range = ((ISourceReference) result).getSourceRange();
}
}
return result;
}
}
Aggregations