Example usage for org.eclipse.jdt.core ISourceRange getLength

List of usage examples for org.eclipse.jdt.core ISourceRange getLength

Introduction

In this page you can find the example usage for org.eclipse.jdt.core ISourceRange getLength.

Prototype

int getLength();

Source Link

Document

Returns the number of characters of the source code for this element, relative to the source buffer in which this element is contained.

Usage

From source file:at.bestsolution.fxide.jdt.editor.JDTJavaDocSupport.java

License:Open Source License

private static HtmlString getMemberJavaDoc(IMember member) throws JavaModelException {
    IBuffer buf = member.getOpenable().getBuffer();
    if (buf != null) {
        ISourceRange javadocRange = member.getJavadocRange();
        if (javadocRange != null) {
            String rawJavadoc = buf.getText(javadocRange.getOffset(), javadocRange.getLength());
            return JDTJavaDocSupport.toHtml(member, rawJavadoc);
        }/*from   ww w .  j  a v  a  2  s  . c  om*/
    }
    return null;
}

From source file:at.bestsolution.fxide.jdt.text.javadoc.JavadocContentAccess.java

License:Open Source License

/**
 * Gets a reader for an IMember's Javadoc comment content from the source attachment.
 * The content does contain only the text from the comment without the Javadoc leading star characters.
 * Returns <code>null</code> if the member does not contain a Javadoc comment or if no source is available.
 * @param member The member to get the Javadoc of.
 * @return Returns a reader for the Javadoc comment content or <code>null</code> if the member
 * does not contain a Javadoc comment or if no source is available
 * @throws JavaModelException is thrown when the elements javadoc can not be accessed
 * @since 3.4/*from  ww  w  .j  a va  2 s . co m*/
 */
private static Reader internalGetContentReader(IMember member) throws JavaModelException {
    IBuffer buf = member.getOpenable().getBuffer();
    if (buf == null) {
        return null; // no source attachment found
    }

    ISourceRange javadocRange = member.getJavadocRange();
    if (javadocRange != null) {
        JavaDocCommentReader reader = new JavaDocCommentReader(buf, javadocRange.getOffset(),
                javadocRange.getOffset() + javadocRange.getLength() - 1);
        if (!containsOnlyInheritDoc(reader, javadocRange.getLength())) {
            reader.reset();
            return reader;
        }
    }

    return null;
}

From source file:at.bestsolution.fxide.jdt.text.javadoc.JavadocContentAccess2.java

License:Open Source License

private static String getHTMLContentFromSource(IJavaElement element) throws JavaModelException {
    IMember member;/*from   w w w . ja  va2  s . c  om*/
    if (element instanceof ILocalVariable) {
        member = ((ILocalVariable) element).getDeclaringMember();
    } else if (element instanceof ITypeParameter) {
        member = ((ITypeParameter) element).getDeclaringMember();
    } else if (element instanceof IMember) {
        member = (IMember) element;
    } else {
        return null;
    }

    IBuffer buf = member.getOpenable().getBuffer();
    if (buf == null) {
        return null; // no source attachment found
    }

    ISourceRange javadocRange = member.getJavadocRange();
    if (javadocRange == null) {
        if (canInheritJavadoc(member)) {
            // Try to use the inheritDoc algorithm.
            String inheritedJavadoc = javadoc2HTML(member, element, "/***/"); //$NON-NLS-1$
            if (inheritedJavadoc != null && inheritedJavadoc.length() > 0) {
                return inheritedJavadoc;
            }
        }
        return getJavaFxPropertyDoc(member);
    }

    String rawJavadoc = buf.getText(javadocRange.getOffset(), javadocRange.getLength());
    return javadoc2HTML(member, element, rawJavadoc);
}

From source file:ca.uvic.chisel.diver.sequencediagrams.sc.java.model.ASTUTils.java

License:Open Source License

