Example usage for org.eclipse.jdt.core SourceRange getOffset

List of usage examples for org.eclipse.jdt.core SourceRange getOffset

Introduction

In this page you can find the example usage for org.eclipse.jdt.core SourceRange getOffset.

Prototype

@Override
public int getOffset() 

Source Link

Usage

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>.
 *///from  w  w w . ja va 2  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 {//from   www . j a  va2s.  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;
    }
}

From source file:org.codehaus.groovy.eclipse.codebrowsing.tests.BrowsingTestCase.java

License:Open Source License

protected void assertCodeSelect(String structureContents, String javaContents, String groovyContents,
        String toFind, SourceRange selectRegion, String elementName, boolean isGroovy)
        throws Exception, JavaModelException {

    if (structureContents != null) {
        if (javaContents != null) {
            if (isGroovy) {
                createUnit("Structure", structureContents);
            } else {
                createJavaUnit("Structure", structureContents);
            }//from   w ww . j ava 2  s.  c  o m
        } else {
            // this is an array test, use a different file name
            if (isGroovy) {
                createJavaUnit("XX", structureContents);
            } else {
                createJavaUnit("XX", structureContents);
            }
        }
    }
    GroovyCompilationUnit groovyUnit = createUnit(groovyContents);
    ICompilationUnit javaUnit = null;
    if (javaContents != null) {
        javaUnit = createJavaUnit("Java", javaContents);
    }
    incrementalBuild();
    expectingNoProblems();

    // check the groovy code select
    IJavaElement[] eltFromGroovy = groovyUnit.codeSelect(selectRegion.getOffset(), selectRegion.getLength());
    assertEquals("Should have found a selection", 1, eltFromGroovy.length);
    assertEquals("Should have found reference to: " + elementName, elementName,
            eltFromGroovy[0].getElementName());

    // check the java code select
    if (javaUnit != null) {
        IJavaElement[] eltFromJava = javaUnit.codeSelect(javaContents.lastIndexOf(toFind), toFind.length());
        assertEquals("Should have found a selection", 1, eltFromJava.length);
        assertEquals("Should have found reference to: " + elementName, elementName,
                eltFromJava[0].getElementName());

        // now check that the unique keys of each of them are the same
        String groovyUniqueKey = getUniqueKey(eltFromGroovy[0]);
        String javaUniqueKey = getUniqueKey(eltFromJava[0]);
        assertEquals("Invalid unique key from groovy", javaUniqueKey, groovyUniqueKey);
    }

}

From source file:org.codehaus.groovy.eclipse.search.GroovyOccurrencesFinder.java

License:Apache License

public OccurrenceLocation[] getOccurrences() {
    Map<org.codehaus.groovy.ast.ASTNode, Integer> occurences = internalFindOccurences();
    OccurrenceLocation[] locations = new OccurrenceLocation[occurences.size()];
    int i = 0;/*from w w  w  . j  a  va2  s.c om*/
    for (Entry<org.codehaus.groovy.ast.ASTNode, Integer> entry : occurences.entrySet()) {
        org.codehaus.groovy.ast.ASTNode node = entry.getKey();
        int flag = entry.getValue();
        OccurrenceLocation occurrenceLocation;
        if (node instanceof FieldNode) {
            FieldNode c = (FieldNode) node;
            occurrenceLocation = new OccurrenceLocation(c.getNameStart(), c.getNameEnd() - c.getNameStart() + 1,
                    flag, "Occurrence of ''" + getElementName() + "''");
        } else if (node instanceof MethodNode) {
            MethodNode c = (MethodNode) node;
            occurrenceLocation = new OccurrenceLocation(c.getNameStart(), c.getNameEnd() - c.getNameStart() + 1,
                    flag, "Occurrence of ''" + getElementName() + "''");
        } else if (node instanceof Parameter) {
            // should be finding the start and end of the name region only,
            // but this finds the entire declaration
            Parameter c = (Parameter) node;
            int start = c.getNameStart();
            int length = c.getNameEnd() - c.getNameStart();
            occurrenceLocation = new OccurrenceLocation(start, length, flag,
                    "Occurrence of ''" + getElementName() + "''");
        } else if (node instanceof ClassNode && ((ClassNode) node).getNameEnd() > 0) {
            // class declaration
            ClassNode c = (ClassNode) node;
            occurrenceLocation = new OccurrenceLocation(c.getNameStart(), c.getNameEnd() - c.getNameStart() + 1,
                    flag, "Occurrence of ''" + getElementName() + "''");
        } else if (node instanceof StaticMethodCallExpression) {
            // special case...for static method calls, the start and end are
            // of the entire expression, but we just want the name.
            StaticMethodCallExpression smce = (StaticMethodCallExpression) node;
            occurrenceLocation = new OccurrenceLocation(smce.getStart(),
                    Math.min(smce.getLength(), smce.getMethod().length()), flag,
                    "Occurrence of ''" + getElementName() + "''");
        } else {
            SourceRange range = getSourceRange(node);

            occurrenceLocation = new OccurrenceLocation(range.getOffset(), range.getLength(), flag,
                    "Occurrence of ''" + getElementName() + "''");
        }
        locations[i++] = occurrenceLocation;
    }
    return locations;
}

