[java-intentions] IDEA-281930 Add intention preview to "Generate overloaded method with default parameter values"

GitOrigin-RevId: eeb962179b247dec1f55c452c1dcaeffad649227
This commit is contained in:
Tagir Valeev
2021-11-12 20:34:32 +07:00
committed by intellij-monorepo-bot
parent a259330771
commit d9160635a7
14 changed files with 54 additions and 20 deletions

View File

@@ -385,7 +385,11 @@ public abstract class CreateFromUsageBaseFix extends BaseIntentionAction {
final TemplateEditingListener listener,
final @NlsContexts.Command String commandName) {
Runnable runnable = () -> TemplateManager.getInstance(project).startTemplate(editor, template, listener);
CommandProcessor.getInstance().executeCommand(project, runnable, commandName, commandName);
if (!ApplicationManager.getApplication().isWriteThread()) {
runnable.run();
} else {
CommandProcessor.getInstance().executeCommand(project, runnable, commandName, commandName);
}
}
@Override

View File

@@ -47,6 +47,7 @@ import com.intellij.refactoring.util.RefactoringUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.containers.ContainerUtil;
import com.siyeh.ig.psiutils.TypeUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -110,13 +111,15 @@ public class DefineParamsDefaultValueAction extends PsiElementBaseIntentionActio
if (containingClass == null) return;
final PsiMethod existingMethod = containingClass.findMethodBySignature(methodPrototype, false);
if (existingMethod != null) {
editor.getCaretModel().moveToOffset(existingMethod.getTextOffset());
HintManager.getInstance().showErrorHint(editor,
JavaBundle.message("default.param.value.warning", existingMethod.isConstructor() ? 0 : 1));
if (containingClass.isPhysical()) {
editor.getCaretModel().moveToOffset(existingMethod.getTextOffset());
HintManager.getInstance().showErrorHint(editor,
JavaBundle.message("default.param.value.warning", existingMethod.isConstructor() ? 0 : 1));
}
return;
}
if (!FileModificationService.getInstance().preparePsiElementForWrite(element)) return;
if (element.isPhysical() && !FileModificationService.getInstance().preparePsiElementForWrite(element)) return;
Runnable runnable = () -> {
final PsiMethod prototype = (PsiMethod)containingClass.addBefore(methodPrototype, method);
@@ -126,7 +129,7 @@ public class DefineParamsDefaultValueAction extends PsiElementBaseIntentionActio
PsiCodeBlock body = prototype.getBody();
final String callArgs =
"(" + StringUtil.join(parameterList.getParameters(), psiParameter -> {
if (ArrayUtil.find(parameters, psiParameter) > -1) return "IntelliJIDEARulezzz";
if (ArrayUtil.find(parameters, psiParameter) > -1) return TypeUtils.getDefaultValue(psiParameter.getType());
return psiParameter.getName();
}, ",") + ");";
final String methodCall;
@@ -156,13 +159,19 @@ public class DefineParamsDefaultValueAction extends PsiElementBaseIntentionActio
startTemplate(project, editor, toDefaults, prototype);
}
};
if (startInWriteAction()) {
if (startInWriteAction() || !containingClass.isPhysical()) {
runnable.run();
} else {
ApplicationManager.getApplication().runWriteAction(runnable);
}
}
@Override
public boolean invokeForPreview(@NotNull Project project, Editor editor, PsiFile file) {
invoke(project, editor, file);
return true;
}
public static void startTemplate(@NotNull Project project,
Editor editor,
PsiExpression[] argsToBeDelegated,
@@ -170,7 +179,7 @@ public class DefineParamsDefaultValueAction extends PsiElementBaseIntentionActio
TemplateBuilderImpl builder = new TemplateBuilderImpl(delegateMethod);
RangeMarker rangeMarker = editor.getDocument().createRangeMarker(delegateMethod.getTextRange());
for (final PsiExpression exprToBeDefault : argsToBeDelegated) {
builder.replaceElement(exprToBeDefault, new TextExpression(""));
builder.replaceElement(exprToBeDefault, new TextExpression(exprToBeDefault.getText()));
}
Template template = builder.buildTemplate();
editor.getCaretModel().moveToOffset(rangeMarker.getStartOffset());
@@ -185,7 +194,7 @@ public class DefineParamsDefaultValueAction extends PsiElementBaseIntentionActio
private static PsiParameter @Nullable [] getParams(@NotNull PsiElement element, @NotNull PsiParameterList parameterList) {
final PsiParameter[] parameters = parameterList.getParameters();
if (parameters.length == 1) {
if (parameters.length == 1 || !parameterList.isPhysical()) {
return parameters;
}
final ParameterClassMember[] members = new ParameterClassMember[parameters.length];

View File

@@ -1,7 +1,7 @@
// "Generate overloaded method with default parameter values" "true"
class Test {
void foo() {
foo(<caret>);
foo(<selection>0<caret></selection>);
}
void foo(int ii){

View File

@@ -1,7 +1,7 @@
// "Generate overloaded method with default parameter values" "true"
abstract class Test {
int foo(boolean... args) {
return foo(<caret>, args);
return foo(<selection>0<caret></selection>, args);
}
abstract int foo(int ii, boolean... args);

View File

@@ -1,7 +1,7 @@
// "Generate overloaded method with default parameter values" "true"
class Test {
int foo() {
return foo();
return foo(0);
}
int foo(int ii){

View File

@@ -1,7 +1,7 @@
// "Generate overloaded constructor with default parameter values" "true"
class Test {
Test() {
this(<caret>);
this(<selection>0<caret></selection>);
}
Test(int ii){}

View File

@@ -1,7 +1,7 @@
// "Generate overloaded method with default parameter values" "true"
interface Test {
default void foo() {
foo();
foo(0);
}
void foo(int ii);

View File

@@ -3,7 +3,7 @@ class Test {
/**
*/
void foo() {
foo();
foo(0);
}
/**

View File

@@ -1,7 +1,7 @@
// "Generate overloaded constructor with default parameter values" "true"
record Test(int x) {
Test() {
this();
this(0);
}
public Test {

View File

@@ -1,7 +1,7 @@
// "Generate overloaded method with default parameter values" "true"
class Test {
int foo() {
return foo(<caret>);
return foo(<selection>0<caret></selection>);
}
int foo(int ii){

View File

@@ -1,7 +1,7 @@
// "Generate overloaded method with default parameter values" "true"
interface Test {
static void foo() {
foo();
foo(0);
}
static void foo(int ii) {}

View File

@@ -1,7 +1,7 @@
// "Generate overloaded method with default parameter values" "true"
class Test {
<T> int foo(boolean... args) {
return foo(<caret>, args);
return foo(<selection>null<caret></selection>, args);
}
<T> int foo(T ii, boolean... args){

View File

@@ -1,7 +1,7 @@
// "Generate overloaded method with default parameter values" "true"
class Test {
int foo(boolean... args) {
return foo(<caret>, args);
return foo(<selection>0<caret></selection>, args);
}
int foo(int ii, boolean... args){

View File

@@ -99,6 +99,27 @@ public class IntentionPreviewTest extends LightQuickFixTestCase {
"}\n", text);
}
public void testDefineDefaultValues() {
configureFromFileText("Test.java",
"public class Test {\n" +
" void test(int <caret>a, String b) {\n" +
"\n" +
" }\n" +
"}\n");
IntentionAction action = findActionWithText("Generate overloaded method with default parameter values");
assertNotNull(action);
String text = getPreviewText(action);
assertEquals("public class Test {\n" +
" void test() {\n" +
" test(0, null);\n" +
" }\n" +
"\n" +
" void test(int a, String b) {\n" +
"\n" +
" }\n" +
"}\n", text);
}
@Override
protected void setupEditorForInjectedLanguage() {
// we want to stay at host editor