mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 13:02:30 +07:00
[java-inspections] CreateInnerClassFromNewFix: preview-friendly
GitOrigin-RevId: 8961061d0a65a1ce182c79d3d108db30b2e2ee18
This commit is contained in:
committed by
intellij-monorepo-bot
parent
8560c6a5d1
commit
ed8c359a4c
@@ -85,8 +85,8 @@ public class CreateClassFromNewFix extends CreateFromUsageBaseFix {
|
||||
|
||||
if (aClass.isPhysical()) {
|
||||
getReferenceElement(newExpression).bindToElement(aClass);
|
||||
aClass = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(aClass);
|
||||
}
|
||||
aClass = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(aClass);
|
||||
final Template template = templateBuilder.buildTemplate();
|
||||
template.setToReformat(true);
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ package com.intellij.codeInsight.daemon.impl.quickfix;
|
||||
import com.intellij.codeInsight.FileModificationService;
|
||||
import com.intellij.codeInsight.daemon.QuickFixBundle;
|
||||
import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
|
||||
import com.intellij.codeInsight.intention.preview.IntentionPreviewUtils;
|
||||
import com.intellij.codeInsight.template.Template;
|
||||
import com.intellij.codeInsight.template.TemplateEditingListener;
|
||||
import com.intellij.codeInsight.template.TemplateManager;
|
||||
@@ -116,6 +117,13 @@ public abstract class CreateFromUsageBaseFix extends BaseIntentionAction {
|
||||
TextRange range = element.getTextRange();
|
||||
LOG.assertTrue(range != null, element.getClass());
|
||||
int textOffset = range.getStartOffset();
|
||||
if (IntentionPreviewUtils.isPreviewElement(targetFile)) {
|
||||
Editor editor = IntentionPreviewUtils.getPreviewEditor();
|
||||
if (editor != null) {
|
||||
editor.getCaretModel().moveToOffset(textOffset);
|
||||
}
|
||||
return editor;
|
||||
}
|
||||
VirtualFile file = targetFile.getVirtualFile();
|
||||
if (file == null) {
|
||||
file = PsiUtilCore.getVirtualFile(element);
|
||||
|
||||
@@ -24,6 +24,7 @@ import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.application.ReadAction;
|
||||
import com.intellij.openapi.application.WriteAction;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.EditorModificationUtil;
|
||||
import com.intellij.openapi.editor.ScrollType;
|
||||
@@ -1013,8 +1014,11 @@ public final class CreateFromUsageUtils {
|
||||
public LookupElement @NotNull [] calculateLookupItems(ExpressionContext context) {
|
||||
Project project = context.getProject();
|
||||
int offset = context.getStartOffset();
|
||||
PsiDocumentManager.getInstance(project).commitAllDocuments();
|
||||
PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(context.getEditor().getDocument());
|
||||
Editor editor = context.getEditor();
|
||||
assert editor != null;
|
||||
Document document = editor.getDocument();
|
||||
PsiDocumentManager.getInstance(project).commitDocument(document);
|
||||
PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(document);
|
||||
assert file != null;
|
||||
PsiElement elementAt = file.findElementAt(offset);
|
||||
Set<String> parameterNames = getPeerNames(elementAt);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
package com.intellij.codeInsight.daemon.impl.quickfix;
|
||||
|
||||
import com.intellij.codeInsight.daemon.QuickFixBundle;
|
||||
import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
@@ -12,6 +13,8 @@ import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.JavaPsiConstructorUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class CreateInnerClassFromNewFix extends CreateClassFromNewFix {
|
||||
private static final Logger LOG = Logger.getInstance(CreateInnerClassFromNewFix.class);
|
||||
@@ -46,8 +49,20 @@ public class CreateInnerClassFromNewFix extends CreateClassFromNewFix {
|
||||
chooseTargetClass(project, editor, this::invokeImpl);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull IntentionPreviewInfo generatePreview(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
|
||||
PsiElement element = PsiTreeUtil.findSameElementInCopy(getElement(), file);
|
||||
List<PsiClass> targetClasses = filterTargetClasses(element, project);
|
||||
if (targetClasses.isEmpty()) return IntentionPreviewInfo.EMPTY;
|
||||
invokeImpl(targetClasses.get(0));
|
||||
return IntentionPreviewInfo.DIFF;
|
||||
}
|
||||
|
||||
private void invokeImpl(final PsiClass targetClass) {
|
||||
PsiNewExpression newExpression = getNewExpression();
|
||||
if (!targetClass.isPhysical()) {
|
||||
newExpression = PsiTreeUtil.findSameElementInCopy(newExpression, targetClass.getContainingFile());
|
||||
}
|
||||
PsiJavaCodeReferenceElement ref = newExpression.getClassOrAnonymousClassReference();
|
||||
assert ref != null;
|
||||
String refName = ref.getReferenceName();
|
||||
|
||||
@@ -9,6 +9,7 @@ import com.intellij.codeInsight.template.PsiTypeResult;
|
||||
import com.intellij.codeInsight.template.Result;
|
||||
import com.intellij.codeInsight.template.impl.JavaTemplateUtil;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -37,7 +38,11 @@ public class TypeExpression extends Expression {
|
||||
@Override
|
||||
public Result calculateResult(ExpressionContext context) {
|
||||
final Project project = context.getProject();
|
||||
PsiDocumentManager.getInstance(project).commitAllDocuments();
|
||||
Editor editor = context.getEditor();
|
||||
if (editor != null) {
|
||||
Document document = editor.getDocument();
|
||||
PsiDocumentManager.getInstance(project).commitDocument(document);
|
||||
}
|
||||
if (myItems.isEmpty()) return null;
|
||||
|
||||
final PsiType type = myItems.iterator().next().getType();
|
||||
@@ -57,7 +62,11 @@ public class TypeExpression extends Expression {
|
||||
@Override
|
||||
public LookupElement[] calculateLookupItems(ExpressionContext context) {
|
||||
if (myItems.size() <= 1) return null;
|
||||
PsiDocumentManager.getInstance(context.getProject()).commitAllDocuments();
|
||||
Editor editor = context.getEditor();
|
||||
if (editor != null) {
|
||||
Document document = editor.getDocument();
|
||||
PsiDocumentManager.getInstance(context.getProject()).commitDocument(document);
|
||||
}
|
||||
|
||||
List<LookupElement> result = new ArrayList<>(myItems.size());
|
||||
for (final SmartTypePointer item : myItems) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'MyCollection'" "true"
|
||||
// "Create inner class 'MyCollection'" "true-preview"
|
||||
import java.util.*;
|
||||
|
||||
public class Test {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'MyArrayList'" "true"
|
||||
// "Create inner class 'MyArrayList'" "true-preview"
|
||||
import java.util.*;
|
||||
|
||||
public class Test {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'MyInteger'" "true"
|
||||
// "Create inner class 'MyInteger'" "true-preview"
|
||||
public class Test {
|
||||
public static void main() {
|
||||
int xxx = 3;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'MyCollection'" "true"
|
||||
// "Create inner class 'MyCollection'" "true-preview"
|
||||
public class Test {
|
||||
public static void main() {
|
||||
Collection[] cc = new MyCollection[10];
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'MyTableModel'" "true"
|
||||
// "Create inner class 'MyTableModel'" "true-preview"
|
||||
|
||||
public class Test {
|
||||
public static void main() {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'MyCollection'" "true"
|
||||
// "Create inner class 'MyCollection'" "true-preview"
|
||||
import java.util.*;
|
||||
|
||||
public class Test {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'Generic'" "true"
|
||||
// "Create inner class 'Generic'" "true-preview"
|
||||
class Test {
|
||||
void foo () {
|
||||
new Generic<String> ();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'MyCollection'" "true"
|
||||
// "Create inner class 'MyCollection'" "true-preview"
|
||||
import java.util.*;
|
||||
|
||||
public interface I {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'MyCollection'" "true"
|
||||
// "Create inner class 'MyCollection'" "true-preview"
|
||||
import java.util.*;
|
||||
|
||||
public interface I {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'Generic'" "true"
|
||||
// "Create inner class 'Generic'" "true-preview"
|
||||
class Base {
|
||||
void foo(){}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'Generic'" "true"
|
||||
// "Create inner class 'Generic'" "true-preview"
|
||||
class Test {
|
||||
Test() {
|
||||
this (new Generic<String> ());
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'ArrayList'" "true"
|
||||
// "Create inner class 'ArrayList'" "true-preview"
|
||||
public class Test {
|
||||
public static void main() {
|
||||
Inner q = new Inner();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'ArrayList'" "true"
|
||||
// "Create inner class 'ArrayList'" "true-preview"
|
||||
public class Test {
|
||||
public static void main() {
|
||||
new B.ArrayList();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner record 'Foo'" "true"
|
||||
// "Create inner record 'Foo'" "true-preview"
|
||||
public class Test {
|
||||
public static void main() {
|
||||
new Foo("bar", "baz")
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner record 'Point'" "true"
|
||||
// "Create inner record 'Point'" "true-preview"
|
||||
public class Test {
|
||||
class Inner {
|
||||
void test(int x, int y) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'AInner'" "true"
|
||||
// "Create inner class 'AInner'" "true-preview"
|
||||
class Test {
|
||||
{
|
||||
AInner aInner = new AInner<String>(42);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'AInner'" "true"
|
||||
// "Create inner class 'AInner'" "true-preview"
|
||||
class Test {
|
||||
{
|
||||
AInner aInner = new AInner<String, String>(42);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'MyCollection'" "true"
|
||||
// "Create inner class 'MyCollection'" "true-preview"
|
||||
import java.util.*;
|
||||
|
||||
public class Test {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'MyArrayList'" "true"
|
||||
// "Create inner class 'MyArrayList'" "true-preview"
|
||||
import java.util.*;
|
||||
|
||||
public class Test {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'MyInteger'" "true"
|
||||
// "Create inner class 'MyInteger'" "true-preview"
|
||||
public class Test {
|
||||
public static void main() {
|
||||
int xxx = 3;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'MyCollection'" "true"
|
||||
// "Create inner class 'MyCollection'" "true-preview"
|
||||
public class Test {
|
||||
public static void main() {
|
||||
Collection[] cc = new <caret>MyCollection[10];
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'MyTableModel'" "true"
|
||||
// "Create inner class 'MyTableModel'" "true-preview"
|
||||
|
||||
public class Test {
|
||||
public static void main() {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'MyCollection'" "true"
|
||||
// "Create inner class 'MyCollection'" "true-preview"
|
||||
import java.util.*;
|
||||
|
||||
public class Test {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'Generic'" "true"
|
||||
// "Create inner class 'Generic'" "true-preview"
|
||||
class Test {
|
||||
void foo () {
|
||||
new <caret>Generic<String> ();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'MyCollection'" "true"
|
||||
// "Create inner class 'MyCollection'" "true-preview"
|
||||
import java.util.*;
|
||||
|
||||
public interface I {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'MyCollection'" "true"
|
||||
// "Create inner class 'MyCollection'" "true-preview"
|
||||
import java.util.*;
|
||||
|
||||
public interface I {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'Generic'" "true"
|
||||
// "Create inner class 'Generic'" "true-preview"
|
||||
class Base {
|
||||
void foo(){}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'Generic'" "true"
|
||||
// "Create inner class 'Generic'" "true-preview"
|
||||
class Test {
|
||||
Test() {
|
||||
this (new <caret>Generic<String> ());
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'ArrayList'" "true"
|
||||
// "Create inner class 'ArrayList'" "true-preview"
|
||||
public class Test {
|
||||
public static void main() {
|
||||
Inner q = new Inner();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'ArrayList'" "true"
|
||||
// "Create inner class 'ArrayList'" "true-preview"
|
||||
public class Test {
|
||||
public static void main() {
|
||||
new B.<caret>ArrayList();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner record 'Foo'" "true"
|
||||
// "Create inner record 'Foo'" "true-preview"
|
||||
public class Test {
|
||||
public static void main() {
|
||||
new F<caret>oo("bar", "baz")
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner record 'Point'" "true"
|
||||
// "Create inner record 'Point'" "true-preview"
|
||||
public class Test {
|
||||
class Inner {
|
||||
void test(int x, int y) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'AInner'" "true"
|
||||
// "Create inner class 'AInner'" "true-preview"
|
||||
class Test {
|
||||
{
|
||||
AInner aInner = new AIn<caret>ner<String>(42);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Create inner class 'AInner'" "true"
|
||||
// "Create inner class 'AInner'" "true-preview"
|
||||
class Test {
|
||||
{
|
||||
AInner aInner = new AIn<caret>ner<String, String>(42);
|
||||
|
||||
Reference in New Issue
Block a user