-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.cpp
More file actions
186 lines (145 loc) · 6.02 KB
/
plugin.cpp
File metadata and controls
186 lines (145 loc) · 6.02 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
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
#include "plugin.h"
#include "constants.h"
#include "settingspage.h"
#include "settings.h"
#include <coreplugin/icore.h>
#include <coreplugin/icontext.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/messagemanager.h>
#include <projectexplorer/session.h>
#include <projectexplorer/project.h>
#include <projectexplorer/target.h>
#include <projectexplorer/buildinfo.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/buildmanager.h>
#include <projectexplorer/projecttree.h>
#include <resourceeditor/resourcenode.h>
#include <texteditor/texteditor.h>
#include <texteditor/textdocument.h>
#include <utils/mimetypes/mimedatabase.h>
#include <QAction>
#include <QMessageBox>
#include <QMainWindow>
#include <QMenu>
#include <QProcess>
#include <QStandardPaths>
#include <QDir>
#include <QTemporaryFile>
namespace
{
bool isCppSightsEnabled(QSharedPointer<CppInsightsPlugin::Internal::Settings> settings) {
if(settings->cppInsightsTool().isEmpty()) return false;
ProjectExplorer::Project *project = ProjectExplorer::SessionManager::startupProject();
if (!project)
return false;
ProjectExplorer::Target *target = project->activeTarget();
if (!target)
return false;
QDir buildBase;
if (auto buildConfig = target->activeBuildConfiguration()) {
buildBase = QDir(buildConfig->buildDirectory().toString());
}
if (buildBase.isEmpty())
return false;
const QFileInfo buildCommandsFile(buildBase.path() + "/compile_commands.json");
if (!buildCommandsFile.exists()) {
return false;
}
const auto *node = ProjectExplorer::ProjectTree::currentNode();
const auto *fileNode = dynamic_cast<const ProjectExplorer::FileNode *>(node);
if (!fileNode) {
return false;
}
return true;
}
}
namespace CppInsightsPlugin {
namespace Internal {
bool Plugin::initialize(const QStringList &arguments, QString *errorString)
{
Q_UNUSED(arguments)
Q_UNUSED(errorString)
auto action = new QAction(tr("CPP Insights compile"), this);
Core::Command *cmd = Core::ActionManager::registerAction(action, Constants::ACTION_ID,
Core::Context(Core::Constants::C_GLOBAL));
connect(action, &QAction::triggered, this, &Plugin::triggerAction);
Core::ActionContainer *menu = Core::ActionManager::createMenu(Constants::MENU_ID);
menu->menu()->setTitle(tr("CppInsights"));
Core::Context projectTreeContext(ProjectExplorer::Constants::C_PROJECT_TREE);
menu = Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_FILECONTEXT);
menu->addAction(cmd);
m_settings.reset(new Settings);
connect(ProjectExplorer::ProjectTree::instance(),
&ProjectExplorer::ProjectTree::currentNodeChanged,
[=]() {
if (!isCppSightsEnabled(m_settings)) {
return;
}
const auto *node = ProjectExplorer::ProjectTree::currentNode();
const auto *fileNode = dynamic_cast<const ProjectExplorer::FileNode *>(node);
const auto mimeType = Utils::mimeTypeForFile(fileNode->filePath().toString());
const bool isMimeTypeSupported = m_settings->cppInsightsMimes().indexOf(mimeType) != -1;
action->setVisible(isMimeTypeSupported);
action->setEnabled(isMimeTypeSupported);
});
new SettingsPage(m_settings, this);
return true;
}
ExtensionSystem::IPlugin::ShutdownFlag Plugin::aboutToShutdown() { return SynchronousShutdown; }
void Plugin::triggerAction() {
const auto *project = ProjectExplorer::SessionManager::startupProject();
const auto *target = project->activeTarget();
const auto buildConfig = target->activeBuildConfiguration();
const auto buildBase = QDir(buildConfig->buildDirectory().toString());
const QString folder = Constants::SETTINGS_GROUP;
const QDir cppInsightsFolder = QDir(buildBase.path() + "/" + folder);
if (!cppInsightsFolder.exists()) {
if (!buildBase.mkdir(folder)) {
Core::MessageManager::write("Error creating the cppinsights folder in build dir");
}
}
const auto *node = ProjectExplorer::ProjectTree::currentNode();
const auto *fileNode = dynamic_cast<const ProjectExplorer::FileNode *>(node);
if (!fileNode) {
return;
}
const auto path = fileNode->filePath();
const QString temporaryFile = cppInsightsFolder.path() + "/" + path.fileName();
QProcess *process = new QProcess(this);
connect(process,
static_cast<void (QProcess::*)(QProcess::ProcessError)>(&QProcess::errorOccurred),
[=](QProcess::ProcessError error) {
if (error == QProcess::ProcessError::FailedToStart) {
Core::MessageManager::write("Cannot start cpp insights");
} else {
Core::MessageManager::write("Unknown error executing cppinsights");
}
});
connect(process,
static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
[=](int, QProcess::ExitStatus) {
if (!process->readAllStandardError().isEmpty()) {
const QString error = QString::fromStdString(
process->readAllStandardError().toStdString());
Core::MessageManager::write(error);
}
if (QFileInfo(temporaryFile).size() == 0) {
Core::MessageManager::write("No output from CPP insights");
return;
};
Core::MessageManager::write("Insights generated");
Core::EditorManager::openEditor(temporaryFile);
});
QStringList arguments;
arguments << node->filePath().toString() << "-p" << buildBase.path();
process->setStandardOutputFile(temporaryFile);
process->start(m_settings->cppInsightsTool(), arguments);
Core::MessageManager::write("Starting cpp insights : "
+ m_settings->cppInsightsTool()
+ " " + arguments.join(" "));
}
} // namespace Internal
} // namespace CppInsightsPlugin