Search in sources :

Example 1 with PsiPerlBlock

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

the class PerlBraceMatcher method getCodeConstructStart.

@Override
public int getCodeConstructStart(PsiFile file, int openingBraceOffset) {
    PsiElement element = file.findElementAt(openingBraceOffset);
    if (element == null || element instanceof PsiFile) {
        return openingBraceOffset;
    }
    PsiElement codeBlock = element.getParent();
    if (codeBlock != null && codeBlock instanceof PsiPerlBlock) {
        PsiElement blockContainer = codeBlock.getParent();
        if (blockContainer != null) {
            if (blockContainer instanceof PerlSubDefinitionElement || blockContainer instanceof PsiPerlForCompound) {
                return blockContainer.getTextOffset();
            } else if (blockContainer instanceof PsiPerlConditionalBlock || blockContainer instanceof PsiPerlIfCompoundImpl) {
                PsiElement keyword = blockContainer.getPrevSibling();
                while (keyword != null && (keyword instanceof PsiWhiteSpace || keyword instanceof PsiComment)) {
                    keyword = keyword.getPrevSibling();
                }
                if (keyword != null) {
                    return keyword.getTextOffset();
                }
            }
        }
    }
    return openingBraceOffset;
}
Also used : PsiPerlConditionalBlock(com.perl5.lang.perl.psi.PsiPerlConditionalBlock) PsiComment(com.intellij.psi.PsiComment) PsiPerlIfCompoundImpl(com.perl5.lang.perl.psi.impl.PsiPerlIfCompoundImpl) PsiPerlBlock(com.perl5.lang.perl.psi.PsiPerlBlock) PsiFile(com.intellij.psi.PsiFile) PerlSubDefinitionElement(com.perl5.lang.perl.psi.PerlSubDefinitionElement) PsiElement(com.intellij.psi.PsiElement) PsiPerlForCompound(com.perl5.lang.perl.psi.PsiPerlForCompound) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 2 with PsiPerlBlock

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

the class PerlLoopControlInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
    PerlSubDefinitionElement breakDefinition = PerlBuiltInSubsService.getInstance(holder.getProject()).findSub("break");
    return new PerlVisitor() {

        @Override
        public void visitNextExpr(@NotNull PsiPerlNextExpr o) {
            processLoopsControl(o);
        }

        @Override
        public void visitRedoExpr(@NotNull PsiPerlRedoExpr o) {
            processLoopsControl(o);
        }

        @Override
        public void visitLastExpr(@NotNull PsiPerlLastExpr o) {
            processLoopsControl(o);
        }

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

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

        /**
         * @return parent psi element for closest parent block element
         */
        @Nullable
        private PsiElement getClosestBlockContainer(@NotNull PsiElement position) {
            PsiPerlBlock enclosingBlock = PsiTreeUtil.getParentOfType(position, PsiPerlBlock.class);
            if (enclosingBlock == null) {
                return null;
            }
            PsiElement blockContainer = enclosingBlock.getParent();
            return PsiUtilCore.getElementType(blockContainer) == LP_CODE_BLOCK ? blockContainer.getParent() : blockContainer;
        }

        /**
         * Traversing blocks up, trying to figure out if last/next/redo are in right place.
         *
         * @param expr last/next/redo expression
         */
        private void processLoopsControl(@NotNull PsiElement expr) {
            PsiElement keyword = expr.getFirstChild();
            if (keyword == null) {
                return;
            }
            String keywordText = keyword.getText();
            // checks modifier
            PsiPerlStatementImpl containingStatement = PsiTreeUtil.getParentOfType(expr, PsiPerlStatementImpl.class);
            PsiPerlStatementModifier modifier = containingStatement == null ? null : containingStatement.getModifier();
            if (modifier instanceof PsiPerlForStatementModifier) {
                return;
            }
            // traversing up
            PsiElement position = expr;
            boolean isInsideGiven = false;
            boolean isInsideWhenOrDefault = false;
            while (true) {
                PsiElement closestBlockContainer = getClosestBlockContainer(position);
                if (closestBlockContainer == null) {
                    break;
                }
                IElementType blockContainerType = PsiUtilCore.getElementType(closestBlockContainer);
                if (LOOPS_CONTAINERS.contains(blockContainerType)) {
                    return;
                } else if (blockContainerType == NAMED_BLOCK) {
                    problem(expr, "perl.inspection.loop.control.in.named.block", keywordText);
                    return;
                } else if (MAP_GREP.contains(blockContainerType)) {
                    problem(expr, "perl.inspection.loop.control.in.map", keywordText);
                    return;
                } else if (BLOCKS_WITH_RETURN_VALUE.contains(blockContainerType)) {
                    problem(expr, "perl.inspection.loop.control.in.do", keywordText);
                    return;
                } else if (blockContainerType == GIVEN_COMPOUND) {
                    isInsideGiven = true;
                } else if (blockContainerType == WHEN_COMPOUND || blockContainerType == DEFAULT_COMPOUND) {
                    isInsideWhenOrDefault = true;
                }
                position = closestBlockContainer;
            }
            if (expr instanceof PsiPerlNextExpr && isInsideWhenOrDefault) {
                holder.registerProblem(expr, PerlBundle.message("perl.inspection.loop.control.next.instead.of.continue"), new ReplaceWithExpressionQuickFix("continue"));
            } else if (expr instanceof PsiPerlLastExpr && isInsideGiven) {
                holder.registerProblem(expr, PerlBundle.message("perl.inspection.loop.control.last.instead.of.break"), new ReplaceWithExpressionQuickFix("break"));
            } else {
                problem(expr, "perl.inspection.loop.control.outside", keywordText);
            }
        }
    };
}
Also used : PsiReference(com.intellij.psi.PsiReference) PsiPerlStatementImpl(com.perl5.lang.perl.psi.impl.PsiPerlStatementImpl) NotNull(org.jetbrains.annotations.NotNull) IElementType(com.intellij.psi.tree.IElementType) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with PsiPerlBlock

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

