Commit c9208db6 authored by Eike Ziller's avatar Eike Ziller

Advanced Find: Check regular expressions for validity

If regular expression flag is enabled, check the search term for a valid
expression and disable searching in case it is not.

Uses QRegularExpression (Perl-compatible) which is used by all the
existing searches.

Task-number: QTCREATORBUG-14928
Change-Id: I37d536d8d0ac190de486790aca87616e9b320eb1
Reviewed-by: Friedemann Kleint's avatarFriedemann Kleint <Friedemann.Kleint@qt.io>
parent b8885205
......@@ -35,6 +35,7 @@
#include <QStringListModel>
#include <QCompleter>
#include <QKeyEvent>
#include <QRegularExpression>
#include <QScrollArea>
using namespace Core;
......@@ -42,6 +43,23 @@ using namespace Core::Internal;
static FindToolWindow *m_instance = 0;
static bool validateRegExp(Utils::FancyLineEdit *edit, QString *errorMessage)
{
if (edit->text().isEmpty()) {
if (errorMessage)
*errorMessage = FindToolWindow::tr("Empty search term");
return false;
}
if (Find::hasFindFlag(FindRegularExpression)) {
QRegularExpression regexp(edit->text());
bool regexpValid = regexp.isValid();
if (!regexpValid && errorMessage)
*errorMessage = regexp.errorString();
return regexpValid;
}
return true;
}
FindToolWindow::FindToolWindow(QWidget *parent)
: QWidget(parent),
m_findCompleter(new QCompleter(this)),
......@@ -61,11 +79,17 @@ FindToolWindow::FindToolWindow(QWidget *parent)
connect(m_ui.regExp, &QAbstractButton::toggled, Find::instance(), &Find::setRegularExpression);
connect(m_ui.filterList, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated),
this, static_cast<void (FindToolWindow::*)(int)>(&FindToolWindow::setCurrentFilter));
connect(m_ui.searchTerm, &QLineEdit::textChanged, this, &FindToolWindow::updateButtonStates);
m_findCompleter->setModel(Find::findCompletionModel());
m_ui.searchTerm->setSpecialCompleter(m_findCompleter);
m_ui.searchTerm->installEventFilter(this);
m_ui.searchTerm->setValidationFunction(validateRegExp);
connect(Find::instance(), &Find::findFlagsChanged,
m_ui.searchTerm, &Utils::FancyLineEdit::validate);
connect(m_ui.searchTerm, &Utils::FancyLineEdit::validChanged,
this, &FindToolWindow::updateButtonStates);
QVBoxLayout *layout = new QVBoxLayout;
layout->setMargin(0);
layout->setSpacing(0);
......@@ -116,7 +140,7 @@ bool FindToolWindow::eventFilter(QObject *obj, QEvent *event)
void FindToolWindow::updateButtonStates()
{
bool filterEnabled = m_currentFilter && m_currentFilter->isEnabled();
bool enabled = !m_ui.searchTerm->text().isEmpty() && filterEnabled && m_currentFilter->isValid();
bool enabled = m_ui.searchTerm->isValid() && filterEnabled && m_currentFilter->isValid();
m_ui.searchButton->setEnabled(enabled);
m_ui.replaceButton->setEnabled(m_currentFilter
&& m_currentFilter->isReplaceSupported() && enabled);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment