Search in sources :

Example 1 with ISourceReference

use of org.eclipse.jdt.core.ISourceReference in project che by eclipse.

the class JavaNavigation method compilationUnitNavigation.

private OpenDeclarationDescriptor compilationUnitNavigation(ICompilationUnit unit, IJavaElement element) throws JavaModelException {
    OpenDeclarationDescriptor dto = DtoFactory.getInstance().createDto(OpenDeclarationDescriptor.class);
    String absolutePath = unit.getPath().toOSString();
    dto.setPath(absolutePath);
    dto.setBinary(false);
    if (element instanceof ISourceReference) {
        ISourceRange nameRange = ((ISourceReference) element).getNameRange();
        dto.setOffset(nameRange.getOffset());
        dto.setLength(nameRange.getLength());
    }
    return dto;
}
Also used : OpenDeclarationDescriptor(org.eclipse.che.ide.ext.java.shared.OpenDeclarationDescriptor) ISourceReference(org.eclipse.jdt.core.ISourceReference) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 2 with ISourceReference

use of org.eclipse.jdt.core.ISourceReference in project che by eclipse.

the class JavaNavigation method classFileNavigation.

private OpenDeclarationDescriptor classFileNavigation(IClassFile classFile, IJavaElement element) throws JavaModelException {
    OpenDeclarationDescriptor dto = DtoFactory.getInstance().createDto(OpenDeclarationDescriptor.class);
    dto.setPath(classFile.getType().getFullyQualifiedName());
    dto.setLibId(classFile.getAncestor(IPackageFragmentRoot.PACKAGE_FRAGMENT_ROOT).hashCode());
    dto.setBinary(true);
    if (classFile.getSourceRange() != null) {
        if (element instanceof ISourceReference) {
            ISourceRange nameRange = ((ISourceReference) element).getNameRange();
            dto.setOffset(nameRange.getOffset());
            dto.setLength(nameRange.getLength());
        }
    }
    return dto;
}
Also used : OpenDeclarationDescriptor(org.eclipse.che.ide.ext.java.shared.OpenDeclarationDescriptor) ISourceReference(org.eclipse.jdt.core.ISourceReference) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 3 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)

Example 4 with ISourceReference

use of org.eclipse.jdt.core.ISourceReference in project che by eclipse.

the class RenameAnalyzeUtil method getOldSourceRange.

private static ISourceRange getOldSourceRange(SearchMatch newMatch) {
    // cannot transfom offset in preview to offset in original -> just show enclosing method
    IJavaElement newMatchElement = (IJavaElement) newMatch.getElement();
    IJavaElement primaryElement = newMatchElement.getPrimaryElement();
    ISourceRange range = null;
    if (primaryElement.exists() && primaryElement instanceof ISourceReference) {
        try {
            range = ((ISourceReference) primaryElement).getSourceRange();
        } catch (JavaModelException e) {
        // can live without source range
        }
    }
    return range;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaModelException(org.eclipse.jdt.core.JavaModelException) ISourceReference(org.eclipse.jdt.core.ISourceReference) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 5 with ISourceReference

use of org.eclipse.jdt.core.ISourceReference in project che by eclipse.

the class CompilationUnitChangeNode method getModifiedJavaElement.

private IJavaElement getModifiedJavaElement(TextEditBasedChangeGroup edit, ICompilationUnit cunit) throws JavaModelException {
    IRegion range = edit.getRegion();
    if (range.getOffset() == 0 && range.getLength() == 0)
        return cunit;
    IJavaElement result = cunit.getElementAt(range.getOffset());
    if (result == null)
        return cunit;
    try {
        while (true) {
            ISourceReference ref = (ISourceReference) result;
            IRegion sRange = new Region(ref.getSourceRange().getOffset(), ref.getSourceRange().getLength());
            if (result.getElementType() == IJavaElement.COMPILATION_UNIT || result.getParent() == null || coveredBy(edit, sRange))
                break;
            result = result.getParent();
        }
    } catch (JavaModelException e) {
    // Do nothing, use old value.
    } catch (ClassCastException e) {
    // Do nothing, use old value.
    }
    return result;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaModelException(org.eclipse.jdt.core.JavaModelException) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) ISourceReference(org.eclipse.jdt.core.ISourceReference) IRegion(org.eclipse.jface.text.IRegion)

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