use of org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner in project intellij-community by JetBrains.
the class GrHighlightExitPointHandler method computeUsages.
@Override
public void computeUsages(List<PsiElement> targets) {
PsiElement parent = myTarget.getParent();
if (!(parent instanceof GrReturnStatement) && !(parent instanceof GrThrowStatement))
return;
final GrControlFlowOwner flowOwner = ControlFlowUtils.findControlFlowOwner(parent);
ControlFlowUtils.visitAllExitPoints(flowOwner, new ControlFlowUtils.ExitPointVisitor() {
@Override
public boolean visitExitPoint(Instruction instruction, @Nullable GrExpression returnValue) {
final PsiElement returnElement = instruction.getElement();
if (returnElement != null && isCorrectReturn(returnElement)) {
final TextRange range = returnElement.getTextRange();
myReadUsages.add(range);
}
return true;
}
});
}
use of org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner in project intellij-community by JetBrains.
the class GrRedundantElseIntention method getElementPredicate.
@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
return new PsiElementPredicate() {
@Override
public boolean satisfiedBy(PsiElement element) {
if (!(element.getNode().getElementType() == GroovyTokenTypes.kELSE))
return false;
final PsiElement parent = element.getParent();
if (!(parent instanceof GrIfStatement))
return false;
final GrIfStatement ifStatement = (GrIfStatement) parent;
final GrStatement branch = ifStatement.getThenBranch();
final GrControlFlowOwner flowOwner = ControlFlowUtils.findControlFlowOwner(ifStatement);
if (flowOwner == null)
return false;
final Instruction[] flow = flowOwner.getControlFlow();
for (Instruction instruction : flow) {
if (instruction instanceof IfEndInstruction && instruction.getElement() == ifStatement) {
for (Instruction pred : instruction.allPredecessors()) {
final PsiElement predElement = pred.getElement();
if (predElement != null && PsiTreeUtil.isAncestor(branch, predElement, false)) {
return false;
}
}
}
}
return true;
}
};
}
use of org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner in project intellij-community by JetBrains.
the class InvertIfIntention method isTailAfterIf.
private static boolean isTailAfterIf(@NotNull GrIfStatement ifStatement, @NotNull GrStatementOwner owner) {
final GrControlFlowOwner flowOwner = ControlFlowUtils.findControlFlowOwner(ifStatement);
if (flowOwner == null)
return false;
final Instruction[] flow = flowOwner.getControlFlow();
final GrStatement[] statements = owner.getStatements();
final int index = ArrayUtilRt.find(statements, ifStatement);
if (index == statements.length - 1)
return false;
final GrStatement then = ifStatement.getThenBranch();
for (Instruction i : flow) {
final PsiElement element = i.getElement();
if (element == null || !PsiTreeUtil.isAncestor(then, element, true))
continue;
for (Instruction succ : i.allSuccessors()) {
if (succ instanceof IfEndInstruction) {
return false;
}
}
}
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner in project intellij-community by JetBrains.
the class GroovyInlineLocalHandler method createSettings.
/**
* Returns Settings object for referenced definition in case of local variable
*/
@Nullable
private static InlineLocalVarSettings createSettings(final GrVariable variable, Editor editor, boolean invokedOnReference) {
final String localName = variable.getName();
final Project project = variable.getProject();
GrExpression initializer = null;
Instruction writeInstr = null;
Instruction[] flow = null;
//search for initializer to inline
if (invokedOnReference) {
LOG.assertTrue(editor != null, "null editor but invokedOnReference==true");
final PsiReference ref = TargetElementUtil.findReference(editor);
LOG.assertTrue(ref != null);
PsiElement cur = ref.getElement();
if (cur instanceof GrReferenceExpression) {
GrControlFlowOwner controlFlowOwner;
do {
controlFlowOwner = ControlFlowUtils.findControlFlowOwner(cur);
if (controlFlowOwner == null)
break;
flow = controlFlowOwner.getControlFlow();
final List<BitSet> writes = ControlFlowUtils.inferWriteAccessMap(flow, variable);
final PsiElement finalCur = cur;
Instruction instruction = ControlFlowUtils.findInstruction(finalCur, flow);
LOG.assertTrue(instruction != null);
final BitSet prev = writes.get(instruction.num());
if (prev.cardinality() == 1) {
writeInstr = flow[prev.nextSetBit(0)];
final PsiElement element = writeInstr.getElement();
if (element instanceof GrVariable) {
initializer = ((GrVariable) element).getInitializerGroovy();
} else if (element instanceof GrReferenceExpression) {
initializer = TypeInferenceHelper.getInitializerFor((GrReferenceExpression) element);
}
}
PsiElement old_cur = cur;
if (controlFlowOwner instanceof GrClosableBlock) {
cur = controlFlowOwner;
} else {
PsiElement parent = controlFlowOwner.getParent();
if (parent instanceof GrMember)
cur = ((GrMember) parent).getContainingClass();
}
if (cur == old_cur)
break;
} while (initializer == null);
}
} else {
flow = ControlFlowUtils.findControlFlowOwner(variable).getControlFlow();
initializer = variable.getInitializerGroovy();
writeInstr = ContainerUtil.find(flow, instruction -> instruction.getElement() == variable);
}
if (initializer == null || writeInstr == null) {
String message = GroovyRefactoringBundle.message("cannot.find.a.single.definition.to.inline.local.var");
CommonRefactoringUtil.showErrorHint(variable.getProject(), editor, message, INLINE_VARIABLE, HelpID.INLINE_VARIABLE);
return null;
}
int writeInstructionNumber = writeInstr.num();
if (ApplicationManager.getApplication().isUnitTestMode()) {
return new InlineLocalVarSettings(initializer, writeInstructionNumber, flow);
}
final String question = GroovyRefactoringBundle.message("inline.local.variable.prompt.0.1", localName);
RefactoringMessageDialog dialog = new RefactoringMessageDialog(INLINE_VARIABLE, question, HelpID.INLINE_VARIABLE, "OptionPane.questionIcon", true, project);
if (dialog.showAndGet()) {
return new InlineLocalVarSettings(initializer, writeInstructionNumber, flow);
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner in project intellij-community by JetBrains.
the class ExpressionGenerator method isImplicitlyCastedToArray.
private static boolean isImplicitlyCastedToArray(GrListOrMap list) {
PsiElement parent = list.getParent();
GrControlFlowOwner owner = ControlFlowUtils.findControlFlowOwner(list);
if (!(owner instanceof GrOpenBlock && owner.getParent() instanceof GrMethod))
return false;
if (!(parent instanceof GrReturnStatement || ControlFlowUtils.isReturnValue(list, owner)))
return false;
PsiType type = ((GrMethod) owner.getParent()).getReturnType();
return type instanceof PsiArrayType;
}
Aggregations