[vcs]: use actionGroup wrapper instead of global counter of more actions

* optimize unnecessary streamApi usages;
* delete and inline variables;
* avoid data transfer through template presentation;
* (IDEA-147476) Git widget popup rework;
This commit is contained in:
Nadya Zabrodina
2017-01-11 16:45:33 +03:00
committed by Nadya Zabrodina
parent f757394561
commit 17e9197960
3 changed files with 54 additions and 41 deletions
@@ -54,7 +54,6 @@ import static com.intellij.util.ui.UIUtil.DEFAULT_VGAP;
public class BranchActionGroupPopup extends FlatSpeedSearchPopup {
private static final String DIMENSION_SERVICE_KEY = "Vcs.Branch.Popup";
private static final DataKey<ListPopupModel> POPUP_MODEL = DataKey.create("VcsPopupModel");
private int myIgnoreNextItem = 0;
private MyPopupListElementRenderer myListElementRenderer;
public BranchActionGroupPopup(@NotNull String title, @NotNull Project project,
@@ -99,6 +98,9 @@ public class BranchActionGroupPopup extends FlatSpeedSearchPopup {
else if (childGroup instanceof BranchActionGroup) {
speedSearchActions.add(createSpeedSearchActionGroupWrapper(childGroup));
}
else if (childGroup instanceof HideableActionGroup) {
speedSearchActions.add(createSpeedSearchActionGroupWrapper(((HideableActionGroup)childGroup).getDelegate()));
}
}
}
}
@@ -140,12 +142,10 @@ public class BranchActionGroupPopup extends FlatSpeedSearchPopup {
@Override
protected boolean shouldShow(@NotNull AnAction action) {
if (action instanceof MoreAction) {
if (getSpeedSearch().isHoldingFilter() || !action.getTemplatePresentation().isVisible()) return false;
myIgnoreNextItem = ((MoreAction)action).getHiddenNodesNum();
return true;
}
return myIgnoreNextItem-- <= 0;
if (getSpeedSearch().isHoldingFilter()) return true;
if (action instanceof MoreAction) return !((MoreAction)action).myIsExpanded;
if (action instanceof MoreHideableActionGroup) return ((MoreHideableActionGroup)action).shouldBeShown();
return true;
}
@Override
@@ -285,23 +285,17 @@ public class BranchActionGroupPopup extends FlatSpeedSearchPopup {
}
private static class MoreAction extends DumbAwareAction implements KeepingPopupOpenAction {
private static final String MORE_TEXT = "more...";
private final int myHiddenNodesNum;
private boolean myIsExpanded = false;
public MoreAction(int numberOfHiddenNodes) {
super();
myHiddenNodesNum = numberOfHiddenNodes;
String num = myHiddenNodesNum > 0 ? myHiddenNodesNum + " " : "";
getTemplatePresentation().setText(num + MORE_TEXT);
}
public int getHiddenNodesNum() {
return myHiddenNodesNum;
assert numberOfHiddenNodes > 0;
getTemplatePresentation().setText(numberOfHiddenNodes + " more...");
}
@Override
public void actionPerformed(AnActionEvent e) {
getTemplatePresentation().setEnabledAndVisible(false);
myIsExpanded = true;
InputEvent event = e.getInputEvent();
if (event != null && event.getSource() instanceof JComponent) {
DataProvider dataProvider = DataManager.getDataProvider((JComponent)event.getSource());
@@ -312,9 +306,36 @@ public class BranchActionGroupPopup extends FlatSpeedSearchPopup {
}
}
public static void addMoreActionIfNeeded(@NotNull List<AnAction> actionList, int maxIndex) {
interface MoreHideableActionGroup {
boolean shouldBeShown();
}
private static class HideableActionGroup extends EmptyAction.MyDelegatingActionGroup implements MoreHideableActionGroup {
@NotNull private final MoreAction myMoreAction;
private HideableActionGroup(@NotNull ActionGroup actionGroup, @NotNull MoreAction moreAction) {
super(actionGroup);
myMoreAction = moreAction;
}
@Override
public boolean shouldBeShown() {
return myMoreAction.myIsExpanded;
}
}
public static void wrapWithMoreActionIfNeeded(@NotNull DefaultActionGroup parentGroup,
@NotNull List<? extends ActionGroup> actionList,
int maxIndex) {
if (actionList.size() > maxIndex) {
actionList.add(maxIndex, new MoreAction(actionList.size() - maxIndex));
MoreAction moreAction = new MoreAction(actionList.size() - maxIndex);
for (int i = 0; i < actionList.size(); i++) {
parentGroup.add(i < maxIndex ? actionList.get(i) : new HideableActionGroup(actionList.get(i), moreAction));
}
parentGroup.add(moreAction);
}
else {
parentGroup.addAll(actionList);
}
}
}
@@ -20,10 +20,12 @@ import com.intellij.dvcs.branch.DvcsBranchPopup;
import com.intellij.dvcs.repo.AbstractRepositoryManager;
import com.intellij.dvcs.ui.BranchActionGroup;
import com.intellij.dvcs.ui.RootAction;
import com.intellij.openapi.actionSystem.ActionGroup;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Condition;
import com.intellij.util.containers.ContainerUtil;
import git4idea.GitUtil;
import git4idea.branch.GitBranchUtil;
import git4idea.config.GitVcsSettings;
@@ -37,7 +39,7 @@ import java.util.List;
import java.util.Objects;
import static com.intellij.dvcs.branch.DvcsBranchPopup.MyMoreIndex.*;
import static com.intellij.dvcs.ui.BranchActionGroupPopup.addMoreActionIfNeeded;
import static com.intellij.dvcs.ui.BranchActionGroupPopup.wrapWithMoreActionIfNeeded;
import static com.intellij.dvcs.ui.BranchActionUtil.FAVORITE_BRANCH_COMPARATOR;
import static com.intellij.dvcs.ui.BranchActionUtil.getNumOfFavorites;
import static com.intellij.util.containers.ContainerUtil.map;
@@ -119,11 +121,8 @@ class GitBranchPopup extends DvcsBranchPopup<GitRepository> {
myMultiRootBranchConfig.getLocalBranchNames().stream().map(l -> createLocalBranchActions(allRepositories, l)).filter(Objects::nonNull)
.collect(toList());
int numOfFavorites = getNumOfFavorites(localBranchActions);
List<AnAction> localBranchPresentationList =
localBranchActions.stream().sorted(FAVORITE_BRANCH_COMPARATOR).collect(toList());
addMoreActionIfNeeded(localBranchPresentationList, numOfFavorites > MAX_BRANCH_NUM ? numOfFavorites : MAX_BRANCH_NUM);
popupGroup.addAll(localBranchPresentationList);
wrapWithMoreActionIfNeeded(popupGroup, ContainerUtil.sorted(localBranchActions, FAVORITE_BRANCH_COMPARATOR),
numOfFavorites > MAX_BRANCH_NUM ? numOfFavorites : MAX_BRANCH_NUM);
popupGroup.addSeparator("Common Remote Branches");
List<BranchActionGroup> remoteBranchActions = map(((GitMultiRootBranchConfig)myMultiRootBranchConfig).getRemoteBranches(),
remoteBranch -> new GitBranchPopupActions.RemoteBranchActions(myProject,
@@ -131,10 +130,8 @@ class GitBranchPopup extends DvcsBranchPopup<GitRepository> {
remoteBranch,
myCurrentRepository));
numOfFavorites = getNumOfFavorites(remoteBranchActions);
List<AnAction> remoteBranchPresentationList =
remoteBranchActions.stream().sorted(FAVORITE_BRANCH_COMPARATOR).collect(toList());
addMoreActionIfNeeded(remoteBranchPresentationList, numOfFavorites > 0 ? numOfFavorites : MAX_BRANCH_NUM);
popupGroup.addAll(remoteBranchPresentationList);
wrapWithMoreActionIfNeeded(popupGroup, ContainerUtil.sorted(remoteBranchActions, FAVORITE_BRANCH_COMPARATOR),
numOfFavorites > 0 ? numOfFavorites : MAX_BRANCH_NUM);
}
@Nullable
@@ -151,14 +148,11 @@ class GitBranchPopup extends DvcsBranchPopup<GitRepository> {
protected DefaultActionGroup createRepositoriesActions() {
DefaultActionGroup popupGroup = new DefaultActionGroup(null, false);
popupGroup.addSeparator("Repositories");
List<AnAction> rootActions = DvcsUtil.sortRepositories(myRepositoryManager.getRepositories()).stream()
List<ActionGroup> rootActions = DvcsUtil.sortRepositories(myRepositoryManager.getRepositories()).stream()
.map(repo -> new RootAction<>(repo, highlightCurrentRepo() ? myCurrentRepository : null,
new GitBranchPopupActions(repo.getProject(), repo).createActions(),
GitBranchUtil.getDisplayableBranchText(repo))).collect(toList());
if (rootActions.size() > MAX_REPO_NUM) {
addMoreActionIfNeeded(rootActions, DEFAULT_REPO_NUM);
}
popupGroup.addAll(rootActions);
wrapWithMoreActionIfNeeded(popupGroup, rootActions, rootActions.size() > MAX_REPO_NUM ? DEFAULT_REPO_NUM : MAX_REPO_NUM);
return popupGroup;
}
@@ -38,7 +38,7 @@ import java.util.Collections;
import java.util.List;
import static com.intellij.dvcs.branch.DvcsBranchPopup.MyMoreIndex.MAX_BRANCH_NUM;
import static com.intellij.dvcs.ui.BranchActionGroupPopup.addMoreActionIfNeeded;
import static com.intellij.dvcs.ui.BranchActionGroupPopup.wrapWithMoreActionIfNeeded;
import static com.intellij.dvcs.ui.BranchActionUtil.FAVORITE_BRANCH_COMPARATOR;
import static com.intellij.dvcs.ui.BranchActionUtil.getNumOfFavorites;
import static git4idea.GitStatisticsCollectorKt.reportUsage;
@@ -79,10 +79,9 @@ class GitBranchPopupActions {
.map(branch -> new LocalBranchActions(myProject, repositoryList, branch.getName(), myRepository))
.collect(toList());
int numOfFavorites = getNumOfFavorites(localBranchActions);
List<AnAction> localBranchPresentationList = localBranchActions.stream().sorted(FAVORITE_BRANCH_COMPARATOR).collect(toList());
// if there are only a few local favorites -> show several non-favorite; for remotes it's better to show only favorites;
addMoreActionIfNeeded(localBranchPresentationList, numOfFavorites > MAX_BRANCH_NUM ? numOfFavorites : MAX_BRANCH_NUM);
popupGroup.addAll(localBranchPresentationList);
wrapWithMoreActionIfNeeded(popupGroup, ContainerUtil.sorted(localBranchActions, FAVORITE_BRANCH_COMPARATOR),
numOfFavorites > MAX_BRANCH_NUM ? numOfFavorites : MAX_BRANCH_NUM);
popupGroup.addSeparator("Remote Branches" + repoInfo);
List<BranchActionGroup> remoteBranchActions =
@@ -91,9 +90,8 @@ class GitBranchPopupActions {
.map(remoteBranch -> new RemoteBranchActions(myProject, repositoryList, remoteBranch.getName(), myRepository))
.collect(toList());
numOfFavorites = getNumOfFavorites(remoteBranchActions);
List<AnAction> remoteBranchPresentationList = remoteBranchActions.stream().sorted(FAVORITE_BRANCH_COMPARATOR).collect(toList());
addMoreActionIfNeeded(remoteBranchPresentationList, numOfFavorites > 0 ? numOfFavorites : MAX_BRANCH_NUM);
popupGroup.addAll(remoteBranchPresentationList);
wrapWithMoreActionIfNeeded(popupGroup, ContainerUtil.sorted(remoteBranchActions, FAVORITE_BRANCH_COMPARATOR),
numOfFavorites > 0 ? numOfFavorites : MAX_BRANCH_NUM);
return popupGroup;
}