forked from h4tr3d/cmakeprojectmanager2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtreebuilder.cpp
337 lines (282 loc) · 9.63 KB
/
treebuilder.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/****************************************************************************
**
** Copyright (C) 2016 Alexander Drozdov.
** Contact: [email protected]
**
** This file is part of CMakeProjectManager2 plugin.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
****************************************************************************/
#include "treebuilder.h"
#include <coreplugin/fileiconprovider.h>
#include <coreplugin/icore.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <utils/runextensions.h>
#include <utils/algorithm.h>
#include <utils/mimetypes/mimedatabase.h>
#include <QDir>
#include <QDebug>
using namespace ProjectExplorer;
namespace CMakeProjectManager {
namespace Internal {
namespace {
Utils::MimeType getFileMimeType(const QString &path)
{
Utils::MimeDatabase mdb;
return mdb.mimeTypeForFile(path);
}
// TODO This code taken from projectnodes.cpp and it marked as HACK. Wait for more clean solution.
FileType getFileType(const QString &file)
{
using namespace ProjectExplorer;
Utils::MimeDatabase mdb;
const Utils::MimeType mt = mdb.mimeTypeForFile(file);
if (!mt.isValid())
return UnknownFileType;
const QString typeName = mt.name();
if (typeName == QLatin1String(ProjectExplorer::Constants::CPP_SOURCE_MIMETYPE)
|| typeName == QLatin1String(ProjectExplorer::Constants::C_SOURCE_MIMETYPE))
return SourceType;
if (typeName == QLatin1String(ProjectExplorer::Constants::CPP_HEADER_MIMETYPE)
|| typeName == QLatin1String(ProjectExplorer::Constants::C_HEADER_MIMETYPE))
return HeaderType;
if (typeName == QLatin1String(ProjectExplorer::Constants::RESOURCE_MIMETYPE))
return ResourceType;
if (typeName == QLatin1String(ProjectExplorer::Constants::FORM_MIMETYPE))
return FormType;
if (typeName == QLatin1String(ProjectExplorer::Constants::QML_MIMETYPE))
return QMLType;
if (typeName == QLatin1String(ProjectExplorer::Constants::SCXML_MIMETYPE))
return StateChartType;
return UnknownFileType;
}
} // ::anonymous
TreeBuilder::TreeBuilder(QObject *parent)
: QObject(parent)
{
connect(&m_watcher, &QFutureWatcherBase::finished,
this, &TreeBuilder::buildTreeFinished);
}
void TreeBuilder::startScanning(const Utils::FileName &baseDir)
{
m_watcher.cancel();
m_watcher.waitForFinished();
m_baseDir = baseDir;
m_filesForFuture.clear();
m_pathsForFuture.clear();
m_parsing = true;
m_watcher.setFuture(Utils::runAsync(&TreeBuilder::run, this));
}
void TreeBuilder::run(QFutureInterface<void> &fi)
{
m_futureCount = 0;
fi.setProgressRange(0, 10);
buildTree(m_baseDir, fi, 5);
fi.setProgressValue(4);
// Sort and prepare
// Step 1: sort collections
Utils::sort(m_filesForFuture);
if (fi.isCanceled()) {
fi.setProgressValue(10);
return;
}
fi.setProgressValue(6);
Utils::sort(m_pathsForFuture);
if (fi.isCanceled()) {
fi.setProgressValue(10);
return;
}
fi.setProgressValue(8);
// Step 2: remove dups
m_paths = Utils::filteredUnique(m_pathsForFuture);
fi.setProgressValue(10);
}
void TreeBuilder::buildTreeFinished()
{
m_files = std::move(m_filesForFuture);
m_paths = std::move(m_pathsForFuture);
m_filesForFuture.clear();
m_pathsForFuture.clear();
m_parsing = false;
emit scanningFinished();
}
void TreeBuilder::cancel()
{
m_watcher.cancel();
m_watcher.waitForFinished();
}
void TreeBuilder::wait()
{
m_watcher.waitForFinished();
}
bool TreeBuilder::isScanning() const
{
return m_parsing;
}
QFuture<void> TreeBuilder::future() const
{
return m_watcher.future();
}
void TreeBuilder::buildTree(const Utils::FileName &baseDir,
QFutureInterface<void> &fi,
int symlinkDepth)
{
if (symlinkDepth == 0)
return;
const QFileInfoList fileInfoList = QDir(baseDir.toString()).entryInfoList(QDir::Files |
QDir::Dirs |
QDir::NoDotAndDotDot |
QDir::NoSymLinks);
foreach (const QFileInfo &fileInfo, fileInfoList) {
Utils::FileName fn = Utils::FileName(fileInfo);
if (m_futureCount % 100) {
emit scanningProgress(fn);
}
if (fi.isCanceled())
return;
++m_futureCount;
if (fileInfo.isDir() && isValidDir(fileInfo)) {
m_pathsForFuture.append(fn);
buildTree(fn, fi, symlinkDepth - fileInfo.isSymLink());
} else if (isValidFile(fileInfo)) {
auto nodeInfo = fileNodeInfo(fn);
m_filesForFuture.append(std::move(nodeInfo));
}
}
}
TreeBuilder::~TreeBuilder()
{
cancel();
}
QList<FileNodeInfo> TreeBuilder::files() const
{
return m_files;
}
Utils::FileNameList TreeBuilder::paths() const
{
return m_paths;
}
void TreeBuilder::clear()
{
m_files.clear();
m_paths.clear();
}
QList<FileNode *> TreeBuilder::fileNodes(const Utils::FileNameList &files)
{
return Utils::transform(files, [](const Utils::FileName &fileName) {
auto nodeInfo = fileNodeInfo(fileName);
return new ProjectExplorer::FileNode(nodeInfo.filePath, nodeInfo.fileType, nodeInfo.generated);
});
}
FileNodeInfo TreeBuilder::fileNodeInfo(const Utils::FileName &fileName)
{
FileNodeInfo node;
bool generated = false;
QString onlyFileName = fileName.fileName();
if ( (onlyFileName.startsWith(QLatin1String("moc_")) && onlyFileName.endsWith(QLatin1String(".cxx")))
|| (onlyFileName.startsWith(QLatin1String("ui_")) && onlyFileName.endsWith(QLatin1String(".h")))
|| (onlyFileName.startsWith(QLatin1String("qrc_")) && onlyFileName.endsWith(QLatin1String(".cxx"))))
generated = true;
if (fileName.endsWith(QLatin1String("CMakeLists.txt"))) {
node = FileNodeInfo(fileName, ProjectExplorer::ProjectFileType, false);
} else {
#if 1
ProjectExplorer::FileType fileType = getFileType(fileName.toString());
#else
auto fileType = SourceType;
if (onlyFileName.endsWith(".qrc"))
fileType = ResourceType;
#endif
node = FileNodeInfo(fileName, fileType, generated);
}
return node;
}
bool TreeBuilder::isValidDir(const QFileInfo &fileInfo)
{
if (!fileInfo.isDir())
return false;
const QString fileName = fileInfo.fileName();
const QString suffix = fileInfo.suffix();
if (fileName.startsWith(QLatin1Char('.')))
return false;
else if (fileName == QLatin1String("CVS"))
return false;
// TODO ### user include/exclude
return true;
}
bool TreeBuilder::isValidFile(const QFileInfo &fileInfo)
{
if (!fileInfo.isFile())
return false;
auto fn = fileInfo.fileName();
auto isValid =
!fn.endsWith(QLatin1String("CMakeLists.txt.user")) &&
!fn.endsWith(QLatin1String(".autosave")) &&
!fn.endsWith(QLatin1String(".a")) &&
!fn.endsWith(QLatin1String(".o")) &&
!fn.endsWith(QLatin1String(".d")) &&
!fn.endsWith(QLatin1String(".exe")) &&
!fn.endsWith(QLatin1String(".dll")) &&
!fn.endsWith(QLatin1String(".obj")) &&
!fn.endsWith(QLatin1String(".elf"));
// Skip in general, all application/octet-stream files
// TODO be careful, some wrong "text" files can be reported as binary one
if (isValid) {
auto type = getFileMimeType(fileInfo.filePath());
if (type.isValid()) {
isValid = type.name() != QLatin1String("application/octet-stream");
if (!isValid) {
auto parents = type.parentMimeTypes();
// We still can edit it
isValid = parents.contains(QLatin1String("text/plain"));
}
}
}
#if 0
if (!isValid) {
qDebug() << "Skip file:" << fileInfo.filePath();
}
#endif
return isValid;
}
#if 0
QList<Glob> TreeBuilder::parseFilter(const QString &filter)
{
QList<Glob> result;
QStringList list = filter.split(QLatin1Char(';'), QString::SkipEmptyParts);
foreach (const QString &e, list) {
QString entry = e.trimmed();
Glob g;
if (entry.indexOf(QLatin1Char('*')) == -1 && entry.indexOf(QLatin1Char('?')) == -1) {
g.mode = Glob::EXACT;
g.matchString = entry;
} else if (entry.startsWith(QLatin1Char('*')) && entry.indexOf(QLatin1Char('*'), 1) == -1
&& entry.indexOf(QLatin1Char('?'), 1) == -1) {
g.mode = Glob::ENDSWITH;
g.matchString = entry.mid(1);
} else {
g.mode = Glob::REGEXP;
g.matchRegexp = QRegExp(entry, Qt::CaseInsensitive, QRegExp::Wildcard);
}
result.append(g);
}
return result;
}
void TreeBuilder::applyFilter(const QString &showFilesfilter, const QString &hideFilesfilter)
{
QList<Glob> filter = parseFilter(showFilesfilter);
m_showFilesFilter = filter;
filter = parseFilter(hideFilesfilter);
m_hideFilesFilter = filter;
}
#endif
} // namespace Internal
} // namespace CMakeProjectManager