Search in sources :

Example 1 with GrAccessorMethod

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod in project intellij-community by JetBrains.

the class GroovyLineMarkerProvider method getLineMarkerInfo.

@Override
public LineMarkerInfo getLineMarkerInfo(@NotNull final PsiElement element) {
    final PsiElement parent = element.getParent();
    if (parent instanceof PsiNameIdentifierOwner) {
        if (parent instanceof GrField && element == ((GrField) parent).getNameIdentifierGroovy()) {
            for (GrAccessorMethod method : GroovyPropertyUtils.getFieldAccessors((GrField) parent)) {
                MethodSignatureBackedByPsiMethod superSignature = SuperMethodsSearch.search(method, null, true, false).findFirst();
                if (superSignature != null) {
                    PsiMethod superMethod = superSignature.getMethod();
                    boolean overrides = method.hasModifierProperty(PsiModifier.ABSTRACT) == superMethod.hasModifierProperty(PsiModifier.ABSTRACT) || superMethod.getBody() != null && GrTraitUtil.isTrait(superMethod.getContainingClass());
                    final Icon icon = overrides ? AllIcons.Gutter.OverridingMethod : AllIcons.Gutter.ImplementingMethod;
                    final MarkerType type = GroovyMarkerTypes.OVERRIDING_PROPERTY_TYPE;
                    return new LineMarkerInfo<>(element, element.getTextRange(), icon, Pass.LINE_MARKERS, type.getTooltip(), type.getNavigationHandler(), GutterIconRenderer.Alignment.LEFT);
                }
            }
        } else if (parent instanceof GrMethod && element == ((GrMethod) parent).getNameIdentifierGroovy() && hasSuperMethods((GrMethod) element.getParent())) {
            final Icon icon = AllIcons.Gutter.OverridingMethod;
            final MarkerType type = GroovyMarkerTypes.GR_OVERRIDING_METHOD;
            return new LineMarkerInfo<>(element, element.getTextRange(), icon, Pass.LINE_MARKERS, type.getTooltip(), type.getNavigationHandler(), GutterIconRenderer.Alignment.LEFT);
        }
    }
    //need to draw method separator above docComment
    if (myDaemonSettings.SHOW_METHOD_SEPARATORS && element.getFirstChild() == null) {
        PsiElement element1 = element;
        boolean isMember = false;
        while (element1 != null && !(element1 instanceof PsiFile) && element1.getPrevSibling() == null) {
            element1 = element1.getParent();
            if (element1 instanceof PsiMember || element1 instanceof GrVariableDeclarationImpl) {
                isMember = true;
                break;
            }
        }
        if (isMember && !(element1 instanceof PsiAnonymousClass || element1.getParent() instanceof PsiAnonymousClass)) {
            PsiFile file = element1.getContainingFile();
            Document document = file == null ? null : PsiDocumentManager.getInstance(file.getProject()).getLastCommittedDocument(file);
            boolean drawSeparator = false;
            if (document != null) {
                CharSequence documentChars = document.getCharsSequence();
                int category = getGroovyCategory(element1, documentChars);
                for (PsiElement child = element1.getPrevSibling(); child != null; child = child.getPrevSibling()) {
                    int category1 = getGroovyCategory(child, documentChars);
                    if (category1 == 0)
                        continue;
                    drawSeparator = category != 1 || category1 != 1;
                    break;
                }
            }
            if (drawSeparator) {
                GrDocComment comment = null;
                if (element1 instanceof GrDocCommentOwner) {
                    comment = ((GrDocCommentOwner) element1).getDocComment();
                }
                LineMarkerInfo info = new LineMarkerInfo<>(element, comment != null ? comment.getTextRange() : element.getTextRange(), null, Pass.LINE_MARKERS, FunctionUtil.<Object, String>nullConstant(), null, GutterIconRenderer.Alignment.RIGHT);
                EditorColorsScheme scheme = myColorsManager.getGlobalScheme();
                info.separatorColor = scheme.getColor(CodeInsightColors.METHOD_SEPARATORS_COLOR);
                info.separatorPlacement = SeparatorPlacement.TOP;
                return info;
            }
        }
    }
    return null;
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) MethodSignatureBackedByPsiMethod(com.intellij.psi.util.MethodSignatureBackedByPsiMethod) MethodSignatureBackedByPsiMethod(com.intellij.psi.util.MethodSignatureBackedByPsiMethod) GrDocCommentOwner(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocCommentOwner) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) Document(com.intellij.openapi.editor.Document) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) LineMarkerInfo(com.intellij.codeInsight.daemon.LineMarkerInfo) MarkerType(com.intellij.codeInsight.daemon.impl.MarkerType) GrVariableDeclarationImpl(org.jetbrains.plugins.groovy.lang.psi.impl.statements.GrVariableDeclarationImpl) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme)

