From 3e2a443d2d1bdede31ef863d108f0aa0e09bb605 Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Wed, 20 Jul 2022 14:09:32 +0200 Subject: [PATCH] [intentions preview] more lines in preview for Create class from new (^Tagir) IJ-CR-27381 GitOrigin-RevId: 1ac3b8f0f4d6958306d059ddda43aff5e7485c6d --- .../impl/quickfix/CreateClassFromNewFix.java | 24 ++++++++----------- .../impl/quickfix/GuessTypeParameters.java | 6 +++-- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateClassFromNewFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateClassFromNewFix.java index 66a4a1f91d75..529b1927af42 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateClassFromNewFix.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateClassFromNewFix.java @@ -12,13 +12,13 @@ import com.intellij.codeInspection.CommonQuickFixBundle; import com.intellij.codeInspection.util.IntentionName; import com.intellij.ide.highlighter.JavaFileType; import com.intellij.ide.scratch.ScratchUtil; -import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.application.WriteAction; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.fileEditor.ex.IdeDocumentHistory; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Segment; import com.intellij.psi.*; +import com.intellij.psi.codeStyle.CodeStyleManager; import com.intellij.psi.util.InheritanceUtil; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.PsiUtilCore; @@ -57,14 +57,10 @@ public class CreateClassFromNewFix extends CreateFromUsageBaseFix { if (element == null) return IntentionPreviewInfo.EMPTY; PsiJavaCodeReferenceElement classReference = getReferenceElement(element); if (classReference == null) return IntentionPreviewInfo.EMPTY; - String text; - if (getKind() == CreateClassKind.RECORD) { - text = "record" + " " + classReference.getReferenceName() + "()"; - } - else { - text = "class" + " " + classReference.getReferenceName(); - } - return new IntentionPreviewInfo.CustomDiff(JavaFileType.INSTANCE, "", text + " {}"); + PsiClass aClass = getKind().create(JavaPsiFacade.getElementFactory(project), classReference.getReferenceName()); + setupClassFromNewExpression(aClass, element); + CodeStyleManager.getInstance(project).reformat(aClass); + return new IntentionPreviewInfo.CustomDiff(JavaFileType.INSTANCE, "", aClass.getText()); } @NotNull @@ -73,13 +69,11 @@ public class CreateClassFromNewFix extends CreateFromUsageBaseFix { } protected void setupClassFromNewExpression(final PsiClass psiClass, final PsiNewExpression newExpression) { - assert ApplicationManager.getApplication().isWriteAccessAllowed(); - PsiClass aClass = psiClass; if (aClass == null) return; final PsiJavaCodeReferenceElement classReference = newExpression.getClassReference(); - if (classReference != null) { + if (classReference != null && aClass.isPhysical()) { classReference.bindToElement(aClass); } setupInheritance(newExpression, aClass); @@ -89,8 +83,10 @@ public class CreateClassFromNewFix extends CreateFromUsageBaseFix { if (argList != null && !argList.isEmpty()) { TemplateBuilderImpl templateBuilder = createConstructorTemplate(aClass, newExpression, argList); - getReferenceElement(newExpression).bindToElement(aClass); - aClass = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(aClass); + if (aClass.isPhysical()) { + getReferenceElement(newExpression).bindToElement(aClass); + aClass = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(aClass); + } final Template template = templateBuilder.buildTemplate(); template.setToReformat(true); diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/GuessTypeParameters.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/GuessTypeParameters.java index f6dc10651cc0..188340964478 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/GuessTypeParameters.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/GuessTypeParameters.java @@ -1,4 +1,4 @@ -// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.codeInsight.daemon.impl.quickfix; import com.intellij.codeInsight.ExpectedTypeInfo; @@ -53,7 +53,9 @@ public class GuessTypeParameters { @Nullable PsiElement context, @NotNull PsiClass targetClass) { LOG.assertTrue(typeElement.isValid()); - ApplicationManager.getApplication().assertWriteAccessAllowed(); + if (typeElement.isPhysical()) { + ApplicationManager.getApplication().assertWriteAccessAllowed(); + } GlobalSearchScope scope = typeElement.getResolveScope();