From 2d52e24a33ec167c6a47257ebcc67be544a14fd0 Mon Sep 17 00:00:00 2001 From: Aleksey Pivovarov Date: Tue, 26 Feb 2019 23:20:40 +0300 Subject: [PATCH] goto action: do not show duplicated actions in "Go To Action" completion ActionWrappers with same AnAction and different MatchMode should not be shown together. --- .../ide/util/gotoByName/GotoActionTest.groovy | 21 ++++++++++--------- .../gotoByName/GotoActionItemProvider.java | 6 +++--- .../ide/util/gotoByName/GotoActionModel.java | 16 +++++++------- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/java/java-tests/testSrc/com/intellij/ide/util/gotoByName/GotoActionTest.groovy b/java/java-tests/testSrc/com/intellij/ide/util/gotoByName/GotoActionTest.groovy index 805817e4040f..fcc5f315e606 100644 --- a/java/java-tests/testSrc/com/intellij/ide/util/gotoByName/GotoActionTest.groovy +++ b/java/java-tests/testSrc/com/intellij/ide/util/gotoByName/GotoActionTest.groovy @@ -14,6 +14,7 @@ import com.intellij.openapi.project.Project import com.intellij.openapi.util.Computable import com.intellij.testFramework.PlatformTestUtil import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase +import gnu.trove.Equality import groovy.transform.CompileStatic import org.jetbrains.annotations.NonNls import org.jetbrains.annotations.NotNull @@ -26,13 +27,17 @@ import java.util.concurrent.TimeUnit @CompileStatic class GotoActionTest extends LightCodeInsightFixtureTestCase { private static final DataKey SHOW_HIDDEN_KEY = DataKey.create("GotoActionTest.DataKey") + private static final Comparator MATCH_COMPARATOR = + { MatchedValue item1, MatchedValue item2 -> return item1.compareWeights(item2) } as Comparator + private static final Equality MATCH_EQUALITY = + { MatchedValue item1, MatchedValue item2 -> item1 == item2 } as Equality void "test shorter actions first despite ellipsis"() { def pattern = 'Rebas' def fork = 'Rebase my GitHub fork' def rebase = 'Rebase...' def items = [matchedAction(fork, pattern), - matchedAction(rebase, pattern)].sort() + matchedAction(rebase, pattern)].toSorted(MATCH_COMPARATOR) assert [rebase, fork] == items.collect { it.valueText } } @@ -41,7 +46,7 @@ class GotoActionTest extends LightCodeInsightFixtureTestCase { def byName = 'By Name' def byDesc = 'By Desc' def items = [matchedAction(byName, pattern), - matchedAction(byDesc, pattern, MatchMode.DESCRIPTION)].sort() + matchedAction(byDesc, pattern, MatchMode.DESCRIPTION)].toSorted(MATCH_COMPARATOR) assert [byName, byDesc] == items.collect { it.valueText } } @@ -60,7 +65,7 @@ class GotoActionTest extends LightCodeInsightFixtureTestCase { matchedAction(eclaire, pattern), matchedAction(deaf, pattern), matchedAction(cut, pattern), - matchedAction(c, pattern)].sort() + matchedAction(c, pattern)].toSorted(MATCH_COMPARATOR) assert [c, copy, cut, aardvark, eclaire, boom, deaf] == items.collect { it.valueText } } @@ -86,20 +91,16 @@ class GotoActionTest extends LightCodeInsightFixtureTestCase { items += matchedBooleanOption(name, pattern) } - PlatformTestUtil.assertComparisonContractNotViolated(items, - { def item1, def item2 -> (item1 <=> item2) }, - { def item1, def item2 -> item1 == item2 }) + PlatformTestUtil.assertComparisonContractNotViolated(items, MATCH_COMPARATOR, MATCH_EQUALITY) // order can be different on EDT and pooled threads ApplicationManager.getApplication().executeOnPooledThread { - PlatformTestUtil.assertComparisonContractNotViolated(items, - { def item1, def item2 -> (item1 <=> item2) }, - { def item1, def item2 -> item1 == item2 }) + PlatformTestUtil.assertComparisonContractNotViolated(items, MATCH_COMPARATOR, MATCH_EQUALITY) }.get(20000, TimeUnit.MILLISECONDS) } void "test same action is not reported twice"() { - def patterns = ["Patch", "Add", "Delete", "Show", "Toggle"] + def patterns = ["Patch", "Add", "Delete", "Show", "Toggle", "New", "New Class"] def model = new GotoActionModel(project, null, null) def provider = new GotoActionItemProvider(model) diff --git a/platform/lang-impl/src/com/intellij/ide/util/gotoByName/GotoActionItemProvider.java b/platform/lang-impl/src/com/intellij/ide/util/gotoByName/GotoActionItemProvider.java index f5432bb0bff7..a238f6630e16 100644 --- a/platform/lang-impl/src/com/intellij/ide/util/gotoByName/GotoActionItemProvider.java +++ b/platform/lang-impl/src/com/intellij/ide/util/gotoByName/GotoActionItemProvider.java @@ -126,7 +126,7 @@ public class GotoActionItemProvider implements ChooseByNameItemProvider { Map map = myModel.getConfigurablesNames(); SearchableOptionsRegistrarImpl registrar = (SearchableOptionsRegistrarImpl)SearchableOptionsRegistrar.getInstance(); - List options = ContainerUtil.newArrayList(); + List options = ContainerUtil.newArrayList(); final Set words = registrar.getProcessedWords(pattern); Set optionDescriptions = null; final String actionManagerName = myActionManager.getComponentName(); @@ -224,10 +224,10 @@ public class GotoActionItemProvider implements ChooseByNameItemProvider { private final static Logger LOG = Logger.getInstance(GotoActionItemProvider.class); - private static boolean processItems(String pattern, JBIterable items, Processor consumer) { + private static boolean processItems(String pattern, JBIterable items, Processor consumer) { List matched = ContainerUtil.newArrayList(items.map(o -> o instanceof MatchedValue ? (MatchedValue)o : new MatchedValue(o, pattern))); try { - Collections.sort(matched); + Collections.sort(matched, (o1, o2) -> o1.compareWeights(o2)); } catch (IllegalArgumentException e) { LOG.error("Comparison method violates its general contract with pattern '" + pattern + "'", e); diff --git a/platform/lang-impl/src/com/intellij/ide/util/gotoByName/GotoActionModel.java b/platform/lang-impl/src/com/intellij/ide/util/gotoByName/GotoActionModel.java index ed516352137f..04cc0d509cf1 100644 --- a/platform/lang-impl/src/com/intellij/ide/util/gotoByName/GotoActionModel.java +++ b/platform/lang-impl/src/com/intellij/ide/util/gotoByName/GotoActionModel.java @@ -167,7 +167,7 @@ public class GotoActionModel implements ChooseByNameModel, Comparator, D public void saveInitialCheckBoxState(boolean state) { } - public static class MatchedValue implements Comparable { + public static class MatchedValue { @NotNull public final Object value; @NotNull final String pattern; @@ -207,8 +207,7 @@ public class GotoActionModel implements ChooseByNameModel, Comparator, D return 0; } - @Override - public int compareTo(@NotNull MatchedValue o) { + public int compareWeights(@NotNull MatchedValue o) { if (o == this) return 0; int diff = o.getMatchingDegree() - getMatchingDegree(); if (diff != 0) return diff; @@ -219,7 +218,7 @@ public class GotoActionModel implements ChooseByNameModel, Comparator, D if (value instanceof ActionWrapper && o.value instanceof ActionWrapper) { ActionWrapper value1 = (ActionWrapper)value; ActionWrapper value2 = (ActionWrapper)o.value; - int compared = value1.compareTo(value2); + int compared = value1.compareWeights(value2); if (compared != 0) return compared; } @@ -299,7 +298,7 @@ public class GotoActionModel implements ChooseByNameModel, Comparator, D public int compare(@NotNull Object o1, @NotNull Object o2) { if (ChooseByNameBase.EXTRA_ELEM.equals(o1)) return 1; if (ChooseByNameBase.EXTRA_ELEM.equals(o2)) return -1; - return ((MatchedValue)o1).compareTo((MatchedValue)o2); + return ((MatchedValue)o1).compareWeights((MatchedValue)o2); } @NotNull @@ -587,7 +586,7 @@ public class GotoActionModel implements ChooseByNameModel, Comparator, D } } - public static class ActionWrapper implements Comparable { + public static class ActionWrapper { @NotNull private final AnAction myAction; @NotNull private final MatchMode myMode; @Nullable private final GroupMapping myGroupMapping; @@ -617,8 +616,7 @@ public class GotoActionModel implements ChooseByNameModel, Comparator, D return myMode; } - @Override - public int compareTo(@NotNull ActionWrapper o) { + public int compareWeights(@NotNull ActionWrapper o) { int compared = myMode.compareTo(o.getMode()); if (compared != 0) return compared; Presentation myPresentation = myAction.getTemplatePresentation(); @@ -679,7 +677,7 @@ public class GotoActionModel implements ChooseByNameModel, Comparator, D @Override public boolean equals(Object obj) { - return obj instanceof ActionWrapper && compareTo((ActionWrapper)obj) == 0; + return obj instanceof ActionWrapper && myAction.equals(((ActionWrapper)obj).myAction); } @Override