public static MethodDeclaration findMethodDeclaration(ASTNode rootNode, IMethod method) {
    if (rootNode != null) {
        ISourceRange range;
        try {//from   w ww.j a  v a 2  s  . co  m
            range = method.getSourceRange();
            NodeFinder finder = new NodeFinder(range.getOffset(), range.getLength());
            rootNode.accept(finder);
            return (MethodDeclaration) finder.getCoveredNode();
        } catch (JavaModelException e) {
        }
    }
    return null;
}

From source file:ca.uvic.chisel.diver.sequencediagrams.sc.java.model.NodeFinder.java

License:Open Source License

public static ASTNode perform(ASTNode root, ISourceRange range) {
    return perform(root, range.getOffset(), range.getLength());
}

From source file:ca.uvic.chisel.javasketch.internal.ast.ASTUTils.java

License:Open Source License

public static MethodDeclaration findMethodDeclaration(ASTNode rootNode, IMethod method) {
    if (rootNode != null) {
        ISourceRange range;
        try {/*from   ww  w  .ja  v a2s. c om*/
            if (!method.getOpenable().isOpen()) {
                method.getOpenable().open(new NullProgressMonitor());
            }
            range = method.getSourceRange();
            NodeFinder finder = new NodeFinder(rootNode, range.getOffset(), range.getLength());
            ASTNode node = finder.getCoveredNode();
            if (node instanceof MethodDeclaration) {
                return (MethodDeclaration) node;
            } else {
                return null;
            }
        } catch (JavaModelException e) {
            SketchPlugin.getDefault().log(e);
        }
    }
    return null;
}

From source file:ca.uvic.chisel.javasketch.internal.ast.NodeFinder.java

License:Open Source License

/**
 * Maps a selection to a given ASTNode, where the selection is defined using a source range.
 * It calls <code>perform(root, range.getOffset(), range.getLength())</code>.
 * /*from w w w . j  a  va 2s  .  co  m*/
 * @return the result node
 * @see #perform(ASTNode, int, int)
 */
public static ASTNode perform(ASTNode root, ISourceRange range) {
    return perform(root, range.getOffset(), range.getLength());
}

From source file:ca.uvic.cs.tagsea.extraction.TagExtractor.java

License:Open Source License

/**
 * Gets the tag regions associated with this document, restricted to the given source referece, if no tag regions 
 * are found an empty array is returned//from  w  w  w  .j  av  a 2  s . co m
 * @param document
 * @param source reference
 * @return Array of tag regions
 */
public static IRegion[] getTagRegions(IDocument document, ISourceReference reference) {
    ISourceRange range;

    try {
        range = reference.getSourceRange();
    } catch (JavaModelException e) {
        TagSEAPlugin.log("", e);
        return new IRegion[0];
    }

    if (range.getOffset() < 0 || range.getOffset() + range.getLength() > document.getLength())
        return new IRegion[0];

    return getTagRegions(document, range.getOffset(), range.getLength());
}

From source file:ch.mlutz.plugins.t4e.tapestry.editor.OgnlHyperlinkDetector.java

License:Open Source License

