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;
}
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);
}
}
};
}
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;
}
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));
}
Aggregations