SilverSearcher: Support regexp substitution

Change-Id: I42cde61fdd052882ae84ae27466a9e79a1782398
Reviewed-by: Przemyslaw Gorszkowski <pgorszkowski@gmail.com>
This commit is contained in:
Orgad Shaneh
2018-10-08 23:15:27 +03:00
committed by Orgad Shaneh
parent 2a40c215e9
commit acd387651e
3 changed files with 30 additions and 4 deletions

View File

@@ -119,7 +119,15 @@ void runSilverSeacher(FutureInterfaceType &fi, FileFindParameters parameters)
process.start("ag", arguments);
if (process.waitForFinished()) {
typedef QList<FileSearchResult> FileSearchResultList;
SilverSearcher::SilverSearcherOutputParser parser(process.readAll());
QRegularExpression regexp;
if (parameters.flags & FindRegularExpression) {
const QRegularExpression::PatternOptions patternOptions =
(parameters.flags & FindCaseSensitively)
? QRegularExpression::NoPatternOption : QRegularExpression::CaseInsensitiveOption;
regexp.setPattern(parameters.text);
regexp.setPatternOptions(patternOptions);
}
SilverSearcher::SilverSearcherOutputParser parser(process.readAll(), regexp);
FileSearchResultList items = parser.parse();
if (!items.isEmpty())
fi.reportResult(items);

View File

@@ -30,10 +30,12 @@
namespace SilverSearcher {
SilverSearcherOutputParser::SilverSearcherOutputParser(
const QByteArray &output)
const QByteArray &output, const QRegularExpression &regexp)
: output(output)
, regexp(regexp)
, outputSize(output.size())
{
hasRegexp = !regexp.pattern().isEmpty();
}
QList<Utils::FileSearchResult> SilverSearcherOutputParser::parse()
@@ -102,6 +104,13 @@ bool SilverSearcherOutputParser::parseMatchLength()
int SilverSearcherOutputParser::parseMatches()
{
int matches = 1;
const int colon = output.indexOf(':', index);
QByteArray text;
if (colon != -1) {
const int textStart = colon + 1;
const int newline = output.indexOf('\n', textStart);
text = output.mid(textStart, newline >= 0 ? newline - textStart : -1);
}
while (index < outputSize && output[index] != ':') {
if (output[index] == ',') {
++matches;
@@ -109,6 +118,10 @@ int SilverSearcherOutputParser::parseMatches()
}
parseMatchIndex();
parseMatchLength();
if (hasRegexp) {
const QString part = QString::fromUtf8(text.mid(item.matchStart, item.matchLength));
item.regexpCapturedTexts = regexp.match(part).capturedTexts();
}
items << item;
}

View File

@@ -28,15 +28,18 @@
#include <coreplugin/find/searchresultwindow.h>
#include <utils/filesearch.h>
#include <QList>
#include <QByteArray>
#include <QList>
#include <QRegularExpression>
namespace SilverSearcher {
class SilverSearcherOutputParser
{
public:
SilverSearcherOutputParser(const QByteArray &output);
SilverSearcherOutputParser(
const QByteArray &output,
const QRegularExpression &regexp = QRegularExpression());
QList<Utils::FileSearchResult> parse();
private:
@@ -48,6 +51,8 @@ private:
bool parseText();
QByteArray output;
QRegularExpression regexp;
bool hasRegexp = false;
int outputSize = 0;
int index = 0;
Utils::FileSearchResult item;