Example usage for org.eclipse.jdt.internal.core PackageFragmentRoot getSourceMapper

List of usage examples for org.eclipse.jdt.internal.core PackageFragmentRoot getSourceMapper

Introduction

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

Prototype

@Override
public SourceMapper getSourceMapper() 

Source Link

Usage

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

License:Open Source License

/**
 * @see org.eclipse.jdt.core.IClassFile//from  w  ww .  j  a v a  2  s  . c om
 */
public IJavaElement getElementAt(int position) throws JavaModelException {
    IJavaElement parentElement = getParent();
    while (parentElement.getElementType() != IJavaElement.PACKAGE_FRAGMENT_ROOT) {
        parentElement = parentElement.getParent();
    }
    PackageFragmentRoot root = (PackageFragmentRoot) parentElement;
    SourceMapper mapper = root.getSourceMapper();
    if (mapper == null) {
        return null;
    } else {
        // ensure this class file's buffer is open so that source ranges are computed
        getBuffer();

        IType type = getType();
        return findElement(type, position, mapper);
    }
}

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 {/*from  w  ww. java2s.  co  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;
    }
}