forked from qt-creator/qt-creator
Beautifier: Get rid of QRegExp
Change-Id: If3f7e6d93ef1f0b6920f0958f1e2eb00c37462bf Reviewed-by: Orgad Shaneh <orgads@gmail.com>
This commit is contained in:
@@ -37,6 +37,7 @@
|
|||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
|
#include <QRegularExpression>
|
||||||
#include <QXmlStreamWriter>
|
#include <QXmlStreamWriter>
|
||||||
|
|
||||||
namespace Beautifier {
|
namespace Beautifier {
|
||||||
@@ -67,10 +68,11 @@ ArtisticStyleSettings::ArtisticStyleSettings() :
|
|||||||
static int parseVersion(const QString &text)
|
static int parseVersion(const QString &text)
|
||||||
{
|
{
|
||||||
// The version in Artistic Style is printed like "Artistic Style Version 2.04"
|
// The version in Artistic Style is printed like "Artistic Style Version 2.04"
|
||||||
const QRegExp rx("([2-9]{1})\\.([0-9]{2})(\\.[1-9]{1})?$");
|
const QRegularExpression rx("([2-9]{1})\\.([0-9]{2})(\\.[1-9]{1})?$");
|
||||||
if (rx.indexIn(text) != -1) {
|
const QRegularExpressionMatch match = rx.match(text);
|
||||||
const int major = rx.cap(1).toInt() * 100;
|
if (match.hasMatch()) {
|
||||||
const int minor = rx.cap(2).toInt();
|
const int major = match.capturedRef(1).toInt() * 100;
|
||||||
|
const int minor = match.capturedRef(2).toInt();
|
||||||
return major + minor;
|
return major + minor;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@@ -200,7 +202,7 @@ void ArtisticStyleSettings::createDocumentationFile() const
|
|||||||
stream.writeTextElement(Constants::DOCUMENTATION_XMLKEY, key);
|
stream.writeTextElement(Constants::DOCUMENTATION_XMLKEY, key);
|
||||||
stream.writeEndElement();
|
stream.writeEndElement();
|
||||||
const QString text = "<p><span class=\"option\">"
|
const QString text = "<p><span class=\"option\">"
|
||||||
+ keys.filter(QRegExp("^\\-")).join(", ") + "</span></p><p>"
|
+ keys.filter(QRegularExpression("^\\-")).join(", ") + "</span></p><p>"
|
||||||
+ (docu.join(' ').toHtmlEscaped()) + "</p>";
|
+ (docu.join(' ').toHtmlEscaped()) + "</p>";
|
||||||
stream.writeTextElement(Constants::DOCUMENTATION_XMLDOC, text);
|
stream.writeTextElement(Constants::DOCUMENTATION_XMLDOC, text);
|
||||||
stream.writeEndElement();
|
stream.writeEndElement();
|
||||||
|
@@ -152,8 +152,11 @@ FormatTask format(FormatTask task)
|
|||||||
const bool returnsCRLF = task.command.returnsCRLF();
|
const bool returnsCRLF = task.command.returnsCRLF();
|
||||||
if (addsNewline || returnsCRLF) {
|
if (addsNewline || returnsCRLF) {
|
||||||
task.formattedData = QString::fromUtf8(process.readAllStandardOutput());
|
task.formattedData = QString::fromUtf8(process.readAllStandardOutput());
|
||||||
if (addsNewline)
|
if (addsNewline && task.formattedData.endsWith('\n')) {
|
||||||
task.formattedData.remove(QRegExp("(\\r\\n|\\n)$"));
|
task.formattedData.chop(1);
|
||||||
|
if (task.formattedData.endsWith('\r'))
|
||||||
|
task.formattedData.chop(1);
|
||||||
|
}
|
||||||
if (returnsCRLF)
|
if (returnsCRLF)
|
||||||
task.formattedData.replace("\r\n", "\n");
|
task.formattedData.replace("\r\n", "\n");
|
||||||
return task;
|
return task;
|
||||||
|
@@ -32,7 +32,7 @@
|
|||||||
#include <texteditor/texteditorsettings.h>
|
#include <texteditor/texteditorsettings.h>
|
||||||
|
|
||||||
#include <QPushButton>
|
#include <QPushButton>
|
||||||
#include <QRegExpValidator>
|
#include <QRegularExpressionValidator>
|
||||||
|
|
||||||
namespace Beautifier {
|
namespace Beautifier {
|
||||||
namespace Internal {
|
namespace Internal {
|
||||||
@@ -44,8 +44,8 @@ ConfigurationDialog::ConfigurationDialog(QWidget *parent) :
|
|||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
|
|
||||||
// Filter out characters which are not allowed in a file name
|
// Filter out characters which are not allowed in a file name
|
||||||
QRegExpValidator *fileNameValidator = new QRegExpValidator(ui->name);
|
QRegularExpressionValidator *fileNameValidator = new QRegularExpressionValidator(
|
||||||
fileNameValidator->setRegExp(QRegExp("^[^\\/\\\\\\?\\>\\<\\*\\%\\:\\\"\\']*$"));
|
QRegularExpression("^[^\\/\\\\\\?\\>\\<\\*\\%\\:\\\"\\']*$"), ui->name);
|
||||||
ui->name->setValidator(fileNameValidator);
|
ui->name->setValidator(fileNameValidator);
|
||||||
|
|
||||||
updateDocumentation();
|
updateDocumentation();
|
||||||
|
@@ -47,7 +47,6 @@ ConfigurationSyntaxHighlighter::ConfigurationSyntaxHighlighter(QTextDocument *pa
|
|||||||
m_formatComment = fs.toTextCharFormat(TextEditor::C_COMMENT);
|
m_formatComment = fs.toTextCharFormat(TextEditor::C_COMMENT);
|
||||||
|
|
||||||
m_expressionComment.setPattern("#[^\\n]*");
|
m_expressionComment.setPattern("#[^\\n]*");
|
||||||
m_expressionComment.setMinimal(false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConfigurationSyntaxHighlighter::setKeywords(const QStringList &keywords)
|
void ConfigurationSyntaxHighlighter::setKeywords(const QStringList &keywords)
|
||||||
@@ -59,35 +58,29 @@ void ConfigurationSyntaxHighlighter::setKeywords(const QStringList &keywords)
|
|||||||
QStringList pattern;
|
QStringList pattern;
|
||||||
for (const QString &word : keywords) {
|
for (const QString &word : keywords) {
|
||||||
if (!word.isEmpty())
|
if (!word.isEmpty())
|
||||||
pattern << QRegExp::escape(word);
|
pattern << QRegularExpression::escape(word);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_expressionKeyword.setPattern("(?:\\s|^)(" + pattern.join('|') + ")(?=\\s|\\:|\\=|\\,|$)");
|
m_expressionKeyword.setPattern("(?:\\s|^)(" + pattern.join('|') + ")(?=\\s|\\:|\\=|\\,|$)");
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConfigurationSyntaxHighlighter::setCommentExpression(const QRegExp &rx)
|
void ConfigurationSyntaxHighlighter::setCommentExpression(const QRegularExpression &rx)
|
||||||
{
|
{
|
||||||
m_expressionComment = rx;
|
m_expressionComment = rx;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConfigurationSyntaxHighlighter::highlightBlock(const QString &text)
|
void ConfigurationSyntaxHighlighter::highlightBlock(const QString &text)
|
||||||
{
|
{
|
||||||
int pos = 0;
|
QRegularExpressionMatchIterator it = m_expressionKeyword.globalMatch(text);
|
||||||
if (!m_expressionKeyword.isEmpty()) {
|
while (it.hasNext()) {
|
||||||
while ((pos = m_expressionKeyword.indexIn(text, pos)) != -1) {
|
const QRegularExpressionMatch match = it.next();
|
||||||
const int length = m_expressionKeyword.matchedLength();
|
setFormat(match.capturedStart(), match.capturedLength(), m_formatKeyword);
|
||||||
setFormat(pos, length, m_formatKeyword);
|
|
||||||
pos += length;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!m_expressionComment.isEmpty()) {
|
it = m_expressionComment.globalMatch(text);
|
||||||
pos = 0;
|
while (it.hasNext()) {
|
||||||
while ((pos = m_expressionComment.indexIn(text, pos)) != -1) {
|
const QRegularExpressionMatch match = it.next();
|
||||||
const int length = m_expressionComment.matchedLength();
|
setFormat(match.capturedStart(), match.capturedLength(), m_formatComment);
|
||||||
setFormat(pos, length, m_formatComment);
|
|
||||||
pos += length;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,7 +116,7 @@ void ConfigurationEditor::setSettings(AbstractSettings *settings)
|
|||||||
m_model->setStringList(keywords);
|
m_model->setStringList(keywords);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConfigurationEditor::setCommentExpression(const QRegExp &rx)
|
void ConfigurationEditor::setCommentExpression(const QRegularExpression &rx)
|
||||||
{
|
{
|
||||||
m_highlighter->setCommentExpression(rx);
|
m_highlighter->setCommentExpression(rx);
|
||||||
}
|
}
|
||||||
|
@@ -26,7 +26,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <QPlainTextEdit>
|
#include <QPlainTextEdit>
|
||||||
#include <QRegExp>
|
#include <QRegularExpression>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
#include <QSyntaxHighlighter>
|
#include <QSyntaxHighlighter>
|
||||||
@@ -50,14 +50,14 @@ class ConfigurationSyntaxHighlighter : public QSyntaxHighlighter
|
|||||||
public:
|
public:
|
||||||
explicit ConfigurationSyntaxHighlighter(QTextDocument *parent);
|
explicit ConfigurationSyntaxHighlighter(QTextDocument *parent);
|
||||||
void setKeywords(const QStringList &keywords);
|
void setKeywords(const QStringList &keywords);
|
||||||
void setCommentExpression(const QRegExp &rx);
|
void setCommentExpression(const QRegularExpression &rx);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void highlightBlock(const QString &text) override;
|
void highlightBlock(const QString &text) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QRegExp m_expressionKeyword;
|
QRegularExpression m_expressionKeyword;
|
||||||
QRegExp m_expressionComment;
|
QRegularExpression m_expressionComment;
|
||||||
QTextCharFormat m_formatKeyword;
|
QTextCharFormat m_formatKeyword;
|
||||||
QTextCharFormat m_formatComment;
|
QTextCharFormat m_formatComment;
|
||||||
};
|
};
|
||||||
@@ -69,7 +69,7 @@ class ConfigurationEditor : public QPlainTextEdit
|
|||||||
public:
|
public:
|
||||||
explicit ConfigurationEditor(QWidget *parent = nullptr);
|
explicit ConfigurationEditor(QWidget *parent = nullptr);
|
||||||
void setSettings(AbstractSettings *settings);
|
void setSettings(AbstractSettings *settings);
|
||||||
void setCommentExpression(const QRegExp &rx);
|
void setCommentExpression(const QRegularExpression &rx);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool eventFilter(QObject *object, QEvent *event) override;
|
bool eventFilter(QObject *object, QEvent *event) override;
|
||||||
|
Reference in New Issue
Block a user