[java-intentions] Better messages for wrap and adapt intentions

GitOrigin-RevId: f0a38046a71722f48f3ff1d59ea501259139237e
This commit is contained in:
Tagir Valeev
2022-08-03 11:19:45 +02:00
committed by intellij-monorepo-bot
parent a277e44614
commit 47924b8886
19 changed files with 60 additions and 32 deletions

View File

@@ -6,6 +6,8 @@ import com.intellij.codeInsight.daemon.impl.HighlightInfo;
import com.intellij.codeInsight.daemon.impl.quickfix.QuickFixAction;
import com.intellij.codeInsight.daemon.impl.quickfix.ReplaceExpressionAction;
import com.intellij.codeInsight.daemon.impl.quickfix.AddTypeCastFix;
import com.intellij.codeInsight.daemon.impl.quickfix.WrapExpressionFix;
import com.intellij.codeInsight.daemon.impl.quickfix.WrapWithAdapterMethodCallFix;
import com.intellij.codeInsight.intention.QuickFixFactory;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
@@ -116,9 +118,11 @@ class AdaptExpressionTypeFixUtil {
static void registerExpectedTypeFixes(@NotNull HighlightInfo info, @NotNull PsiExpression expression, @Nullable PsiType expectedType) {
if (expectedType == null) return;
expectedType = GenericsUtil.getVariableTypeByExpressionType(expectedType);
QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createWrapWithAdapterFix(expectedType, expression));
TextRange range = expression.getTextRange();
String role = info.startOffset == range.getStartOffset() && info.endOffset == range.getEndOffset() ? null : getRole(expression);
QuickFixAction.registerQuickFixAction(info, new WrapWithAdapterMethodCallFix(expectedType, expression, role));
QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createWrapWithOptionalFix(expectedType, expression));
QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createWrapExpressionFix(expectedType, expression));
QuickFixAction.registerQuickFixAction(info, new WrapExpressionFix(expectedType, expression, role));
PsiType argType = expression.getType();
if (expression instanceof PsiMethodCallExpression) {
JavaResolveResult result = ((PsiMethodCallExpression)expression).resolveMethodGenerics();
@@ -128,8 +132,6 @@ class AdaptExpressionTypeFixUtil {
}
PsiType castToType = suggestCastTo(expectedType, argType);
if (castToType != null) {
TextRange range = expression.getTextRange();
String role = info.startOffset == range.getStartOffset() && info.endOffset == range.getEndOffset() ? null : getRole(expression);
QuickFixAction.registerQuickFixAction(info, new AddTypeCastFix(castToType, expression, role));
}
if (expression instanceof PsiMethodCallExpression) {

View File

@@ -30,22 +30,29 @@ public class WrapExpressionFix implements IntentionAction {
private static final Logger LOG = Logger.getInstance(WrapExpressionFix.class);
private final PsiExpression myExpression;
private final @Nullable String myRole;
private final PsiClassType myExpectedType;
private final boolean myPrimitiveExpected;
private final String myMethodPresentation;
public WrapExpressionFix(@NotNull PsiType expectedType, @NotNull PsiExpression expression) {
public WrapExpressionFix(@NotNull PsiType expectedType, @NotNull PsiExpression expression, @Nullable String role) {
myExpression = expression;
myRole = role;
myExpectedType = getClassType(expectedType, expression);
myPrimitiveExpected = expectedType instanceof PsiPrimitiveType;
myMethodPresentation = getMethodPresentation(myExpression, myExpectedType, myPrimitiveExpected);
}
private WrapExpressionFix(PsiExpression expression, PsiClassType expectedType, boolean primitiveExpected, String methodPresentation) {
private WrapExpressionFix(PsiExpression expression,
PsiClassType expectedType,
boolean primitiveExpected,
String methodPresentation,
@Nullable String role) {
myExpression = expression;
myExpectedType = expectedType;
myPrimitiveExpected = primitiveExpected;
myMethodPresentation = methodPresentation;
myRole = role;
}
@Nullable
@@ -62,7 +69,8 @@ public class WrapExpressionFix implements IntentionAction {
@Override
@NotNull
public String getText() {
return QuickFixBundle.message("wrap.expression.using.static.accessor.text", myMethodPresentation);
return myRole == null ? QuickFixBundle.message("wrap.expression.using.static.accessor.text", myMethodPresentation) :
QuickFixBundle.message("wrap.expression.using.static.accessor.text.role", myMethodPresentation, myRole);
}
private static String getMethodPresentation(PsiExpression expression, PsiClassType expectedType, boolean primitiveExpected) {
@@ -196,7 +204,7 @@ public class WrapExpressionFix implements IntentionAction {
}
if (expectedType != null) {
QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, new WrapExpressionFix(expectedType, expr));
QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, new WrapExpressionFix(expectedType, expr, null));
}
}
@@ -205,6 +213,7 @@ public class WrapExpressionFix implements IntentionAction {
return new WrapExpressionFix(PsiTreeUtil.findSameElementInCopy(myExpression, target),
myExpectedType,
myPrimitiveExpected,
myMethodPresentation);
myMethodPresentation,
myRole);
}
}

View File

@@ -240,18 +240,23 @@ public final class WrapWithAdapterMethodCallFix extends LocalQuickFixAndIntentio
}
@SafeFieldForPreview
@Nullable private final PsiType myType;
private final @Nullable PsiType myType;
@SafeFieldForPreview
@Nullable private final AbstractWrapper myWrapper;
private final @Nullable AbstractWrapper myWrapper;
private final @Nullable String myRole;
public WrapWithAdapterMethodCallFix(@Nullable PsiType type, @NotNull PsiExpression expression) {
this(type, expression, ContainerUtil.find(WRAPPERS, w -> w.isApplicable(expression, expression.getType(), type)));
public WrapWithAdapterMethodCallFix(@Nullable PsiType type, @NotNull PsiExpression expression, @Nullable String role) {
this(type, expression, ContainerUtil.find(WRAPPERS, w -> w.isApplicable(expression, expression.getType(), type)), role);
}
private WrapWithAdapterMethodCallFix(@Nullable PsiType type, @NotNull PsiExpression expression, @Nullable AbstractWrapper wrapper) {
private WrapWithAdapterMethodCallFix(@Nullable PsiType type,
@NotNull PsiExpression expression,
@Nullable AbstractWrapper wrapper,
@Nullable String role) {
super(expression);
myType = type;
myWrapper = wrapper;
myRole = role;
}
@Nls
@@ -262,7 +267,9 @@ public final class WrapWithAdapterMethodCallFix extends LocalQuickFixAndIntentio
if (wrapperText == null) {
return getFamilyName();
}
return QuickFixBundle.message("wrap.with.adapter.text", wrapperText);
return myRole == null ?
QuickFixBundle.message("wrap.with.adapter.text", wrapperText) :
QuickFixBundle.message("wrap.with.adapter.text.role", wrapperText, myRole);
}
@Nls

View File

@@ -35,11 +35,17 @@ add.typecast.cast.text=Cast {1} to ''{0}''
# {1} = one of fix.expression.role.xyz values
add.typecast.convert.text=Convert {1} to ''{0}''
# use object (accusative) case in translation, if applicable
fix.expression.role.qualifier=qualifier
# use object (accusative) case in translation, if applicable
fix.expression.role.literal=literal
# use object (accusative) case in translation, if applicable
fix.expression.role.expression=expression
# use object (accusative) case in translation, if applicable
fix.expression.role.argument=argument
# use object (accusative) case in translation, if applicable
fix.expression.role.nth.argument={0, choice, 1#1st|2#2nd|3#3rd|4#{0,number}th} argument
# use object (accusative) case in translation, if applicable
fix.expression.role.lambda.return=lambda return
add.docTag.to.custom.tags=Add ''@{0}'' to custom tags
@@ -209,6 +215,8 @@ fix.receiver.parameter.name.family=Fix the name of the receiver parameter
# Sample: Boolean b = "true"; -> Boolean b = Boolean.valueOf("true");
wrap.expression.using.static.accessor.family=Wrap expression
wrap.expression.using.static.accessor.text=Wrap using ''{0}()''
# {1} = one of fix.expression.role.xyz values
wrap.expression.using.static.accessor.text.role=Wrap {1} using ''{0}()''
# {0} - qualified class name suggested to be imported.
side.effect.action.remove=&Remove
@@ -342,6 +350,8 @@ insert.sam.method.call.fix.family.name=Insert single abstract method call
wrap.with.adapter.call.family.name=Adapt using call or new object
wrap.with.adapter.text=Adapt using ''{0}''
# {1} = one of fix.expression.role.xyz values
wrap.with.adapter.text.role=Adapt {1} using ''{0}''
wrap.with.adapter.parameter.single.text=Adapt argument using ''{0}''
wrap.with.adapter.parameter.multiple.text=Adapt {0, choice, 1#1st|2#2nd|3#3rd|4#{0,number}th} argument using ''{1}''

View File

@@ -260,7 +260,7 @@ public final class QuickFixFactoryImpl extends QuickFixFactory {
@NotNull
@Override
public IntentionAction createWrapExpressionFix(@NotNull PsiType type, @NotNull PsiExpression expression) {
return new WrapExpressionFix(type, expression);
return new WrapExpressionFix(type, expression, null);
}
@NotNull
@@ -922,7 +922,7 @@ public final class QuickFixFactoryImpl extends QuickFixFactory {
@NotNull
@Override
public IntentionAction createWrapWithAdapterFix(@Nullable PsiType type, @NotNull PsiExpression expression) {
return new WrapWithAdapterMethodCallFix(type, expression);
return new WrapWithAdapterMethodCallFix(type, expression, null);
}
@NotNull

View File

@@ -1,4 +1,4 @@
// "Wrap using 'String.valueOf()'" "true-preview"
// "Wrap 2nd argument using 'String.valueOf()'" "true-preview"
import java.util.*;
class Demo {

View File

@@ -1,4 +1,4 @@
// "Wrap using 'String.valueOf()'" "true-preview"
// "Wrap argument using 'String.valueOf()'" "true-preview"
import java.util.*;
class Demo {

View File

@@ -1,4 +1,4 @@
// "Adapt using 'new File()'" "true-preview"
// "Adapt argument using 'new File()'" "true-preview"
import java.util.*;
import java.io.File;

View File

@@ -1,4 +1,4 @@
// "Wrap using 'String.valueOf()'" "true-preview"
// "Wrap argument using 'String.valueOf()'" "true-preview"
import java.util.*;
class Demo {

View File

@@ -1,4 +1,4 @@
// "Wrap using 'String.valueOf()'" "true-preview"
// "Wrap lambda return using 'String.valueOf()'" "true-preview"
import java.util.stream.*;
class Demo {

View File

@@ -1,4 +1,4 @@
// "Adapt using 'Math.toIntExact()'" "true-preview"
// "Adapt argument using 'Math.toIntExact()'" "true-preview"
import java.util.*;
class Demo {

View File

@@ -1,4 +1,4 @@
// "Adapt using 'Math.toIntExact()'" "true-preview"
// "Adapt argument using 'Math.toIntExact()'" "true-preview"
import java.util.*;
class Demo {

View File

@@ -1,4 +1,4 @@
// "Wrap using 'String.valueOf()'" "true-preview"
// "Wrap 2nd argument using 'String.valueOf()'" "true-preview"
import java.util.*;
class Demo {

View File

@@ -1,4 +1,4 @@
// "Wrap using 'String.valueOf()'" "true-preview"
// "Wrap argument using 'String.valueOf()'" "true-preview"
import java.util.*;
class Demo {

View File

@@ -1,4 +1,4 @@
// "Adapt using 'new File()'" "true-preview"
// "Adapt argument using 'new File()'" "true-preview"
import java.util.*;
import java.io.File;

View File

@@ -1,4 +1,4 @@
// "Wrap using 'String.valueOf()'" "true-preview"
// "Wrap argument using 'String.valueOf()'" "true-preview"
import java.util.*;
class Demo {

View File

@@ -1,4 +1,4 @@
// "Wrap using 'String.valueOf()'" "true-preview"
// "Wrap lambda return using 'String.valueOf()'" "true-preview"
import java.util.stream.*;
class Demo {

View File

@@ -1,4 +1,4 @@
// "Adapt using 'Math.toIntExact()'" "true-preview"
// "Adapt argument using 'Math.toIntExact()'" "true-preview"
import java.util.*;
class Demo {

View File

@@ -1,4 +1,4 @@
// "Adapt using 'Math.toIntExact()'" "true-preview"
// "Adapt argument using 'Math.toIntExact()'" "true-preview"
import java.util.*;
class Demo {