From 9a0285ba0e1f2aeae4dd8e3d75d005acefcfe2ef Mon Sep 17 00:00:00 2001 From: hjk Date: Fri, 20 Aug 2021 14:11:30 +0200 Subject: [PATCH] SilverSearcher: Use more FilePath and QtcProcess Change-Id: Ifbc1108885d0dfc67c09bd6f5cacaef902a2bf54 Reviewed-by: Christian Stenger --- .../findinfilessilversearcher.cpp | 32 ++++-------- .../silversearcher/outputparser_test.cpp | 52 +++++++++---------- .../silversearcheroutputparser.cpp | 16 +++--- .../silversearcheroutputparser.h | 8 ++- 4 files changed, 48 insertions(+), 60 deletions(-) diff --git a/src/plugins/silversearcher/findinfilessilversearcher.cpp b/src/plugins/silversearcher/findinfilessilversearcher.cpp index ac4750b17ac..ae2bc5b34ea 100644 --- a/src/plugins/silversearcher/findinfilessilversearcher.cpp +++ b/src/plugins/silversearcher/findinfilessilversearcher.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include "silversearcheroutputparser.h" @@ -39,7 +40,6 @@ #include #include #include -#include #include using namespace Core; @@ -84,22 +84,13 @@ QString convertWildcardToRegex(const QString &wildcard) return regex; } -QString silverSearcherExecutable() -{ - return Utils::Environment::systemEnvironment().searchInPath("ag").toString(); -} - bool isSilverSearcherAvailable() { - const QString exe = silverSearcherExecutable(); - if (exe.isEmpty()) - return false; - QProcess silverSearcherProcess; - silverSearcherProcess.setProcessEnvironment( - Utils::Environment::systemEnvironment().toProcessEnvironment()); - silverSearcherProcess.start(exe, {"--version"}); + QtcProcess silverSearcherProcess; + silverSearcherProcess.setCommand({"ag", {"--version"}}); + silverSearcherProcess.start(); if (silverSearcherProcess.waitForFinished(1000)) { - if (silverSearcherProcess.readAll().contains("ag version")) + if (silverSearcherProcess.stdOut().contains("ag version")) return true; } @@ -109,7 +100,7 @@ bool isSilverSearcherAvailable() void runSilverSeacher(FutureInterfaceType &fi, FileFindParameters parameters) { ProgressTimer progress(fi, 5); - const QString directory = parameters.additionalParameters.toString(); + const FilePath directory = FilePath::fromUserInput(parameters.additionalParameters.toString()); QStringList arguments = {"--parallel", "--ackmate"}; if (parameters.flags & FindCaseSensitively) @@ -138,12 +129,11 @@ void runSilverSeacher(FutureInterfaceType &fi, FileFindParameters parameters) if (!params.searchOptions.isEmpty()) arguments << params.searchOptions.split(' '); - const FilePath path = FilePath::fromUserInput(FileUtils::normalizedPathName(directory)); - arguments << "--" << parameters.text << path.toString(); + arguments << "--" << parameters.text << directory.normalizePathName().toString(); - QProcess process; - process.setProcessEnvironment(Utils::Environment::systemEnvironment().toProcessEnvironment()); - process.start(silverSearcherExecutable(), arguments); + QtcProcess process; + process.setCommand({"ag", arguments}); + process.start(); if (process.waitForFinished()) { typedef QList FileSearchResultList; QRegularExpression regexp; @@ -155,7 +145,7 @@ void runSilverSeacher(FutureInterfaceType &fi, FileFindParameters parameters) regexp.setPattern(parameters.text); regexp.setPatternOptions(patternOptions); } - SilverSearcher::SilverSearcherOutputParser parser(process.readAll(), regexp); + SilverSearcher::SilverSearcherOutputParser parser(process.stdOut(), regexp); FileSearchResultList items = parser.parse(); if (!items.isEmpty()) fi.reportResult(items); diff --git a/src/plugins/silversearcher/outputparser_test.cpp b/src/plugins/silversearcher/outputparser_test.cpp index ddbb3e9df40..1c1fda4c2c1 100644 --- a/src/plugins/silversearcher/outputparser_test.cpp +++ b/src/plugins/silversearcher/outputparser_test.cpp @@ -35,42 +35,42 @@ namespace Internal { void OutputParserTest::test_data() { - QTest::addColumn("parserOutput"); + QTest::addColumn("parserOutput"); QTest::addColumn("results"); - QTest::addRow("nothing") << QByteArray("\n") << FileSearchResultList(); + QTest::addRow("nothing") << QString("\n") << FileSearchResultList(); QTest::addRow("oneFileOneMatch") - << QByteArray(":/file/path/to/filename.h\n" - "1;1 5:match\n") - << FileSearchResultList({{"/file/path/to/filename.h", 1, "match", 1, 5, {}}}); + << QString(":/file/path/to/filename.h\n" + "1;1 5:match\n") + << FileSearchResultList({{"/file/path/to/filename.h", 1, "match", 1, 5, {}}}); QTest::addRow("multipleFilesWithOneMatch") - << QByteArray(":/file/path/to/filename1.h\n" - "1;1 5:match\n" - "\n" - ":/file/path/to/filename2.h\n" - "2;2 5: match\n") - << FileSearchResultList({{"/file/path/to/filename1.h", 1, "match", 1, 5, {}}, - {"/file/path/to/filename2.h", 2, " match", 2, 5, {}}}); + << QString(":/file/path/to/filename1.h\n" + "1;1 5:match\n" + "\n" + ":/file/path/to/filename2.h\n" + "2;2 5: match\n") + << FileSearchResultList({{"/file/path/to/filename1.h", 1, "match", 1, 5, {}}, + {"/file/path/to/filename2.h", 2, " match", 2, 5, {}}}); QTest::addRow("oneFileMultipleMatches") - << QByteArray(":/file/path/to/filename.h\n" - "1;1 5,7 5:match match\n") - << FileSearchResultList({{"/file/path/to/filename.h", 1, "match match", 1, 5, {}}, - {"/file/path/to/filename.h", 1, "match match", 7, 5, {}}}); + << QString(":/file/path/to/filename.h\n" + "1;1 5,7 5:match match\n") + << FileSearchResultList({{"/file/path/to/filename.h", 1, "match match", 1, 5, {}}, + {"/file/path/to/filename.h", 1, "match match", 7, 5, {}}}); QTest::addRow("multipleFilesWithMultipleMatches") - << QByteArray(":/file/path/to/filename1.h\n" - "1;1 5,7 5:match match\n" - "\n" - ":/file/path/to/filename2.h\n" - "2;2 5,8 5: match match\n") - << FileSearchResultList({{"/file/path/to/filename1.h", 1, "match match", 1, 5, {}}, - {"/file/path/to/filename1.h", 1, "match match", 7, 5, {}}, - {"/file/path/to/filename2.h", 2, " match match", 2, 5, {}}, - {"/file/path/to/filename2.h", 2, " match match", 8, 5, {}}}); + << QString(":/file/path/to/filename1.h\n" + "1;1 5,7 5:match match\n" + "\n" + ":/file/path/to/filename2.h\n" + "2;2 5,8 5: match match\n") + << FileSearchResultList({{"/file/path/to/filename1.h", 1, "match match", 1, 5, {}}, + {"/file/path/to/filename1.h", 1, "match match", 7, 5, {}}, + {"/file/path/to/filename2.h", 2, " match match", 2, 5, {}}, + {"/file/path/to/filename2.h", 2, " match match", 8, 5, {}}}); } void OutputParserTest::test() { - QFETCH(QByteArray, parserOutput); + QFETCH(QString, parserOutput); QFETCH(FileSearchResultList, results); SilverSearcher::SilverSearcherOutputParser ssop(parserOutput); const QList items = ssop.parse(); diff --git a/src/plugins/silversearcher/silversearcheroutputparser.cpp b/src/plugins/silversearcher/silversearcheroutputparser.cpp index 0dab3250f9a..48e78ddc816 100644 --- a/src/plugins/silversearcher/silversearcheroutputparser.cpp +++ b/src/plugins/silversearcher/silversearcheroutputparser.cpp @@ -30,7 +30,7 @@ namespace SilverSearcher { SilverSearcherOutputParser::SilverSearcherOutputParser( - const QByteArray &output, const QRegularExpression ®exp) + const QString &output, const QRegularExpression ®exp) : output(output) , regexp(regexp) , outputSize(output.size()) @@ -67,7 +67,7 @@ bool SilverSearcherOutputParser::parseFilePath() int startIndex = ++index; while (index < outputSize && output[index] != '\n') ++index; - item.fileName = QString::fromUtf8(output.data() + startIndex, index - startIndex); + item.fileName = QString(output.data() + startIndex, index - startIndex); ++index; return true; } @@ -77,7 +77,7 @@ bool SilverSearcherOutputParser::parseLineNumber() int startIndex = index; while (index < outputSize && output[++index] != ';') { } - item.lineNumber = QString::fromUtf8(output.data() + startIndex, index - startIndex).toInt(); + item.lineNumber = QString(output.data() + startIndex, index - startIndex).toInt(); ++index; return true; } @@ -87,7 +87,7 @@ bool SilverSearcherOutputParser::parseMatchIndex() int startIndex = index; while (index < outputSize && output[++index] != ' ') { } - item.matchStart = QString::fromUtf8(output.data() + startIndex, index - startIndex).toInt(); + item.matchStart = QString(output.data() + startIndex, index - startIndex).toInt(); ++index; return true; } @@ -97,7 +97,7 @@ bool SilverSearcherOutputParser::parseMatchLength() int startIndex = index; while (index < outputSize && output[++index] != ':' && output[index] != ',') { } - item.matchLength = QString::fromUtf8(output.data() + startIndex, index - startIndex).toInt(); + item.matchLength = QString(output.data() + startIndex, index - startIndex).toInt(); return true; } @@ -105,7 +105,7 @@ int SilverSearcherOutputParser::parseMatches() { int matches = 1; const int colon = output.indexOf(':', index); - QByteArray text; + QString text; if (colon != -1) { const int textStart = colon + 1; const int newline = output.indexOf('\n', textStart); @@ -119,7 +119,7 @@ int SilverSearcherOutputParser::parseMatches() parseMatchIndex(); parseMatchLength(); if (hasRegexp) { - const QString part = QString::fromUtf8(text.mid(item.matchStart, item.matchLength)); + const QString part = QString(text.mid(item.matchStart, item.matchLength)); item.regexpCapturedTexts = regexp.match(part).capturedTexts(); } items << item; @@ -133,7 +133,7 @@ bool SilverSearcherOutputParser::parseText() { int startIndex = index; while (index < outputSize && output[++index] != '\n') { } - item.matchingLine = QString::fromUtf8(output.data() + startIndex, index - startIndex); + item.matchingLine = QString(output.data() + startIndex, index - startIndex); ++index; return true; } diff --git a/src/plugins/silversearcher/silversearcheroutputparser.h b/src/plugins/silversearcher/silversearcheroutputparser.h index 1bf0af4685a..0c39820b4a1 100644 --- a/src/plugins/silversearcher/silversearcheroutputparser.h +++ b/src/plugins/silversearcher/silversearcheroutputparser.h @@ -28,7 +28,6 @@ #include #include -#include #include #include @@ -37,11 +36,10 @@ namespace SilverSearcher { class SilverSearcherOutputParser { public: - SilverSearcherOutputParser( - const QByteArray &output, - const QRegularExpression ®exp = QRegularExpression()); + SilverSearcherOutputParser(const QString &output, const QRegularExpression ®exp = {}); QList parse(); + private: int parseMatches(); bool parseMatchLength(); @@ -50,7 +48,7 @@ private: bool parseFilePath(); bool parseText(); - QByteArray output; + QString output; QRegularExpression regexp; bool hasRegexp = false; int outputSize = 0;