Skip to content
Snippets Groups Projects
Commit ffb0fe65 authored by con's avatar con
Browse files

Could not replace regular expressions with captured text.

Reviewed-by: Oswald Buddenhagen
parent 0792d458
No related branches found
No related tags found
No related merge requests found
...@@ -142,14 +142,53 @@ bool BaseTextFind::findStep(const QString &txt, IFindSupport::FindFlags findFlag ...@@ -142,14 +142,53 @@ bool BaseTextFind::findStep(const QString &txt, IFindSupport::FindFlags findFlag
return found; return found;
} }
namespace {
QString expandRegExpReplacement(const QString &replaceText, const QRegExp &regexp)
{
QString result;
for (int i = 0; i < replaceText.length(); ++i) {
QChar c = replaceText.at(i);
if (c == QLatin1Char('\\') && i < replaceText.length() - 1) {
c = replaceText.at(++i);
if (c == QLatin1Char('\\')) {
result += QLatin1Char('\\');
} else if (c == QLatin1Char('&')) {
result += QLatin1Char('&');
} else if (c.isDigit()) {
int index = c.unicode()-'1';
if (index < regexp.numCaptures()) {
result += regexp.cap(index+1);
} else {
result += QLatin1Char('\\');
result += c;
}
} else {
result += QLatin1Char('\\');
result += c;
}
} else if (c == QLatin1Char('&')) {
result += regexp.cap(0);
} else {
result += c;
}
}
return result;
}
}
bool BaseTextFind::replaceStep(const QString &before, const QString &after, bool BaseTextFind::replaceStep(const QString &before, const QString &after,
IFindSupport::FindFlags findFlags) IFindSupport::FindFlags findFlags)
{ {
QTextCursor cursor = textCursor(); QTextCursor cursor = textCursor();
if (cursor.selectedText().compare(before, bool usesRegExp = (findFlags & IFindSupport::FindRegularExpression);
((findFlags&IFindSupport::FindCaseSensitively)!=0) ? Qt::CaseSensitive : Qt::CaseInsensitive) == 0) { QRegExp regexp(before,
(findFlags & IFindSupport::FindCaseSensitively) ? Qt::CaseSensitive : Qt::CaseInsensitive,
usesRegExp ? QRegExp::RegExp : QRegExp::FixedString);
if (regexp.exactMatch(cursor.selectedText())) {
QString realAfter = usesRegExp ? expandRegExpReplacement(after, regexp) : after;
int start = cursor.selectionStart(); int start = cursor.selectionStart();
cursor.insertText(after); cursor.insertText(realAfter);
if ((findFlags&IFindSupport::FindBackward) != 0) if ((findFlags&IFindSupport::FindBackward) != 0)
cursor.setPosition(start); cursor.setPosition(start);
} }
...@@ -166,15 +205,18 @@ int BaseTextFind::replaceAll(const QString &before, const QString &after, ...@@ -166,15 +205,18 @@ int BaseTextFind::replaceAll(const QString &before, const QString &after,
editCursor.movePosition(QTextCursor::Start); editCursor.movePosition(QTextCursor::Start);
editCursor.beginEditBlock(); editCursor.beginEditBlock();
int count = 0; int count = 0;
bool usesRegExp = (findFlags & IFindSupport::FindRegularExpression);
QRegExp regexp(before); QRegExp regexp(before);
regexp.setPatternSyntax((findFlags&IFindSupport::FindRegularExpression) ? QRegExp::RegExp : QRegExp::FixedString); regexp.setPatternSyntax(usesRegExp ? QRegExp::RegExp : QRegExp::FixedString);
regexp.setCaseSensitivity((findFlags&IFindSupport::FindCaseSensitively) ? Qt::CaseSensitive : Qt::CaseInsensitive); regexp.setCaseSensitivity((findFlags & IFindSupport::FindCaseSensitively) ? Qt::CaseSensitive : Qt::CaseInsensitive);
QTextCursor found = document()->find(regexp, editCursor, IFindSupport::textDocumentFlagsForFindFlags(findFlags)); QTextCursor found = document()->find(regexp, editCursor, IFindSupport::textDocumentFlagsForFindFlags(findFlags));
while (!found.isNull() && inScope(found.selectionStart(), found.selectionEnd())) { while (!found.isNull() && inScope(found.selectionStart(), found.selectionEnd())) {
++count; ++count;
editCursor.setPosition(found.selectionStart()); editCursor.setPosition(found.selectionStart());
editCursor.setPosition(found.selectionEnd(), QTextCursor::KeepAnchor); editCursor.setPosition(found.selectionEnd(), QTextCursor::KeepAnchor);
editCursor.insertText(after); regexp.exactMatch(found.selectedText());
QString realAfter = usesRegExp ? expandRegExpReplacement(after, regexp) : after;
editCursor.insertText(realAfter);
found = document()->find(regexp, editCursor, IFindSupport::textDocumentFlagsForFindFlags(findFlags)); found = document()->find(regexp, editCursor, IFindSupport::textDocumentFlagsForFindFlags(findFlags));
} }
editCursor.endEditBlock(); editCursor.endEditBlock();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment