mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
CreateLocalVarFromInstanceofAction: use VariableNameGenerator (produces better results)
GitOrigin-RevId: 10d07e83576c67df180c8652d1003cf3de6810d1
This commit is contained in:
committed by
intellij-monorepo-bot
parent
5b813d826b
commit
b990c89c65
@@ -88,7 +88,7 @@ create.getter.for.field=Create getter for ''{0}''
|
||||
create.setter.for.field=Create setter for ''{0}''
|
||||
create.getter.and.setter.for.field=Create getter and setter for ''{0}''
|
||||
create.local.from.usage.family=Create Local from Usage
|
||||
create.local.from.instanceof.usage.family=Create Local Var from instanceof Usage
|
||||
create.local.from.instanceof.usage.family=Create local variable from instanceof usage
|
||||
create.local.from.instanceof.usage.text=Insert ''({0}){1}'' declaration
|
||||
create.member.from.usage.family=Create member from usage
|
||||
create.method.from.usage.family=Create method from usage
|
||||
|
||||
+13
-11
@@ -19,20 +19,21 @@ import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
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.refactoring.introduceVariable.IntroduceVariableBase;
|
||||
import com.intellij.refactoring.util.RefactoringChangeUtil;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.ArrayUtilRt;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.siyeh.ig.psiutils.EquivalenceChecker;
|
||||
import com.siyeh.ig.psiutils.VariableNameGenerator;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
@@ -198,18 +199,18 @@ public class CreateLocalVarFromInstanceofAction extends BaseIntentionAction {
|
||||
|
||||
PsiLocalVariable localVariable = (PsiLocalVariable)decl.getDeclaredElements()[0];
|
||||
PsiExpression initializer = Objects.requireNonNull(localVariable.getInitializer());
|
||||
SuggestedNameInfo nameInfo = IntroduceVariableBase.getSuggestedName(localVariable.getType(), initializer, initializer);
|
||||
String defaultName = ArrayUtil.getFirstElement(nameInfo.names);
|
||||
List<String> names = new VariableNameGenerator(initializer, VariableKind.LOCAL_VARIABLE).byExpression(initializer)
|
||||
.byType(localVariable.getType()).generateAll(true);
|
||||
PsiIdentifier identifier = Objects.requireNonNull(localVariable.getNameIdentifier());
|
||||
if (defaultName != null) {
|
||||
identifier = (PsiIdentifier)identifier.replace(JavaPsiFacade.getElementFactory(project).createIdentifier(defaultName));
|
||||
if (!file.isPhysical()) {
|
||||
identifier.replace(JavaPsiFacade.getElementFactory(project).createIdentifier(names.get(0)));
|
||||
return;
|
||||
}
|
||||
if (!file.isPhysical()) return;
|
||||
|
||||
TemplateBuilderImpl builder = new TemplateBuilderImpl(localVariable);
|
||||
builder.setEndVariableAfter(localVariable.getNameIdentifier());
|
||||
|
||||
Template template = generateTemplate(project, nameInfo);
|
||||
Template template = generateTemplate(project, names);
|
||||
Editor newEditor = CreateFromUsageBaseFix.positionCursor(project, file, identifier);
|
||||
if (newEditor == null) return;
|
||||
TextRange range = localVariable.getNameIdentifier().getTextRange();
|
||||
@@ -454,14 +455,15 @@ public class CreateLocalVarFromInstanceofAction extends BaseIntentionAction {
|
||||
return element instanceof PsiPrefixExpression && ((PsiPrefixExpression)element).getOperationTokenType() == JavaTokenType.EXCL;
|
||||
}
|
||||
|
||||
private static Template generateTemplate(Project project, SuggestedNameInfo suggestedNameInfo) {
|
||||
private static Template generateTemplate(Project project, List<String> names) {
|
||||
final TemplateManager templateManager = TemplateManager.getInstance(project);
|
||||
final Template template = templateManager.createTemplate("", "");
|
||||
template.setToReformat(true);
|
||||
|
||||
final Result result = suggestedNameInfo.names.length == 0 ? null : new TextResult(suggestedNameInfo.names[0]);
|
||||
final Result result = new TextResult(names.get(0));
|
||||
|
||||
Expression expr = new ConstantNode(result).withLookupStrings(suggestedNameInfo.names.length > 1 ? suggestedNameInfo.names : ArrayUtilRt.EMPTY_STRING_ARRAY);
|
||||
Expression expr = new ConstantNode(result).withLookupStrings(
|
||||
names.size() > 1 ? ArrayUtil.toStringArray(names) : ArrayUtilRt.EMPTY_STRING_ARRAY);
|
||||
template.addVariable("", expr, expr, true);
|
||||
|
||||
return template;
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
class C {
|
||||
void f(Object o) {
|
||||
if (o instanceof String) {
|
||||
String o1 = (String) o;
|
||||
String s = (String) o;
|
||||
<caret>
|
||||
o = "";
|
||||
}
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
class X {
|
||||
void foo(Object o) {
|
||||
if (o instanceof String) {
|
||||
String o1 = (String) o;
|
||||
String s = (String) o;
|
||||
<caret>
|
||||
String substring = o.();
|
||||
}
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ import java.io.IOException;
|
||||
class C {
|
||||
void f(Object o) {
|
||||
if (o instanceof IOException) {
|
||||
IOException o1 = (IOException) o;
|
||||
IOException ioException = (IOException) o;
|
||||
<caret>
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
public abstract class A {
|
||||
public void getNodeElements(Object parent) {
|
||||
if (!(parent instanceof NodeInfo)) return;
|
||||
NodeInfo parent1 = (NodeInfo) parent;
|
||||
NodeInfo nodeInfo = (NodeInfo) parent;
|
||||
<caret>
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ class X {
|
||||
if (o instanceof String) {
|
||||
String a;
|
||||
String b;
|
||||
String o1 = (String) o;
|
||||
String s = (String) o;
|
||||
<caret>
|
||||
|
||||
String c;
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
class C {
|
||||
void f(Object o, Object f) {
|
||||
if (o instanceof String && f == null) {
|
||||
String o1 = (String) o;
|
||||
String s = (String) o;
|
||||
<caret>
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
class C {
|
||||
void f(Object o, Object f) {
|
||||
if (o instanceof String) {//todo comment
|
||||
String o1 = (String) o;
|
||||
String s = (String) o;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
class C {
|
||||
void f(Object o) {
|
||||
if (o instanceof Runnable) {
|
||||
Runnable o1 = (Runnable) o;
|
||||
Runnable runnable = (Runnable) o;
|
||||
<caret>
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -1,11 +1,11 @@
|
||||
// "Insert '(String)x' declaration" "true"
|
||||
|
||||
class C {
|
||||
Object x = new Object();
|
||||
Object s = new Object();
|
||||
|
||||
void x() {
|
||||
if (x instanceof String) {
|
||||
String x = (String) this.x;
|
||||
String s1 = (String) x;
|
||||
<caret>
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
// "Insert '(String)x' declaration" "true"
|
||||
|
||||
class C {
|
||||
Object x = new Object();
|
||||
Object s = new Object();
|
||||
|
||||
void x() {
|
||||
if (x insta<caret>nceof String) {
|
||||
|
||||
+14
-4
@@ -11,10 +11,7 @@ import com.intellij.psi.codeStyle.VariableKind;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* A convenience helper class to generate unique name for new variable. To use it, call several by* methods in chain, then call
|
||||
@@ -109,4 +106,17 @@ public final class VariableNameGenerator {
|
||||
}
|
||||
return suffixed;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public List<String> generateAll(boolean lookForward) {
|
||||
List<String> suffixed = new ArrayList<>();
|
||||
List<String> result = new ArrayList<>();
|
||||
for (String candidate : candidates.isEmpty() ? Collections.singleton("v") : candidates) {
|
||||
String name = myManager.suggestUniqueVariableName(candidate, myContext, lookForward);
|
||||
if (name.equals(candidate)) result.add(name);
|
||||
else suffixed.add(name);
|
||||
}
|
||||
result.addAll(suffixed);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user