@Override
public IHyperlink[] detectHyperlinks(ITextViewer viewer, IRegion region, boolean canHandleMultipleLinks) {

    IDocument document = viewer.getDocument();
    String contentBefore;//from w w  w  .  ja va2s . c  o m
    ITypedRegion partition;
    try {
        partition = document.getPartition(region.getOffset());

        if (!partition.getType().equals(TapestryPartitionScanner.XML_TAG)) {
            return null;
        }

        contentBefore = document.get(partition.getOffset(), partition.getLength());
    } catch (BadLocationException e) {
        log.warn("Could not detect hyperlinks: ", e);
        return null;
    }

    List<IHyperlink> result = new ArrayList<IHyperlink>();

    // go back until whitespace is found and
    int charIndex = region.getOffset() - partition.getOffset();
    while (charIndex >= 0 && contentBefore.charAt(charIndex) != '"') {
        charIndex--;
    }

    // charIndex might be -1, but that's ok
    String prefix = contentBefore.substring(charIndex + 1);

    final String expectedPrefix = "ognl:";
    if (!prefix.startsWith(expectedPrefix)) {
        return null;
    }

    int ognlExpressionStart = charIndex + 1 + expectedPrefix.length();

    charIndex = region.getOffset() - partition.getOffset();
    while (charIndex < contentBefore.length() && contentBefore.charAt(charIndex) != '"'
            && contentBefore.charAt(charIndex) != '.') {
        charIndex++;
    }

    final int hyperLinkOffset = partition.getOffset() + ognlExpressionStart;

    final int hyperLinkLength = partition.getOffset() + charIndex - hyperLinkOffset;

    if (hyperLinkLength < 0) {
        // caret might rest on the first quote
        log.warn(
                "OGNL hyperLinkLength: " + hyperLinkLength + "; partition.getOffset(): " + partition.getOffset()
                        + "; charIndex: " + charIndex + "; hyperLinkOffset: " + hyperLinkOffset);
        return null;
    }

    final String hyperlinkText = contentBefore.substring(ognlExpressionStart,
            ognlExpressionStart + hyperLinkLength);

    TapestryIndex tapestryIndex = Activator.getDefault().getTapestryIndex();
    IFile documentFile = tapestryIndex.getDocumentToFileMapping(document);

    if (documentFile == null) {
        return null;
    }

    ICompilationUnit javaCompilationUnit = tapestryIndex.getRelatedCompilationUnit(documentFile);

    if (javaCompilationUnit == null) {
        return null;
    }

    /*
    TapestryModule module= tapestryIndex.getModuleForResource(documentFile);
            
    TapestryHtmlElement linkedComponent= null;
    for (TapestryHtmlElement component: module.getComponents()) {
       if (hyperlinkText.equals(component.getPath())) {
    linkedComponent= component;
       }
    }
            
    final TapestryHtmlElement finalLinkedComponent= linkedComponent;
    */

    IType[] types = new IType[0];
    try {
        types = javaCompilationUnit.getTypes();
    } catch (JavaModelException e) {
        log.warn("Could not get types of compilation unit " + javaCompilationUnit.getElementName(), e);
    }

    IMethod methodMatch = null;
    outer: for (IType type : types) {
        try {
            if (type.isClass() && Flags.isPublic(type.getFlags())) {
                IMethod[] methods = type.getMethods();
                for (IMethod method : methods) {
                    String ognlMethodName = method.getElementName() + "()";
                    if (ognlMethodName.equals(hyperlinkText)) {
                        methodMatch = method;
                        break outer;
                    }

                    ognlMethodName = ognlMethodName.replaceAll("^(?:get|is)(.+)\\(\\)$", "$1");
                    // TODO: check this match!
                    if (ognlMethodName.equalsIgnoreCase(hyperlinkText)) {
                        methodMatch = method;
                        break outer;
                    }
                }
            }
        } catch (JavaModelException e) {
            log.warn("Could not get information for type " + type.getElementName(), e);
        }
    }

    IFile javaFile = null;
    try {
        javaFile = (IFile) javaCompilationUnit.getCorrespondingResource().getAdapter(IFile.class);
    } catch (JavaModelException e) {
        log.warn("Could not get corresponding resource for " + javaCompilationUnit.getElementName(), e);
    }
    final IFile finalJavaFile = javaFile;

    ISourceRange sourceRange = null;
    if (methodMatch != null) {
        try {
            sourceRange = methodMatch.getSourceRange();
        } catch (JavaModelException e) {
            log.warn("Could not get source range for method " + methodMatch.getElementName(), e);
        }
    }

    final ISourceRange finalSourceRange = sourceRange;

    if (finalJavaFile != null) {
        result.add(new IHyperlink() {

            @Override
            public void open() {
                IWorkbench wb = PlatformUI.getWorkbench();
                IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
                IWorkbenchPage page = win.getActivePage();

                IEditorPart editorPart = EclipseTools.openFileInEditor(finalJavaFile, page);

                if (!(editorPart instanceof ITextEditor)) {
                    log.info("editorPart !instanceof ITextEditor");
                }

                if (finalSourceRange == null) {
                    log.info("finalSourceRange == null");
                }

                if (editorPart instanceof ITextEditor && finalSourceRange != null) {

                    log.info("Opening source range: " + finalSourceRange);

                    ((ITextEditor) editorPart).selectAndReveal(finalSourceRange.getOffset(),
                            finalSourceRange.getLength());
                }
            }

            @Override
            public String getTypeLabel() {
                return null;
            }

            @Override
            public String getHyperlinkText() {
                return "Open method";
            }

            @Override
            public IRegion getHyperlinkRegion() {
                return new Region(hyperLinkOffset, hyperLinkLength);
            }
        });
    }
    /*
    log.info("detectHyperlinks called: " + hyperLinkOffset + " / "
    + hyperLinkLength + ", size: " + result.size());
    */

    if (result.size() > 0) {
        return result.toArray(new IHyperlink[result.size()]);
    } else {
        return null;
    }
}

