From 9861a58400ba16d1f85537baf5d555613e0bdb1b Mon Sep 17 00:00:00 2001 From: Nikolai Kosjar Date: Fri, 16 Dec 2016 15:30:08 +0100 Subject: [PATCH] CppTools: Prioritize project parts with selectedForBuilding=true ...when selecting one for the editor document. This flag is only set by the QmakeProject. Change-Id: I648886e12148bd1ebeccca52d9faaf4b528597c9 Reviewed-by: David Schulz --- .../cpptools/cppprojectpartchooser.cpp | 31 ++++++++++++++----- .../unittest/cppprojectpartchooser-test.cpp | 14 +++++++++ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/plugins/cpptools/cppprojectpartchooser.cpp b/src/plugins/cpptools/cppprojectpartchooser.cpp index 11e4ad01f25..19c40c5fe0f 100644 --- a/src/plugins/cpptools/cppprojectpartchooser.cpp +++ b/src/plugins/cpptools/cppprojectpartchooser.cpp @@ -31,12 +31,29 @@ namespace CppTools { namespace Internal { -static ProjectPart::Ptr selectFromActiveProject(const QList &projectParts, - const ProjectExplorer::Project *activeProject) +static int priority(const ProjectPart &projectPart, const ProjectExplorer::Project *activeProject) { - return Utils::findOr(projectParts, projectParts.first(), [&](const ProjectPart::Ptr &projectPart){ - return projectPart->project == activeProject; - }); + int thePriority = 0; + + if (projectPart.project == activeProject) + thePriority += 10; + + if (projectPart.selectedForBuilding) + thePriority += 1; + + return thePriority; +} + +static ProjectPart::Ptr chooseFromMultiple(const QList &projectParts, + const ProjectExplorer::Project *activeProject) +{ + QList projectPartsPrioritized = projectParts; + const auto lessThan = [activeProject] (const ProjectPart::Ptr &p1, const ProjectPart::Ptr &p2) { + return priority(*p1, activeProject) > priority(*p2, activeProject); + }; + std::stable_sort(projectPartsPrioritized.begin(), projectPartsPrioritized.end(), lessThan); + + return projectPartsPrioritized.first(); } ProjectPart::Ptr ProjectPartChooser::choose(const QString &filePath, @@ -68,10 +85,10 @@ ProjectPart::Ptr ProjectPartChooser::choose(const QString &filePath, // Fall-back step 2: Use fall-back part from the model manager: projectPart = m_fallbackProjectPart(); else - projectPart = selectFromActiveProject(projectParts, activeProject); + projectPart = chooseFromMultiple(projectParts, activeProject); } else { if (projectHasChanged || !projectParts.contains(projectPart)) - projectPart = selectFromActiveProject(projectParts, activeProject); + projectPart = chooseFromMultiple(projectParts, activeProject); } return projectPart; diff --git a/tests/unit/unittest/cppprojectpartchooser-test.cpp b/tests/unit/unittest/cppprojectpartchooser-test.cpp index fd1456a038d..a8b47d6a85a 100644 --- a/tests/unit/unittest/cppprojectpartchooser-test.cpp +++ b/tests/unit/unittest/cppprojectpartchooser-test.cpp @@ -89,6 +89,20 @@ TEST_F(ProjectPartChooser, ForMultipleChooseFromActiveProject) ASSERT_THAT(chosen, Eq(secondProjectPart)); } +TEST_F(ProjectPartChooser, ForMultiplePreferSelectedForBuilding) +{ + const ProjectPart::Ptr firstProjectPart{new ProjectPart}; + const ProjectPart::Ptr secondProjectPart{new ProjectPart}; + firstProjectPart->selectedForBuilding = false; + secondProjectPart->selectedForBuilding = true; + projectPartsForFile += firstProjectPart; + projectPartsForFile += secondProjectPart; + + const ProjectPart::Ptr chosen = choose(); + + ASSERT_THAT(chosen, Eq(secondProjectPart)); +} + TEST_F(ProjectPartChooser, ForMultipleFromDependenciesChooseFromActiveProject) { const QList projectParts = createProjectPartsWithDifferentProjects();