From 9b798e3ece9c56d96d19efc58c85ed0dcaff7cd6 Mon Sep 17 00:00:00 2001 From: "Roman.Ivanov" Date: Tue, 8 Oct 2019 16:35:16 +0700 Subject: [PATCH] SortContentAction: replace with proper annotation member value instead of expression : IDEA-224254 GitOrigin-RevId: 687fc8771babc84d3ebc07ad4fe04c4543fed36e --- .../intention/impl/SortContentAction.java | 54 ++++++++++++------- .../sortContent/afterWithoutFormatter.java | 19 +++++++ .../sortContent/beforeWithoutFormatter.java | 19 +++++++ 3 files changed, 72 insertions(+), 20 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/sortContent/afterWithoutFormatter.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/sortContent/beforeWithoutFormatter.java diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/SortContentAction.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/SortContentAction.java index 19f377f4b7e9..8e3fa000a0f7 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/SortContentAction.java +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/SortContentAction.java @@ -231,12 +231,6 @@ public class SortContentAction extends PsiElementBaseIntentionAction { } return newLineNeed; } - - SortableEntry copy() { - List afterSeparator = ContainerUtil.map(myAfterSeparator, el -> (PsiComment)el.copy()); - List beforeSeparator = ContainerUtil.map(myBeforeSeparator, el -> (PsiComment)el.copy()); - return new SortableEntry(myElement.copy(), beforeSeparator, afterSeparator); - } } private static class SortableList { @@ -396,7 +390,7 @@ public class SortContentAction extends PsiElementBaseIntentionAction { ReadStateMachine(@NotNull PsiElement current, @NotNull SortingStrategy strategy, - @NotNull Sortable block) { + @NotNull Sortable block) { // Expect that current element is myCurrent = current; myStrategy = strategy; @@ -655,7 +649,8 @@ public class SortContentAction extends PsiElementBaseIntentionAction { } } - private static class AnnotationArraySortable extends ElementBasedSortable { + private static class AnnotationArraySortable extends Sortable { + @Override boolean isEnd(@NotNull PsiElement element) { return element instanceof PsiJavaToken && ((PsiJavaToken)element).getTokenType() == JavaTokenType.RBRACE; @@ -667,9 +662,39 @@ public class SortContentAction extends PsiElementBaseIntentionAction { return EXPRESSION_SORTING_STRATEGIES; } + @Nullable + @Override + PsiArrayInitializerMemberValue getContext(@NotNull PsiElement origin) { + return PsiTreeUtil.getParentOfType(origin, PsiArrayInitializerMemberValue.class); + } + + @NotNull + @Override + List getElements(@NotNull PsiArrayInitializerMemberValue context) { + return Arrays.asList(context.getInitializers()); + } @Override - String generateReplacementText(@NotNull SortableList list, @NotNull PsiArrayInitializerMemberValue elementToSort) { + PsiElement getFirst(PsiArrayInitializerMemberValue context) { + return context.getFirstChild(); + } + + @Override + void replaceWithSorted(PsiElement origin) { + PsiArrayInitializerMemberValue context = getContext(origin); + if (context == null) return; + SortableList sortableList = readEntries(context); + if (sortableList == null) return; + sortableList.sort(); + String replacement = generateReplacementText(sortableList); + PsiElementFactory factory = JavaPsiFacade.getElementFactory(origin.getProject()); + PsiAnnotation annotation = factory.createAnnotationFromText("@Ann(" + replacement + ")", null); + PsiAnnotationMemberValue replacementElement = annotation.getParameterList().getAttributes()[0].getValue(); + assert replacementElement != null; + context.replace(replacementElement); + } + + String generateReplacementText(@NotNull SortableList list) { StringBuilder sb = new StringBuilder(); boolean newLineRequired = list.generate(sb); if (newLineRequired) { @@ -678,17 +703,6 @@ public class SortContentAction extends PsiElementBaseIntentionAction { sb.append("}"); return sb.toString(); } - - @Nullable - @Override - PsiArrayInitializerMemberValue getElementToSort(@NotNull PsiElement origin) { - return PsiTreeUtil.getParentOfType(origin, PsiArrayInitializerMemberValue.class); - } - - @Override - List getElements(@NotNull PsiArrayInitializerMemberValue elementToSort) { - return Arrays.asList(elementToSort.getInitializers()); - } } private static class VarargSortable extends Sortable { diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/sortContent/afterWithoutFormatter.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/sortContent/afterWithoutFormatter.java new file mode 100644 index 000000000000..0645cd216927 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/sortContent/afterWithoutFormatter.java @@ -0,0 +1,19 @@ +// "Sort content" "true" + +interface A { + + // @formatter:off + + int TO_UPPER = 0; + int TO_LOWER = 1; + int DO_NOT_CHANGE = 2; + int TO_TITLE = 5; + + @Anno(intValues = {TO_UPPER,TO_LOWER,DO_NOT_CHANGE,TO_TITLE}) + void foo(); + + @interface Anno { + int[] intValues(); + } + +} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/sortContent/beforeWithoutFormatter.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/sortContent/beforeWithoutFormatter.java new file mode 100644 index 000000000000..ca3ddd30e170 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/sortContent/beforeWithoutFormatter.java @@ -0,0 +1,19 @@ +// "Sort content" "true" + +interface A { + + // @formatter:off + + int TO_UPPER = 0; + int TO_LOWER = 1; + int DO_NOT_CHANGE = 2; + int TO_TITLE = 5; + + @Anno(intValues = {TO_UPPER, TO_LOWER, TO_TITLE, DO_NOT_CHANGE}) + void foo(); + + @interface Anno { + int[] intValues(); + } + +}