Search in sources :

Example 6 with ISourceReference

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;
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IClassFile(org.eclipse.jdt.core.IClassFile) ISourceReference(org.eclipse.jdt.core.ISourceReference) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Aggregations

ISourceReference (org.eclipse.jdt.core.ISourceReference)6 ISourceRange (org.eclipse.jdt.core.ISourceRange)5 IJavaElement (org.eclipse.jdt.core.IJavaElement)4 JavaModelException (org.eclipse.jdt.core.JavaModelException)3 OpenDeclarationDescriptor (org.eclipse.che.ide.ext.java.shared.OpenDeclarationDescriptor)2 IClassFile (org.eclipse.jdt.core.IClassFile)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 IResource (org.eclipse.core.resources.IResource)1 IRegion (org.eclipse.jface.text.IRegion)1 Region (org.eclipse.jface.text.Region)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1