Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public void invoke(@Nonnull Project project, Editor editor, PsiFile file) {
return;
}

PsiMethod method = SuperMethodWarningUtil.checkSuperMethod(myTargetMethod, RefactoringLocalize.toRefactor().get());
PsiMethod method = SuperMethodWarningUtil.checkSuperMethod(myTargetMethod, RefactoringLocalize.toRefactor());
if (method == null) {
return;
}
Expand Down Expand Up @@ -457,8 +457,8 @@ else if (expressions.length > parameters.length) {
if (exprType == null || PsiType.VOID.equals(exprType)) {
return null;
}
if (exprType instanceof PsiDisjunctionType) {
exprType = ((PsiDisjunctionType) exprType).getLeastUpperBound();
if (exprType instanceof PsiDisjunctionType disjunctionType) {
exprType = disjunctionType.getLeastUpperBound();
}
if (!PsiTypesUtil.allTypeParametersResolved(myTargetMethod, exprType)) {
return null;
Expand Down Expand Up @@ -548,8 +548,8 @@ else if (expression != null) {
if (exprType == null || PsiType.VOID.equals(exprType)) {
return false;
}
if (exprType instanceof PsiDisjunctionType) {
exprType = ((PsiDisjunctionType) exprType).getLeastUpperBound();
if (exprType instanceof PsiDisjunctionType disjunctionType) {
exprType = disjunctionType.getLeastUpperBound();
}
if (!PsiTypesUtil.allTypeParametersResolved(myTargetMethod, exprType)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import com.intellij.java.language.psi.*;
import com.intellij.java.language.psi.util.PsiTypesUtil;
import com.intellij.java.language.psi.util.PsiUtil;
import consulo.annotation.access.RequiredWriteAction;
import consulo.application.Application;
import consulo.application.ApplicationManager;
import consulo.application.Result;
import consulo.java.analysis.impl.localize.JavaQuickFixLocalize;
import consulo.language.editor.WriteCommandAction;
Expand All @@ -35,6 +35,7 @@
import consulo.localize.LocalizeValue;
import consulo.logging.Logger;
import consulo.project.Project;
import consulo.ui.annotation.RequiredUIAccess;
import consulo.util.lang.Comparing;

import java.util.ArrayList;
Expand All @@ -45,111 +46,131 @@
* @author Mike
*/
public class CreateParameterFromUsageFix extends CreateVarFromUsageFix {
private static final Logger LOG = Logger.getInstance(CreateParameterFromUsageFix.class);
private static final Logger LOG = Logger.getInstance(CreateParameterFromUsageFix.class);

public CreateParameterFromUsageFix(PsiReferenceExpression referenceElement) {
super(referenceElement);
setText(JavaQuickFixLocalize.createParameterFromUsageFamily());
}
public CreateParameterFromUsageFix(PsiReferenceExpression referenceElement) {
super(referenceElement);
setText(JavaQuickFixLocalize.createParameterFromUsageFamily());
}

@Override
protected boolean isAvailableImpl(int offset) {
if (!super.isAvailableImpl(offset)) return false;
if(myReferenceExpression.isQualified()) return false;
PsiElement scope = myReferenceExpression;
do {
scope = PsiTreeUtil.getParentOfType(scope, PsiMethod.class, PsiClass.class);
if (!(scope instanceof PsiAnonymousClass)) {
return scope instanceof PsiMethod &&
((PsiMethod)scope).getParameterList().isPhysical();
}
@Override
protected boolean isAvailableImpl(int offset) {
if (!super.isAvailableImpl(offset)) {
return false;
}
if (myReferenceExpression.isQualified()) {
return false;
}
PsiElement scope = myReferenceExpression;
do {
scope = PsiTreeUtil.getParentOfType(scope, PsiMethod.class, PsiClass.class);
if (!(scope instanceof PsiAnonymousClass)) {
return scope instanceof PsiMethod method
&& method.getParameterList().isPhysical();
}
}
while (true);
}
while (true);
}

@Override
public LocalizeValue getText(String varName) {
return JavaQuickFixLocalize.createParameterFromUsageText(varName);
}
@Override
public LocalizeValue getText(String varName) {
return JavaQuickFixLocalize.createParameterFromUsageText(varName);
}

@Override
protected void invokeImpl(PsiClass targetClass) {
if (CreateFromUsageUtils.isValidReference(myReferenceExpression, false)) return;
@Override
@RequiredUIAccess
protected void invokeImpl(PsiClass targetClass) {
if (CreateFromUsageUtils.isValidReference(myReferenceExpression, false)) {
return;
}

final Project project = myReferenceExpression.getProject();
final Project project = myReferenceExpression.getProject();

PsiType[] expectedTypes = CreateFromUsageUtils.guessType(myReferenceExpression, false);
PsiType type = expectedTypes[0];
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 = IntroduceParameterHandler.chooseEnclosingMethod(method);
if (method == null) return;
String varName = myReferenceExpression.getReferenceName();
PsiMethod method = PsiTreeUtil.getParentOfType(myReferenceExpression, PsiMethod.class);
LOG.assertTrue(method != null);
method = IntroduceParameterHandler.chooseEnclosingMethod(method);
if (method == null) {
return;
}

method = SuperMethodWarningUtil.checkSuperMethod(method, RefactoringLocalize.toRefactor().get());
if (method == null) return;
method = SuperMethodWarningUtil.checkSuperMethod(method, RefactoringLocalize.toRefactor());
if (method == null) {
return;
}

final List<ParameterInfoImpl> parameterInfos =
new ArrayList<ParameterInfoImpl>(Arrays.asList(ParameterInfoImpl.fromMethod(method)));
ParameterInfoImpl parameterInfo = new ParameterInfoImpl(-1, varName, type, PsiTypesUtil.getDefaultValueOfType(type), false);
if (!method.isVarArgs()) {
parameterInfos.add(parameterInfo);
}
else {
parameterInfos.add(parameterInfos.size() - 1, parameterInfo);
}
List<ParameterInfoImpl> parameterInfos = new ArrayList<>(Arrays.asList(ParameterInfoImpl.fromMethod(method)));
ParameterInfoImpl parameterInfo = new ParameterInfoImpl(-1, varName, type, PsiTypesUtil.getDefaultValueOfType(type), false);
if (!method.isVarArgs()) {
parameterInfos.add(parameterInfo);
}
else {
parameterInfos.add(parameterInfos.size() - 1, parameterInfo);
}

Application application = ApplicationManager.getApplication();
if (application.isUnitTestMode()) {
ParameterInfoImpl[] array = parameterInfos.toArray(new ParameterInfoImpl[parameterInfos.size()]);
String modifier = PsiUtil.getAccessModifier(PsiUtil.getAccessLevel(method.getModifierList()));
ChangeSignatureProcessor processor =
new ChangeSignatureProcessor(project, method, false, modifier, method.getName(), method.getReturnType(), array);
processor.run();
}
else {
final PsiMethod finalMethod = method;
application.invokeLater(new Runnable() {
@Override
public void run() {
if (project.isDisposed()) return;
try {
JavaChangeSignatureDialog dialog = JavaChangeSignatureDialog.createAndPreselectNew(project, finalMethod, parameterInfos, true, myReferenceExpression);
dialog.setParameterInfos(parameterInfos);
dialog.show();
if (dialog.isOK()) {
for (ParameterInfoImpl info : parameterInfos) {
if (info.getOldIndex() == -1) {
String newParamName = info.getName();
if (!Comparing.strEqual(varName, newParamName)) {
final PsiExpression newExpr = JavaPsiFacade.getElementFactory(project).createExpressionFromText(newParamName, finalMethod);
new WriteCommandAction(project){
@Override
protected void run(Result result) throws Throwable {
PsiReferenceExpression[] refs =
CreateFromUsageUtils.collectExpressions(myReferenceExpression, PsiMember.class, PsiFile.class);
for (PsiReferenceExpression ref : refs) {
ref.replace(newExpr.copy());
Application application = Application.get();
if (application.isUnitTestMode()) {
ParameterInfoImpl[] array = parameterInfos.toArray(new ParameterInfoImpl[parameterInfos.size()]);
String modifier = PsiUtil.getAccessModifier(PsiUtil.getAccessLevel(method.getModifierList()));
ChangeSignatureProcessor processor =
new ChangeSignatureProcessor(project, method, false, modifier, method.getName(), method.getReturnType(), array);
processor.run();
}
else {
PsiMethod finalMethod = method;
application.invokeLater(() -> {
if (project.isDisposed()) {
return;
}
try {
JavaChangeSignatureDialog dialog = JavaChangeSignatureDialog.createAndPreselectNew(
project,
finalMethod,
parameterInfos,
true,
myReferenceExpression
);
dialog.setParameterInfos(parameterInfos);
dialog.show();
if (dialog.isOK()) {
for (ParameterInfoImpl info : parameterInfos) {
if (info.getOldIndex() == -1) {
String newParamName = info.getName();
if (!Comparing.strEqual(varName, newParamName)) {
final PsiExpression newExpr =
JavaPsiFacade.getElementFactory(project).createExpressionFromText(newParamName, finalMethod);
new WriteCommandAction(project) {
@Override
@RequiredWriteAction
protected void run(Result result) throws Throwable {
PsiReferenceExpression[] refs = CreateFromUsageUtils.collectExpressions(
myReferenceExpression,
PsiMember.class,
PsiFile.class
);
for (PsiReferenceExpression ref : refs) {
ref.replace(newExpr.copy());
}
}
}.execute();
}
break;
}
}
}
}.execute();
}
break;
}
}
}
}
}
catch (Exception e) {
throw new RuntimeException(e);
}
catch (Exception e) {
throw new RuntimeException(e);
}
});
}
});
}
}

@Override
protected boolean isAllowOuterTargetClass() {
return false;
}
@Override
protected boolean isAllowOuterTargetClass() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@
import consulo.localize.LocalizeValue;
import consulo.logging.Logger;
import consulo.project.Project;
import consulo.ui.annotation.RequiredUIAccess;
import consulo.usage.UsageViewUtil;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class VariableTypeFix extends LocalQuickFixAndIntentionActionOnPsiElement {
static final Logger LOG = Logger.getInstance(VariableTypeFix.class);
Expand Down Expand Up @@ -121,46 +123,42 @@ protected void run() throws Throwable {
}.execute();
}

@RequiredUIAccess
private boolean changeMethodSignatureIfNeeded(PsiVariable myVariable) {
if (myVariable instanceof PsiParameter) {
PsiElement scope = ((PsiParameter) myVariable).getDeclarationScope();
if (scope instanceof PsiMethod) {
PsiMethod method = (PsiMethod) scope;
PsiMethod psiMethod = SuperMethodWarningUtil.checkSuperMethod(method, RefactoringLocalize.toRefactor().get());
if (psiMethod == null) {
return true;
}
int parameterIndex = method.getParameterList().getParameterIndex((PsiParameter) myVariable);
if (!FileModificationService.getInstance().prepareFileForWrite(psiMethod.getContainingFile())) {
return true;
}
ArrayList<ParameterInfoImpl> infos = new ArrayList<ParameterInfoImpl>();
int i = 0;
for (PsiParameter parameter : psiMethod.getParameterList().getParameters()) {
boolean changeType = i == parameterIndex;
infos.add(new ParameterInfoImpl(i++, parameter.getName(), changeType ? getReturnType() : parameter.getType()));
}

if (!ApplicationManager.getApplication().isUnitTestMode()) {
JavaChangeSignatureDialog dialog =
new JavaChangeSignatureDialog(psiMethod.getProject(), psiMethod, false, myVariable);
dialog.setParameterInfos(infos);
dialog.show();
}
else {
ChangeSignatureProcessor processor = new ChangeSignatureProcessor(
psiMethod.getProject(),
psiMethod,
false,
null,
psiMethod.getName(),
psiMethod.getReturnType(),
infos.toArray(new ParameterInfoImpl[infos.size()])
);
processor.run();
}
if (myVariable instanceof PsiParameter param && param.getDeclarationScope() instanceof PsiMethod method) {
PsiMethod psiMethod = SuperMethodWarningUtil.checkSuperMethod(method, RefactoringLocalize.toRefactor());
if (psiMethod == null) {
return true;
}
int parameterIndex = method.getParameterList().getParameterIndex(param);
if (!FileModificationService.getInstance().prepareFileForWrite(psiMethod.getContainingFile())) {
return true;
}
List<ParameterInfoImpl> infos = new ArrayList<>();
int i = 0;
for (PsiParameter parameter : psiMethod.getParameterList().getParameters()) {
boolean changeType = i == parameterIndex;
infos.add(new ParameterInfoImpl(i++, parameter.getName(), changeType ? getReturnType() : parameter.getType()));
}

if (!param.getApplication().isUnitTestMode()) {
JavaChangeSignatureDialog dialog = new JavaChangeSignatureDialog(psiMethod.getProject(), psiMethod, false, param);
dialog.setParameterInfos(infos);
dialog.show();
}
else {
ChangeSignatureProcessor processor = new ChangeSignatureProcessor(
psiMethod.getProject(),
psiMethod,
false,
null,
psiMethod.getName(),
psiMethod.getReturnType(),
infos.toArray(new ParameterInfoImpl[infos.size()])
);
processor.run();
}
return true;
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public PsiElement[] getSecondaryElements() {
for (PsiMethod accessor : accessors) {
ContainerUtil.addAll(
elements,
SuperMethodWarningUtil.checkSuperMethods(accessor, FindLocalize.findSuperMethodWarningActionVerb().get())
SuperMethodWarningUtil.checkSuperMethods(accessor, FindLocalize.findSuperMethodWarningActionVerb())
);
}
return PsiUtilCore.toPsiElementArray(elements);
Expand Down
Loading
Loading