[java-inspections] CreateInnerClassFromUsageFix: preview-friendly

GitOrigin-RevId: ed0dbc12d26e87c17315f369da687dd4fe65d63d
This commit is contained in:
Tagir Valeev
2022-07-26 11:22:08 +02:00
committed by intellij-monorepo-bot
parent ed8c359a4c
commit d3341c8cc9
31 changed files with 56 additions and 35 deletions

View File

@@ -17,6 +17,7 @@ package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.FileModificationService;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo;
import com.intellij.ide.util.PsiClassListCellRenderer;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Editor;
@@ -71,6 +72,17 @@ public class CreateInnerClassFromUsageFix extends CreateClassFromUsageBaseFix {
}
}
@Override
public @NotNull IntentionPreviewInfo generatePreview(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
PsiJavaCodeReferenceElement element = getRefElement();
if (element == null) return IntentionPreviewInfo.EMPTY;
element = PsiTreeUtil.findSameElementInCopy(element, file);
PsiClass[] targets = getPossibleTargets(element);
if (targets.length == 0) return IntentionPreviewInfo.EMPTY;
doInvoke(targets[0], getSuperClassName(element));
return IntentionPreviewInfo.DIFF;
}
@Override
public boolean isAvailable(@NotNull final Project project, final Editor editor, final PsiFile file) {
return super.isAvailable(project, editor, file) && getPossibleTargets(getRefElement()).length > 0;
@@ -117,9 +129,13 @@ public class CreateInnerClassFromUsageFix extends CreateClassFromUsageBaseFix {
}
private void doInvoke(final PsiClass aClass, final String superClassName) throws IncorrectOperationException {
PsiJavaCodeReferenceElement ref = getRefElement();
PsiJavaCodeReferenceElement ref;
if (!aClass.isPhysical()) {
ref = PsiTreeUtil.findSameElementInCopy(getRefElement(), aClass.getContainingFile());
} else {
ref = getRefElement();
}
assert ref != null;
if (!FileModificationService.getInstance().preparePsiElementForWrite(aClass)) return;
String refName = ref.getReferenceName();
LOG.assertTrue(refName != null);
PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(aClass.getProject());
@@ -139,8 +155,13 @@ public class CreateInnerClassFromUsageFix extends CreateClassFromUsageBaseFix {
}
CreateFromUsageBaseFix.setupGenericParameters(created, ref);
WriteCommandAction.runWriteCommandAction(aClass.getProject(), getText(), null,
() -> ref.bindToElement(aClass.add(created)),
aClass.getContainingFile());
if (!aClass.isPhysical()) {
aClass.add(created);
} else {
if (!FileModificationService.getInstance().preparePsiElementForWrite(aClass)) return;
WriteCommandAction.runWriteCommandAction(aClass.getProject(), getText(), null,
() -> ref.bindToElement(aClass.add(created)),
aClass.getContainingFile());
}
}
}

View File