the class PerlConvertableCompound method isConvertableToModifier.

/**
 * @return true if compound may be converted to the statement modifier
 */
default boolean isConvertableToModifier() {
    PsiPerlBlock mainBlock = getBlock();
    if (mainBlock == null) {
        return false;
    }
    PsiElement[] children = mainBlock.getChildren();
    return children.length == 1 && children[0] instanceof PerlStatementMixin && ((PerlStatementMixin) children[0]).getModifier() == null;
}
Also used : PsiPerlBlock(com.perl5.lang.perl.psi.PsiPerlBlock) PerlStatementMixin(com.perl5.lang.perl.psi.mixins.PerlStatementMixin) PsiElement(com.intellij.psi.PsiElement)

Example 4 with PsiPerlBlock

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

the class MojoHelperWrapper method calcLightElementsFromPsi.

@NotNull
@Override
public List<PerlDelegatingLightNamedElement> calcLightElementsFromPsi() {
    List<PsiElement> listElements = getCallArgumentsList();
    if (listElements.size() != 2) {
        return Collections.emptyList();
    }
    PsiElement identifierElement = listElements.get(0);
    if (!isAcceptableIdentifierElement(identifierElement)) {
        return Collections.emptyList();
    }
    PsiElement bodyElement = listElements.get(1);
    // fixme we could handle reference here
    if (!(bodyElement instanceof PerlSubExpr)) {
        return Collections.emptyList();
    }
    String subName = ElementManipulators.getValueText(identifierElement);
    if (StringUtil.isEmpty(subName)) {
        return Collections.emptyList();
    }
    PsiPerlBlock subDefinitionBody = ((PerlSubExpr) bodyElement).getBlock();
    return Collections.singletonList(new MojoHelperDefinition(this, subName, LIGHT_METHOD_DEFINITION, identifierElement, MOJO_CONTROLLER_NS, PerlSubDefinitionElement.getPerlSubArgumentsFromBody(subDefinitionBody), computeSubAnnotations(this, identifierElement), subDefinitionBody));
}
Also used : PsiPerlBlock(com.perl5.lang.perl.psi.PsiPerlBlock) PerlSubExpr(com.perl5.lang.perl.psi.PerlSubExpr) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiElement (com.intellij.psi.PsiElement)4 PsiPerlBlock (com.perl5.lang.perl.psi.PsiPerlBlock)3 NotNull (org.jetbrains.annotations.NotNull)2 PsiComment (com.intellij.psi.PsiComment)1 PsiFile (com.intellij.psi.PsiFile)1 PsiReference (com.intellij.psi.PsiReference)1 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 IElementType (com.intellij.psi.tree.IElementType)1 PerlSubDefinitionElement (com.perl5.lang.perl.psi.PerlSubDefinitionElement)1 PerlSubExpr (com.perl5.lang.perl.psi.PerlSubExpr)1 PsiPerlConditionalBlock (com.perl5.lang.perl.psi.PsiPerlConditionalBlock)1 PsiPerlForCompound (com.perl5.lang.perl.psi.PsiPerlForCompound)1 PsiPerlIfCompoundImpl (com.perl5.lang.perl.psi.impl.PsiPerlIfCompoundImpl)1 PsiPerlStatementImpl (com.perl5.lang.perl.psi.impl.PsiPerlStatementImpl)1 PerlStatementMixin (com.perl5.lang.perl.psi.mixins.PerlStatementMixin)1