From source file:com.android.ide.eclipse.adt.internal.editors.CompletionProposal.java

License:Open Source License

@Override
public String getAdditionalProposalInfo() {
    if (mAdditionalProposalInfo == null) {
        if (mChoice instanceof ElementDescriptor) {
            String tooltip = ((ElementDescriptor) mChoice).getTooltip();
            mAdditionalProposalInfo = DescriptorsUtils.formatTooltip(tooltip);
        } else if (mChoice instanceof TextAttributeDescriptor) {
            mAdditionalProposalInfo = ((TextAttributeDescriptor) mChoice).getTooltip();
        } else if (mChoice instanceof String) {
            // Try to produce it lazily for strings like @android
            String value = (String) mChoice;
            Matcher matcher = ATTRIBUTE_PATTERN.matcher(value);
            if (matcher.matches()) {
                String attrName = matcher.group(1);
                AndroidTargetData data = mAssist.getEditor().getTargetData();
                if (data != null) {
                    IDescriptorProvider descriptorProvider = data
                            .getDescriptorProvider(mAssist.getRootDescriptorId());
                    if (descriptorProvider != null) {
                        ElementDescriptor[] rootElementDescriptors = descriptorProvider
                                .getRootElementDescriptors();
                        for (ElementDescriptor elementDesc : rootElementDescriptors) {
                            for (AttributeDescriptor desc : elementDesc.getAttributes()) {
                                String name = desc.getXmlLocalName();
                                if (attrName.equals(name)) {
                                    IAttributeInfo attributeInfo = desc.getAttributeInfo();
                                    if (attributeInfo != null) {
                                        return attributeInfo.getJavaDoc();
                                    }/*from w w  w .  j av  a  2  s  .c  om*/
                                }
                            }
                        }
                    }
                }

            }
        } else if (mChoice instanceof IType) {
            IType type = (IType) mChoice;
            try {
                ISourceRange javadocRange = type.getJavadocRange();
                if (javadocRange != null && javadocRange.getLength() > 0) {
                    ISourceRange sourceRange = type.getSourceRange();
                    if (sourceRange != null) {
                        String source = type.getSource();
                        int start = javadocRange.getOffset() - sourceRange.getOffset();
                        int length = javadocRange.getLength();
                        String doc = source.substring(start, start + length);
                        return doc;
                    }
                }
                return type.getAttachedJavadoc(new NullProgressMonitor());
            } catch (JavaModelException e) {
                AdtPlugin.log(e, null);
            }
        }
    }

    return mAdditionalProposalInfo;
}