[vcs]: add favourite branch comparator and correct more actions place

* (IDEA-154756) Allow the user to choose top favorites branches;
This commit is contained in:
Nadya Zabrodina
2017-01-11 16:45:33 +03:00
parent 9bc60ea17f
commit d3f6e0a13f
3 changed files with 59 additions and 12 deletions
@@ -0,0 +1,30 @@
/*
* Copyright 2000-2016 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.dvcs.ui;
import java.util.Comparator;
import java.util.List;
public class BranchActionUtil {
public static final Comparator<BranchActionGroup> FAVOURITE_BRANCH_COMPARATOR = (o1, o2) -> {
if (o1.isFavourite() ^ o2.isFavourite()) return o1.isFavourite() ? -1 : 1;
return 0;
};
public static int getNumOfFavourites(List<? extends BranchActionGroup> branchActions) {
return (int)branchActions.stream().filter(BranchActionGroup::isFavourite).count();
}
}
@@ -18,6 +18,7 @@ package git4idea.ui.branch;
import com.intellij.dvcs.DvcsUtil;
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.AnAction;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
@@ -37,6 +38,8 @@ 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.BranchActionUtil.FAVOURITE_BRANCH_COMPARATOR;
import static com.intellij.dvcs.ui.BranchActionUtil.getNumOfFavourites;
import static java.util.stream.Collectors.toList;
/**
@@ -111,18 +114,24 @@ class GitBranchPopup extends DvcsBranchPopup<GitRepository> {
popupGroup.addAll(createRepositoriesActions());
popupGroup.addSeparator("Common Local Branches");
List<AnAction> localBranchActions =
List<BranchActionGroup> localBranchActions =
myMultiRootBranchConfig.getLocalBranchNames().stream().map(l -> createLocalBranchActions(allRepositories, l)).filter(Objects::nonNull)
.collect(toList());
addMoreActionIfNeeded(localBranchActions, MAX_BRANCH_NUM);
popupGroup.addAll(localBranchActions);
int numOfFavourites = getNumOfFavourites(localBranchActions);
List<AnAction> localBranchPresentationList =
localBranchActions.stream().sorted(FAVOURITE_BRANCH_COMPARATOR).collect(toList());
addMoreActionIfNeeded(localBranchPresentationList, numOfFavourites > MAX_BRANCH_NUM ? numOfFavourites : MAX_BRANCH_NUM);
popupGroup.addAll(localBranchPresentationList);
popupGroup.addSeparator("Common Remote Branches");
List<AnAction> remoteBranchActions =
List<BranchActionGroup> remoteBranchActions =
((GitMultiRootBranchConfig)myMultiRootBranchConfig).getRemoteBranches().stream()
.map(r -> new GitBranchPopupActions.RemoteBranchActions(myProject, allRepositories, r, myCurrentRepository)).collect(toList());
addMoreActionIfNeeded(remoteBranchActions, MAX_BRANCH_NUM);
popupGroup.addAll(remoteBranchActions);
numOfFavourites = getNumOfFavourites(remoteBranchActions);
List<AnAction> remoteBranchPresentationList =
remoteBranchActions.stream().sorted(FAVOURITE_BRANCH_COMPARATOR).collect(toList());
addMoreActionIfNeeded(remoteBranchPresentationList, numOfFavourites > MAX_BRANCH_NUM ? numOfFavourites : MAX_BRANCH_NUM);
popupGroup.addAll(remoteBranchPresentationList);
}
@Nullable
@@ -41,6 +41,8 @@ import java.util.stream.Collectors;
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.BranchActionUtil.FAVOURITE_BRANCH_COMPARATOR;
import static com.intellij.dvcs.ui.BranchActionUtil.getNumOfFavourites;
import static git4idea.GitStatisticsCollectorKt.reportUsage;
import static git4idea.branch.GitBranchType.GIT_LOCAL;
import static git4idea.branch.GitBranchType.GIT_REMOTE;
@@ -71,19 +73,25 @@ class GitBranchPopupActions {
}
popupGroup.addSeparator("Local Branches" + repoInfo);
List<AnAction> localBranchActions =
List<BranchActionGroup> localBranchActions =
myRepository.getBranches().getLocalBranches().stream().sorted().filter(l -> !l.equals(myRepository.getCurrentBranch()))
.map(l -> new LocalBranchActions(myProject, repositoryList, l.getName(), myRepository)).collect(
Collectors.toList());
addMoreActionIfNeeded(localBranchActions, MAX_BRANCH_NUM);
popupGroup.addAll(localBranchActions);
int numOfFavourites = getNumOfFavourites(localBranchActions);
List<AnAction> localBranchPresentationList =
localBranchActions.stream().sorted(FAVOURITE_BRANCH_COMPARATOR).collect(Collectors.toList());
addMoreActionIfNeeded(localBranchPresentationList, numOfFavourites > MAX_BRANCH_NUM ? numOfFavourites : MAX_BRANCH_NUM);
popupGroup.addAll(localBranchPresentationList);
popupGroup.addSeparator("Remote Branches" + repoInfo);
List<AnAction> remoteBranchActions = myRepository.getBranches().getRemoteBranches().stream().sorted()
List<BranchActionGroup> remoteBranchActions = myRepository.getBranches().getRemoteBranches().stream().sorted()
.map(r -> new RemoteBranchActions(myProject, repositoryList, r.getName(), myRepository))
.collect(Collectors.toList());
addMoreActionIfNeeded(remoteBranchActions, MAX_BRANCH_NUM);
popupGroup.addAll(remoteBranchActions);
numOfFavourites = getNumOfFavourites(remoteBranchActions);
List<AnAction> remoteBranchPresentationList =
remoteBranchActions.stream().sorted(FAVOURITE_BRANCH_COMPARATOR).collect(Collectors.toList());
addMoreActionIfNeeded(remoteBranchPresentationList, numOfFavourites > MAX_BRANCH_NUM ? numOfFavourites : MAX_BRANCH_NUM);
popupGroup.addAll(remoteBranchPresentationList);
return popupGroup;
}