mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-20 21:41:24 +07:00
ConvertToLocalInspection: changed quick-fix to explicitly show type of code block where field is used (IDEA-CR-42222)
This commit is contained in:
@@ -38,7 +38,6 @@ import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class FieldCanBeLocalInspection extends AbstractBaseJavaLocalInspectionTool {
|
||||
@NonNls public static final String SHORT_NAME = "FieldCanBeLocal";
|
||||
@@ -376,22 +375,30 @@ public class FieldCanBeLocalInspection extends AbstractBaseJavaLocalInspectionTo
|
||||
private static class ConvertFieldToLocalQuickFix extends BaseConvertToLocalQuickFix<PsiField> {
|
||||
private final String myName;
|
||||
|
||||
private ConvertFieldToLocalQuickFix(Map<PsiCodeBlock, Collection<PsiReference>> refs) {
|
||||
Set<PsiMethod> methods = refs.keySet().stream()
|
||||
.filter(block -> block.getParent() instanceof PsiMethod)
|
||||
.map(block -> (PsiMethod)block.getParent())
|
||||
.collect(Collectors.toSet());
|
||||
private ConvertFieldToLocalQuickFix(@NotNull Map<PsiCodeBlock, Collection<PsiReference>> refs) {
|
||||
final Set<PsiCodeBlock> blocks = refs.keySet();
|
||||
final PsiElement block;
|
||||
if (blocks.size() == 1) {
|
||||
block =
|
||||
PsiTreeUtil.getNonStrictParentOfType(blocks.toArray(PsiCodeBlock.EMPTY_ARRAY)[0], PsiClassInitializer.class, PsiMethod.class);
|
||||
}
|
||||
else {
|
||||
block = null;
|
||||
}
|
||||
|
||||
myName = determineName(methods, refs);
|
||||
myName = determineName(block);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String determineName(Set<PsiMethod> methods, Map<PsiCodeBlock, Collection<PsiReference>> refs) {
|
||||
if (methods.isEmpty() || methods.size() != refs.size()) return getFamilyName();
|
||||
if (methods.size() > 1) return InspectionsBundle.message("inspection.field.can.be.local.quickfix.multiple.methods", methods.size());
|
||||
private String determineName(@Nullable PsiElement block) {
|
||||
if (block instanceof PsiClassInitializer) return InspectionsBundle.message("inspection.field.can.be.local.quickfix.initializer");
|
||||
|
||||
final String methodName = methods.toArray(PsiMethod.EMPTY_ARRAY)[0].getName();
|
||||
return InspectionsBundle.message("inspection.field.can.be.local.quickfix.one.method", methodName);
|
||||
if (block instanceof PsiMethod) {
|
||||
if (((PsiMethod)block).isConstructor()) return InspectionsBundle.message("inspection.field.can.be.local.quickfix.constructor");
|
||||
return InspectionsBundle.message("inspection.field.can.be.local.quickfix.one.method", ((PsiMethod)block).getName());
|
||||
}
|
||||
|
||||
return getFamilyName();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Convert field to local variable in method 'IntelliJBugConvertToLocal'" "true"
|
||||
// "Convert field to local variable in constructor" "true"
|
||||
import java.util.ArrayList;
|
||||
|
||||
class ITest {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Convert field to local variable in method 'Test'" "true"
|
||||
// "Convert field to local variable in constructor" "true"
|
||||
class Test {
|
||||
|
||||
public Test(String param) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Convert to local" "true"
|
||||
// "Convert field to local variable in initializer section" "true"
|
||||
class TestInitializer {
|
||||
|
||||
{
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Convert field to local variables in 2 methods" "true"
|
||||
// "Convert to local" "true"
|
||||
class Test {
|
||||
|
||||
int getFoo1() {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Convert field to local variable in method 'FieldCanBeLocalTest'" "true"
|
||||
// "Convert field to local variable in constructor" "true"
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Convert field to local variable in method 'IntelliJBugConvertToLocal'" "true"
|
||||
// "Convert field to local variable in constructor" "true"
|
||||
import java.util.ArrayList;
|
||||
|
||||
class ITest {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Convert field to local variable in method 'Test'" "true"
|
||||
// "Convert field to local variable in constructor" "true"
|
||||
class Test {
|
||||
|
||||
private final String f<caret>ield;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Convert to local" "true"
|
||||
// "Convert field to local variable in initializer section" "true"
|
||||
class TestInitializer {
|
||||
|
||||
private boolean fie<caret>ld;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Convert field to local variables in 2 methods" "true"
|
||||
// "Convert to local" "true"
|
||||
class Test {
|
||||
private int my<caret>Foo;
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Convert to local" "false"
|
||||
// "Fix all 'Field can be local' problems in file" "false"
|
||||
class MyClassTest {
|
||||
|
||||
private boolean edit<caret>able = false;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Convert field to local variable in method 'FieldCanBeLocalTest'" "true"
|
||||
// "Convert field to local variable in constructor" "true"
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Convert to local" "false"
|
||||
// "Fix all 'Field can be local' problems in file" "false"
|
||||
class TestFieldConversion
|
||||
{
|
||||
private static int som<caret>eInt = 0;
|
||||
|
||||
@@ -196,7 +196,8 @@ inspection.field.can.be.local.problem.descriptor=Field can be converted to a loc
|
||||
inspection.parameter.can.be.local.display.name=Parameter can be local
|
||||
inspection.parameter.can.be.local.problem.descriptor=Parameter can be converted to a local variable
|
||||
inspection.convert.to.local.quickfix=Convert to local
|
||||
inspection.field.can.be.local.quickfix.multiple.methods=Convert field to local variables in {0} methods
|
||||
inspection.field.can.be.local.quickfix.initializer=Convert field to local variable in initializer section
|
||||
inspection.field.can.be.local.quickfix.constructor=Convert field to local variable in constructor
|
||||
inspection.field.can.be.local.quickfix.one.method=Convert field to local variable in method ''{0}''
|
||||
|
||||
inspection.unused.return.value.display.name=Method can be void
|
||||
|
||||
Reference in New Issue
Block a user