Example usage for org.eclipse.jdt.internal.core SourceMapper getSourceRange

List of usage examples for org.eclipse.jdt.internal.core SourceMapper getSourceRange

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core SourceMapper getSourceRange.

Prototype

public SourceRange getSourceRange(IJavaElement element) 

Source Link

Document

Returns the SourceRange for the given element, or {-1, -1} if no source range is known for the element.

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.BinaryMember.java

License:Open Source License

public ISourceRange getSourceRange() throws JavaModelException {
    SourceMapper mapper = getSourceMapper();
    if (mapper != null) {
        // ensure the class file's buffer is open so that source ranges are computed
        ((ClassFile) getClassFile()).getBuffer();

        return mapper.getSourceRange(this);
    } else {//from   w  w w  . ja va2s  .  c o  m
        return SourceMapper.UNKNOWN_RANGE;
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.ClassFile.java

License:Open Source License

/**
 * Finds the deepest <code>IJavaElement</code> in the hierarchy of
 * <code>elt</elt>'s children (including <code>elt</code> itself)
 * which has a source range that encloses <code>position</code>
 * according to <code>mapper</code>.
 *//*w  w w  . j  a va2  s . c  o  m*/
protected IJavaElement findElement(IJavaElement elt, int position, SourceMapper mapper) {
    SourceRange range = mapper.getSourceRange(elt);
    if (range == null || position < range.getOffset() || range.getOffset() + range.getLength() - 1 < position) {
        return null;
    }
    if (elt instanceof IParent) {
        try {
            IJavaElement[] children = ((IParent) elt).getChildren();
            for (int i = 0; i < children.length; i++) {
                IJavaElement match = findElement(children[i], position, mapper);
                if (match != null) {
                    return match;
                }
            }
        } catch (JavaModelException npe) {
            // elt doesn't exist: return the element
        }
    }
    return elt;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.ClassFile.java

License:Open Source License

public IJavaElement getElementAtConsideringSibling(int position) throws JavaModelException {
    IPackageFragment fragment = (IPackageFragment) getParent();
    PackageFragmentRoot root = (PackageFragmentRoot) fragment.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
    SourceMapper mapper = root.getSourceMapper();
    if (mapper == null) {
        return null;
    } else {/*w ww .  j a va 2  s.c  o  m*/
        int index = this.name.indexOf('$');
        int prefixLength = index < 0 ? this.name.length() : index;

        IType type = null;
        int start = -1;
        int end = Integer.MAX_VALUE;
        IJavaElement[] children = fragment.getChildren();
        for (int i = 0; i < children.length; i++) {
            String childName = children[i].getElementName();

            int childIndex = childName.indexOf('$');
            int childPrefixLength = childIndex < 0 ? childName.indexOf('.') : childIndex;
            if (prefixLength == childPrefixLength && this.name.regionMatches(0, childName, 0, prefixLength)) {
                IClassFile classFile = (IClassFile) children[i];

                // ensure this class file's buffer is open so that source ranges are computed
                classFile.getBuffer();

                SourceRange range = mapper.getSourceRange(classFile.getType());
                if (range == SourceMapper.UNKNOWN_RANGE)
                    continue;
                int newStart = range.getOffset();
                int newEnd = newStart + range.getLength() - 1;
                if (newStart > start && newEnd < end && newStart <= position && newEnd >= position) {
                    type = classFile.getType();
                    start = newStart;
                    end = newEnd;
                }
            }
        }
        if (type != null) {
            return findElement(type, position, mapper);
        }
        return null;
    }
}