Example 2 with GrAccessorMethod

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod in project intellij-community by JetBrains.

the class RenameGrFieldProcessor method findReferences.

@NotNull
@Override
public Collection<PsiReference> findReferences(PsiElement element) {
    assert element instanceof GrField;
    ArrayList<PsiReference> refs = new ArrayList<>();
    GrField field = (GrField) element;
    GlobalSearchScope projectScope = GlobalSearchScope.projectScope(element.getProject());
    PsiMethod setter = field.getSetter();
    if (setter != null) {
        refs.addAll(RenameAliasedUsagesUtil.filterAliasedRefs(MethodReferencesSearch.search(setter, projectScope, true).findAll(), setter));
    }
    GrAccessorMethod[] getters = field.getGetters();
    for (GrAccessorMethod getter : getters) {
        refs.addAll(RenameAliasedUsagesUtil.filterAliasedRefs(MethodReferencesSearch.search(getter, projectScope, true).findAll(), getter));
    }
    refs.addAll(RenameAliasedUsagesUtil.filterAliasedRefs(ReferencesSearch.search(field, projectScope, false).findAll(), field));
    return refs;
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with GrAccessorMethod

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod in project intellij-community by JetBrains.

the class OldReferencesResolver method getQualifierFromGetterCall.

/**
   * checks for the case: qualifier.getFoo()(args)
   * @param methodExpression
   */
@Nullable
private static GrExpression getQualifierFromGetterCall(GrMethodCall methodExpression) {
    final GroovyResolveResult result = methodExpression.advancedResolve();
    if (!(result.getElement() instanceof GrAccessorMethod) || result.isInvokedOnProperty())
        return null;
    final GrExpression invoked = methodExpression.getInvokedExpression();
    if (invoked instanceof GrReferenceExpression)
        return ((GrReferenceExpression) invoked).getQualifier();
    return null;
}
Also used : GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with GrAccessorMethod

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod in project intellij-community by JetBrains.

the class ConvertClosureToMethodIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrField field;
    if (element.getParent() instanceof GrField) {
        field = (GrField) element.getParent();
    } else {
        final PsiReference ref = element.getReference();
        LOG.assertTrue(ref != null);
        PsiElement resolved = ref.resolve();
        if (resolved instanceof GrAccessorMethod) {
            resolved = ((GrAccessorMethod) resolved).getProperty();
        }
        LOG.assertTrue(resolved instanceof GrField);
        field = (GrField) resolved;
    }
    final HashSet<PsiReference> usages = new HashSet<>();
    usages.addAll(ReferencesSearch.search(field).findAll());
    final GrAccessorMethod[] getters = field.getGetters();
    for (GrAccessorMethod getter : getters) {
        usages.addAll(MethodReferencesSearch.search(getter).findAll());
    }
    final GrAccessorMethod setter = field.getSetter();
    if (setter != null) {
        usages.addAll(MethodReferencesSearch.search(setter).findAll());
    }
    final String fieldName = field.getName();
    LOG.assertTrue(fieldName != null);
    final Collection<PsiElement> fieldUsages = new HashSet<>();
    MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    for (PsiReference usage : usages) {
        final PsiElement psiElement = usage.getElement();
        if (PsiUtil.isMethodUsage(psiElement))
            continue;
        if (!GroovyLanguage.INSTANCE.equals(psiElement.getLanguage())) {
            conflicts.putValue(psiElement, GroovyIntentionsBundle.message("closure.is.accessed.outside.of.groovy", fieldName));
        } else {
            if (psiElement instanceof GrReferenceExpression) {
                fieldUsages.add(psiElement);
                if (PsiUtil.isAccessedForWriting((GrExpression) psiElement)) {
                    conflicts.putValue(psiElement, GroovyIntentionsBundle.message("write.access.to.closure.variable", fieldName));
                }
            } else if (psiElement instanceof GrArgumentLabel) {
                conflicts.putValue(psiElement, GroovyIntentionsBundle.message("field.is.used.in.argument.label", fieldName));
            }
        }
    }
    final PsiClass containingClass = field.getContainingClass();
    final GrExpression initializer = field.getInitializerGroovy();
    LOG.assertTrue(initializer != null);
    final PsiType type = initializer.getType();
    LOG.assertTrue(type instanceof GrClosureType);
    final GrSignature signature = ((GrClosureType) type).getSignature();
    final List<MethodSignature> signatures = GrClosureSignatureUtil.generateAllMethodSignaturesBySignature(fieldName, signature);
    for (MethodSignature s : signatures) {
        final PsiMethod method = MethodSignatureUtil.findMethodBySignature(containingClass, s, true);
        if (method != null) {
            conflicts.putValue(method, GroovyIntentionsBundle.message("method.with.signature.already.exists", GroovyPresentationUtil.getSignaturePresentation(s)));
        }
    }
    if (!conflicts.isEmpty()) {
        final ConflictsDialog conflictsDialog = new ConflictsDialog(project, conflicts, () -> execute(field, fieldUsages));
        conflictsDialog.show();
        if (conflictsDialog.getExitCode() != DialogWrapper.OK_EXIT_CODE)
            return;
    }
    execute(field, fieldUsages);
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) MethodSignature(com.intellij.psi.util.MethodSignature) GrArgumentLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) MultiMap(com.intellij.util.containers.MultiMap) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) GrSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature) HashSet(com.intellij.util.containers.HashSet) GrClosureType(org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType)

