QmlJS: Allow reset of the customized analyzer

Provide a context menu to reset the enabled and disabled
messages of the static analyzer to the internal default.

Change-Id: I98bde71899ad4de50d0e170bf0d2892b87989ceb
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io>
This commit is contained in:
Christian Stenger
2023-05-04 09:05:27 +02:00
parent 427640063e
commit 6e2e7a63e6

View File

@@ -21,6 +21,7 @@
#include <QComboBox>
#include <QLabel>
#include <QLineEdit>
#include <QMenu>
#include <QSettings>
#include <QTextStream>
#include <QTreeView>
@@ -44,6 +45,22 @@ const char DEFAULT_CUSTOM_FORMAT_COMMAND[] = "%{CurrentDocument:Project:QT_HOST_
using namespace QmlJSEditor;
using namespace QmlJSEditor::Internal;
static QList<int> defaultDisabledMessages()
{
static const QList<int> disabledByDefault = Utils::transform(
QmlJS::Check::defaultDisabledMessages(),
[](QmlJS::StaticAnalysis::Type t) { return int(t); });
return disabledByDefault;
}
static QList<int> defaultDisabledMessagesNonQuickUi()
{
static const QList<int> disabledForNonQuickUi = Utils::transform(
QmlJS::Check::defaultDisabledMessagesForNonQuickUi(),
[](QmlJS::StaticAnalysis::Type t){ return int(t); });
return disabledForNonQuickUi;
}
void QmlJsEditingSettings::set()
{
if (get() != *this)
@@ -67,19 +84,13 @@ void QmlJsEditingSettings::fromSettings(QSettings *settings)
m_useCustomFormatCommand = settings->value(CUSTOM_COMMAND, QVariant(false)).toBool();
m_useCustomAnalyzer = settings->value(CUSTOM_ANALYZER, QVariant(false)).toBool();
const QList<int> disabledByDefault = Utils::transform(
QmlJS::Check::defaultDisabledMessages(),
[](QmlJS::StaticAnalysis::Type t) { return int(t); });
m_disabledMessages = Utils::transform<QSet>(
settings->value(DISABLED_MESSAGES,
QVariant::fromValue(disabledByDefault)).toList(),
QVariant::fromValue(defaultDisabledMessages())).toList(),
[](const QVariant &v){ return v.toInt(); });
const QList<int> disabledForNonQuickUi = Utils::transform(
QmlJS::Check::defaultDisabledMessagesForNonQuickUi(),
[](QmlJS::StaticAnalysis::Type t){ return int(t); });
m_disabledMessagesForNonQuickUi = Utils::transform<QSet>(
settings->value(DISABLED_MESSAGES_NONQUICKUI,
QVariant::fromValue(disabledForNonQuickUi)).toList(),
QVariant::fromValue(defaultDisabledMessagesNonQuickUi())).toList(),
[](const QVariant &v) { return v.toInt(); });
settings->endGroup();
@@ -109,20 +120,14 @@ void QmlJsEditingSettings::toSettings(QSettings *settings) const
CUSTOM_ANALYZER,
m_useCustomAnalyzer,
false);
const QList<int> disabledByDefault = Utils::transform(
QmlJS::Check::defaultDisabledMessages(),
[](QmlJS::StaticAnalysis::Type t){ return int(t); });
Utils::QtcSettings::setValueWithDefault(settings,
DISABLED_MESSAGES,
Utils::sorted(Utils::toList(m_disabledMessages)),
disabledByDefault);
const QList<int> disabledNonQuickUi = Utils::transform(
QmlJS::Check::defaultDisabledMessagesForNonQuickUi(),
[](QmlJS::StaticAnalysis::Type t){ return int(t); });
defaultDisabledMessages());
Utils::QtcSettings::setValueWithDefault(settings,
DISABLED_MESSAGES_NONQUICKUI,
Utils::sorted(Utils::toList(m_disabledMessagesForNonQuickUi)),
disabledNonQuickUi);
defaultDisabledMessagesNonQuickUi());
settings->endGroup();
QmllsSettingsManager::instance()->checkForChanges();
}
@@ -394,7 +399,9 @@ public:
analyzerMessagesView->setToolTip(Tr::tr("Enabled checks can be disabled for non Qt Quick UI"
" files,\nbut disabled checks cannot get explicitly"
" enabled for non Qt Quick UI files."));
analyzerMessagesView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(analyzerMessagesView, &QTreeView::customContextMenuRequested,
this, &QmlJsEditingSettingsPageWidget::showContextMenu);
using namespace Layouting;
// clang-format off
QWidget *formattingGroup = nullptr;
@@ -503,6 +510,20 @@ private:
analyzerMessagesView->resizeColumnToContents(column);
}
void showContextMenu(const QPoint &position)
{
QMenu menu;
QAction *reset = new QAction(Tr::tr("Reset to Default"), &menu);
menu.addAction(reset);
connect(reset, &QAction::triggered, this, [this](){
analyzerMessageModel->clear();
populateAnalyzerMessages(Utils::toSet(defaultDisabledMessages()),
Utils::toSet(defaultDisabledMessagesNonQuickUi()));
});
menu.exec(analyzerMessagesView->mapToGlobal(position));
}
QCheckBox *autoFormatOnSave;
QCheckBox *autoFormatOnlyCurrentProject;
QCheckBox *useCustomFormatCommand;