Search in sources :

Example 11 with PerlSubCallElement

use of com.perl5.lang.perl.psi.impl.PerlSubCallElement in project Perl5-IDEA by Camelcade.

the class PerlSwitchInspection method buildVisitor.

@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
    PerlSubDefinitionElement breakDefinition = Objects.requireNonNull(PerlImplicitDeclarationsService.getInstance(holder.getProject()).getCoreSub(PerlSyntax.BREAK_KEYWORD));
    return new PerlVisitor() {

        private void problem(@NotNull PsiElement anchor, @NotNull @PropertyKey(resourceBundle = PATH_TO_BUNDLE) String key, @NotNull String... args) {
            registerProblem(holder, anchor, PerlBundle.message(key, (Object[]) args));
        }

        @Override
        public void visitContinueExpr(@NotNull PsiPerlContinueExpr o) {
            PsiElement position = o;
            boolean isInsideTheLoop = false;
            while (true) {
                PsiElement closestBlockContainer = PerlBlock.getClosestBlockCompoundContainer(position);
                if (closestBlockContainer == null) {
                    break;
                }
                IElementType blockContainerElementType = PsiUtilCore.getElementType(closestBlockContainer);
                if (blockContainerElementType == WHEN_COMPOUND || blockContainerElementType == DEFAULT_COMPOUND) {
                    return;
                } else if (PerlBlock.LOOPS_CONTAINERS.contains(blockContainerElementType)) {
                    isInsideTheLoop = true;
                } else if (blockContainerElementType == NAMED_BLOCK) {
                    break;
                } else if (MAP_GREP.contains(blockContainerElementType)) {
                    break;
                } else if (PerlBlock.BLOCKS_WITH_RETURN_VALUE.contains(blockContainerElementType)) {
                    break;
                } else if (blockContainerElementType == GIVEN_COMPOUND) {
                    break;
                }
                position = closestBlockContainer;
            }
            if (isInsideTheLoop) {
                holder.registerProblem(o, PerlBundle.message("perl.inspection.loop.control.continue.instead.of.next"), new ReplaceWithExpressionQuickFix("next"));
            } else {
                problem(o, "perl.inspection.loop.control.continue");
            }
        }

        @Override
        public void visitSubNameElement(@NotNull PerlSubNameElement o) {
            PsiReference reference = o.getReference();
            if (reference == null) {
                return;
            }
            if (reference.resolve() != breakDefinition) {
                return;
            }
            PsiElement methodElement = o.getParent();
            if (!(methodElement instanceof PsiPerlMethod)) {
                return;
            }
            PsiElement callExpr = methodElement.getParent();
            if (!(callExpr instanceof PerlSubCallElement)) {
                return;
            }
            PsiElement position = o;
            boolean isInsideTheLoop = false;
            while (true) {
                PsiElement closestBlockContainer = PerlBlock.getClosestBlockCompoundContainer(position);
                if (closestBlockContainer == null) {
                    break;
                }
                IElementType blockContainerElementType = PsiUtilCore.getElementType(closestBlockContainer);
                if (PerlBlock.LOOPS_CONTAINERS.contains(blockContainerElementType)) {
                    isInsideTheLoop = true;
                } else if (blockContainerElementType == NAMED_BLOCK) {
                    break;
                } else if (MAP_GREP.contains(blockContainerElementType)) {
                    break;
                } else if (PerlBlock.BLOCKS_WITH_RETURN_VALUE.contains(blockContainerElementType)) {
                    break;
                } else if (blockContainerElementType == GIVEN_COMPOUND) {
                    return;
                }
                position = closestBlockContainer;
            }
            if (isInsideTheLoop) {
                holder.registerProblem(callExpr, PerlBundle.message("perl.inspection.loop.control.break.instead.of.last"), new ReplaceWithExpressionQuickFix("last"));
            } else {
                problem(o, "perl.inspection.loop.control.break");
            }
        }

        @Override
        public void visitWhenCompound(@NotNull PsiPerlWhenCompound o) {
            if (PerlSwitchTopicalizer.wrapping(o) == null) {
                problem(o, "perl.inspection.switch.when.outside");
            }
            super.visitWhenCompound(o);
        }

        @Override
        public void visitWhenStatementModifier(@NotNull PsiPerlWhenStatementModifier o) {
            if (PerlSwitchTopicalizer.wrapping(o) == null) {
                problem(o, "perl.inspection.switch.when.modifier.outside");
            }
            super.visitWhenStatementModifier(o);
        }

        @Override
        public void visitDefaultCompound(@NotNull PsiPerlDefaultCompound o) {
            if (PerlSwitchTopicalizer.wrapping(o) == null) {
                problem(o, "perl.inspection.switch.default.outside");
            }
            super.visitDefaultCompound(o);
        }
    };
}
Also used : PsiReference(com.intellij.psi.PsiReference) NotNull(org.jetbrains.annotations.NotNull) IElementType(com.intellij.psi.tree.IElementType) PerlSubCallElement(com.perl5.lang.perl.psi.impl.PerlSubCallElement) PsiElement(com.intellij.psi.PsiElement) PropertyKey(org.jetbrains.annotations.PropertyKey) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with PerlSubCallElement

use of com.perl5.lang.perl.psi.impl.PerlSubCallElement in project Perl5-IDEA by Camelcade.

the class PerlFancyMethodQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement method = descriptor.getPsiElement();
    assert method instanceof PerlMethod;
    PsiElement callExpression = method.getParent();
    assert callExpression instanceof PerlSubCallElement;
    PerlNamespaceElement namespaceElement = ((PerlMethod) method).getNamespaceElement();
    assert namespaceElement != null;
    PerlSubNameElement subNameElement = ((PerlMethod) method).getSubNameElement();
    assert subNameElement != null;
    StringBuilder argsBuilder = new StringBuilder();
    PsiElement run = callExpression.getFirstChild();
    while (run != null) {
        if (run != method) {
            argsBuilder.append(run.getNode().getChars());
        }
        run = run.getNextSibling();
    }
    callExpression.replace(PerlElementFactory.createMethodCall(project, namespaceElement.getText(), subNameElement.getName(), argsBuilder.toString()));
}
Also used : PerlMethod(com.perl5.lang.perl.psi.PerlMethod) PerlSubNameElement(com.perl5.lang.perl.psi.PerlSubNameElement) PerlSubCallElement(com.perl5.lang.perl.psi.impl.PerlSubCallElement) PerlNamespaceElement(com.perl5.lang.perl.psi.PerlNamespaceElement) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiElement (com.intellij.psi.PsiElement)10 PerlSubCallElement (com.perl5.lang.perl.psi.impl.PerlSubCallElement)9 NotNull (org.jetbrains.annotations.NotNull)8 PerlDelegatingLightNamedElement (com.perl5.lang.perl.psi.light.PerlDelegatingLightNamedElement)4 PerlValue (com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValue)3 PerlLightMethodDefinitionElement (com.perl5.lang.perl.psi.light.PerlLightMethodDefinitionElement)3 IElementType (com.intellij.psi.tree.IElementType)2 PerlSubNameElement (com.perl5.lang.perl.psi.PerlSubNameElement)2 PerlSubAnnotations (com.perl5.lang.perl.psi.utils.PerlSubAnnotations)2 ArrayList (java.util.ArrayList)2 ASTNode (com.intellij.lang.ASTNode)1 Logger (com.intellij.openapi.diagnostic.Logger)1 Ref (com.intellij.openapi.util.Ref)1 StringUtil (com.intellij.openapi.util.text.StringUtil)1 ElementManipulators (com.intellij.psi.ElementManipulators)1 PsiReference (com.intellij.psi.PsiReference)1 StubInputStream (com.intellij.psi.stubs.StubInputStream)1 StubOutputStream (com.intellij.psi.stubs.StubOutputStream)1 CachedValueProvider (com.intellij.psi.util.CachedValueProvider)1 CachedValuesManager (com.intellij.psi.util.CachedValuesManager)1