aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/cppeditor/cppprojectpartchooser.cpp
blob: ca2e5943d779e4f37132f80572d12513825a7cd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/****************************************************************************
**
** 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"

#include <utils/algorithm.h>
#include <utils/qtcassert.h>

using namespace Utils;

namespace CppEditor::Internal {

class ProjectPartPrioritizer
{
public:
    struct PrioritizedProjectPart
    {
        PrioritizedProjectPart(const ProjectPart::ConstPtr &projectPart, int priority)
            : projectPart(projectPart), priority(priority) {}

        ProjectPart::ConstPtr projectPart;
        int priority = 0;
    };

    ProjectPartPrioritizer(const QList<ProjectPart::ConstPtr> &projectParts,
                           const QString &preferredProjectPartId,
                           const Utils::FilePath &activeProject,
                           Language languagePreference,
                           bool areProjectPartsFromDependencies)
        : m_preferredProjectPartId(preferredProjectPartId)
        , m_activeProject(activeProject)
        , m_languagePreference(languagePreference)
    {
        // Prioritize
        const QList<PrioritizedProjectPart> prioritized = prioritize(projectParts);
        for (const PrioritizedProjectPart &ppp : prioritized)
            m_info.projectParts << ppp.projectPart;

        // Best project part
        m_info.projectPart = m_info.projectParts.first();

        // Hints
        if (m_info.projectParts.size() > 1)
            m_info.hints |= ProjectPartInfo::IsAmbiguousMatch;
        if (prioritized.first().priority > 1000)
            m_info.hints |= ProjectPartInfo::IsPreferredMatch;
        if (areProjectPartsFromDependencies)
            m_info.hints |= ProjectPartInfo::IsFromDependenciesMatch;
        else
            m_info.hints |= ProjectPartInfo::IsFromProjectMatch;
    }

    ProjectPartInfo info() const
    {
        return m_info;
    }

private:
    QList<PrioritizedProjectPart> prioritize(const QList<ProjectPart::ConstPtr> &projectParts) const
    {
        // Prioritize
        QList<PrioritizedProjectPart> prioritized = Utils::transform(projectParts,
                [&](const ProjectPart::ConstPtr &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;
    }

    int priority(const ProjectPart &projectPart) const
    {
        int thePriority = 0;

        if (!m_preferredProjectPartId.isEmpty() && projectPart.id() == m_preferredProjectPartId)
            thePriority += 1000;

        if (projectPart.belongsToProject(m_activeProject))
            thePriority += 100;

        if (projectPart.selectedForBuilding)
            thePriority += 10;

        if (isPreferredLanguage(projectPart))
            thePriority += 1;

        return thePriority;
    }

    bool isPreferredLanguage(const ProjectPart &projectPart) const
    {
        const bool isCProjectPart = projectPart.languageVersion <= LanguageVersion::LatestC;
        return (m_languagePreference == Language::C && isCProjectPart)
            || (m_languagePreference == Language::Cxx && !isCProjectPart);
    }

private:
    const QString m_preferredProjectPartId;
    const Utils::FilePath m_activeProject;
    Language m_languagePreference = Language::Cxx;

    // Results
    ProjectPartInfo m_info;
};

ProjectPartInfo ProjectPartChooser::choose(const QString &filePath,
        const ProjectPartInfo &currentProjectPartInfo,
        const QString &preferredProjectPartId,
        const Utils::FilePath &activeProject,
        Language languagePreference,
        bool projectsUpdated) const
{
    QTC_CHECK(m_projectPartsForFile);
    QTC_CHECK(m_projectPartsFromDependenciesForFile);
    QTC_CHECK(m_fallbackProjectPart);

    ProjectPart::ConstPtr projectPart = currentProjectPartInfo.projectPart;
    QList<ProjectPart::ConstPtr> projectParts = m_projectPartsForFile(filePath);
    bool areProjectPartsFromDependencies = false;

    if (projectParts.isEmpty()) {
        if (!projectsUpdated && projectPart
                && currentProjectPartInfo.hints & ProjectPartInfo::IsFallbackMatch)
            // Avoid re-calculating the expensive dependency table for non-project files.
            return ProjectPartInfo(projectPart, {projectPart}, ProjectPartInfo::IsFallbackMatch);

        // Fall-back step 1: Get some parts through the dependency table:
        projectParts = m_projectPartsFromDependenciesForFile(filePath);
        if (projectParts.isEmpty()) {
            // Fall-back step 2: Use fall-back part from the model manager:
            projectPart = m_fallbackProjectPart();
            return ProjectPartInfo(projectPart, {projectPart}, ProjectPartInfo::IsFallbackMatch);
        }
        areProjectPartsFromDependencies = true;
    }

    return ProjectPartPrioritizer(projectParts,
                                  preferredProjectPartId,
                                  activeProject,
                                  languagePreference,
                                  areProjectPartsFromDependencies).info();
}

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 CppEditor::Internal