mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-20 05:21:29 +07:00
Java: intention preview for CreateParameterFromUsageFix
GitOrigin-RevId: 5eaf9006989f3f6d16327a48cc80b26adef5d4f4
This commit is contained in:
committed by
intellij-monorepo-bot
parent
2436b544e9
commit
4a1b49caa9
@@ -1,8 +1,10 @@
|
||||
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.codeInsight.daemon.impl.quickfix;
|
||||
|
||||
import com.intellij.codeInsight.daemon.QuickFixBundle;
|
||||
import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo;
|
||||
import com.intellij.codeInspection.CommonQuickFixBundle;
|
||||
import com.intellij.ide.highlighter.JavaFileType;
|
||||
import com.intellij.ide.util.SuperMethodWarningUtil;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.command.WriteCommandAction;
|
||||
@@ -11,6 +13,7 @@ import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.fileEditor.ex.IdeDocumentHistory;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Comparing;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.JavaElementKind;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
@@ -59,16 +62,25 @@ public class CreateParameterFromUsageFix extends CreateVarFromUsageFix {
|
||||
return currentFile;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull IntentionPreviewInfo generatePreview(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
|
||||
PsiMethod method = PsiTreeUtil.getParentOfType(myReferenceExpression, PsiMethod.class);
|
||||
if (method == null) return IntentionPreviewInfo.EMPTY;
|
||||
List<ParameterInfoImpl> infos = getParameterInfos(method);
|
||||
String newParameters = "(" + StringUtil.join(infos, i -> i.getTypeText() + " " + i.getName(), ", ") + ")";
|
||||
StringBuilder newText = new StringBuilder();
|
||||
for (PsiElement child : method.getChildren()) {
|
||||
newText.append(child instanceof PsiParameterList ? newParameters : child.getText());
|
||||
}
|
||||
return new IntentionPreviewInfo.CustomDiff(JavaFileType.INSTANCE, method.getText(), newText.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(@NotNull Project project, Editor editor, PsiFile file) {
|
||||
if (CreateFromUsageUtils.isValidReference(myReferenceExpression, false)) return;
|
||||
|
||||
IdeDocumentHistory.getInstance(project).includeCurrentPlaceAsChangePlace();
|
||||
|
||||
PsiType[] expectedTypes = CreateFromUsageUtils.guessType(myReferenceExpression, false);
|
||||
PsiType type = expectedTypes[0];
|
||||
|
||||
final String varName = myReferenceExpression.getReferenceName();
|
||||
PsiMethod method = PsiTreeUtil.getParentOfType(myReferenceExpression, PsiMethod.class);
|
||||
LOG.assertTrue(method != null);
|
||||
method = CommonJavaRefactoringUtil.chooseEnclosingMethod(method);
|
||||
@@ -77,16 +89,7 @@ public class CreateParameterFromUsageFix extends CreateVarFromUsageFix {
|
||||
method = SuperMethodWarningUtil.checkSuperMethod(method);
|
||||
if (method == null) return;
|
||||
|
||||
final List<ParameterInfoImpl> parameterInfos =
|
||||
new ArrayList<>(Arrays.asList(ParameterInfoImpl.fromMethod(method)));
|
||||
ParameterInfoImpl parameterInfo = ParameterInfoImpl.createNew().withName(varName).withType(type).withDefaultValue(varName);
|
||||
if (!method.isVarArgs()) {
|
||||
parameterInfos.add(parameterInfo);
|
||||
}
|
||||
else {
|
||||
parameterInfos.add(parameterInfos.size() - 1, parameterInfo);
|
||||
}
|
||||
|
||||
final List<ParameterInfoImpl> parameterInfos = getParameterInfos(method);
|
||||
if (ApplicationManager.getApplication().isUnitTestMode()) {
|
||||
ParameterInfoImpl[] array = parameterInfos.toArray(new ParameterInfoImpl[0]);
|
||||
String modifier = PsiUtil.getAccessModifier(PsiUtil.getAccessLevel(method.getModifierList()));
|
||||
@@ -100,6 +103,7 @@ public class CreateParameterFromUsageFix extends CreateVarFromUsageFix {
|
||||
JavaChangeSignatureDialog.createAndPreselectNew(project, method, parameterInfos, true, myReferenceExpression);
|
||||
dialog.setParameterInfos(parameterInfos);
|
||||
if (dialog.showAndGet()) {
|
||||
final String varName = myReferenceExpression.getReferenceName();
|
||||
for (ParameterInfoImpl info : parameterInfos) {
|
||||
if (info.isNew()) {
|
||||
final String newParamName = info.getName();
|
||||
@@ -125,6 +129,23 @@ public class CreateParameterFromUsageFix extends CreateVarFromUsageFix {
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private List<ParameterInfoImpl> getParameterInfos(PsiMethod method) {
|
||||
final String parameterName = myReferenceExpression.getReferenceName();
|
||||
PsiType[] expectedTypes = CreateFromUsageUtils.guessType(myReferenceExpression, false);
|
||||
final List<ParameterInfoImpl> parameterInfos =
|
||||
new ArrayList<>(Arrays.asList(ParameterInfoImpl.fromMethod(method)));
|
||||
ParameterInfoImpl parameterInfo =
|
||||
ParameterInfoImpl.createNew().withName(parameterName).withType(expectedTypes[0]).withDefaultValue(parameterName);
|
||||
if (!method.isVarArgs()) {
|
||||
parameterInfos.add(parameterInfo);
|
||||
}
|
||||
else {
|
||||
parameterInfos.add(parameterInfos.size() - 1, parameterInfo);
|
||||
}
|
||||
return parameterInfos;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isAllowOuterTargetClass() {
|
||||
return false;
|
||||
@@ -135,5 +156,4 @@ public class CreateParameterFromUsageFix extends CreateVarFromUsageFix {
|
||||
public String getFamilyName() {
|
||||
return QuickFixBundle.message("create.parameter.from.usage.family");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create parameter 'popop'" "true"
|
||||
// "Create parameter 'popop'" "true-preview"
|
||||
class Calculator {
|
||||
public void printError(int popop) {
|
||||
int i = popop;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create parameter 'flag'" "true"
|
||||
// "Create parameter 'flag'" "true-preview"
|
||||
class Foo {
|
||||
void test(Object obj, boolean flag) {
|
||||
switch (obj) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create parameter 'popop'" "true"
|
||||
// "Create parameter 'popop'" "true-preview"
|
||||
class Calculator {
|
||||
public void printError(String detail, int line, String file, String popop) {
|
||||
int i = popop;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create parameter 'popop'" "true"
|
||||
// "Create parameter 'popop'" "true-preview"
|
||||
class Calculator {
|
||||
public void printError(String detail, int line, String file, int popop) {
|
||||
int i = <caret>popop;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create parameter 'flag'" "true"
|
||||
// "Create parameter 'flag'" "true-preview"
|
||||
class Foo {
|
||||
void test(Object obj, boolean flag) {
|
||||
switch (obj) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create parameter 'popop'" "true"
|
||||
// "Create parameter 'popop'" "true-preview"
|
||||
class Calculator {
|
||||
public void printError() {
|
||||
int i = <caret>popop;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create parameter 'flag'" "true"
|
||||
// "Create parameter 'flag'" "true-preview"
|
||||
class Foo {
|
||||
void test(Object obj) {
|
||||
switch (obj) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create parameter 'popop'" "true"
|
||||
// "Create parameter 'popop'" "true-preview"
|
||||
class Calculator {
|
||||
public void printError(String detail, int line, String file) {
|
||||
int i = popop;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create parameter 'popop'" "true"
|
||||
// "Create parameter 'popop'" "true-preview"
|
||||
class Calculator {
|
||||
public void printError(String detail, int line, String file) {
|
||||
int i = <caret>popop;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create parameter 'flag'" "true"
|
||||
// "Create parameter 'flag'" "true-preview"
|
||||
class Foo {
|
||||
void test(Object obj) {
|
||||
switch (obj) {
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
public void printError(int popop) {
|
||||
int i = popop;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
void test(Object obj, boolean flag) {
|
||||
switch (obj) {
|
||||
case String s && flag -> System.out.println(1);
|
||||
default -> System.out.println(2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
public void printError(String detail, int line, String file, String popop) {
|
||||
String s = popop;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
public void printError(String detail, int line, String file, int popop) {
|
||||
int i = popop;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
void test(Object obj, boolean flag) {
|
||||
switch (obj) {
|
||||
case String s when flag -> System.out.println(1);
|
||||
default -> System.out.println(2);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user