[java-refactoring] InlineLocalHandler: fixes after review of IDEA-371649

1. Exclude an option to rename the original variable if the type differs
2. Rename actions


(cherry picked from commit 2fd3bb261163cbd5b0d4af0f986e27250abb0956)

IJ-CR-162628

GitOrigin-RevId: 5d489d1030cf375d2a511be9edc9200a62bd045b
This commit is contained in:
Tagir Valeev
2025-06-18 11:57:39 +02:00
committed by intellij-monorepo-bot
parent 625e18edd4
commit 965bef7bb4
10 changed files with 24 additions and 14 deletions

View File

@@ -257,8 +257,8 @@ public final class InlineLocalHandler extends JavaInlineActionHandler {
} }
if (defToInline == local.getInitializer() && PsiUtil.skipParenthesizedExprDown(defToInline) instanceof PsiReferenceExpression ref && if (defToInline == local.getInitializer() && PsiUtil.skipParenthesizedExprDown(defToInline) instanceof PsiReferenceExpression ref &&
ControlFlowUtil.isEffectivelyFinal(local, containerBlock)) { ControlFlowUtil.isEffectivelyFinal(local, containerBlock)) {
PsiElement target = ref.resolve(); if (ref.resolve() instanceof PsiVariable var && PsiUtil.isJvmLocalVariable(var) &&
if (PsiUtil.isJvmLocalVariable(target)) { var.getType().equals(local.getType())) {
return createRenameChooser(local, refsToInlineList); return createRenameChooser(local, refsToInlineList);
} }
} }
@@ -587,10 +587,9 @@ public final class InlineLocalHandler extends JavaInlineActionHandler {
case ASK -> JavaRefactoringBundle.message("inline.popup.ignore.conflicts"); case ASK -> JavaRefactoringBundle.message("inline.popup.ignore.conflicts");
case INLINE_ONE -> RefactoringBundle.message("inline.popup.this.only"); case INLINE_ONE -> RefactoringBundle.message("inline.popup.this.only");
case INLINE_ALL_AND_DELETE -> RefactoringBundle.message("inline.popup.all", myAllRefs.size()); case INLINE_ALL_AND_DELETE -> RefactoringBundle.message("inline.popup.all", myAllRefs.size());
case INLINE_ALL_KEEP_OLD_NAME -> RefactoringBundle.message("inline.popup.all.keep", requireNonNull( case INLINE_ALL_KEEP_OLD_NAME -> RefactoringBundle.message("inline.popup.all.keep.name", requireNonNull(
PsiUtil.skipParenthesizedExprDown(myVariable.getInitializer())).getText()); PsiUtil.skipParenthesizedExprDown(myVariable.getInitializer())).getText());
case INLINE_ALL_RENAME_INITIALIZER -> RefactoringBundle.message("inline.popup.all.rename", requireNonNull( case INLINE_ALL_RENAME_INITIALIZER -> RefactoringBundle.message("inline.popup.all.keep.name", myVariable.getName());
PsiUtil.skipParenthesizedExprDown(myVariable.getInitializer())).getText(), myVariable.getName());
default -> throw new IllegalStateException("Unexpected value: " + myMode); default -> throw new IllegalStateException("Unexpected value: " + myMode);
}; };
} }

View File

@@ -1,4 +1,4 @@
// "Inline variable|->Keep the 'vExposure' variable" "true-preview" // "Inline variable|->Keep 'vExposure' name" "true-preview"
package com.example; package com.example;
import java.util.ArrayList; import java.util.ArrayList;

View File

@@ -1,4 +1,4 @@
// "Inline variable|->Inline and rename 'vExposure' to 'exp'" "true-preview" // "Inline variable|->Keep 'exp' name" "true-preview"
package com.example; package com.example;
import java.util.ArrayList; import java.util.ArrayList;

View File

@@ -1,4 +1,4 @@
// "Inline variable|->Inline and rename 'vExposure' to 'exp'" "true-preview" // "Inline variable|->Keep 'exp' name" "true-preview"
package com.example; package com.example;
import java.util.ArrayList; import java.util.ArrayList;

View File

@@ -0,0 +1,10 @@
// "Inline variable|->Keep 'pd' name" "false"
package com.example;
class CastNeeded {
double m(int p) {
double p<caret>d = p;
return pd/100;
}
}

View File

@@ -1,4 +1,4 @@
// "Inline variable|->Keep the 'vExposure' variable" "true-preview" // "Inline variable|->Keep 'vExposure' name" "true-preview"
package com.example; package com.example;
import java.util.ArrayList; import java.util.ArrayList;

View File

@@ -1,4 +1,4 @@
// "Inline variable|->Inline and rename 'vExposure' to 'exp'" "true-preview" // "Inline variable|->Keep 'exp' name" "true-preview"
package com.example; package com.example;
import java.util.ArrayList; import java.util.ArrayList;

View File

@@ -1,4 +1,4 @@
// "Inline variable|->Inline and rename 'vExposure' to 'exp'" "true-preview" // "Inline variable|->Keep 'exp' name" "true-preview"
package com.example; package com.example;
import java.util.ArrayList; import java.util.ArrayList;

View File

@@ -230,8 +230,7 @@ all.references.and.remove.the.local=Inline &all references and remove the variab
this.reference.only.and.keep.the.variable=Inline this reference only and &keep the variable this.reference.only.and.keep.the.variable=Inline this reference only and &keep the variable
inline.popup.this.only=This reference only inline.popup.this.only=This reference only
inline.popup.all=All {0} references and remove the variable inline.popup.all=All {0} references and remove the variable
inline.popup.all.keep=Keep the ''{0}'' variable inline.popup.all.keep.name=Keep ''{0}'' name
inline.popup.all.rename=Inline and rename ''{0}'' to ''{1}''
inline.variable.title=Inline Variable inline.variable.title=Inline Variable
variable.is.referenced.in.multiple.files=Variable {0} is referenced in multiple files variable.is.referenced.in.multiple.files=Variable {0} is referenced in multiple files
inline.command=Inline {0} inline.command=Inline {0}

View File

@@ -128,7 +128,9 @@ public final class ActionHint {
} }
ModCommand command = action.perform(context); ModCommand command = action.perform(context);
if (!(command instanceof ModChooseAction chooseAction)) { if (!(command instanceof ModChooseAction chooseAction)) {
fail(exceptionHeader(curStep) + " does not produce a chooser"); if (myShouldPresent) {
fail(exceptionHeader(curStep) + " does not produce a chooser");
}
return null; return null;
} }
commonActions = chooseAction.actions(); commonActions = chooseAction.actions();