mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-38591 (Can't use "Create Local Var from instanceof" intention on multiple 'instanceof' with &&)
This commit is contained in:
+19
-26
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2013 JetBrains s.r.o.
|
||||
* Copyright 2000-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -37,7 +37,6 @@ import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.codeStyle.SuggestedNameInfo;
|
||||
import com.intellij.psi.codeStyle.VariableKind;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.psi.util.TypeConversionUtil;
|
||||
import com.intellij.refactoring.JavaRefactoringSettings;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
@@ -45,9 +44,7 @@ import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -106,7 +103,10 @@ public class CreateLocalVarFromInstanceofAction extends BaseIntentionAction {
|
||||
PsiExpression initializer = ((PsiLocalVariable)element).getInitializer();
|
||||
if (!(initializer instanceof PsiTypeCastExpression)) continue;
|
||||
|
||||
PsiTypeElement castTypeElement = ((PsiTypeCastExpression)initializer).getCastType();
|
||||
final PsiTypeCastExpression typeCastExpression = (PsiTypeCastExpression)initializer;
|
||||
final PsiExpression operand = typeCastExpression.getOperand();
|
||||
if (operand != null && !PsiEquivalenceUtil.areElementsEquivalent(operand, instanceOfExpression.getOperand())) continue;
|
||||
PsiTypeElement castTypeElement = typeCastExpression.getCastType();
|
||||
if (castTypeElement == null) continue;
|
||||
PsiType castType = castTypeElement.getType();
|
||||
if (castType.equals(type)) return true;
|
||||
@@ -213,7 +213,7 @@ public class CreateLocalVarFromInstanceofAction extends BaseIntentionAction {
|
||||
TemplateBuilderImpl builder = new TemplateBuilderImpl(localVariable);
|
||||
builder.setEndVariableAfter(localVariable.getNameIdentifier());
|
||||
|
||||
Template template = generateTemplate(project, localVariable.getInitializer(), localVariable.getType());
|
||||
Template template = generateTemplate(project, localVariable.getInitializer(), localVariable.getType(), instanceOfExpression);
|
||||
|
||||
Editor newEditor = CreateFromUsageBaseFix.positionCursor(project, file, localVariable.getNameIdentifier());
|
||||
if (newEditor == null) return;
|
||||
@@ -343,11 +343,7 @@ public class CreateLocalVarFromInstanceofAction extends BaseIntentionAction {
|
||||
anchorAfter = newBranch.getCodeBlock().getLBrace();
|
||||
}
|
||||
else {
|
||||
final PsiJavaToken lBrace = ((PsiBlockStatement)thenBranch).getCodeBlock().getLBrace();
|
||||
if (lBrace != null) {
|
||||
final PsiElement nextSibling = PsiTreeUtil.skipSiblingsForward(lBrace, PsiWhiteSpace.class);
|
||||
anchorAfter = nextSibling instanceof PsiComment ? PsiTreeUtil.skipSiblingsForward(nextSibling, PsiComment.class) : lBrace;
|
||||
}
|
||||
anchorAfter = ((PsiBlockStatement)thenBranch).getCodeBlock().getLBrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -377,6 +373,13 @@ public class CreateLocalVarFromInstanceofAction extends BaseIntentionAction {
|
||||
if (anchorAfter == null) {
|
||||
return null;
|
||||
}
|
||||
PsiElement nextSibling = PsiTreeUtil.skipSiblingsForward(anchorAfter, PsiWhiteSpace.class);
|
||||
anchorAfter = nextSibling instanceof PsiComment ? PsiTreeUtil.skipSiblingsForward(nextSibling, PsiComment.class) : anchorAfter;
|
||||
nextSibling = PsiTreeUtil.getNextSiblingOfType(anchorAfter, PsiStatement.class);
|
||||
while (nextSibling instanceof PsiDeclarationStatement) {
|
||||
anchorAfter = nextSibling;
|
||||
nextSibling = PsiTreeUtil.getNextSiblingOfType(anchorAfter, PsiStatement.class);
|
||||
}
|
||||
return anchorAfter.getParent().addAfter(toInsert, anchorAfter);
|
||||
}
|
||||
|
||||
@@ -395,31 +398,21 @@ public class CreateLocalVarFromInstanceofAction extends BaseIntentionAction {
|
||||
return element instanceof PsiPrefixExpression && ((PsiPrefixExpression)element).getOperationTokenType() == JavaTokenType.EXCL;
|
||||
}
|
||||
|
||||
private static Template generateTemplate(Project project, PsiExpression initializer, PsiType type) {
|
||||
private static Template generateTemplate(Project project, PsiExpression initializer, PsiType type, PsiElement place) {
|
||||
final TemplateManager templateManager = TemplateManager.getInstance(project);
|
||||
final Template template = templateManager.createTemplate("", "");
|
||||
template.setToReformat(true);
|
||||
|
||||
SuggestedNameInfo suggestedNameInfo = JavaCodeStyleManager.getInstance(project).suggestVariableName(VariableKind.LOCAL_VARIABLE, null,
|
||||
initializer, type);
|
||||
List<String> uniqueNames = new ArrayList<String>();
|
||||
for (String name : suggestedNameInfo.names) {
|
||||
if (PsiUtil.isVariableNameUnique(name, initializer)) {
|
||||
uniqueNames.add(name);
|
||||
}
|
||||
}
|
||||
if (uniqueNames.isEmpty() && suggestedNameInfo.names.length != 0) {
|
||||
String baseName = suggestedNameInfo.names[0];
|
||||
String name = JavaCodeStyleManager.getInstance(project).suggestUniqueVariableName(baseName, initializer, true);
|
||||
uniqueNames.add(name);
|
||||
}
|
||||
initializer, type);
|
||||
suggestedNameInfo = JavaCodeStyleManager.getInstance(project).suggestUniqueVariableName(suggestedNameInfo, place, true);
|
||||
|
||||
Set<LookupElement> itemSet = new LinkedHashSet<LookupElement>();
|
||||
for (String name : uniqueNames) {
|
||||
for (String name : suggestedNameInfo.names) {
|
||||
itemSet.add(LookupElementBuilder.create(name));
|
||||
}
|
||||
final LookupElement[] lookupItems = itemSet.toArray(new LookupElement[itemSet.size()]);
|
||||
final Result result = uniqueNames.isEmpty() ? null : new TextResult(uniqueNames.get(0));
|
||||
final Result result = suggestedNameInfo.names.length == 0 ? null : new TextResult(suggestedNameInfo.names[0]);
|
||||
|
||||
Expression expr = new Expression() {
|
||||
@Override
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
class C {
|
||||
void f(Object o) {
|
||||
if (o instanceof String) {
|
||||
String s = (String) o;
|
||||
String o1 = (String) o;
|
||||
<caret>
|
||||
o = "";
|
||||
}
|
||||
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Insert '(String)o' declaration" "true"
|
||||
class C {
|
||||
void f(Object o, Object f) {
|
||||
if (o instanceof String) {
|
||||
Float s = 1f;
|
||||
String o1 = (String) o;
|
||||
<caret>
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -4,7 +4,7 @@ import java.io.IOException;
|
||||
class C {
|
||||
void f(Object o) {
|
||||
if (o instanceof IOException) {
|
||||
IOException ioException = (IOException) o;
|
||||
IOException o1 = (IOException) o;
|
||||
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
public abstract class A {
|
||||
public void getNodeElements(Object parent) {
|
||||
if (!(parent instanceof NodeInfo)) return;
|
||||
NodeInfo nodeInfo = (NodeInfo) parent;
|
||||
NodeInfo parent1 = (NodeInfo) parent;
|
||||
<caret>
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
class C {
|
||||
void f(Object o, Object f) {
|
||||
if (o instanceof String && f == null) {
|
||||
String s = (String) o;
|
||||
String o1 = (String) o;
|
||||
<caret>
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@ class C {
|
||||
void f(Object o, Object f) {
|
||||
if (o instanceof String) {//todo comment
|
||||
|
||||
String s = (String) o;
|
||||
String o1 = (String) o;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Insert '(String)f' declaration" "true"
|
||||
class C {
|
||||
void f(Object o, Object f) {
|
||||
if (o instanceof String && f instanceof String) {
|
||||
String s = (String) o;
|
||||
String f1 = (String) f;
|
||||
<caret>
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
class C {
|
||||
void f(Object o) {
|
||||
if (o instanceof Runnable) {
|
||||
Runnable runnable = (Runnable) o;
|
||||
Runnable o1 = (Runnable) o;
|
||||
<caret>
|
||||
}
|
||||
}
|
||||
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Insert '(String)o' declaration" "true"
|
||||
class C {
|
||||
void f(Object o, Object f) {
|
||||
if (<caret>o instanceof String) {
|
||||
Float s = 1f;
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Insert '(String)f' declaration" "true"
|
||||
class C {
|
||||
void f(Object o, Object f) {
|
||||
if (o instanceof String && f<caret> instanceof String) {
|
||||
String s = (String) o;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user