@@ -1,4 +1,4 @@
// "Create inner class 'Abc'" "true"
// "Create inner class 'Abc'" "true-preview"
public class Test {
public foo(int ppp) {
<caret>Abc.foo();

View File

@@ -1,4 +1,4 @@
// "Create inner class 'ClassB'" "true"
// "Create inner class 'ClassB'" "true-preview"
public class ClassA
{
public void mA(Object arg)

View File

@@ -1,4 +1,4 @@
// "Create inner class 'Foo'" "true"
// "Create inner class 'Foo'" "true-preview"
public class Test {
void foo(Class<Number> n){}
void bar() {

View File

@@ -1,4 +1,4 @@
// "Create inner class 'Foo'" "true"
// "Create inner class 'Foo'" "true-preview"
public class Test {
void foo(Class<? extends Number> n){}
void bar() {

View File

@@ -1,4 +1,4 @@
// "Create inner class 'Foo'" "true"
// "Create inner class 'Foo'" "true-preview"
public class Test {
void foo(Class<? super Number> n){}
void bar() {

View File

@@ -1,4 +1,4 @@
// "Create inner class 'Foo'" "true"
// "Create inner class 'Foo'" "true-preview"
public class Test {
void foo(Class<? extends Number> n){}
void bar() {

View File

@@ -1,4 +1,4 @@
// "Create inner class 'Abc'" "true"
// "Create inner class 'Abc'" "true-preview"
public class Test {
private <caret>Abc fld;

View File

@@ -1,4 +1,4 @@
// "Create inner class 'Param'" "true"
// "Create inner class 'Param'" "true-preview"
import java.util.*;
public interface I {

View File

@@ -1,4 +1,4 @@
// "Create inner class 'Foo'" "true"
// "Create inner class 'Foo'" "true-preview"
public interface Test {
void foo(Fo<caret>o f){}

View File

@@ -1,4 +1,4 @@
// "Create inner class 'Foo'" "true"
// "Create inner class 'Foo'" "true-preview"
public class Test {
boolean foo(Object o) {
return o instanceof Foo;

View File

@@ -1,4 +1,4 @@
// "Create inner class 'Abc'" "true"
// "Create inner class 'Abc'" "true-preview"
public class Test {
private <caret>Abc foo() {}

View File

@@ -1,4 +1,4 @@
// "Create inner class 'Abc'" "true"
// "Create inner class 'Abc'" "true-preview"
public class Test {
private void foo(<caret>Abc param) {}

View File

@@ -1,4 +1,4 @@
// "Create inner class 'Foo'" "true"
// "Create inner class 'Foo'" "true-preview"
public class Test {
static class A extends Foo {

View File

@@ -1,4 +1,4 @@
// "Create inner class 'MyRunnable'" "true"
// "Create inner class 'MyRunnable'" "true-preview"
public class Test {
void bar(Class<? extends Runnable> c) {
}

View File

@@ -1,4 +1,4 @@
// "Create inner class 'Foo'" "true"
// "Create inner class 'Foo'" "true-preview"
public class Test {
<R> void foo(Fo<caret>o<R, String> f){}

View File

@@ -1,4 +1,4 @@
// "Create inner class 'Abc'" "true"
// "Create inner class 'Abc'" "true-preview"
public class Test {
public foo(int ppp) {
<caret>Abc.foo();

View File

@@ -1,4 +1,4 @@
// "Create inner class 'ClassB'" "true"
// "Create inner class 'ClassB'" "true-preview"
public class ClassA
{
public void mA(Object arg)

View File

@@ -1,4 +1,4 @@
// "Create inner class 'Foo'" "true"
// "Create inner class 'Foo'" "true-preview"
public class Test {
void foo(Class<Number> n){}
void bar() {

View File

@@ -1,4 +1,4 @@
// "Create inner class 'Foo'" "true"
// "Create inner class 'Foo'" "true-preview"
public class Test {
void foo(Class<? extends Number> n){}
void bar() {

View File

@@ -1,4 +1,4 @@
// "Create inner class 'Foo'" "true"
// "Create inner class 'Foo'" "true-preview"
public class Test {
void foo(Class<? super Number> n){}
void bar() {

View File

@@ -1,4 +1,4 @@
// "Create inner class 'Foo'" "true"
// "Create inner class 'Foo'" "true-preview"
public class Test {
void foo(Class<? extends Number> n){}
void bar() {

View File

@@ -1,4 +1,4 @@
// "Create inner class 'Abc'" "true"
// "Create inner class 'Abc'" "true-preview"
public class Test {
private <caret>Abc fld;
}

View File

@@ -1,4 +1,4 @@
// "Create inner class 'Param'" "true"
// "Create inner class 'Param'" "true-preview"
import java.util.*;
public interface I {

View File

@@ -1,4 +1,4 @@
// "Create inner class 'Foo'" "true"
// "Create inner class 'Foo'" "true-preview"
public interface Test {
void foo(Fo<caret>o f){}
}

View File

@@ -1,4 +1,4 @@
// "Create inner class 'Foo'" "true"
// "Create inner class 'Foo'" "true-preview"
public class Test {
boolean foo(Object o) {
return o instanceof F<caret>oo;

View File

@@ -1,4 +1,4 @@
// "Create inner class 'Abc'" "true"
// "Create inner class 'Abc'" "true-preview"
public class Test {
private <caret>Abc foo() {}
}

View File

@@ -1,4 +1,4 @@
// "Create inner class 'Abc'" "true"
// "Create inner class 'Abc'" "true-preview"
public class Test {
private void foo(<caret>Abc param) {}
}

View File

@@ -1,4 +1,4 @@
// "Create inner class 'Foo'" "true"
// "Create inner class 'Foo'" "true-preview"
public class Test {
static class A extends F<caret>oo {

View File

@@ -1,4 +1,4 @@
// "Create inner class 'MyRunnable'" "true"
// "Create inner class 'MyRunnable'" "true-preview"
public class Test {
void bar(Class<? extends Runnable> c) {
}

View File

@@ -1,4 +1,4 @@
// "Create inner class 'Foo'" "true"
// "Create inner class 'Foo'" "true-preview"
public class Test {
<R> void foo(Fo<caret>o<R, String> f){}
}