mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-20 05:21:29 +07:00
[java-inspections] ExtractSetFromComparisonChainAction: preview-friendly+tests
GitOrigin-RevId: 4749198f7b5c3aa0da3efe82eafd8161b429b789
This commit is contained in:
committed by
intellij-monorepo-bot
parent
dd689d3a39
commit
11130867f8
@@ -4,11 +4,13 @@ package com.intellij.codeInsight.intention.impl;
|
||||
import com.intellij.codeInsight.FileModificationService;
|
||||
import com.intellij.codeInsight.PsiEquivalenceUtil;
|
||||
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
|
||||
import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo;
|
||||
import com.intellij.codeInspection.RemoveRedundantTypeArgumentsUtil;
|
||||
import com.intellij.java.JavaBundle;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.application.ApplicationNamesInfo;
|
||||
import com.intellij.openapi.application.WriteAction;
|
||||
import com.intellij.openapi.editor.CaretModel;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.ui.Messages;
|
||||
@@ -63,12 +65,25 @@ public class ExtractSetFromComparisonChainAction extends PsiElementBaseIntention
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull IntentionPreviewInfo generatePreview(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
|
||||
PsiElement element = file.findElementAt(editor.getCaretModel().getOffset());
|
||||
if (element == null) return IntentionPreviewInfo.EMPTY;
|
||||
extract(project, editor, element);
|
||||
return IntentionPreviewInfo.DIFF;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
|
||||
if (!FileModificationService.getInstance().preparePsiElementForWrite(element)) return;
|
||||
|
||||
PsiDocumentManager.getInstance(project).commitAllDocuments();
|
||||
|
||||
extract(project, editor, element);
|
||||
}
|
||||
|
||||
private void extract(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
|
||||
boolean preview = !element.isPhysical();
|
||||
List<ExpressionToConstantComparison> comparisons = comparisons(element).toList();
|
||||
if (comparisons.size() < 2) return;
|
||||
PsiClass containingClass = ClassUtils.getContainingStaticClass(element);
|
||||
@@ -107,11 +122,16 @@ public class ExtractSetFromComparisonChainAction extends PsiElementBaseIntention
|
||||
}
|
||||
}
|
||||
Extractor extractor = new Extractor();
|
||||
WriteAction.run(extractor);
|
||||
if (preview) {
|
||||
extractor.run();
|
||||
}
|
||||
else {
|
||||
WriteAction.run(extractor);
|
||||
}
|
||||
PsiElement result = extractor.resultPtr == null ? null : extractor.resultPtr.getElement();
|
||||
if (result == null || !result.isValid()) return;
|
||||
|
||||
if (!copies.isEmpty()) {
|
||||
if (!copies.isEmpty() && !preview) {
|
||||
int answer = ApplicationManager.getApplication().isUnitTestMode() ? Messages.YES :
|
||||
Messages.showYesNoDialog(project,
|
||||
JavaBundle.message("intention.extract.set.from.comparison.chain.duplicates",
|
||||
|
||||
@@ -3,7 +3,7 @@ import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
// "Extract Set from comparison chain" "true"
|
||||
// "Extract Set from comparison chain" "true-preview"
|
||||
public class Test {
|
||||
private static final Set<String> NAMES = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("foo", "bar", "baz")));
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
// "Extract Set from comparison chain" "true"
|
||||
// "Extract Set from comparison chain" "true-preview"
|
||||
public class Test {
|
||||
private static final Set<String> NAMES = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("foo", "bar", "baz")));
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Extract Set from comparison chain" "true"
|
||||
// "Extract Set from comparison chain" "true-preview"
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Extract Set from comparison chain" "true"
|
||||
// "Extract Set from comparison chain" "true-preview"
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Extract Set from comparison chain" "true"
|
||||
// "Extract Set from comparison chain" "true-preview"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Extract Set from comparison chain" "true"
|
||||
// "Extract Set from comparison chain" "true-preview"
|
||||
|
||||
package com.google.common.collect;
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
// "Extract Set from comparison chain" "true"
|
||||
// "Extract Set from comparison chain" "true-preview"
|
||||
public interface Test {
|
||||
Set<String> S = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("foo", "bar", "baz")));
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
// "Extract Set from comparison chain" "true"
|
||||
// "Extract Set from comparison chain" "true-preview"
|
||||
public class Test {
|
||||
private static final Set S = Collections.unmodifiableSet(new HashSet(Arrays.asList(new String[]{"foo", "bar", "baz"})));
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import java.util.Set;
|
||||
|
||||
// "Extract Set from comparison chain" "true"
|
||||
// "Extract Set from comparison chain" "true-preview"
|
||||
public class Test {
|
||||
private static final Set<String> PROPERTIES = Set.of("foo", "bar", "baz");
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
// "Extract Set from comparison chain" "true"
|
||||
// "Extract Set from comparison chain" "true-preview"
|
||||
public class Test {
|
||||
public static final String BAR = "bar";
|
||||
private static final Set<String> PROPERTIES = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("foo", BAR, "baz")));
|
||||
|
||||
@@ -3,7 +3,7 @@ import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
// "Extract Set from comparison chain" "true"
|
||||
// "Extract Set from comparison chain" "true-preview"
|
||||
public class Test {
|
||||
private static final Set<String> S = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("foo", "bar", "baz")));
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Extract Set from comparison chain" "true"
|
||||
// "Extract Set from comparison chain" "true-preview"
|
||||
import java.util.*;
|
||||
|
||||
public class Test {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Extract Set from comparison chain" "true"
|
||||
// "Extract Set from comparison chain" "true-preview"
|
||||
public class Test {
|
||||
void testOr(String name) {
|
||||
if(name == null || <caret>"foo".equals(name) || "bar".equals(name) || "baz".equals(name)) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Extract Set from comparison chain" "true"
|
||||
// "Extract Set from comparison chain" "true-preview"
|
||||
public class Test {
|
||||
interface Person {
|
||||
String getName();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Extract Set from comparison chain" "true"
|
||||
// "Extract Set from comparison chain" "true-preview"
|
||||
|
||||
public class Test {
|
||||
enum Status {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Extract Set from comparison chain" "true"
|
||||
// "Extract Set from comparison chain" "true-preview"
|
||||
|
||||
public class Test {
|
||||
enum Status {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Extract Set from comparison chain" "true"
|
||||
// "Extract Set from comparison chain" "true-preview"
|
||||
|
||||
public class Test {
|
||||
enum Status {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Extract Set from comparison chain" "true"
|
||||
// "Extract Set from comparison chain" "true-preview"
|
||||
|
||||
package com.google.common.collect;
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Extract Set from comparison chain" "true"
|
||||
// "Extract Set from comparison chain" "true-preview"
|
||||
public interface Test {
|
||||
default void testOr(String s) {
|
||||
if("foo"<caret>.equals(s) || "bar".equals(s) || "baz".equals(s)) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Extract Set from comparison chain" "true"
|
||||
// "Extract Set from comparison chain" "true-preview"
|
||||
public class Test {
|
||||
void testOr(String s) {
|
||||
if("foo"<caret>.equals(s) || "bar".equals(s) || "baz".equals(s)) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Extract Set from comparison chain" "true"
|
||||
// "Extract Set from comparison chain" "true-preview"
|
||||
public class Test {
|
||||
void testOr(int i, String property) {
|
||||
if(i > 0 || "foo"<caret>.equals(property) || "bar".equals(property) || "baz".equals(property) || i == -10) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Extract Set from comparison chain" "true"
|
||||
// "Extract Set from comparison chain" "true-preview"
|
||||
public class Test {
|
||||
public static final String BAR = "bar";
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Extract Set from comparison chain" "true"
|
||||
// "Extract Set from comparison chain" "true-preview"
|
||||
public class Test {
|
||||
void testOr(String s) {
|
||||
if("foo"<caret>.equals(s) || "bar".equals(s) || "baz".equals(s)) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "Extract Set from comparison chain" "true"
|
||||
// "Extract Set from comparison chain" "true-preview"
|
||||
import java.util.Objects;
|
||||
|
||||
public class Test {
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
// "Extract Set from comparison chain" "true-preview"
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class Test {
|
||||
private static final Set<Status> STATUSES = Collections.unmodifiableSet(EnumSet.of(Status.VALID, Status.PENDING));
|
||||
|
||||
enum Status {
|
||||
VALID, PENDING, INVALID, UNKNOWN;
|
||||
}
|
||||
|
||||
void test1(Status status) {
|
||||
if(STATUSES.contains(status)) {
|
||||
System.out.println("ok");
|
||||
}
|
||||
}
|
||||
|
||||
static class Another {
|
||||
static final String STATUSES = "";
|
||||
|
||||
void test2(Status st) {
|
||||
if(st == null || Status.PENDING == st || Status.VALID == st || Math.random() > 0.5) {
|
||||
System.out.println("Replace here as well");
|
||||
}
|
||||
}
|
||||
|
||||
void test3(Status st2) {
|
||||
if(st2 == Status.VALID || st2 == Status.PENDING || st2 == Status.UNKNOWN) {
|
||||
System.out.println("Do not replace as we test three statuses");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user