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