From a5e5058c41582d88d2648c4a183960e266929a03 Mon Sep 17 00:00:00 2001 From: Christian Stenger Date: Wed, 26 Apr 2023 12:14:12 +0200 Subject: [PATCH] FakeVim: Avoid using invalid regular expressions Print a warning in case of an invalid regular expression and do not apply it to the settings. This silently drops invalid regular expressions entered and restored from settings. Change-Id: I2f7686066541cf2307cf7cf96b3c6f89f6a677d8 Reviewed-by: hjk Reviewed-by: --- src/plugins/fakevim/fakevimplugin.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/plugins/fakevim/fakevimplugin.cpp b/src/plugins/fakevim/fakevimplugin.cpp index 789ceaa9d16..d71c0376457 100644 --- a/src/plugins/fakevim/fakevimplugin.cpp +++ b/src/plugins/fakevim/fakevimplugin.cpp @@ -605,12 +605,17 @@ FakeVimExCommandsMappings::FakeVimExCommandsMappings() m_commandBox = new QGroupBox(Tr::tr("Ex Command"), this); m_commandBox->setEnabled(false); - auto boxLayout = new QHBoxLayout(m_commandBox); + auto commandBoxLayout = new QVBoxLayout(m_commandBox); + auto boxLayout = new QHBoxLayout; + commandBoxLayout->addLayout(boxLayout); m_commandEdit = new FancyLineEdit(m_commandBox); m_commandEdit->setFiltering(true); m_commandEdit->setPlaceholderText(QString()); connect(m_commandEdit, &FancyLineEdit::textChanged, this, &FakeVimExCommandsMappings::commandChanged); + m_commandEdit->setValidationFunction([](FancyLineEdit *e, QString *){ + return QRegularExpression(e->text()).isValid(); + }); auto resetButton = new QPushButton(Tr::tr("Reset"), m_commandBox); resetButton->setToolTip(Tr::tr("Reset to default.")); connect(resetButton, &QPushButton::clicked, @@ -619,6 +624,12 @@ FakeVimExCommandsMappings::FakeVimExCommandsMappings() boxLayout->addWidget(m_commandEdit); boxLayout->addWidget(resetButton); + auto infoLabel = new InfoLabel(Tr::tr("Invalid regular expression."), InfoLabel::Error); + infoLabel->setVisible(false); + connect(m_commandEdit, &FancyLineEdit::validChanged, [infoLabel](bool valid){ + infoLabel->setVisible(!valid); + }); + commandBoxLayout->addWidget(infoLabel); layout()->addWidget(m_commandBox); QMap sections; @@ -675,7 +686,9 @@ ExCommandMap FakeVimExCommandsMappings::exCommandMapFromWidget() if ((regex.isEmpty() && pattern.isEmpty()) || (!regex.isEmpty() && pattern == regex)) continue; - map[name] = QRegularExpression(regex); + const QRegularExpression expression(regex); + if (expression.isValid()) + map[name] = expression; } } return map; @@ -1285,7 +1298,9 @@ void FakeVimPluginPrivate::readSettings() settings->setArrayIndex(i); const QString id = settings->value(idKey).toString(); const QString re = settings->value(reKey).toString(); - m_exCommandMap[id] = QRegularExpression(re); + const QRegularExpression regEx(re); + if (regEx.isValid()) + m_exCommandMap[id] = regEx; } settings->endArray();