Search in sources :

Example 1 with ReferenceData

use of com.intellij.codeInsight.editorActions.ReferenceData in project intellij-community by JetBrains.

the class GroovyReferenceCopyPasteProcessor method findReferencesToRestore.

@NotNull
@Override
protected GrReferenceElement[] findReferencesToRestore(PsiFile file, RangeMarker bounds, ReferenceData[] referenceData) {
    PsiManager manager = file.getManager();
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(manager.getProject());
    PsiResolveHelper helper = facade.getResolveHelper();
    GrReferenceElement[] refs = new GrReferenceElement[referenceData.length];
    for (int i = 0; i < referenceData.length; i++) {
        ReferenceData data = referenceData[i];
        PsiClass refClass = facade.findClass(data.qClassName, file.getResolveScope());
        if (refClass == null)
            continue;
        int startOffset = data.startOffset + bounds.getStartOffset();
        int endOffset = data.endOffset + bounds.getStartOffset();
        PsiElement element = file.findElementAt(startOffset);
        if (element != null && element.getParent() instanceof GrReferenceElement && !PsiUtil.isThisOrSuperRef(element.getParent())) {
            GrReferenceElement reference = (GrReferenceElement) element.getParent();
            TextRange range = reference.getTextRange();
            if (range.getStartOffset() == startOffset && range.getEndOffset() == endOffset) {
                if (data.staticMemberName == null) {
                    PsiClass refClass1 = helper.resolveReferencedClass(reference.getText(), reference);
                    if (refClass1 == null || !manager.areElementsEquivalent(refClass, refClass1)) {
                        refs[i] = reference;
                    }
                } else {
                    if (reference instanceof GrReferenceExpression) {
                        PsiElement referent = resolveReferenceIgnoreOverriding(reference);
                        if (!(referent instanceof PsiNamedElement) || !data.staticMemberName.equals(((PsiNamedElement) referent).getName()) || !(referent instanceof PsiMember) || ((PsiMember) referent).getContainingClass() == null || !data.qClassName.equals(((PsiMember) referent).getContainingClass().getQualifiedName())) {
                            refs[i] = reference;
                        }
                    }
                }
            }
        }
    }
    return refs;
}
Also used : TextRange(com.intellij.openapi.util.TextRange) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) ReferenceData(com.intellij.codeInsight.editorActions.ReferenceData) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with ReferenceData

use of com.intellij.codeInsight.editorActions.ReferenceData in project intellij-community by JetBrains.

the class GroovyReferenceCopyPasteProcessor method restoreReferences.

@Override
protected void restoreReferences(ReferenceData[] referenceData, GrReferenceElement[] refs) {
    for (int i = 0; i < refs.length; i++) {
        GrReferenceElement reference = refs[i];
        if (reference == null)
            continue;
        try {
            PsiManager manager = reference.getManager();
            ReferenceData refData = referenceData[i];
            PsiClass refClass = JavaPsiFacade.getInstance(manager.getProject()).findClass(refData.qClassName, reference.getResolveScope());
            if (refClass != null) {
                if (refData.staticMemberName == null) {
                    reference.bindToElement(refClass);
                } else {
                    LOG.assertTrue(reference instanceof GrReferenceExpression);
                    PsiMember member = findMember(refData, refClass);
                    if (member != null) {
                        ((GrReferenceExpression) reference).bindToElementViaStaticImport(member);
                    }
                }
            }
        } catch (IncorrectOperationException e) {
            LOG.error(e);
        }
    }
}
Also used : ReferenceData(com.intellij.codeInsight.editorActions.ReferenceData) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Aggregations

ReferenceData (com.intellij.codeInsight.editorActions.ReferenceData)2 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)2 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)2 TextRange (com.intellij.openapi.util.TextRange)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 NotNull (org.jetbrains.annotations.NotNull)1