inline local: show warning if syntax errors prevent inlining (IDEA-191147)

This commit is contained in:
Anna Kozlova
2018-05-02 20:06:11 +02:00
parent 4acf542669
commit d56ddff7b8
2 changed files with 51 additions and 37 deletions
@@ -151,7 +151,14 @@ public class InlineLocalHandler extends JavaInlineActionHandler {
}
List<PsiElement> refsToInlineList = new ArrayList<>();
Collections.addAll(refsToInlineList, DefUseUtil.getRefs(containerBlock, local, defToInline));
try {
Collections.addAll(refsToInlineList, DefUseUtil.getVariableRefs(containerBlock, local, defToInline));
}
catch (AnalysisCanceledException e) {
String message = RefactoringBundle.message("extract.method.control.flow.analysis.failed", localName);
CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.INLINE_VARIABLE);
return;
}
for (PsiElement innerClassUsage : innerClassUsages) {
if (!refsToInlineList.contains(innerClassUsage)) {
refsToInlineList.add(innerClassUsage);
@@ -336,48 +336,55 @@ public class DefUseUtil {
@NotNull
public static PsiElement[] getRefs(@NotNull PsiCodeBlock body, @NotNull PsiVariable def, @NotNull PsiElement ref) {
try {
RefsDefs refsDefs = new RefsDefs(body) {
@Override
protected int nNext(int index) {
return instructions.get(index).nNext();
}
@Override
protected int getNext(int index, int no) {
return instructions.get(index).getNext(index, no);
}
@Override
protected boolean defs() {
return false;
}
@Override
protected void processInstruction(@NotNull final Set<PsiElement> res, @NotNull final Instruction instruction, int index) {
if (instruction instanceof ReadVariableInstruction) {
ReadVariableInstruction instructionR = (ReadVariableInstruction)instruction;
if (instructionR.variable == def) {
final PsiElement element = flow.getElement(index);
element.accept(new JavaRecursiveElementWalkingVisitor() {
@Override
public void visitReferenceExpression(PsiReferenceExpression ref) {
if (ref.resolve() == def) {
res.add(ref);
}
}
});
}
}
}
};
return refsDefs.get(def, ref);
return getVariableRefs(body, def, ref);
}
catch (AnalysisCanceledException e) {
return PsiElement.EMPTY_ARRAY;
}
}
/**
* @throws AnalysisCanceledException when body contains syntax error
*/
public static PsiElement[] getVariableRefs(@NotNull PsiCodeBlock body, @NotNull PsiVariable def, @NotNull PsiElement ref) throws AnalysisCanceledException {
RefsDefs refsDefs = new RefsDefs(body) {
@Override
protected int nNext(int index) {
return instructions.get(index).nNext();
}
@Override
protected int getNext(int index, int no) {
return instructions.get(index).getNext(index, no);
}
@Override
protected boolean defs() {
return false;
}
@Override
protected void processInstruction(@NotNull final Set<PsiElement> res, @NotNull final Instruction instruction, int index) {
if (instruction instanceof ReadVariableInstruction) {
ReadVariableInstruction instructionR = (ReadVariableInstruction)instruction;
if (instructionR.variable == def) {
final PsiElement element = flow.getElement(index);
element.accept(new JavaRecursiveElementWalkingVisitor() {
@Override
public void visitReferenceExpression(PsiReferenceExpression ref) {
if (ref.resolve() == def) {
res.add(ref);
}
}
});
}
}
}
};
return refsDefs.get(def, ref);
}
private abstract static class RefsDefs {
protected abstract int nNext(int index);
protected abstract int getNext(int index, int no);