Search in sources :

Example 41 with ISourceRange

use of org.eclipse.jdt.core.ISourceRange in project che by eclipse.

the class SourceTypeConverter method convertAnnotations.

private Annotation[] convertAnnotations(IAnnotatable element) throws JavaModelException {
    IAnnotation[] annotations = element.getAnnotations();
    int length = annotations.length;
    Annotation[] astAnnotations = new Annotation[length];
    if (length > 0) {
        char[] cuSource = getSource();
        int recordedAnnotations = 0;
        for (int i = 0; i < length; i++) {
            ISourceRange positions = annotations[i].getSourceRange();
            int start = positions.getOffset();
            int end = start + positions.getLength();
            char[] annotationSource = CharOperation.subarray(cuSource, start, end);
            if (annotationSource != null) {
                Expression expression = parseMemberValue(annotationSource);
                /*
	    			 * expression can be null or not an annotation if the source has changed between
	    			 * the moment where the annotation source positions have been retrieved and the moment were
	    			 * this parsing occurred.
	    			 * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=90916
	    			 */
                if (expression instanceof Annotation) {
                    astAnnotations[recordedAnnotations++] = (Annotation) expression;
                }
            }
        }
        if (length != recordedAnnotations) {
            // resize to remove null annotations
            System.arraycopy(astAnnotations, 0, (astAnnotations = new Annotation[recordedAnnotations]), 0, recordedAnnotations);
        }
    }
    return astAnnotations;
}
Also used : IAnnotation(org.eclipse.jdt.core.IAnnotation) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) QualifiedAllocationExpression(org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) IAnnotation(org.eclipse.jdt.core.IAnnotation) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 42 with ISourceRange

use of org.eclipse.jdt.core.ISourceRange in project tdi-studio-se by Talend.

the class OpenDeclarationAction method highlightMethod.

/**
     * Highlights the method on editor.
     * 
     * @param editorInput The editor input
     * @param editorPart The editor part
     * @throws CoreException
     */
private void highlightMethod(IEditorInput editorInput, IEditorPart editorPart) throws CoreException {
    if (!(editorPart instanceof ITextEditor) || methodName == null || parameters == null) {
        return;
    }
    ITextEditor textEditor = (ITextEditor) editorPart;
    IDocumentProvider provider = textEditor.getDocumentProvider();
    provider.connect(editorInput);
    int offset = 0;
    int length = 0;
    ISourceRange range = getMethodNameRange(editorInput);
    if (range != null) {
        offset = range.getOffset();
        length = range.getLength();
    }
    textEditor.selectAndReveal(offset, length);
    provider.disconnect(editorInput);
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 43 with ISourceRange

use of org.eclipse.jdt.core.ISourceRange in project webtools.sourceediting by eclipse.

the class JSPJavaHyperlinkDetector method createHyperlink.

private IHyperlink createHyperlink(IJavaElement element, IRegion region, IDocument document) {
    IHyperlink link = null;
    if (region != null) {
        // open local variable in the JSP file...
        boolean isInTranslationCU = false;
        if (element instanceof ISourceReference) {
            IFile file = null;
            int jspOffset = 0;
            // try to locate the file in the workspace
            ITextFileBuffer textFileBuffer = FileBuffers.getTextFileBufferManager().getTextFileBuffer(document);
            if (textFileBuffer != null && textFileBuffer.getLocation() != null) {
                file = getFile(textFileBuffer.getLocation().toString());
            }
            // get Java range and translate to JSP range
            try {
                ISourceRange range = null;
                IJSPTranslation jspTranslation = getJSPTranslation(document);
                if (jspTranslation != null) {
                    // link to local variable definitions
                    if (element instanceof ILocalVariable) {
                        range = ((ILocalVariable) element).getNameRange();
                        Object cu = ((ILocalVariable) element).getAncestor(IJavaElement.COMPILATION_UNIT);
                        if (cu != null && cu.equals(jspTranslation.getCompilationUnit()))
                            isInTranslationCU = true;
                    } else // linking to fields of the same compilation unit
                    if (element.getElementType() == IJavaElement.FIELD) {
                        Object cu = ((IField) element).getCompilationUnit();
                        if (cu != null && cu.equals(jspTranslation.getCompilationUnit())) {
                            range = ((ISourceReference) element).getSourceRange();
                            isInTranslationCU = true;
                        }
                    } else // linking to methods of the same compilation unit
                    if (element.getElementType() == IJavaElement.METHOD) {
                        Object cu = ((IMethod) element).getCompilationUnit();
                        if (cu != null && cu.equals(jspTranslation.getCompilationUnit())) {
                            range = ((ISourceReference) element).getSourceRange();
                            isInTranslationCU = true;
                        }
                    }
                }
                if (jspTranslation != null && range != null && file != null) {
                    jspOffset = jspTranslation.getJspOffset(range.getOffset());
                    if (jspOffset >= 0) {
                        link = new WorkspaceFileHyperlink(region, file, new Region(jspOffset, range.getLength()));
                    }
                }
            } catch (JavaModelException jme) {
                Logger.log(Logger.WARNING_DEBUG, jme.getMessage(), jme);
            }
        }
        if (link == null && !isInTranslationCU) {
            // Don't try to open the translation CU
            link = new JSPJavaHyperlink(region, element);
        }
    }
    return link;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) ILocalVariable(org.eclipse.jdt.core.ILocalVariable) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) IMethod(org.eclipse.jdt.core.IMethod) ISourceReference(org.eclipse.jdt.core.ISourceReference) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 44 with ISourceRange

