diff options
author | Orgad Shaneh <orgad.shaneh@audiocodes.com> | 2018-10-08 23:15:27 +0300 |
---|---|---|
committer | Orgad Shaneh <orgads@gmail.com> | 2018-10-09 09:17:54 +0000 |
commit | acd387651e26989b90c2b88f0c2e58d68d7a6154 (patch) | |
tree | f6e67db13aaae9a7f9357c2e93f7e21adad82cce | |
parent | 2a40c215e91ab739ed6e237f60424faa85e2cfd0 (diff) |
SilverSearcher: Support regexp substitution
Change-Id: I42cde61fdd052882ae84ae27466a9e79a1782398
Reviewed-by: Przemyslaw Gorszkowski <pgorszkowski@gmail.com>
3 files changed, 30 insertions, 4 deletions
diff --git a/src/plugins/silversearcher/findinfilessilversearcher.cpp b/src/plugins/silversearcher/findinfilessilversearcher.cpp index 3f5b34ee870..3c8f19bc487 100644 --- a/src/plugins/silversearcher/findinfilessilversearcher.cpp +++ b/src/plugins/silversearcher/findinfilessilversearcher.cpp @@ -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); diff --git a/src/plugins/silversearcher/silversearcheroutputparser.cpp b/src/plugins/silversearcher/silversearcheroutputparser.cpp index cbc82c5632f..88ec7cdde05 100644 --- a/src/plugins/silversearcher/silversearcheroutputparser.cpp +++ b/src/plugins/silversearcher/silversearcheroutputparser.cpp @@ -30,10 +30,12 @@ namespace SilverSearcher { SilverSearcherOutputParser::SilverSearcherOutputParser( - const QByteArray &output) + const QByteArray &output, const QRegularExpression ®exp) : 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; } diff --git a/src/plugins/silversearcher/silversearcheroutputparser.h b/src/plugins/silversearcher/silversearcheroutputparser.h index fd96253427a..1bf0af4685a 100644 --- a/src/plugins/silversearcher/silversearcheroutputparser.h +++ b/src/plugins/silversearcher/silversearcheroutputparser.h @@ -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 ®exp = 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; |