mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 13:02:30 +07:00
[java-inspections] CreateInnerClassFromUsageFix: preview-friendly
GitOrigin-RevId: ed0dbc12d26e87c17315f369da687dd4fe65d63d
This commit is contained in:
committed by
intellij-monorepo-bot
parent
ed8c359a4c
commit
d3341c8cc9
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'ClassB'" "true"
|
||||
// "Create inner class 'ClassB'" "true-preview"
|
||||
public class ClassA
|
||||
{
|
||||
public void mA(Object arg)
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'Abc'" "true"
|
||||
// "Create inner class 'Abc'" "true-preview"
|
||||
public class Test {
|
||||
private <caret>Abc fld;
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'Param'" "true"
|
||||
// "Create inner class 'Param'" "true-preview"
|
||||
import java.util.*;
|
||||
|
||||
public interface I {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'Foo'" "true"
|
||||
// "Create inner class 'Foo'" "true-preview"
|
||||
public interface Test {
|
||||
void foo(Fo<caret>o f){}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'Abc'" "true"
|
||||
// "Create inner class 'Abc'" "true-preview"
|
||||
public class Test {
|
||||
private <caret>Abc foo() {}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'Abc'" "true"
|
||||
// "Create inner class 'Abc'" "true-preview"
|
||||
public class Test {
|
||||
private void foo(<caret>Abc param) {}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'Foo'" "true"
|
||||
// "Create inner class 'Foo'" "true-preview"
|
||||
public class Test {
|
||||
static class A extends Foo {
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'MyRunnable'" "true"
|
||||
// "Create inner class 'MyRunnable'" "true-preview"
|
||||
public class Test {
|
||||
void bar(Class<? extends Runnable> c) {
|
||||
}
|
||||
|
||||
@@ -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){}
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'ClassB'" "true"
|
||||
// "Create inner class 'ClassB'" "true-preview"
|
||||
public class ClassA
|
||||
{
|
||||
public void mA(Object arg)
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'Abc'" "true"
|
||||
// "Create inner class 'Abc'" "true-preview"
|
||||
public class Test {
|
||||
private <caret>Abc fld;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'Param'" "true"
|
||||
// "Create inner class 'Param'" "true-preview"
|
||||
import java.util.*;
|
||||
|
||||
public interface I {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'Foo'" "true"
|
||||
// "Create inner class 'Foo'" "true-preview"
|
||||
public interface Test {
|
||||
void foo(Fo<caret>o f){}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'Abc'" "true"
|
||||
// "Create inner class 'Abc'" "true-preview"
|
||||
public class Test {
|
||||
private <caret>Abc foo() {}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'Abc'" "true"
|
||||
// "Create inner class 'Abc'" "true-preview"
|
||||
public class Test {
|
||||
private void foo(<caret>Abc param) {}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'MyRunnable'" "true"
|
||||
// "Create inner class 'MyRunnable'" "true-preview"
|
||||
public class Test {
|
||||
void bar(Class<? extends Runnable> c) {
|
||||
}
|
||||
|
||||
@@ -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){}
|
||||
}
|
||||
Reference in New Issue
Block a user