use of org.eclipse.jdt.core.ISourceRange in project eclipse.jdt.ls by eclipse.

the class JavadocContentAccess method internalGetContentReader.

/**
 * 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
 */
private static Reader internalGetContentReader(IMember member) throws JavaModelException {
    IBuffer buf = member.getOpenable().getBuffer();
    if (buf == null) {
        // no source attachment found
        return null;
    }
    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;
}
Also used : IBuffer(org.eclipse.jdt.core.IBuffer) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 45 with ISourceRange

use of org.eclipse.jdt.core.ISourceRange in project eclipse.jdt.ls by eclipse.

the class CorrectPackageDeclarationProposal method addEdits.

@Override
protected void addEdits(IDocument doc, TextEdit root) throws CoreException {
    super.addEdits(doc, root);
    ICompilationUnit cu = getCompilationUnit();
    IPackageFragment parentPack = (IPackageFragment) cu.getParent();
    IPackageDeclaration[] decls = cu.getPackageDeclarations();
    if (parentPack.isDefaultPackage() && decls.length > 0) {
        for (int i = 0; i < decls.length; i++) {
            ISourceRange range = decls[i].getSourceRange();
            root.addChild(new DeleteEdit(range.getOffset(), range.getLength()));
        }
        return;
    }
    if (!parentPack.isDefaultPackage() && decls.length == 0) {
        String lineDelim = "\n";
        // $NON-NLS-1$
        String str = "package " + parentPack.getElementName() + ';' + lineDelim + lineDelim;
        root.addChild(new InsertEdit(0, str));
        return;
    }
    root.addChild(new ReplaceEdit(fLocation.getOffset(), fLocation.getLength(), parentPack.getElementName()));
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) InsertEdit(org.eclipse.text.edits.InsertEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) IPackageDeclaration(org.eclipse.jdt.core.IPackageDeclaration) DeleteEdit(org.eclipse.text.edits.DeleteEdit) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Aggregations

ISourceRange (org.eclipse.jdt.core.ISourceRange)53 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)18 IJavaElement (org.eclipse.jdt.core.IJavaElement)14 JavaModelException (org.eclipse.jdt.core.JavaModelException)13 ASTNode (org.eclipse.jdt.core.dom.ASTNode)12 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)10 ISourceReference (org.eclipse.jdt.core.ISourceReference)9 IType (org.eclipse.jdt.core.IType)8 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)7 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)6 SourceRange (org.eclipse.jdt.core.SourceRange)6 IBuffer (org.eclipse.jdt.core.IBuffer)5 IMethod (org.eclipse.jdt.core.IMethod)5 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)5 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)5 ArrayList (java.util.ArrayList)4 IField (org.eclipse.jdt.core.IField)4 IMember (org.eclipse.jdt.core.IMember)4 CoreException (org.eclipse.core.runtime.CoreException)3 IClassFile (org.eclipse.jdt.core.IClassFile)3