From source file:org.jboss.tools.ws.jaxrs.ui.quickfix.AddHttpMethodRetentionValueCompletionProposal.java

License:Open Source License

/**
 * Full constructor//from   w  ww.j ava 2s. com
 * @param compilationUnit
 * @param sourceRange
 */
public AddHttpMethodRetentionValueCompletionProposal(final ICompilationUnit compilationUnit,
        final SourceRange sourceRange) {
    super(compilationUnit,
            "@Retention(" + AddHttpMethodRetentionAnnotationMarkerResolution.ANNOTATION_VALUE + ")",
            NLS.bind(JaxrsQuickFixMessages.UPDATE_RETENTION_ANNOTATION_VALUE_MARKER_RESOLUTION_TITLE,
                    AddHttpMethodRetentionAnnotationMarkerResolution.ANNOTATION_VALUE),
            sourceRange.getOffset(), sourceRange.getLength(),
            JBossJaxrsUIPlugin.getDefault().getImage("annotation_obj.gif"), null);
    includeImportDeclarationAddition(JaxrsClassnames.RETENTION_POLICY);
}

From source file:org.jboss.tools.ws.jaxrs.ui.quickfix.AddHttpMethodTargetValuesCompletionProposal.java

License:Open Source License

/**
 * Full constructor//from  w  w  w .j  a  v a2  s  . c o m
 * @param compilationUnit
 * @param sourceRange
 */
public AddHttpMethodTargetValuesCompletionProposal(final ICompilationUnit compilationUnit,
        final SourceRange sourceRange) {
    super(compilationUnit, "@Target(" + ANNOTATION_VALUE + ")",
            NLS.bind(JaxrsQuickFixMessages.UPDATE_TARGET_ANNOTATION_VALUE_MARKER_RESOLUTION_TITLE,
                    ANNOTATION_VALUE),
            sourceRange.getOffset(), sourceRange.getLength(),
            JBossJaxrsUIPlugin.getDefault().getImage("annotation_obj.gif"), null);
    includeImportDeclarationAddition(JaxrsClassnames.ELEMENT_TYPE);
}

From source file:org.jboss.tools.ws.jaxrs.ui.quickfix.AddHttpMethodValueCompletionProposal.java

License:Open Source License

/**
 * Full constructor// w  ww  .jav a  2  s. co  m
 * @param compilationUnit
 * @param annotationValue
 * @param sourceRange
 */
public AddHttpMethodValueCompletionProposal(final ICompilationUnit compilationUnit,
        final String annotationValue, final SourceRange sourceRange) {
    super(compilationUnit, "@HttpMethod(" + annotationValue + ")",
            NLS.bind(JaxrsQuickFixMessages.UPDATE_HTTP_METHOD_ANNOTATION_VALUE_MARKER_RESOLUTION_TITLE,
                    annotationValue),
            sourceRange.getOffset(), sourceRange.getLength(),
            JBossJaxrsUIPlugin.getDefault().getImage("annotation_obj.gif"), null);
}

From source file:org.jboss.tools.ws.jaxrs.ui.quickfix.AddNameBindingRetentionValueCompletionProposal.java

License:Open Source License

/**
 * Full constructor/* www .  j a v  a 2 s. c  o m*/
 * @param compilationUnit
 * @param sourceRange
 */
public AddNameBindingRetentionValueCompletionProposal(final ICompilationUnit compilationUnit,
        final SourceRange sourceRange) {
    super(compilationUnit,
            "@Retention(" + AddHttpMethodRetentionAnnotationMarkerResolution.ANNOTATION_VALUE + ")",
            NLS.bind(JaxrsQuickFixMessages.UPDATE_RETENTION_ANNOTATION_VALUE_MARKER_RESOLUTION_TITLE,
                    AddHttpMethodRetentionAnnotationMarkerResolution.ANNOTATION_VALUE),
            sourceRange.getOffset(), sourceRange.getLength(),
            JBossJaxrsUIPlugin.getDefault().getImage("annotation_obj.gif"), null);
    includeImportDeclarationAddition(JaxrsClassnames.RETENTION_POLICY);
}

From source file:org.jboss.tools.ws.jaxrs.ui.quickfix.AddNameBindingTargetValuesCompletionProposal.java

License:Open Source License

/**
 * Full constructor//w w w. j  a v a  2  s. co  m
 * @param compilationUnit
 * @param sourceRange
 */
public AddNameBindingTargetValuesCompletionProposal(final ICompilationUnit compilationUnit,
        final SourceRange sourceRange) {
    super(compilationUnit, "@Target(" + ANNOTATION_VALUE + ")",
            NLS.bind(JaxrsQuickFixMessages.UPDATE_TARGET_ANNOTATION_VALUE_MARKER_RESOLUTION_TITLE,
                    ANNOTATION_VALUE),
            sourceRange.getOffset(), sourceRange.getLength(),
            JBossJaxrsUIPlugin.getDefault().getImage("annotation_obj.gif"), null);
    includeImportDeclarationAddition(JaxrsClassnames.ELEMENT_TYPE);
}