2016-12-16 13:10:43 +01:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
|
**
|
|
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "cppprojectpartchooser.h"
|
|
|
|
|
|
2016-12-16 13:12:04 +01:00
|
|
|
#include <utils/algorithm.h>
|
2016-12-16 13:10:43 +01:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
|
|
|
|
namespace CppTools {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2017-01-17 15:27:31 +01:00
|
|
|
class ProjectPartPrioritizer
|
2017-01-12 18:01:12 +01:00
|
|
|
{
|
2017-01-17 15:27:31 +01:00
|
|
|
public:
|
|
|
|
|
struct PrioritizedProjectPart
|
|
|
|
|
{
|
|
|
|
|
PrioritizedProjectPart(const ProjectPart::Ptr &projectPart, int priority)
|
|
|
|
|
: projectPart(projectPart) , priority(priority) {}
|
|
|
|
|
|
|
|
|
|
ProjectPart::Ptr projectPart;
|
|
|
|
|
int priority = 0;
|
|
|
|
|
};
|
2017-01-12 18:01:12 +01:00
|
|
|
|
2017-01-17 15:27:31 +01:00
|
|
|
ProjectPartPrioritizer(const QList<ProjectPart::Ptr> &projectParts,
|
|
|
|
|
const ProjectExplorer::Project *activeProject,
|
|
|
|
|
Language languagePreference)
|
|
|
|
|
: m_projectParts(projectParts)
|
|
|
|
|
, m_activeProject(activeProject)
|
|
|
|
|
, m_languagePreference(languagePreference)
|
|
|
|
|
{
|
|
|
|
|
// Determine best project part
|
|
|
|
|
const QList<PrioritizedProjectPart> prioritized = prioritize();
|
|
|
|
|
m_projectPart = prioritized.first().projectPart;
|
|
|
|
|
|
|
|
|
|
// Determine ambiguity
|
|
|
|
|
m_isAmbiguous = prioritized.size() > 1
|
|
|
|
|
? prioritized[0].priority == prioritized[1].priority
|
|
|
|
|
: false;
|
|
|
|
|
}
|
2016-12-16 15:30:08 +01:00
|
|
|
|
2017-01-17 15:27:31 +01:00
|
|
|
ProjectPart::Ptr projectPart()
|
|
|
|
|
{
|
|
|
|
|
return m_projectPart;
|
|
|
|
|
}
|
2016-12-16 15:30:08 +01:00
|
|
|
|
2017-01-17 15:27:31 +01:00
|
|
|
bool isAmbiguous() const
|
|
|
|
|
{
|
|
|
|
|
return m_isAmbiguous;
|
|
|
|
|
}
|
2017-01-12 18:01:12 +01:00
|
|
|
|
2017-01-17 15:27:31 +01:00
|
|
|
private:
|
|
|
|
|
QList<PrioritizedProjectPart> prioritize()
|
|
|
|
|
{
|
|
|
|
|
// Prioritize
|
|
|
|
|
QList<PrioritizedProjectPart> prioritized = Utils::transform(m_projectParts,
|
|
|
|
|
[&](const ProjectPart::Ptr &projectPart) {
|
|
|
|
|
return PrioritizedProjectPart{projectPart, priority(*projectPart)};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Sort according to priority
|
|
|
|
|
const auto lessThan = [&] (const PrioritizedProjectPart &p1,
|
|
|
|
|
const PrioritizedProjectPart &p2) {
|
|
|
|
|
return p1.priority > p2.priority;
|
|
|
|
|
};
|
|
|
|
|
std::stable_sort(prioritized.begin(), prioritized.end(), lessThan);
|
|
|
|
|
|
|
|
|
|
return prioritized;
|
|
|
|
|
}
|
2016-12-16 15:30:08 +01:00
|
|
|
|
2017-01-17 15:27:31 +01:00
|
|
|
int priority(const ProjectPart &projectPart) const
|
|
|
|
|
{
|
|
|
|
|
int thePriority = 0;
|
2016-12-16 15:30:08 +01:00
|
|
|
|
2017-01-17 15:27:31 +01:00
|
|
|
if (projectPart.project == m_activeProject)
|
|
|
|
|
thePriority += 100;
|
2016-12-16 15:30:08 +01:00
|
|
|
|
2017-01-17 15:27:31 +01:00
|
|
|
if (projectPart.selectedForBuilding)
|
|
|
|
|
thePriority += 10;
|
|
|
|
|
|
|
|
|
|
if (isPreferredLanguage(projectPart))
|
|
|
|
|
thePriority += 1;
|
|
|
|
|
|
|
|
|
|
return thePriority;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isPreferredLanguage(const ProjectPart &projectPart) const
|
|
|
|
|
{
|
|
|
|
|
const bool isCProjectPart = projectPart.languageVersion <= ProjectPart::LatestCVersion;
|
|
|
|
|
return (m_languagePreference == Language::C && isCProjectPart)
|
|
|
|
|
|| (m_languagePreference == Language::Cxx && !isCProjectPart);
|
|
|
|
|
}
|
2016-12-16 13:12:04 +01:00
|
|
|
|
2017-01-17 15:27:31 +01:00
|
|
|
private:
|
|
|
|
|
const QList<ProjectPart::Ptr> m_projectParts;
|
|
|
|
|
const ProjectExplorer::Project *m_activeProject = nullptr;
|
|
|
|
|
Language m_languagePreference = Language::Cxx;
|
|
|
|
|
|
|
|
|
|
// Results
|
|
|
|
|
ProjectPart::Ptr m_projectPart;
|
|
|
|
|
bool m_isAmbiguous = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ProjectPartInfo ProjectPartChooser::choose(
|
|
|
|
|
const QString &filePath,
|
|
|
|
|
const ProjectPart::Ptr ¤tProjectPart,
|
|
|
|
|
const ProjectPart::Ptr &manuallySetProjectPart,
|
|
|
|
|
bool stickToPreviousProjectPart,
|
|
|
|
|
const ProjectExplorer::Project *activeProject,
|
|
|
|
|
Language languagePreference,
|
|
|
|
|
bool projectHasChanged) const
|
2016-12-16 13:10:43 +01:00
|
|
|
{
|
|
|
|
|
QTC_CHECK(m_projectPartsForFile);
|
|
|
|
|
QTC_CHECK(m_projectPartsFromDependenciesForFile);
|
|
|
|
|
QTC_CHECK(m_fallbackProjectPart);
|
|
|
|
|
|
|
|
|
|
if (manuallySetProjectPart)
|
2017-01-17 15:27:31 +01:00
|
|
|
return {manuallySetProjectPart, ProjectPartInfo::NoHint};
|
2016-12-16 13:10:43 +01:00
|
|
|
|
|
|
|
|
ProjectPart::Ptr projectPart = currentProjectPart;
|
2017-01-17 15:27:31 +01:00
|
|
|
ProjectPartInfo::Hint hint = ProjectPartInfo::NoHint;
|
2016-12-16 13:10:43 +01:00
|
|
|
|
|
|
|
|
QList<ProjectPart::Ptr> projectParts = m_projectPartsForFile(filePath);
|
|
|
|
|
if (projectParts.isEmpty()) {
|
|
|
|
|
if (projectPart && stickToPreviousProjectPart)
|
|
|
|
|
// File is not directly part of any project, but we got one before. We will re-use it,
|
|
|
|
|
// because re-calculating this can be expensive when the dependency table is big.
|
2017-01-17 15:27:31 +01:00
|
|
|
return {projectPart, ProjectPartInfo::NoHint};
|
2016-12-16 13:10:43 +01:00
|
|
|
|
|
|
|
|
// Fall-back step 1: Get some parts through the dependency table:
|
|
|
|
|
projectParts = m_projectPartsFromDependenciesForFile(filePath);
|
2017-01-17 15:27:31 +01:00
|
|
|
if (projectParts.isEmpty()) {
|
2016-12-16 13:10:43 +01:00
|
|
|
// Fall-back step 2: Use fall-back part from the model manager:
|
|
|
|
|
projectPart = m_fallbackProjectPart();
|
2017-01-17 15:27:31 +01:00
|
|
|
hint = ProjectPartInfo::IsFallbackMatch;
|
|
|
|
|
} else {
|
|
|
|
|
ProjectPartPrioritizer prioritizer(projectParts, activeProject, languagePreference);
|
|
|
|
|
projectPart = prioritizer.projectPart();
|
|
|
|
|
}
|
2016-12-16 13:10:43 +01:00
|
|
|
} else {
|
2017-01-17 15:27:31 +01:00
|
|
|
if (projectHasChanged || !projectParts.contains(projectPart)) {
|
|
|
|
|
ProjectPartPrioritizer prioritizer(projectParts, activeProject, languagePreference);
|
|
|
|
|
projectPart = prioritizer.projectPart();
|
|
|
|
|
hint = prioritizer.isAmbiguous()
|
|
|
|
|
? ProjectPartInfo::IsAmbiguousMatch
|
|
|
|
|
: ProjectPartInfo::NoHint;
|
|
|
|
|
}
|
2016-12-16 13:10:43 +01:00
|
|
|
}
|
|
|
|
|
|
2017-01-17 15:27:31 +01:00
|
|
|
return {projectPart, hint};
|
2016-12-16 13:10:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ProjectPartChooser::setFallbackProjectPart(const FallBackProjectPart &getter)
|
|
|
|
|
{
|
|
|
|
|
m_fallbackProjectPart = getter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ProjectPartChooser::setProjectPartsForFile(const ProjectPartsForFile &getter)
|
|
|
|
|
{
|
|
|
|
|
m_projectPartsForFile = getter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ProjectPartChooser::setProjectPartsFromDependenciesForFile(
|
|
|
|
|
const ProjectPartsFromDependenciesForFile &getter)
|
|
|
|
|
{
|
|
|
|
|
m_projectPartsFromDependenciesForFile = getter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace CppTools
|