forked from qt-creator/qt-creator
FakeVim: Finish port to QRegularExpression
Change-Id: Id4eaab8f41be3b724ddf22f74384a60995cf4aa5 Reviewed-by: Lukas Holecek <hluk@email.cz> Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
@@ -63,7 +63,6 @@
|
|||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QPointer>
|
#include <QPointer>
|
||||||
#include <QProcess>
|
#include <QProcess>
|
||||||
#include <QRegExp>
|
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
@@ -388,9 +387,9 @@ static bool eatString(const QString &prefix, QString *str)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static QRegExp vimPatternToQtPattern(QString needle, bool ignoreCaseOption, bool smartCaseOption)
|
static QRegularExpression vimPatternToQtPattern(QString needle, bool ignoreCaseOption, bool smartCaseOption)
|
||||||
{
|
{
|
||||||
/* Transformations (Vim regexp -> QRegExp):
|
/* Transformations (Vim regexp -> QRegularExpression):
|
||||||
* \a -> [A-Za-z]
|
* \a -> [A-Za-z]
|
||||||
* \A -> [^A-Za-z]
|
* \A -> [^A-Za-z]
|
||||||
* \h -> [A-Za-z_]
|
* \h -> [A-Za-z_]
|
||||||
@@ -531,7 +530,7 @@ static QRegExp vimPatternToQtPattern(QString needle, bool ignoreCaseOption, bool
|
|||||||
else if (brace)
|
else if (brace)
|
||||||
pattern.append('[');
|
pattern.append('[');
|
||||||
|
|
||||||
return QRegExp(pattern);
|
return QRegularExpression(pattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool afterEndOfLine(const QTextDocument *doc, int position)
|
static bool afterEndOfLine(const QTextDocument *doc, int position)
|
||||||
@@ -540,7 +539,7 @@ static bool afterEndOfLine(const QTextDocument *doc, int position)
|
|||||||
&& doc->findBlock(position).length() > 1;
|
&& doc->findBlock(position).length() > 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void searchForward(QTextCursor *tc, QRegExp &needleExp, int *repeat)
|
static void searchForward(QTextCursor *tc, const QRegularExpression &needleExp, int *repeat)
|
||||||
{
|
{
|
||||||
const QTextDocument *doc = tc->document();
|
const QTextDocument *doc = tc->document();
|
||||||
const int startPos = tc->position();
|
const int startPos = tc->position();
|
||||||
@@ -578,16 +577,18 @@ static void searchForward(QTextCursor *tc, QRegExp &needleExp, int *repeat)
|
|||||||
tc->movePosition(Left);
|
tc->movePosition(Left);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void searchBackward(QTextCursor *tc, QRegExp &needleExp, int *repeat)
|
static void searchBackward(QTextCursor *tc, const QRegularExpression &needleExp, int *repeat)
|
||||||
{
|
{
|
||||||
// Search from beginning of line so that matched text is the same.
|
// Search from beginning of line so that matched text is the same.
|
||||||
QTextBlock block = tc->block();
|
QTextBlock block = tc->block();
|
||||||
QString line = block.text();
|
QString line = block.text();
|
||||||
|
|
||||||
int i = line.indexOf(needleExp, 0);
|
QRegularExpressionMatch match;
|
||||||
|
int i = line.indexOf(needleExp, 0, &match);
|
||||||
while (i != -1 && i < tc->positionInBlock()) {
|
while (i != -1 && i < tc->positionInBlock()) {
|
||||||
--*repeat;
|
--*repeat;
|
||||||
i = line.indexOf(needleExp, i + qMax(1, needleExp.matchedLength()));
|
const int offset = i + qMax(1, match.capturedLength());
|
||||||
|
i = line.indexOf(needleExp, offset, &match);
|
||||||
if (i == line.size())
|
if (i == line.size())
|
||||||
i = -1;
|
i = -1;
|
||||||
}
|
}
|
||||||
@@ -600,10 +601,11 @@ static void searchBackward(QTextCursor *tc, QRegExp &needleExp, int *repeat)
|
|||||||
if (!block.isValid())
|
if (!block.isValid())
|
||||||
break;
|
break;
|
||||||
line = block.text();
|
line = block.text();
|
||||||
i = line.indexOf(needleExp, 0);
|
i = line.indexOf(needleExp, 0, &match);
|
||||||
while (i != -1) {
|
while (i != -1) {
|
||||||
--*repeat;
|
--*repeat;
|
||||||
i = line.indexOf(needleExp, i + qMax(1, needleExp.matchedLength()));
|
const int offset = i + qMax(1, match.capturedLength());
|
||||||
|
i = line.indexOf(needleExp, offset, &match);
|
||||||
if (i == line.size())
|
if (i == line.size())
|
||||||
i = -1;
|
i = -1;
|
||||||
}
|
}
|
||||||
@@ -614,19 +616,20 @@ static void searchBackward(QTextCursor *tc, QRegExp &needleExp, int *repeat)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
i = line.indexOf(needleExp, 0);
|
i = line.indexOf(needleExp, 0, &match);
|
||||||
while (*repeat < 0) {
|
while (*repeat < 0) {
|
||||||
i = line.indexOf(needleExp, i + qMax(1, needleExp.matchedLength()));
|
const int offset = i + qMax(1, match.capturedLength());
|
||||||
|
i = line.indexOf(needleExp, offset, &match);
|
||||||
++*repeat;
|
++*repeat;
|
||||||
}
|
}
|
||||||
tc->setPosition(block.position() + i);
|
tc->setPosition(block.position() + i);
|
||||||
tc->setPosition(tc->position() + needleExp.matchedLength(), KeepAnchor);
|
tc->setPosition(tc->position() + match.capturedLength(), KeepAnchor);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Commands [[, []
|
// Commands [[, []
|
||||||
static void bracketSearchBackward(QTextCursor *tc, const QString &needleExp, int repeat)
|
static void bracketSearchBackward(QTextCursor *tc, const QString &needleExp, int repeat)
|
||||||
{
|
{
|
||||||
QRegExp re(needleExp);
|
const QRegularExpression re(needleExp);
|
||||||
QTextCursor tc2 = *tc;
|
QTextCursor tc2 = *tc;
|
||||||
tc2.setPosition(tc2.position() - 1);
|
tc2.setPosition(tc2.position() - 1);
|
||||||
searchBackward(&tc2, re, &repeat);
|
searchBackward(&tc2, re, &repeat);
|
||||||
@@ -639,7 +642,7 @@ static void bracketSearchBackward(QTextCursor *tc, const QString &needleExp, int
|
|||||||
static void bracketSearchForward(QTextCursor *tc, const QString &needleExp, int repeat,
|
static void bracketSearchForward(QTextCursor *tc, const QString &needleExp, int repeat,
|
||||||
bool searchWithCommand)
|
bool searchWithCommand)
|
||||||
{
|
{
|
||||||
QRegExp re(searchWithCommand ? QString("^\\}|^\\{") : needleExp);
|
QRegularExpression re(searchWithCommand ? QString("^\\}|^\\{") : needleExp);
|
||||||
QTextCursor tc2 = *tc;
|
QTextCursor tc2 = *tc;
|
||||||
tc2.setPosition(tc2.position() + 1);
|
tc2.setPosition(tc2.position() + 1);
|
||||||
searchForward(&tc2, re, &repeat);
|
searchForward(&tc2, re, &repeat);
|
||||||
@@ -669,17 +672,21 @@ static char backslashed(char t)
|
|||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool substituteText(QString *text, QRegExp &pattern, const QString &replacement,
|
static bool substituteText(QString *text,
|
||||||
bool global)
|
const QRegularExpression &pattern,
|
||||||
|
const QString &replacement,
|
||||||
|
bool global)
|
||||||
{
|
{
|
||||||
bool substituted = false;
|
bool substituted = false;
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
int right = -1;
|
int right = -1;
|
||||||
while (true) {
|
while (true) {
|
||||||
pos = pattern.indexIn(*text, pos, QRegExp::CaretAtZero);
|
const QRegularExpressionMatch match = pattern.match(*text, pos);
|
||||||
if (pos == -1)
|
if (!match.hasMatch())
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
pos = match.capturedStart();
|
||||||
|
|
||||||
// ensure that substitution is advancing towards end of line
|
// ensure that substitution is advancing towards end of line
|
||||||
if (right == text->size() - pos) {
|
if (right == text->size() - pos) {
|
||||||
++pos;
|
++pos;
|
||||||
@@ -691,7 +698,7 @@ static bool substituteText(QString *text, QRegExp &pattern, const QString &repla
|
|||||||
right = text->size() - pos;
|
right = text->size() - pos;
|
||||||
|
|
||||||
substituted = true;
|
substituted = true;
|
||||||
QString matched = text->mid(pos, pattern.cap(0).size());
|
QString matched = text->mid(pos, match.captured(0).size());
|
||||||
QString repl;
|
QString repl;
|
||||||
bool escape = false;
|
bool escape = false;
|
||||||
// insert captured texts
|
// insert captured texts
|
||||||
@@ -700,8 +707,8 @@ static bool substituteText(QString *text, QRegExp &pattern, const QString &repla
|
|||||||
if (escape) {
|
if (escape) {
|
||||||
escape = false;
|
escape = false;
|
||||||
if (c.isDigit()) {
|
if (c.isDigit()) {
|
||||||
if (c.digitValue() <= pattern.captureCount())
|
if (c.digitValue() <= match.lastCapturedIndex())
|
||||||
repl += pattern.cap(c.digitValue());
|
repl += match.captured(c.digitValue());
|
||||||
} else {
|
} else {
|
||||||
repl += backslashed(c.unicode());
|
repl += backslashed(c.unicode());
|
||||||
}
|
}
|
||||||
@@ -709,7 +716,7 @@ static bool substituteText(QString *text, QRegExp &pattern, const QString &repla
|
|||||||
if (c == '\\')
|
if (c == '\\')
|
||||||
escape = true;
|
escape = true;
|
||||||
else if (c == '&')
|
else if (c == '&')
|
||||||
repl += pattern.cap(0);
|
repl += match.captured(0);
|
||||||
else
|
else
|
||||||
repl += c;
|
repl += c;
|
||||||
}
|
}
|
||||||
@@ -5552,8 +5559,9 @@ bool FakeVimHandler::Private::handleExSubstituteCommand(const ExCommand &cmd)
|
|||||||
if (g.lastSubstituteFlags.contains('i'))
|
if (g.lastSubstituteFlags.contains('i'))
|
||||||
needle.prepend("\\c");
|
needle.prepend("\\c");
|
||||||
|
|
||||||
QRegExp pattern = vimPatternToQtPattern(needle, hasConfig(ConfigIgnoreCase),
|
const QRegularExpression pattern = vimPatternToQtPattern(needle,
|
||||||
hasConfig(ConfigSmartCase));
|
hasConfig(ConfigIgnoreCase),
|
||||||
|
hasConfig(ConfigSmartCase));
|
||||||
|
|
||||||
QTextBlock lastBlock;
|
QTextBlock lastBlock;
|
||||||
QTextBlock firstBlock;
|
QTextBlock firstBlock;
|
||||||
@@ -6347,8 +6355,9 @@ void FakeVimHandler::Private::searchBalanced(bool forward, QChar needle, QChar o
|
|||||||
QTextCursor FakeVimHandler::Private::search(const SearchData &sd, int startPos, int count,
|
QTextCursor FakeVimHandler::Private::search(const SearchData &sd, int startPos, int count,
|
||||||
bool showMessages)
|
bool showMessages)
|
||||||
{
|
{
|
||||||
QRegExp needleExp = vimPatternToQtPattern(sd.needle, hasConfig(ConfigIgnoreCase),
|
const QRegularExpression needleExp = vimPatternToQtPattern(sd.needle,
|
||||||
hasConfig(ConfigSmartCase));
|
hasConfig(ConfigIgnoreCase),
|
||||||
|
hasConfig(ConfigSmartCase));
|
||||||
if (!needleExp.isValid()) {
|
if (!needleExp.isValid()) {
|
||||||
if (showMessages) {
|
if (showMessages) {
|
||||||
QString error = needleExp.errorString();
|
QString error = needleExp.errorString();
|
||||||
|
Reference in New Issue
Block a user