Commit 08c681c4 authored by peter's avatar peter
Browse files

IDEA-185564 Match Refactor | Extract | Method ... with "refactor method" in Find action

parent 29332ace
Branches unavailable Tags unavailable
No related merge requests found
Showing with 53 additions and 56 deletions
+53 -56
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.java.navigation
// 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.
package com.intellij.ide.util.gotoByName
import com.intellij.ide.util.gotoByName.GotoActionModel
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
/**
* @author peter
*/
......@@ -57,6 +42,17 @@ class GotoActionTest extends LightCodeInsightFixtureTestCase {
matchedValue(eclaire, pattern), matchedValue(deaf, pattern), matchedValue(cut, pattern), matchedValue(c, pattern)].sort()
assert [c, copy, cut, aardvark, eclaire, boom, deaf] == items.collect { it.valueText }
}
void "test match action by parent and grandparent group name"() {
def extractMethod = ActionManager.instance.getAction("ExtractMethod")
assert actionMatches('method', extractMethod) == GotoActionModel.MatchMode.NAME
assert actionMatches('extract method', extractMethod) == GotoActionModel.MatchMode.GROUP
assert actionMatches('refactor method', extractMethod) == GotoActionModel.MatchMode.GROUP
}
def actionMatches(String pattern, AnAction action) {
return new GotoActionModel(project, null, null).actionMatches(pattern, GotoActionItemProvider.buildMatcher(pattern), action)
}
def matchedValue(String fork, String pattern) {
matchedValue(fork, pattern, GotoActionModel.MatchMode.NAME)
......
......@@ -178,7 +178,7 @@ public class GotoActionItemProvider implements ChooseByNameItemProvider {
private boolean processActions(String pattern, Processor<MatchedValue> consumer, DataContext dataContext) {
Set<String> ids = ((ActionManagerImpl)myActionManager).getActionIds();
JBIterable<AnAction> actions = JBIterable.from(ids).filterMap(myActionManager::getAction);
MinusculeMatcher matcher = NameUtil.buildMatcher("*" + pattern, NameUtil.MatchingCaseSensitivity.NONE);
MinusculeMatcher matcher = buildMatcher(pattern);
QuickActionProvider provider = dataContext.getData(QuickActionProvider.KEY);
if (provider != null) {
......@@ -188,13 +188,18 @@ public class GotoActionItemProvider implements ChooseByNameItemProvider {
JBIterable<ActionWrapper> actionWrappers = actions.unique().filterMap(action -> {
MatchMode mode = myModel.actionMatches(pattern, matcher, action);
if (mode == MatchMode.NONE) return null;
return new ActionWrapper(action, myModel.myActionGroups.get(action), mode, dataContext, myModel);
return new ActionWrapper(action, myModel.getAnyGroupPath(action), mode, dataContext, myModel);
});
return processItems(pattern, actionWrappers, consumer);
}
@NotNull
static MinusculeMatcher buildMatcher(String pattern) {
return NameUtil.buildMatcher("*" + pattern, NameUtil.MatchingCaseSensitivity.NONE);
}
private boolean processIntentions(String pattern, Processor<MatchedValue> consumer, DataContext dataContext) {
MinusculeMatcher matcher = NameUtil.buildMatcher("*" + pattern, NameUtil.MatchingCaseSensitivity.NONE);
MinusculeMatcher matcher = buildMatcher(pattern);
Map<String, ApplyIntentionAction> intentionMap = myIntentions.getValue();
JBIterable<ActionWrapper> intentions = JBIterable.from(intentionMap.keySet())
.filterMap(intentionText -> {
......@@ -207,7 +212,7 @@ public class GotoActionItemProvider implements ChooseByNameItemProvider {
@NotNull
private ActionWrapper wrapAnAction(@NotNull AnAction action, DataContext dataContext) {
return new ActionWrapper(action, myModel.myActionGroups.get(action), MatchMode.NAME, dataContext, myModel);
return new ActionWrapper(action, myModel.getAnyGroupPath(action), MatchMode.NAME, dataContext, myModel);
}
private final static Logger LOG = Logger.getInstance(GotoActionItemProvider.class);
......
......@@ -51,6 +51,7 @@ import com.intellij.util.ObjectUtils;
import com.intellij.util.concurrency.Semaphore;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.ContainerUtilRt;
import com.intellij.util.containers.MultiMap;
import com.intellij.util.ui.EmptyIcon;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.UIUtil;
......@@ -81,7 +82,7 @@ public class GotoActionModel implements ChooseByNameModel, Comparator<Object>, D
private static final Icon EMPTY_ICON = EmptyIcon.ICON_18;
protected final Map<AnAction, String> myActionGroups = ContainerUtil.newHashMap();
private final MultiMap<AnAction, String> myActionGroups = MultiMap.create();
private final NotNullLazyValue<Map<String, String>> myConfigurablesNames = VolatileNotNullLazyValue.createValue(() -> {
Map<String, String> map = ContainerUtil.newTroveMap();
......@@ -106,7 +107,7 @@ public class GotoActionModel implements ChooseByNameModel, Comparator<Object>, D
myModality = modalityState;
ActionGroup mainMenu = (ActionGroup)myActionManager.getActionOrStub(IdeActions.GROUP_MAIN_MENU);
assert mainMenu != null;
collectActions(myActionGroups, mainMenu, mainMenu.getTemplatePresentation().getText());
collectActions(mainMenu, "");
}
@NotNull
......@@ -319,24 +320,25 @@ public class GotoActionModel implements ChooseByNameModel, Comparator<Object>, D
return myConfigurablesNames.getValue();
}
private void collectActions(@NotNull Map<AnAction, String> result, @NotNull ActionGroup group, @Nullable String containingGroupName) {
private void collectActions(@NotNull ActionGroup group, @NotNull String path) {
AnAction[] actions = group.getChildren(null);
includeGroup(result, group, actions, containingGroupName);
if (!groupHasMeaningfulChildren(actions)) {
myActionGroups.putValue(group, path);
}
for (AnAction action : actions) {
if (action == null || action instanceof Separator) continue;
if (action instanceof ActionGroup) {
ActionGroup actionGroup = (ActionGroup)action;
String groupName = actionGroup.getTemplatePresentation().getText();
collectActions(result, actionGroup, getGroupName(StringUtil.isEmpty(groupName) || !actionGroup.isPopup() ? containingGroupName : groupName));
String groupName = getGroupName(actionGroup.getTemplatePresentation().getText());
String childPath = StringUtil.isEmpty(groupName) || !actionGroup.isPopup() ? path :
path.isEmpty() ? groupName :
path + " | " + groupName;
collectActions(actionGroup, childPath);
}
else {
String groupName = group.getTemplatePresentation().getText();
if (result.containsKey(action)) {
result.put(action, null);
}
else {
result.put(action, getGroupName(StringUtil.isEmpty(groupName) ? containingGroupName : groupName));
}
myActionGroups.putValue(action, path);
}
}
}
......@@ -350,20 +352,13 @@ public class GotoActionModel implements ChooseByNameModel, Comparator<Object>, D
return groupName;
}
private void includeGroup(@NotNull Map<AnAction, String> result,
@NotNull ActionGroup group,
@NotNull AnAction[] actions,
@Nullable String containingGroupName) {
boolean showGroup = true;
for (AnAction action : actions) {
if (myActionManager.getId(action) != null) {
showGroup = false;
break;
}
}
if (showGroup) {
result.put(group, getGroupName(containingGroupName));
}
private boolean groupHasMeaningfulChildren(AnAction[] children) {
return Arrays.stream(children).anyMatch(action -> myActionManager.getId(action) != null);
}
@Nullable
String getAnyGroupPath(@NotNull AnAction action) {
return ContainerUtil.getFirstItem(myActionGroups.get(action));
}
@Override
......@@ -394,7 +389,6 @@ public class GotoActionModel implements ChooseByNameModel, Comparator<Object>, D
Presentation presentation = anAction.getTemplatePresentation();
String text = presentation.getText();
String description = presentation.getDescription();
String groupName = myActionGroups.get(anAction);
if (text != null && matcher.matches(text)) {
return MatchMode.NAME;
}
......@@ -404,11 +398,13 @@ public class GotoActionModel implements ChooseByNameModel, Comparator<Object>, D
if (text == null) {
return MatchMode.NONE;
}
if (matcher.matches(groupName + " " + text)) {
return anAction instanceof ToggleAction ? MatchMode.NAME : MatchMode.GROUP;
}
if (matcher.matches(text + " " + groupName)) {
return MatchMode.GROUP;
for (String groupName : myActionGroups.get(anAction)) {
if (matcher.matches(groupName + " " + text)) {
return anAction instanceof ToggleAction ? MatchMode.NAME : MatchMode.GROUP;
}
if (matcher.matches(text + " " + groupName)) {
return MatchMode.GROUP;
}
}
for (GotoActionAliasMatcher m : GotoActionAliasMatcher.EP_NAME.getExtensions()) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment