[java-intentions] IDEA-375979 'Replace constructor with factory method' does nothing in the compact source file

GitOrigin-RevId: 24a5376587965f0ed27409f5a1917201237a6425
This commit is contained in:
Mikhail Pyltsin
2025-07-28 17:12:09 +00:00
committed by intellij-monorepo-bot
parent 1976cc97d5
commit a7878d49e9
4 changed files with 26 additions and 10 deletions
@@ -89,11 +89,12 @@ class JavaCommandCompletionFactory implements CommandCompletionFactory, DumbAwar
if (open != null && curParent.getTextRange().getEndOffset() == element.getTextRange().getEndOffset()) {
while (curParent.getTextRange().getEndOffset() == element.getTextRange().getEndOffset()) {
int nextOffset = curParent.getTextRange().getStartOffset();
if (results.add(nextOffset)) {
curParent = curParent.getParent();
boolean stopParent = curParent == null || curParent instanceof PsiClass || curParent instanceof PsiFile;
if (results.add(nextOffset) && !stopParent) {
queue.add(nextOffset);
}
curParent = curParent.getParent();
if (curParent == null) {
if (stopParent) {
break;
}
}
@@ -38,7 +38,8 @@ public final class ReplaceConstructorWithFactoryAction implements ModCommandActi
@Override
public @Nullable Presentation getPresentation(@NotNull ActionContext context) {
if (!BaseIntentionAction.canModify(context.file())) return null;
return getConstructorOrClass(context.findLeaf()) != null
PsiMember member = getConstructorOrClass(context.findLeaf());
return member != null && !getTargets(member).isEmpty()
? Presentation.of(getFamilyName()).withIcon(NewUiValue.isEnabled() ? null : AllIcons.Actions.RefactoringBulb)
: null;
}
@@ -48,12 +49,7 @@ public final class ReplaceConstructorWithFactoryAction implements ModCommandActi
PsiElement element = context.findLeaf();
PsiMember constructorOrClass = getConstructorOrClass(element);
if (constructorOrClass == null) return ModCommand.nop();
List<PsiClass> targets = StreamEx.iterate(constructorOrClass, Objects::nonNull, PsiMember::getContainingClass)
.select(PsiClass.class)
.filter(cls -> !(cls instanceof PsiImplicitClass))
.filter(cls -> cls.hasModifierProperty(PsiModifier.STATIC) || cls.getContainingClass() == null)
.toList();
List<PsiClass> targets = getTargets(constructorOrClass);
SmartPsiElementPointer<PsiMember> constructorOrClassPtr = SmartPointerManager.createPointer(constructorOrClass);
List<ModCommandAction> options =
ContainerUtil.map(targets, target -> ModCommand.psiUpdateStep(
@@ -66,6 +62,14 @@ public final class ReplaceConstructorWithFactoryAction implements ModCommandActi
return ModCommand.chooseAction(JavaBundle.message("popup.title.choose.target.class"), options);
}
private @NotNull List<PsiClass> getTargets(PsiMember constructorOrClass) {
return StreamEx.iterate(constructorOrClass, Objects::nonNull, PsiMember::getContainingClass)
.select(PsiClass.class)
.filter(cls -> !(cls instanceof PsiImplicitClass))
.filter(cls -> cls.hasModifierProperty(PsiModifier.STATIC) || cls.getContainingClass() == null)
.toList();
}
private static void invoke(@NotNull PsiClass cls,
@NotNull ModPsiUpdater updater,
@NotNull SmartPsiElementPointer<PsiMember> constructorOrClassPtr) {
@@ -0,0 +1,7 @@
class nested<caret> {
}
void main() {
}
@@ -123,6 +123,10 @@ public class ReplaceConstructorWithFactoryTest extends LightRefactoringTestCase
assertNotAvailable("RedCode");
}
public void testImplicitClassNotAvailable() {
assertNotAvailable("ImplicitClassAfterName");
}
public void testRedCodeFromIDEA376351() {
assertNotAvailable("RedCodeFromIDEA376351");
}