-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcmakelocatorfilter.cpp
More file actions
158 lines (134 loc) · 5.82 KB
/
cmakelocatorfilter.cpp
File metadata and controls
158 lines (134 loc) · 5.82 KB
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
// Copyright (C) 2016 Kläralvdalens Datakonsult AB, a KDAB Group company.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "cmakelocatorfilter.h"
#include "cmakebuildstep.h"
#include "cmakebuildsystem.h"
#include "cmakeproject.h"
#include "cmakeprojectmanagertr.h"
#include <projectexplorer/buildmanager.h>
#include <projectexplorer/buildsteplist.h>
#include <projectexplorer/projectmanager.h>
#include <projectexplorer/target.h>
#include <utils/algorithm.h>
using namespace Core;
using namespace ProjectExplorer;
using namespace Utils;
namespace CMakeProjectManager::Internal {
using BuildAcceptor = std::function<void(const FilePath &, const QString &)>;
static LocatorMatcherTasks cmakeMatchers(const BuildAcceptor &acceptor)
{
using namespace Tasking;
Storage<LocatorStorage> storage;
const auto onSetup = [storage, acceptor] {
const QString input = storage->input();
const QList<Project *> projects = ProjectManager::projects();
LocatorFilterEntries entries;
for (Project *project : projects) {
const auto cmakeProject = qobject_cast<const CMakeProject *>(project);
if (!cmakeProject || !cmakeProject->activeTarget())
continue;
const auto bs = qobject_cast<CMakeBuildSystem *>(
cmakeProject->activeTarget()->buildSystem());
if (!bs)
continue;
const QList<CMakeBuildTarget> buildTargets = bs->buildTargets();
for (const CMakeBuildTarget &target : buildTargets) {
if (CMakeBuildSystem::filteredOutTarget(target))
continue;
const int index = target.title.indexOf(input, 0, Qt::CaseInsensitive);
if (index >= 0) {
const FilePath path = target.backtrace.isEmpty()
? cmakeProject->projectFilePath()
: target.backtrace.last().path;
const int line = target.backtrace.isEmpty() ? 0 : target.backtrace.last().line;
const FilePath projectPath = cmakeProject->projectFilePath();
const QString displayName = target.title;
LocatorFilterEntry entry;
entry.displayName = displayName;
if (acceptor) {
entry.acceptor = [projectPath, displayName, acceptor] {
acceptor(projectPath, displayName);
return AcceptResult();
};
}
entry.linkForEditor = {path, line};
entry.extraInfo = path.shortNativePath();
entry.highlightInfo = {index, int(input.length())};
entry.filePath = cmakeProject->projectFilePath();
entries.append(entry);
}
}
}
storage->reportOutput(entries);
};
return {{Sync(onSetup), storage}};
}
void setupFilter(ILocatorFilter *filter)
{
const auto projectListUpdated = [filter] {
filter->setEnabled(Utils::contains(ProjectManager::projects(),
[](Project *p) { return qobject_cast<CMakeProject *>(p); }));
};
QObject::connect(ProjectManager::instance(), &ProjectManager::projectAdded,
filter, projectListUpdated);
QObject::connect(ProjectManager::instance(), &ProjectManager::projectRemoved,
filter, projectListUpdated);
}
// --------------------------------------------------------------------
// BuildCMakeTargetLocatorFilter:
// --------------------------------------------------------------------
static void buildAcceptor(const FilePath &projectPath, const QString &displayName)
{
// Get the project containing the target selected
const auto cmakeProject = qobject_cast<CMakeProject *>(
Utils::findOrDefault(ProjectManager::projects(), [projectPath](Project *p) {
return p->projectFilePath() == projectPath;
}));
if (!cmakeProject || !cmakeProject->activeTarget()
|| !cmakeProject->activeTarget()->activeBuildConfiguration())
return;
if (BuildManager::isBuilding(cmakeProject))
BuildManager::cancel();
// Find the make step
const BuildStepList *buildStepList =
cmakeProject->activeTarget()->activeBuildConfiguration()->buildSteps();
const auto buildStep = buildStepList->firstOfType<CMakeBuildStep>();
if (!buildStep)
return;
// Change the make step to build only the given target
const QStringList oldTargets = buildStep->buildTargets();
buildStep->setBuildTargets({displayName});
// Build
BuildManager::buildProjectWithDependencies(cmakeProject);
buildStep->setBuildTargets(oldTargets);
}
CMakeBuildTargetFilter::CMakeBuildTargetFilter()
{
setId("Build CMake target");
setDisplayName(Tr::tr("Build CMake Target"));
setDescription(Tr::tr("Builds a target of any open CMake project."));
setDefaultShortcutString("cm");
setPriority(High);
setupFilter(this);
}
Core::LocatorMatcherTasks CMakeBuildTargetFilter::matchers()
{
return cmakeMatchers(&buildAcceptor);
}
// --------------------------------------------------------------------
// OpenCMakeTargetLocatorFilter:
// --------------------------------------------------------------------
CMakeOpenTargetFilter::CMakeOpenTargetFilter()
{
setId("Open CMake target definition");
setDisplayName(Tr::tr("Open CMake Target"));
setDescription(Tr::tr("Locates the definition of a target of any open CMake project."));
setDefaultShortcutString("cmo");
setPriority(Medium);
setupFilter(this);
}
Core::LocatorMatcherTasks CMakeOpenTargetFilter::matchers()
{
return cmakeMatchers({});
}
} // namespace CMakeProjectManager::Internal