forked from qt-creator/qt-creator
CppEditor: Improve ExtractFunction's input dialog
Use FancyLineEdit and validate the function name before accepting the dialog. Change-Id: I1634ac757127d3cb39674eeed79388e720d570fa Reviewed-by: Nikolai Kosjar <nikolai.kosjar@theqtcompany.com>
This commit is contained in:
@@ -57,6 +57,7 @@
|
|||||||
|
|
||||||
#include <extensionsystem/pluginmanager.h>
|
#include <extensionsystem/pluginmanager.h>
|
||||||
|
|
||||||
|
#include <utils/fancylineedit.h>
|
||||||
#include <utils/qtcassert.h>
|
#include <utils/qtcassert.h>
|
||||||
|
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
@@ -66,6 +67,7 @@
|
|||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
#include <QFormLayout>
|
#include <QFormLayout>
|
||||||
#include <QInputDialog>
|
#include <QInputDialog>
|
||||||
|
#include <QPushButton>
|
||||||
#include <QSharedPointer>
|
#include <QSharedPointer>
|
||||||
#include <QStack>
|
#include <QStack>
|
||||||
#include <QTextCursor>
|
#include <QTextCursor>
|
||||||
@@ -3148,9 +3150,14 @@ public:
|
|||||||
ExtractFunctionOptions() : access(InsertionPointLocator::Public)
|
ExtractFunctionOptions() : access(InsertionPointLocator::Public)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
static bool isValidFunctionName(const QString &name)
|
||||||
|
{
|
||||||
|
return !name.isEmpty() && isValidIdentifier(name);
|
||||||
|
}
|
||||||
|
|
||||||
bool hasValidFunctionName() const
|
bool hasValidFunctionName() const
|
||||||
{
|
{
|
||||||
return !funcName.isEmpty() && isValidIdentifier(funcName);
|
return isValidFunctionName(funcName);
|
||||||
}
|
}
|
||||||
|
|
||||||
QString funcName;
|
QString funcName;
|
||||||
@@ -3332,7 +3339,10 @@ public:
|
|||||||
"Extract Function Refactoring"));
|
"Extract Function Refactoring"));
|
||||||
auto layout = new QFormLayout(&dlg);
|
auto layout = new QFormLayout(&dlg);
|
||||||
|
|
||||||
auto funcNameEdit = new QLineEdit;
|
auto funcNameEdit = new Utils::FancyLineEdit;
|
||||||
|
funcNameEdit->setValidationFunction([this](Utils::FancyLineEdit *edit, QString *) {
|
||||||
|
return ExtractFunctionOptions::isValidFunctionName(edit->text());
|
||||||
|
});
|
||||||
layout->addRow(QCoreApplication::translate("QuickFix::ExtractFunction",
|
layout->addRow(QCoreApplication::translate("QuickFix::ExtractFunction",
|
||||||
"Function name"), funcNameEdit);
|
"Function name"), funcNameEdit);
|
||||||
|
|
||||||
@@ -3361,21 +3371,17 @@ public:
|
|||||||
auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
|
auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
|
||||||
QObject::connect(buttonBox, &QDialogButtonBox::accepted, &dlg, &QDialog::accept);
|
QObject::connect(buttonBox, &QDialogButtonBox::accepted, &dlg, &QDialog::accept);
|
||||||
QObject::connect(buttonBox, &QDialogButtonBox::rejected, &dlg, &QDialog::reject);
|
QObject::connect(buttonBox, &QDialogButtonBox::rejected, &dlg, &QDialog::reject);
|
||||||
|
QPushButton *ok = buttonBox->button(QDialogButtonBox::Ok);
|
||||||
|
ok->setEnabled(false);
|
||||||
|
QObject::connect(funcNameEdit, &Utils::FancyLineEdit::validChanged,
|
||||||
|
ok, &QPushButton::setEnabled);
|
||||||
layout->addWidget(buttonBox);
|
layout->addWidget(buttonBox);
|
||||||
|
|
||||||
if (dlg.exec() == QDialog::Accepted) {
|
if (dlg.exec() == QDialog::Accepted) {
|
||||||
ExtractFunctionOptions options;
|
ExtractFunctionOptions options;
|
||||||
options.funcName = funcNameEdit->text().trimmed();
|
options.funcName = funcNameEdit->text();
|
||||||
options.access = static_cast<InsertionPointLocator::AccessSpec>(accessCombo->
|
options.access = static_cast<InsertionPointLocator::AccessSpec>(accessCombo->
|
||||||
currentData().toInt());
|
currentData().toInt());
|
||||||
if (!options.hasValidFunctionName()) {
|
|
||||||
Core::AsynchronousMessageBox::critical(
|
|
||||||
QCoreApplication::translate("QuickFix::ExtractFunction",
|
|
||||||
"Extract Function Refactoring"),
|
|
||||||
QCoreApplication::translate("QuickFix::ExtractFunction",
|
|
||||||
"Invalid function name"));
|
|
||||||
return ExtractFunctionOptions();
|
|
||||||
}
|
|
||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
return ExtractFunctionOptions();
|
return ExtractFunctionOptions();
|
||||||
|
|||||||
Reference in New Issue
Block a user