Example usage for org.eclipse.jdt.internal.core SourceRefElement getSourceElementAt

List of usage examples for org.eclipse.jdt.internal.core SourceRefElement getSourceElementAt

Introduction

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

Prototype

protected IJavaElement getSourceElementAt(int position) throws JavaModelException 

Source Link

Document

Returns the element that is located at the given source position in this element.

Usage

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

License:Open Source License

/**
 * Returns the element that is located at the given source position
 * in this element.  This is a helper method for <code>ICompilationUnit#getElementAt</code>,
 * and only works on compilation units and types. The position given is
 * known to be within this element's source range already, and if no finer
 * grained element is found at the position, this element is returned.
 *///from w  w  w. ja  v  a2 s . co  m
protected IJavaElement getSourceElementAt(int position) throws JavaModelException {
    if (this instanceof ISourceReference) {
        IJavaElement[] children = getChildren();
        for (int i = children.length - 1; i >= 0; i--) {
            IJavaElement aChild = children[i];
            if (aChild instanceof SourceRefElement) {
                SourceRefElement child = (SourceRefElement) children[i];
                ISourceRange range = child.getSourceRange();
                int start = range.getOffset();
                int end = start + range.getLength();
                if (start <= position && position <= end) {
                    if (child instanceof IField) {
                        // check muti-declaration case (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=39943)
                        int declarationStart = start;
                        SourceRefElement candidate = null;
                        do {
                            // check name range
                            range = ((IField) child).getNameRange();
                            if (position <= range.getOffset() + range.getLength()) {
                                candidate = child;
                            } else {
                                return candidate == null ? child.getSourceElementAt(position)
                                        : candidate.getSourceElementAt(position);
                            }
                            child = --i >= 0 ? (SourceRefElement) children[i] : null;
                        } while (child != null && child.getSourceRange().getOffset() == declarationStart);
                        // position in field's type: use first field
                        return candidate.getSourceElementAt(position);
                    } else if (child instanceof IParent) {
                        return child.getSourceElementAt(position);
                    } else {
                        return child;
                    }
                }
            }
        }
    } else {
        // should not happen
        Assert.isTrue(false);
    }
    return this;
}