Example 5 with GrAccessorMethod

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod in project intellij-community by JetBrains.

the class GrVariableInliner method inlineReference.

static void inlineReference(UsageInfo usage, PsiElement referenced, GrExpression initializer) {
    if (initializer == null)
        return;
    GrExpression exprToBeReplaced = (GrExpression) usage.getElement();
    if (exprToBeReplaced == null)
        return;
    if ((referenced instanceof GrAccessorMethod || referenced instanceof GrField) && exprToBeReplaced instanceof GrReferenceExpression) {
        final GroovyResolveResult resolveResult = ((GrReferenceExpression) exprToBeReplaced).advancedResolve();
        if (resolveResult.getElement() instanceof GrAccessorMethod && !resolveResult.isInvokedOnProperty()) {
            final PsiElement parent = exprToBeReplaced.getParent();
            if (parent instanceof GrCall && parent instanceof GrExpression) {
                exprToBeReplaced = (GrExpression) parent;
            } else {
                return;
            }
        }
    }
    GrExpression newExpr = exprToBeReplaced.replaceWithExpression((GrExpression) initializer.copy(), true);
    final Project project = usage.getProject();
    JavaCodeStyleManager.getInstance(project).shortenClassReferences(newExpr);
    Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
    GroovyRefactoringUtil.highlightOccurrences(project, editor, new PsiElement[] { newExpr });
    WindowManager.getInstance().getStatusBar(project).setInfo(GroovyRefactoringBundle.message("press.escape.to.remove.the.highlighting"));
}
Also used : GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) Project(com.intellij.openapi.project.Project) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Aggregations

GrAccessorMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod)25 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)13 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)7 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)6 PsiElement (com.intellij.psi.PsiElement)5 ArrayList (java.util.ArrayList)5 NotNull (org.jetbrains.annotations.NotNull)5 Nullable (org.jetbrains.annotations.Nullable)5 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)5 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)4 MultiMap (com.intellij.util.containers.MultiMap)3 GrClosureSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)3 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)3 Editor (com.intellij.openapi.editor.Editor)2 Project (com.intellij.openapi.project.Project)2 PsiMethod (com.intellij.psi.PsiMethod)2 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)2 UsageInfo (com.intellij.usageView.UsageInfo)2 GroovyFileBase (org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase)2 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)2