[java-generation] IJ-MR-136429 IDEA-344399 generate annotation based on type_use option

- fix non-standard-root-packages
- introduce project descriptors

GitOrigin-RevId: 41db7240e29fbfed81957df2d68e2de29c573ef2
This commit is contained in:
Mikhail Pyltsin
2024-06-12 15:41:34 +02:00
committed by intellij-monorepo-bot
parent 2023228d8c
commit 9bf2727703
7 changed files with 32 additions and 32 deletions

View File

@@ -1,5 +1,5 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.refactoring;
package com.intellij.refactoring.util;
import com.intellij.codeInsight.AnnotationTargetUtil;
import com.intellij.psi.*;
@@ -29,7 +29,7 @@ public final class ModifierListUtil {
* {@link ModifierComparator}) list as a PsiModifierList object
*/
public static @Nullable PsiModifierList createSortedModifierList(@NotNull PsiModifierList modifierList,
@Nullable Comparator<PsiElement> customAnnotationComparator,
@Nullable Comparator<? super PsiAnnotation> customAnnotationComparator,
boolean allowAnnotationTypeBeInAnyAllowedPlace) {
@NonNls final String text = String.join(" ", getSortedModifiers(modifierList, customAnnotationComparator, allowAnnotationTypeBeInAnyAllowedPlace));
return createNewModifierList(modifierList, text);
@@ -74,7 +74,7 @@ public final class ModifierListUtil {
* @see ModifierListUtil#createSortedModifierList(PsiModifierList, Comparator, boolean)
*/
public static List<String> getSortedModifiers(@NotNull PsiModifierList modifierList,
@Nullable Comparator<PsiElement> customAnnotationComparator,
@Nullable Comparator<? super PsiAnnotation> customAnnotationComparator,
boolean allowAnnotationTypeBeInAnyAllowedPlace) {
final List<String> modifiers = new SmartList<>();
final List<PsiAnnotation> typeAnnotations = new SmartList<>();

View File

@@ -23,7 +23,7 @@ import com.intellij.modcommand.ModPsiUpdater;
import com.intellij.modcommand.PsiUpdateModCommandQuickFix;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.refactoring.ModifierListUtil;
import com.intellij.refactoring.util.ModifierListUtil;
import com.intellij.util.SmartList;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;

View File

@@ -29,7 +29,7 @@ import com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.*;
import com.intellij.refactoring.ModifierListUtil;
import com.intellij.refactoring.util.ModifierListUtil;
import com.intellij.util.*;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.text.UniqueNameGenerator;
@@ -377,10 +377,7 @@ public final class GenerateMembersUtil {
}
}
}
Comparator<PsiElement> comparator = (o1, o2) -> {
if (!(o1 instanceof PsiAnnotation a1) || !(o2 instanceof PsiAnnotation a2)) {
return 0;
}
Comparator<PsiAnnotation> comparator = (a1, a2) -> {
String q1 = a1.getQualifiedName();
String q2 = a2.getQualifiedName();
if (q1 == null || q2 == null) return 0;

View File

@@ -10,9 +10,6 @@ import com.intellij.ide.IdePopupManager
import com.intellij.openapi.actionSystem.*
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.module.Module
import com.intellij.openapi.roots.ContentEntry
import com.intellij.openapi.roots.ModifiableRootModel
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.util.TextRange
import com.intellij.pom.java.LanguageLevel
@@ -26,7 +23,6 @@ import com.intellij.refactoring.util.CommonRefactoringUtil.RefactoringErrorHintE
import com.intellij.testFramework.IdeaTestUtil
import com.intellij.testFramework.LightJavaCodeInsightTestCase
import com.intellij.testFramework.LightProjectDescriptor
import com.intellij.testFramework.fixtures.DefaultLightProjectDescriptor
import com.intellij.ui.ChooserInterceptor
import com.intellij.ui.UiInterceptors
import com.intellij.util.ui.UIUtil
@@ -574,11 +570,7 @@ class ExtractMethodAndDuplicatesInplaceTest: LightJavaCodeInsightTestCase() {
}
override fun getProjectDescriptor(): LightProjectDescriptor {
return object : SimpleLightProjectDescriptor(moduleTypeId, projectJDK) {
override fun configureModule(module: Module, model: ModifiableRootModel, contentEntry: ContentEntry) {
DefaultLightProjectDescriptor.addJetBrainsAnnotationsWithTypeUse(model)
}
}
return ExtractMethodNewTest.SimpleLightProjectDescriptorWithAnnotations(moduleTypeId, projectJDK)
}
override fun setUp() {

View File

@@ -10,6 +10,7 @@ import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.ContentEntry;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.util.registry.Registry;
@@ -1932,11 +1933,18 @@ public class ExtractMethodNewTest extends LightJavaCodeInsightTestCase {
@Override
protected @NotNull LightProjectDescriptor getProjectDescriptor() {
return new SimpleLightProjectDescriptor(getModuleTypeId(), getProjectJDK()) {
@Override
protected void configureModule(@NotNull Module module, @NotNull ModifiableRootModel model, @NotNull ContentEntry contentEntry) {
DefaultLightProjectDescriptor.addJetBrainsAnnotationsWithTypeUse(model);
}
};
return new SimpleLightProjectDescriptorWithAnnotations(getModuleTypeId(), getProjectJDK());
}
static class SimpleLightProjectDescriptorWithAnnotations extends SimpleLightProjectDescriptor{
protected SimpleLightProjectDescriptorWithAnnotations(@NotNull String moduleTypeId, @Nullable Sdk sdk) {
super(moduleTypeId, sdk);
}
@Override
protected void configureModule(@NotNull Module module, @NotNull ModifiableRootModel model, @NotNull ContentEntry contentEntry) {
DefaultLightProjectDescriptor.addJetBrainsAnnotationsWithTypeUse(model);
}
}
}

View File

@@ -16,6 +16,15 @@ import org.jetbrains.annotations.NotNull;
import static com.intellij.pom.java.LanguageLevel.JDK_21_PREVIEW;
public class ExtractMethodRecommenderInspectionTest extends LightJavaCodeInsightFixtureTestCase {
private static final ProjectDescriptor JDK_21_PREVIEW_WITH_ANNOTATIONS = new ProjectDescriptor(JDK_21_PREVIEW) {
@Override
public void configureModule(@NotNull Module module, @NotNull ModifiableRootModel model, @NotNull ContentEntry contentEntry) {
model.getModuleExtension(LanguageLevelModuleExtension.class).setLanguageLevel(myLanguageLevel);
addJetBrainsAnnotationsWithTypeUse(model);
}
};
public void testExtractMethodRecommender() {
ExtractMethodRecommenderInspection inspection = new ExtractMethodRecommenderInspection();
inspection.minLength = 10;
@@ -73,13 +82,7 @@ public class ExtractMethodRecommenderInspectionTest extends LightJavaCodeInsight
@Override
protected @NotNull LightProjectDescriptor getProjectDescriptor() {
return new ProjectDescriptor(JDK_21_PREVIEW){
@Override
public void configureModule(@NotNull Module module, @NotNull ModifiableRootModel model, @NotNull ContentEntry contentEntry) {
model.getModuleExtension(LanguageLevelModuleExtension.class).setLanguageLevel(myLanguageLevel);
addJetBrainsAnnotationsWithTypeUse(model);
}
};
return JDK_21_PREVIEW_WITH_ANNOTATIONS;
}
@Override

View File

@@ -1345,7 +1345,7 @@ usage.target.exception=Exception
usage.target.package.in.directory={0} (in {1})
use.external.annotations=Use &external annotations
generate.type.use.before.type=Generate annotations allowed for TYPE_USE directly &before a type
generate.type.use.before.type.description=Annotations, containing TYPE_USE as target, will be placed right before a type. Otherwise, it will be tried to place before all modifiers.
generate.type.use.before.type.description=Annotations with TYPE_USE as their target will be placed directly before a type. Otherwise, they will be placed before all modifiers
wrapping.annotation.enums=Enum field annotations
wrapping.annotation.parameters=Annotation parameters
wrapping.record.components=Record components