forked from qt-creator/qt-creator
SilverSearcher: Use more FilePath and QtcProcess
Change-Id: Ifbc1108885d0dfc67c09bd6f5cacaef902a2bf54 Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
@@ -32,6 +32,7 @@
|
||||
#include <utils/environment.h>
|
||||
#include <utils/fileutils.h>
|
||||
#include <utils/qtcassert.h>
|
||||
#include <utils/qtcprocess.h>
|
||||
#include <utils/runextensions.h>
|
||||
|
||||
#include "silversearcheroutputparser.h"
|
||||
@@ -39,7 +40,6 @@
|
||||
#include <QHBoxLayout>
|
||||
#include <QLabel>
|
||||
#include <QLineEdit>
|
||||
#include <QProcess>
|
||||
#include <QSettings>
|
||||
|
||||
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<FileSearchResult> 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);
|
||||
|
@@ -35,42 +35,42 @@ namespace Internal {
|
||||
|
||||
void OutputParserTest::test_data()
|
||||
{
|
||||
QTest::addColumn<QByteArray>("parserOutput");
|
||||
QTest::addColumn<QString>("parserOutput");
|
||||
QTest::addColumn<FileSearchResultList>("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<Utils::FileSearchResult> items = ssop.parse();
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -28,7 +28,6 @@
|
||||
#include <coreplugin/find/searchresultwindow.h>
|
||||
#include <utils/filesearch.h>
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QList>
|
||||
#include <QRegularExpression>
|
||||
|
||||
@@ -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<Utils::FileSearchResult> 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;
|
||||
|
Reference in New Issue
Block a user