-
Notifications
You must be signed in to change notification settings - Fork 9
/
projecttreehelper.cpp
223 lines (189 loc) · 8.2 KB
/
projecttreehelper.cpp
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "projecttreehelper.h"
#include "cmakeproject.h"
#include "cmakeprojectmanagertr.h"
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/projectmanager.h>
#include <utils/algorithm.h>
#include <utils/fsengine/fileiconprovider.h>
#include <utils/qtcassert.h>
using namespace ProjectExplorer;
namespace CMakeProjectManager::Internal {
std::unique_ptr<FolderNode> createCMakeVFolder(const Utils::FilePath &basePath,
int priority,
const QString &displayName,
bool sourcesOrHeaders)
{
auto newFolder = std::make_unique<VirtualFolderNode>(basePath);
newFolder->setPriority(priority);
newFolder->setDisplayName(displayName);
newFolder->setIsSourcesOrHeaders(sourcesOrHeaders);
return newFolder;
}
void addCMakeVFolder(FolderNode *base,
const Utils::FilePath &basePath,
int priority,
const QString &displayName,
std::vector<std::unique_ptr<FileNode>> &&files,
bool sourcesOrHeaders)
{
if (files.size() == 0)
return;
FolderNode *folder = base;
if (!displayName.isEmpty()) {
auto newFolder = createCMakeVFolder(basePath, priority, displayName, sourcesOrHeaders);
folder = newFolder.get();
base->addNode(std::move(newFolder));
}
folder->addNestedNodes(std::move(files));
folder->forEachFolderNode([] (FolderNode *fn) { fn->compress(); });
}
std::vector<std::unique_ptr<FileNode>> &&removeKnownNodes(
const QSet<Utils::FilePath> &knownFiles, std::vector<std::unique_ptr<FileNode>> &&files)
{
Utils::erase(files, [&knownFiles](const std::unique_ptr<FileNode> &n) {
return knownFiles.contains(n->filePath());
});
return std::move(files);
}
void addCMakeInputs(FolderNode *root,
const Utils::FilePath &sourceDir,
const Utils::FilePath &buildDir,
std::vector<std::unique_ptr<FileNode>> &&sourceInputs,
std::vector<std::unique_ptr<FileNode>> &&buildInputs,
std::vector<std::unique_ptr<FileNode>> &&rootInputs)
{
std::unique_ptr<ProjectNode> cmakeVFolder = std::make_unique<CMakeInputsNode>(root->filePath());
QSet<Utils::FilePath> knownFiles;
root->forEachGenericNode([&knownFiles](const Node *n) {
if (n->listInProject())
knownFiles.insert(n->filePath());
});
addCMakeVFolder(cmakeVFolder.get(),
sourceDir,
1000,
QString(),
removeKnownNodes(knownFiles, std::move(sourceInputs)));
addCMakeVFolder(cmakeVFolder.get(),
buildDir,
100,
Tr::tr("<Build Directory>"),
removeKnownNodes(knownFiles, std::move(buildInputs)));
addCMakeVFolder(cmakeVFolder.get(),
Utils::FilePath(),
10,
Tr::tr("<Other Locations>"),
removeKnownNodes(knownFiles, std::move(rootInputs)));
root->addNode(std::move(cmakeVFolder));
}
void addCMakePresets(FolderNode *root, const Utils::FilePath &sourceDir)
{
QStringList presetFileNames;
presetFileNames << "CMakePresets.json";
presetFileNames << "CMakeUserPresets.json";
const CMakeProject *cp = static_cast<const CMakeProject *>(
ProjectManager::projectForFile(sourceDir.pathAppended("CMakeLists.txt")));
if (cp && cp->presetsData().include)
presetFileNames.append(cp->presetsData().include.value());
std::vector<std::unique_ptr<FileNode>> presets;
for (const auto &fileName : presetFileNames) {
Utils::FilePath file = sourceDir.pathAppended(fileName);
if (file.exists())
presets.push_back(std::make_unique<FileNode>(file, Node::fileTypeForFileName(file)));
}
if (presets.empty())
return;
std::unique_ptr<ProjectNode> cmakeVFolder = std::make_unique<CMakePresetsNode>(root->filePath());
addCMakeVFolder(cmakeVFolder.get(), sourceDir, 1000, QString(), std::move(presets));
root->addNode(std::move(cmakeVFolder));
}
QHash<Utils::FilePath, ProjectNode *> addCMakeLists(
CMakeProjectNode *root, std::vector<std::unique_ptr<FileNode>> &&cmakeLists)
{
QHash<Utils::FilePath, ProjectNode *> cmakeListsNodes;
cmakeListsNodes.insert(root->filePath(), root);
const QSet<Utils::FilePath> cmakeDirs
= Utils::transform<QSet>(cmakeLists, [](const std::unique_ptr<FileNode> &n) {
return n->filePath().parentDir();
});
root->addNestedNodes(std::move(cmakeLists),
Utils::FilePath(),
[&cmakeDirs, &cmakeListsNodes](const Utils::FilePath &fp)
-> std::unique_ptr<ProjectExplorer::FolderNode> {
if (cmakeDirs.contains(fp)) {
auto fn = std::make_unique<CMakeListsNode>(fp);
cmakeListsNodes.insert(fp, fn.get());
return fn;
}
return std::make_unique<FolderNode>(fp);
});
root->compress();
return cmakeListsNodes;
}
void createProjectNode(const QHash<Utils::FilePath, ProjectNode *> &cmakeListsNodes,
const Utils::FilePath &dir,
const QString &displayName)
{
ProjectNode *cmln = cmakeListsNodes.value(dir);
QTC_ASSERT(cmln, return );
const Utils::FilePath projectName = dir.pathAppended(".project::" + displayName);
ProjectNode *pn = cmln->projectNode(projectName);
if (!pn) {
auto newNode = std::make_unique<CMakeProjectNode>(projectName);
pn = newNode.get();
cmln->addNode(std::move(newNode));
}
pn->setDisplayName(displayName);
}
CMakeTargetNode *createTargetNode(const QHash<Utils::FilePath, ProjectNode *> &cmakeListsNodes,
const Utils::FilePath &dir,
const QString &displayName)
{
ProjectNode *cmln = cmakeListsNodes.value(dir);
QTC_ASSERT(cmln, return nullptr);
QString targetId = displayName;
CMakeTargetNode *tn = static_cast<CMakeTargetNode *>(
cmln->findNode([&targetId](const Node *n) { return n->buildKey() == targetId; }));
if (!tn) {
auto newNode = std::make_unique<CMakeTargetNode>(dir, displayName);
tn = newNode.get();
cmln->addNode(std::move(newNode));
}
tn->setDisplayName(displayName);
return tn;
}
template<typename Result>
static std::unique_ptr<Result> cloneFolderNode(FolderNode *node)
{
auto folderNode = std::make_unique<Result>(node->filePath());
folderNode->setDisplayName(node->displayName());
for (Node *node : node->nodes()) {
if (FileNode *fn = node->asFileNode()) {
folderNode->addNode(std::unique_ptr<FileNode>(fn->clone()));
} else if (FolderNode *fn = node->asFolderNode()) {
folderNode->addNode(cloneFolderNode<FolderNode>(fn));
} else {
QTC_CHECK(false);
}
}
return folderNode;
}
void addFileSystemNodes(ProjectNode *root, const std::shared_ptr<FolderNode> &folderNode)
{
QTC_ASSERT(root, return );
auto fileSystemNode = cloneFolderNode<VirtualFolderNode>(folderNode.get());
// just before special nodes like "CMake Modules"
fileSystemNode->setPriority(Node::DefaultPriority - 6);
fileSystemNode->setDisplayName(Tr::tr("<File System>"));
fileSystemNode->setIcon(DirectoryIcon(ProjectExplorer::Constants::FILEOVERLAY_UNKNOWN));
if (!fileSystemNode->isEmpty()) {
// make file system nodes less probable to be selected when syncing with the current document
fileSystemNode->forEachGenericNode([](Node *n) {
n->setPriority(n->priority() + Node::DefaultProjectFilePriority + 1);
n->setEnabled(false);
});
root->addNode(std::move(fileSystemNode));
}
}
} // CMakeProjectManager::Internal