Commit f4815da8 authored by Ahmad Samir's avatar Ahmad Samir
Browse files

[DefualtHighlighter] Port QRegExp to QRegularExpression

parent c9fed55e
......@@ -22,15 +22,17 @@
#include "luakeywords.h"
#include "luahelper.h"
#include <QRegularExpression>
LuaHighlighter::LuaHighlighter(QObject* parent): DefaultHighlighter(parent)
{
addKeywords (LuaKeywords::instance()->keywords());
addFunctions(LuaKeywords::instance()->functions());
addVariables(LuaKeywords::instance()->variables());
addRule(QRegExp(QLatin1String("[A-Za-z0-9_]+(?=\\()")) , functionFormat());
addRule(QRegExp(QLatin1String("\"[^\"]*\"")) , stringFormat());
addRule(QRegExp(QLatin1String("'[^\'].*'")) , stringFormat());
addRule(QRegExp(QLatin1String("--[^\n]*")) , commentFormat());
addRule(QRegularExpression(QStringLiteral("[A-Za-z0-9_]+(?=\\()")) , functionFormat());
addRule(QRegularExpression(QStringLiteral("\"[^\"]*\"")) , stringFormat());
addRule(QRegularExpression(QStringLiteral("'[^\'].*'")) , stringFormat());
addRule(QRegularExpression(QStringLiteral("--[^\n]*")) , commentFormat());
// did not add support for the multiline comment or multiline string
}
......@@ -40,11 +40,11 @@ OctaveHighlighter::OctaveHighlighter(QObject* parent, Session* session): Default
<< QLatin1String("not") << QLatin1String("||") << QLatin1String("&&") << QLatin1String("==");
addRules(operators, operatorFormat());
addRule(QRegExp(QLatin1String("\"[^\"]*\"")), stringFormat());
addRule(QRegExp(QLatin1String("'[^']*'")), stringFormat());
addRule(QRegularExpression(QStringLiteral("\"[^\"]*\"")), stringFormat());
addRule(QRegularExpression(QStringLiteral("'[^']*'")), stringFormat());
addRule(QRegExp(QLatin1String("#[^\n]*")), commentFormat());
addRule(QRegExp(QLatin1String("%[^\n]*")), commentFormat());
addRule(QRegularExpression(QStringLiteral("#[^\n]*")), commentFormat());
addRule(QRegularExpression(QStringLiteral("%[^\n]*")), commentFormat());
rehighlight();
}
......@@ -28,7 +28,7 @@
PythonHighlighter::PythonHighlighter(QObject* parent, PythonSession* session, const int pythonVersion) : Cantor::DefaultHighlighter(parent, session)
{
qDebug() << "PythonHighlighter constructor";
addRule(QRegExp(QLatin1String("\\b\\w+(?=\\()")), functionFormat());
addRule(QRegularExpression(QStringLiteral("\\b\\w+(?=\\()")), functionFormat());
//Code highlighting the different keywords
addKeywords(PythonKeywords::instance()->keywords());
......
......@@ -21,13 +21,15 @@
#include "sagehighlighter.h"
#include "sagekeywords.h"
#include <QRegularExpression>
SageHighlighter::SageHighlighter(QObject* parent) : Cantor::DefaultHighlighter(parent)
{
addRule(QRegExp(QLatin1String("[A-Za-z0-9_]+(?=\\()")), functionFormat());
addRule(QRegularExpression(QStringLiteral("[A-Za-z0-9_]+(?=\\()")), functionFormat());
addKeywords(SageKeywords::instance()->keywords());
addFunctions(SageKeywords::instance()->functions());
addVariables(SageKeywords::instance()->variables());
addRule(QRegExp(QLatin1String("#[^\n]*")), commentFormat());
addRule(QRegularExpression(QStringLiteral("#[^\n]*")), commentFormat());
}
......@@ -33,14 +33,14 @@ ScilabHighlighter::ScilabHighlighter(QObject* parent, Cantor::Session* session)
addFunctions(ScilabKeywords::instance()->functions());
addVariables(ScilabKeywords::instance()->variables());
addRule(QRegExp(QLatin1String("\\b[A-Za-z0-9_]+(?=\\()")), functionFormat());
addRule(QRegularExpression(QStringLiteral("\\b[A-Za-z0-9_]+(?=\\()")), functionFormat());
addRule(QLatin1String("FIXME"), commentFormat());
addRule(QLatin1String("TODO"), commentFormat());
addRule(QRegExp(QLatin1String("\"[^\"]*\"")), stringFormat());
addRule(QRegExp(QLatin1String("'[^']*'")), stringFormat());
addRule(QRegExp(QLatin1String("//[^\n]*")), commentFormat());
addRule(QRegularExpression(QStringLiteral("\"[^\"]*\"")), stringFormat());
addRule(QRegularExpression(QStringLiteral("'[^']*'")), stringFormat());
addRule(QRegularExpression(QStringLiteral("//[^\n]*")), commentFormat());
commentStartExpression = QRegExp(QLatin1String("/\\*"));
commentEndExpression = QRegExp(QLatin1String("\\*/"));
......
......@@ -31,13 +31,14 @@
#include <QGraphicsTextItem>
#include <KColorScheme>
#include <QDebug>
#include <QRegularExpression>
#include <QStack>
using namespace Cantor;
struct HighlightingRule
{
QRegExp regExp;
QRegularExpression regExp;
QTextCharFormat format;
};
......@@ -215,7 +216,7 @@ void DefaultHighlighter::highlightWords(const QString& text)
{
//qDebug() << "DefaultHighlighter::highlightWords";
const QStringList& words = text.split(QRegExp(QLatin1String("\\b")), QString::SkipEmptyParts);
const QStringList& words = text.split(QRegularExpression(QStringLiteral("\\b")), QString::SkipEmptyParts);
int count;
int pos = 0;
......@@ -239,7 +240,7 @@ void DefaultHighlighter::highlightWords(const QString& text)
const QString& w = words[j];
const QString exp = QStringLiteral("(%1)*$").arg(nonSeparatingCharacters());
//qDebug() << "exp: " << exp;
int idx = w.indexOf(QRegExp(exp));
int idx = w.indexOf(QRegularExpression(exp));
const QString& s = w.mid(idx);
//qDebug() << "s: " << s;
......@@ -271,11 +272,10 @@ void DefaultHighlighter::highlightRegExps(const QString& text)
{
foreach (const HighlightingRule& rule, d->regExpRules)
{
int index = rule.regExp.indexIn(text);
while (index >= 0) {
int length = rule.regExp.matchedLength();
setFormat(index, length, rule.format);
index = rule.regExp.indexIn(text, index + length);
QRegularExpressionMatchIterator iter = rule.regExp.globalMatch(text);
while (iter.hasNext()) {
QRegularExpressionMatch match = iter.next();
setFormat(match.capturedStart(0), match.capturedLength(0), rule.format);
}
}
}
......@@ -409,7 +409,7 @@ void DefaultHighlighter::addRule(const QString& word, const QTextCharFormat& for
emit rulesChanged();
}
void DefaultHighlighter::addRule(const QRegExp& regexp, const QTextCharFormat& format)
void DefaultHighlighter::addRule(const QRegularExpression& regexp, const QTextCharFormat& format)
{
HighlightingRule rule = { regexp, format };
d->regExpRules.removeAll(rule);
......@@ -425,7 +425,7 @@ void DefaultHighlighter::removeRule(const QString& word)
emit rulesChanged();
}
void DefaultHighlighter::removeRule(const QRegExp& regexp)
void DefaultHighlighter::removeRule(const QRegularExpression& regexp)
{
HighlightingRule rule = { regexp, QTextCharFormat() };
d->regExpRules.removeAll(rule);
......
......@@ -23,6 +23,7 @@
#include "cantor_export.h"
#include <QRegularExpression>
#include <QSyntaxHighlighter>
class QGraphicsTextItem;
......@@ -129,7 +130,7 @@ class CANTOR_EXPORT DefaultHighlighter : public QSyntaxHighlighter
* @param regexp the regular expression used to look for matches
* @param format the format used to display the matching parts of the text
*/
void addRule(const QRegExp& regexp, const QTextCharFormat& format);
void addRule(const QRegularExpression& regexp, const QTextCharFormat& format);
/**
* Convenience method, highlights all items in @p conditions with the specified @p format
......@@ -138,7 +139,7 @@ class CANTOR_EXPORT DefaultHighlighter : public QSyntaxHighlighter
* greenWords << "tree" << "forest" << "grass";
* addRules(greenWords, greenWordFormat);
* @endcode
* @param conditions any Qt container of QRegExp or QString.
* @param conditions any Qt container of QRegularExpression or QString.
* @param format the format used to display the matching parts of the text
*/
void addRules(const QStringList& conditions, const QTextCharFormat& format);
......@@ -149,7 +150,7 @@ class CANTOR_EXPORT DefaultHighlighter : public QSyntaxHighlighter
/**
* Removes any rules previously added for the regular expression @p regexp
*/
void removeRule(const QRegExp& regexp);
void removeRule(const QRegularExpression& regex);
/**
* Highlight pairs added with addPair()
......
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