2022-12-12 16:45:31 +01:00
|
|
|
// Copyright (C) 2022 The Qt Company Ltd.
|
2023-05-24 10:27:35 +02:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2022-11-28 09:48:11 +01:00
|
|
|
|
|
|
|
|
#include "axivionplugin.h"
|
|
|
|
|
|
|
|
|
|
#include "axivionoutputpane.h"
|
2022-12-13 11:17:12 +01:00
|
|
|
#include "axivionprojectsettings.h"
|
2022-12-14 12:11:03 +01:00
|
|
|
#include "axivionquery.h"
|
|
|
|
|
#include "axivionresultparser.h"
|
2022-12-13 11:17:12 +01:00
|
|
|
#include "axiviontr.h"
|
2022-11-28 09:48:11 +01:00
|
|
|
|
2022-12-16 15:12:39 +01:00
|
|
|
#include <coreplugin/editormanager/documentmodel.h>
|
2022-12-16 23:11:46 +01:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2022-12-14 12:11:03 +01:00
|
|
|
#include <coreplugin/messagemanager.h>
|
2023-03-01 17:54:39 +01:00
|
|
|
|
2022-12-12 16:45:31 +01:00
|
|
|
#include <extensionsystem/pluginmanager.h>
|
2023-03-01 17:54:39 +01:00
|
|
|
|
2022-12-16 15:12:39 +01:00
|
|
|
#include <projectexplorer/buildsystem.h>
|
2022-12-13 11:17:12 +01:00
|
|
|
#include <projectexplorer/project.h>
|
2023-03-01 17:54:39 +01:00
|
|
|
#include <projectexplorer/projectmanager.h>
|
2022-12-13 11:17:12 +01:00
|
|
|
#include <projectexplorer/projectpanelfactory.h>
|
2023-03-01 17:54:39 +01:00
|
|
|
|
2022-12-16 23:11:46 +01:00
|
|
|
#include <texteditor/textdocument.h>
|
|
|
|
|
#include <texteditor/texteditor.h>
|
|
|
|
|
#include <texteditor/textmark.h>
|
2023-03-01 17:54:39 +01:00
|
|
|
|
2022-12-13 11:17:12 +01:00
|
|
|
#include <utils/qtcassert.h>
|
2023-01-13 14:38:39 +01:00
|
|
|
#include <utils/utilsicons.h>
|
2022-11-28 09:48:11 +01:00
|
|
|
|
2023-01-13 14:38:39 +01:00
|
|
|
#include <QAction>
|
2022-12-14 12:11:03 +01:00
|
|
|
#include <QTimer>
|
2022-12-13 15:13:13 +01:00
|
|
|
|
2023-01-09 07:31:50 +01:00
|
|
|
constexpr char AxivionTextMarkId[] = "AxivionTextMark";
|
|
|
|
|
|
2022-11-28 09:48:11 +01:00
|
|
|
namespace Axivion::Internal {
|
|
|
|
|
|
2022-12-14 12:11:03 +01:00
|
|
|
class AxivionPluginPrivate : public QObject
|
2022-11-28 09:48:11 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2022-12-16 23:01:01 +01:00
|
|
|
void onStartupProjectChanged();
|
2022-12-14 12:11:03 +01:00
|
|
|
void fetchProjectInfo(const QString &projectName);
|
|
|
|
|
void handleProjectInfo(const ProjectInfo &info);
|
2022-12-16 15:12:39 +01:00
|
|
|
void handleOpenedDocs(ProjectExplorer::Project *project);
|
2022-12-16 23:11:46 +01:00
|
|
|
void onDocumentOpened(Core::IDocument *doc);
|
|
|
|
|
void onDocumentClosed(Core::IDocument * doc);
|
2022-12-16 22:34:56 +01:00
|
|
|
void clearAllMarks();
|
2022-12-16 23:11:46 +01:00
|
|
|
void handleIssuesForFile(const IssuesList &issues);
|
2023-01-13 14:38:39 +01:00
|
|
|
void fetchRuleInfo(const QString &id);
|
2022-12-14 12:11:03 +01:00
|
|
|
|
2023-01-09 07:41:22 +01:00
|
|
|
AxivionOutputPane m_axivionOutputPane;
|
|
|
|
|
ProjectInfo m_currentProjectInfo;
|
|
|
|
|
bool m_runningQuery = false;
|
2022-11-28 09:48:11 +01:00
|
|
|
};
|
|
|
|
|
|
2022-12-13 11:17:12 +01:00
|
|
|
static AxivionPluginPrivate *dd = nullptr;
|
|
|
|
|
|
2023-01-13 13:49:40 +01:00
|
|
|
class AxivionTextMark : public TextEditor::TextMark
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
AxivionTextMark(const Utils::FilePath &filePath, const ShortIssue &issue);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
QString m_id;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
AxivionTextMark::AxivionTextMark(const Utils::FilePath &filePath, const ShortIssue &issue)
|
|
|
|
|
: TextEditor::TextMark(filePath, issue.lineNumber, {Tr::tr("Axivion"), AxivionTextMarkId})
|
|
|
|
|
, m_id(issue.id)
|
|
|
|
|
{
|
|
|
|
|
const QString markText = issue.entity.isEmpty() ? issue.message
|
|
|
|
|
: issue.entity + ": " + issue.message;
|
|
|
|
|
setToolTip(issue.errorNumber + " " + markText);
|
|
|
|
|
setPriority(TextEditor::TextMark::NormalPriority);
|
|
|
|
|
setLineAnnotation(markText);
|
2023-01-13 14:38:39 +01:00
|
|
|
setActionsProvider([this]{
|
|
|
|
|
auto action = new QAction;
|
|
|
|
|
action->setIcon(Utils::Icons::INFO.icon());
|
|
|
|
|
action->setToolTip(Tr::tr("Show rule details"));
|
|
|
|
|
QObject::connect(action, &QAction::triggered,
|
|
|
|
|
dd, [this]{ dd->fetchRuleInfo(m_id); });
|
|
|
|
|
return QList{action};
|
|
|
|
|
});
|
2023-01-13 13:49:40 +01:00
|
|
|
}
|
|
|
|
|
|
2022-11-28 09:48:11 +01:00
|
|
|
AxivionPlugin::~AxivionPlugin()
|
|
|
|
|
{
|
2023-08-17 16:27:44 +02:00
|
|
|
AxivionProjectSettings::destroyProjectSettings();
|
2022-12-13 11:17:12 +01:00
|
|
|
delete dd;
|
|
|
|
|
dd = nullptr;
|
2022-11-28 09:48:11 +01:00
|
|
|
}
|
|
|
|
|
|
2023-08-16 16:42:25 +02:00
|
|
|
void AxivionPlugin::initialize()
|
2022-11-28 09:48:11 +01:00
|
|
|
{
|
2022-12-13 11:17:12 +01:00
|
|
|
dd = new AxivionPluginPrivate;
|
|
|
|
|
|
|
|
|
|
auto panelFactory = new ProjectExplorer::ProjectPanelFactory;
|
|
|
|
|
panelFactory->setPriority(250);
|
|
|
|
|
panelFactory->setDisplayName(Tr::tr("Axivion"));
|
2023-08-17 16:35:08 +02:00
|
|
|
panelFactory->setCreateWidgetFunction(&AxivionProjectSettings::createSettingsWidget);
|
2022-12-13 11:17:12 +01:00
|
|
|
ProjectExplorer::ProjectPanelFactory::registerFactory(panelFactory);
|
2023-03-01 17:54:39 +01:00
|
|
|
connect(ProjectExplorer::ProjectManager::instance(),
|
|
|
|
|
&ProjectExplorer::ProjectManager::startupProjectChanged,
|
2022-12-16 23:01:01 +01:00
|
|
|
dd, &AxivionPluginPrivate::onStartupProjectChanged);
|
2022-12-16 23:11:46 +01:00
|
|
|
connect(Core::EditorManager::instance(), &Core::EditorManager::documentOpened,
|
|
|
|
|
dd, &AxivionPluginPrivate::onDocumentOpened);
|
|
|
|
|
connect(Core::EditorManager::instance(), &Core::EditorManager::documentClosed,
|
|
|
|
|
dd, &AxivionPluginPrivate::onDocumentClosed);
|
2022-11-28 09:48:11 +01:00
|
|
|
}
|
|
|
|
|
|
2022-12-14 12:11:03 +01:00
|
|
|
void AxivionPlugin::fetchProjectInfo(const QString &projectName)
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(dd, return);
|
|
|
|
|
dd->fetchProjectInfo(projectName);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-14 13:53:00 +01:00
|
|
|
ProjectInfo AxivionPlugin::projectInfo()
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(dd, return {});
|
2023-01-09 07:41:22 +01:00
|
|
|
return dd->m_currentProjectInfo;
|
2022-12-14 13:53:00 +01:00
|
|
|
}
|
|
|
|
|
|
2022-12-16 23:01:01 +01:00
|
|
|
void AxivionPluginPrivate::onStartupProjectChanged()
|
|
|
|
|
{
|
2023-03-01 17:54:39 +01:00
|
|
|
ProjectExplorer::Project *project = ProjectExplorer::ProjectManager::startupProject();
|
2022-12-16 23:01:01 +01:00
|
|
|
if (!project) {
|
|
|
|
|
clearAllMarks();
|
2023-01-09 07:41:22 +01:00
|
|
|
m_currentProjectInfo = ProjectInfo();
|
|
|
|
|
m_axivionOutputPane.updateDashboard();
|
2022-12-16 23:01:01 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 16:27:44 +02:00
|
|
|
const AxivionProjectSettings *projSettings = AxivionProjectSettings::projectSettings(project);
|
2022-12-16 23:01:01 +01:00
|
|
|
fetchProjectInfo(projSettings->dashboardProjectName());
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-14 12:11:03 +01:00
|
|
|
void AxivionPluginPrivate::fetchProjectInfo(const QString &projectName)
|
|
|
|
|
{
|
2023-01-09 07:41:22 +01:00
|
|
|
if (m_runningQuery) { // re-schedule
|
2023-08-01 19:59:54 +02:00
|
|
|
QTimer::singleShot(3000, this, [this, projectName] { fetchProjectInfo(projectName); });
|
2022-12-14 12:11:03 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2022-12-16 22:34:56 +01:00
|
|
|
clearAllMarks();
|
2022-12-14 12:11:03 +01:00
|
|
|
if (projectName.isEmpty()) {
|
2023-01-09 07:41:22 +01:00
|
|
|
m_currentProjectInfo = ProjectInfo();
|
|
|
|
|
m_axivionOutputPane.updateDashboard();
|
2022-12-14 12:11:03 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2023-01-09 07:41:22 +01:00
|
|
|
m_runningQuery = true;
|
2022-12-14 12:11:03 +01:00
|
|
|
|
|
|
|
|
AxivionQuery query(AxivionQuery::ProjectInfo, {projectName});
|
|
|
|
|
AxivionQueryRunner *runner = new AxivionQueryRunner(query, this);
|
|
|
|
|
connect(runner, &AxivionQueryRunner::resultRetrieved, this, [this](const QByteArray &result){
|
|
|
|
|
handleProjectInfo(ResultParser::parseProjectInfo(result));
|
|
|
|
|
});
|
|
|
|
|
connect(runner, &AxivionQueryRunner::finished, [runner]{ runner->deleteLater(); });
|
|
|
|
|
runner->start();
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-13 14:38:39 +01:00
|
|
|
void AxivionPluginPrivate::fetchRuleInfo(const QString &id)
|
|
|
|
|
{
|
|
|
|
|
if (m_runningQuery) {
|
2023-08-01 19:59:54 +02:00
|
|
|
QTimer::singleShot(3000, this, [this, id] { fetchRuleInfo(id); });
|
2023-01-13 14:38:39 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QStringList args = id.split(':');
|
|
|
|
|
QTC_ASSERT(args.size() == 2, return);
|
|
|
|
|
m_runningQuery = true;
|
|
|
|
|
AxivionQuery query(AxivionQuery::RuleInfo, args);
|
|
|
|
|
AxivionQueryRunner *runner = new AxivionQueryRunner(query, this);
|
|
|
|
|
connect(runner, &AxivionQueryRunner::resultRetrieved, this, [this](const QByteArray &result){
|
|
|
|
|
m_runningQuery = false;
|
|
|
|
|
m_axivionOutputPane.updateAndShowRule(ResultParser::parseRuleInfo(result));
|
|
|
|
|
});
|
|
|
|
|
connect(runner, &AxivionQueryRunner::finished, [runner]{ runner->deleteLater(); });
|
|
|
|
|
runner->start();
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-16 15:12:39 +01:00
|
|
|
void AxivionPluginPrivate::handleOpenedDocs(ProjectExplorer::Project *project)
|
|
|
|
|
{
|
2023-03-01 17:54:39 +01:00
|
|
|
if (project && ProjectExplorer::ProjectManager::startupProject() != project)
|
2022-12-16 15:12:39 +01:00
|
|
|
return;
|
|
|
|
|
const QList<Core::IDocument *> openDocuments = Core::DocumentModel::openedDocuments();
|
|
|
|
|
for (Core::IDocument *doc : openDocuments)
|
|
|
|
|
onDocumentOpened(doc);
|
|
|
|
|
if (project)
|
2023-03-01 17:54:39 +01:00
|
|
|
disconnect(ProjectExplorer::ProjectManager::instance(),
|
|
|
|
|
&ProjectExplorer::ProjectManager::projectFinishedParsing,
|
2022-12-16 15:12:39 +01:00
|
|
|
this, &AxivionPluginPrivate::handleOpenedDocs);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-16 22:34:56 +01:00
|
|
|
void AxivionPluginPrivate::clearAllMarks()
|
|
|
|
|
{
|
|
|
|
|
const QList<Core::IDocument *> openDocuments = Core::DocumentModel::openedDocuments();
|
|
|
|
|
for (Core::IDocument *doc : openDocuments)
|
|
|
|
|
onDocumentClosed(doc);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-14 12:11:03 +01:00
|
|
|
void AxivionPluginPrivate::handleProjectInfo(const ProjectInfo &info)
|
|
|
|
|
{
|
2023-01-09 07:41:22 +01:00
|
|
|
m_runningQuery = false;
|
2022-12-14 12:11:03 +01:00
|
|
|
if (!info.error.isEmpty()) {
|
|
|
|
|
Core::MessageManager::writeFlashing("Axivion: " + info.error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-09 07:41:22 +01:00
|
|
|
m_currentProjectInfo = info;
|
|
|
|
|
m_axivionOutputPane.updateDashboard();
|
2022-12-16 23:11:46 +01:00
|
|
|
|
2023-01-09 07:41:22 +01:00
|
|
|
if (m_currentProjectInfo.name.isEmpty())
|
2022-12-16 23:11:46 +01:00
|
|
|
return;
|
2022-12-16 15:12:39 +01:00
|
|
|
|
|
|
|
|
// handle already opened documents
|
2023-03-01 17:54:39 +01:00
|
|
|
if (auto buildSystem = ProjectExplorer::ProjectManager::startupBuildSystem();
|
2023-01-12 09:52:24 +01:00
|
|
|
!buildSystem || !buildSystem->isParsing()) {
|
2022-12-16 15:12:39 +01:00
|
|
|
handleOpenedDocs(nullptr);
|
|
|
|
|
} else {
|
2023-03-01 17:54:39 +01:00
|
|
|
connect(ProjectExplorer::ProjectManager::instance(),
|
|
|
|
|
&ProjectExplorer::ProjectManager::projectFinishedParsing,
|
2022-12-16 15:12:39 +01:00
|
|
|
this, &AxivionPluginPrivate::handleOpenedDocs);
|
|
|
|
|
}
|
2022-12-16 23:11:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AxivionPluginPrivate::onDocumentOpened(Core::IDocument *doc)
|
|
|
|
|
{
|
2023-01-09 07:41:22 +01:00
|
|
|
if (m_currentProjectInfo.name.isEmpty()) // we do not have a project info (yet)
|
2022-12-16 23:11:46 +01:00
|
|
|
return;
|
|
|
|
|
|
2023-03-01 17:54:39 +01:00
|
|
|
ProjectExplorer::Project *project = ProjectExplorer::ProjectManager::startupProject();
|
2022-12-16 23:11:46 +01:00
|
|
|
if (!doc || !project->isKnownFile(doc->filePath()))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Utils::FilePath relative = doc->filePath().relativeChildPath(project->projectDirectory());
|
|
|
|
|
// for now only style violations
|
2023-01-09 07:41:22 +01:00
|
|
|
AxivionQuery query(AxivionQuery::IssuesForFileList, {m_currentProjectInfo.name, "SV",
|
2022-12-16 23:11:46 +01:00
|
|
|
relative.path() } );
|
|
|
|
|
AxivionQueryRunner *runner = new AxivionQueryRunner(query, this);
|
|
|
|
|
connect(runner, &AxivionQueryRunner::resultRetrieved, this, [this](const QByteArray &result){
|
|
|
|
|
handleIssuesForFile(ResultParser::parseIssuesList(result));
|
|
|
|
|
});
|
|
|
|
|
connect(runner, &AxivionQueryRunner::finished, [runner]{ runner->deleteLater(); });
|
|
|
|
|
runner->start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AxivionPluginPrivate::onDocumentClosed(Core::IDocument *doc)
|
|
|
|
|
{
|
|
|
|
|
const auto document = qobject_cast<TextEditor::TextDocument *>(doc);
|
|
|
|
|
if (!document)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const TextEditor::TextMarks marks = document->marks();
|
|
|
|
|
for (auto m : marks) {
|
2023-01-12 12:01:51 +01:00
|
|
|
if (m->category().id == AxivionTextMarkId)
|
2022-12-16 23:11:46 +01:00
|
|
|
delete m;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AxivionPluginPrivate::handleIssuesForFile(const IssuesList &issues)
|
|
|
|
|
{
|
|
|
|
|
if (issues.issues.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
2023-03-01 17:54:39 +01:00
|
|
|
ProjectExplorer::Project *project = ProjectExplorer::ProjectManager::startupProject();
|
2022-12-16 23:11:46 +01:00
|
|
|
if (!project)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const Utils::FilePath filePath = project->projectDirectory()
|
|
|
|
|
.pathAppended(issues.issues.first().filePath);
|
|
|
|
|
|
2023-01-09 07:31:50 +01:00
|
|
|
const Utils::Id axivionId(AxivionTextMarkId);
|
2022-12-16 23:11:46 +01:00
|
|
|
for (const ShortIssue &issue : std::as_const(issues.issues)) {
|
|
|
|
|
// FIXME the line location can be wrong (even the whole issue could be wrong)
|
|
|
|
|
// depending on whether this line has been changed since the last axivion run and the
|
|
|
|
|
// current state of the file - some magic has to happen here
|
2023-01-13 13:49:40 +01:00
|
|
|
new AxivionTextMark(filePath, issue);
|
2022-12-16 23:11:46 +01:00
|
|
|
}
|
2022-12-14 12:11:03 +01:00
|
|
|
}
|
|
|
|
|
|
2022-11-28 09:48:11 +01:00
|
|
|
} // Axivion::Internal
|