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/environment.h>
|
||||||
#include <utils/fileutils.h>
|
#include <utils/fileutils.h>
|
||||||
#include <utils/qtcassert.h>
|
#include <utils/qtcassert.h>
|
||||||
|
#include <utils/qtcprocess.h>
|
||||||
#include <utils/runextensions.h>
|
#include <utils/runextensions.h>
|
||||||
|
|
||||||
#include "silversearcheroutputparser.h"
|
#include "silversearcheroutputparser.h"
|
||||||
@@ -39,7 +40,6 @@
|
|||||||
#include <QHBoxLayout>
|
#include <QHBoxLayout>
|
||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
#include <QLineEdit>
|
#include <QLineEdit>
|
||||||
#include <QProcess>
|
|
||||||
#include <QSettings>
|
#include <QSettings>
|
||||||
|
|
||||||
using namespace Core;
|
using namespace Core;
|
||||||
@@ -84,22 +84,13 @@ QString convertWildcardToRegex(const QString &wildcard)
|
|||||||
return regex;
|
return regex;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString silverSearcherExecutable()
|
|
||||||
{
|
|
||||||
return Utils::Environment::systemEnvironment().searchInPath("ag").toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isSilverSearcherAvailable()
|
bool isSilverSearcherAvailable()
|
||||||
{
|
{
|
||||||
const QString exe = silverSearcherExecutable();
|
QtcProcess silverSearcherProcess;
|
||||||
if (exe.isEmpty())
|
silverSearcherProcess.setCommand({"ag", {"--version"}});
|
||||||
return false;
|
silverSearcherProcess.start();
|
||||||
QProcess silverSearcherProcess;
|
|
||||||
silverSearcherProcess.setProcessEnvironment(
|
|
||||||
Utils::Environment::systemEnvironment().toProcessEnvironment());
|
|
||||||
silverSearcherProcess.start(exe, {"--version"});
|
|
||||||
if (silverSearcherProcess.waitForFinished(1000)) {
|
if (silverSearcherProcess.waitForFinished(1000)) {
|
||||||
if (silverSearcherProcess.readAll().contains("ag version"))
|
if (silverSearcherProcess.stdOut().contains("ag version"))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,7 +100,7 @@ bool isSilverSearcherAvailable()
|
|||||||
void runSilverSeacher(FutureInterfaceType &fi, FileFindParameters parameters)
|
void runSilverSeacher(FutureInterfaceType &fi, FileFindParameters parameters)
|
||||||
{
|
{
|
||||||
ProgressTimer progress(fi, 5);
|
ProgressTimer progress(fi, 5);
|
||||||
const QString directory = parameters.additionalParameters.toString();
|
const FilePath directory = FilePath::fromUserInput(parameters.additionalParameters.toString());
|
||||||
QStringList arguments = {"--parallel", "--ackmate"};
|
QStringList arguments = {"--parallel", "--ackmate"};
|
||||||
|
|
||||||
if (parameters.flags & FindCaseSensitively)
|
if (parameters.flags & FindCaseSensitively)
|
||||||
@@ -138,12 +129,11 @@ void runSilverSeacher(FutureInterfaceType &fi, FileFindParameters parameters)
|
|||||||
if (!params.searchOptions.isEmpty())
|
if (!params.searchOptions.isEmpty())
|
||||||
arguments << params.searchOptions.split(' ');
|
arguments << params.searchOptions.split(' ');
|
||||||
|
|
||||||
const FilePath path = FilePath::fromUserInput(FileUtils::normalizedPathName(directory));
|
arguments << "--" << parameters.text << directory.normalizePathName().toString();
|
||||||
arguments << "--" << parameters.text << path.toString();
|
|
||||||
|
|
||||||
QProcess process;
|
QtcProcess process;
|
||||||
process.setProcessEnvironment(Utils::Environment::systemEnvironment().toProcessEnvironment());
|
process.setCommand({"ag", arguments});
|
||||||
process.start(silverSearcherExecutable(), arguments);
|
process.start();
|
||||||
if (process.waitForFinished()) {
|
if (process.waitForFinished()) {
|
||||||
typedef QList<FileSearchResult> FileSearchResultList;
|
typedef QList<FileSearchResult> FileSearchResultList;
|
||||||
QRegularExpression regexp;
|
QRegularExpression regexp;
|
||||||
@@ -155,7 +145,7 @@ void runSilverSeacher(FutureInterfaceType &fi, FileFindParameters parameters)
|
|||||||
regexp.setPattern(parameters.text);
|
regexp.setPattern(parameters.text);
|
||||||
regexp.setPatternOptions(patternOptions);
|
regexp.setPatternOptions(patternOptions);
|
||||||
}
|
}
|
||||||
SilverSearcher::SilverSearcherOutputParser parser(process.readAll(), regexp);
|
SilverSearcher::SilverSearcherOutputParser parser(process.stdOut(), regexp);
|
||||||
FileSearchResultList items = parser.parse();
|
FileSearchResultList items = parser.parse();
|
||||||
if (!items.isEmpty())
|
if (!items.isEmpty())
|
||||||
fi.reportResult(items);
|
fi.reportResult(items);
|
||||||
|
@@ -35,16 +35,16 @@ namespace Internal {
|
|||||||
|
|
||||||
void OutputParserTest::test_data()
|
void OutputParserTest::test_data()
|
||||||
{
|
{
|
||||||
QTest::addColumn<QByteArray>("parserOutput");
|
QTest::addColumn<QString>("parserOutput");
|
||||||
QTest::addColumn<FileSearchResultList>("results");
|
QTest::addColumn<FileSearchResultList>("results");
|
||||||
|
|
||||||
QTest::addRow("nothing") << QByteArray("\n") << FileSearchResultList();
|
QTest::addRow("nothing") << QString("\n") << FileSearchResultList();
|
||||||
QTest::addRow("oneFileOneMatch")
|
QTest::addRow("oneFileOneMatch")
|
||||||
<< QByteArray(":/file/path/to/filename.h\n"
|
<< QString(":/file/path/to/filename.h\n"
|
||||||
"1;1 5:match\n")
|
"1;1 5:match\n")
|
||||||
<< FileSearchResultList({{"/file/path/to/filename.h", 1, "match", 1, 5, {}}});
|
<< FileSearchResultList({{"/file/path/to/filename.h", 1, "match", 1, 5, {}}});
|
||||||
QTest::addRow("multipleFilesWithOneMatch")
|
QTest::addRow("multipleFilesWithOneMatch")
|
||||||
<< QByteArray(":/file/path/to/filename1.h\n"
|
<< QString(":/file/path/to/filename1.h\n"
|
||||||
"1;1 5:match\n"
|
"1;1 5:match\n"
|
||||||
"\n"
|
"\n"
|
||||||
":/file/path/to/filename2.h\n"
|
":/file/path/to/filename2.h\n"
|
||||||
@@ -52,12 +52,12 @@ void OutputParserTest::test_data()
|
|||||||
<< FileSearchResultList({{"/file/path/to/filename1.h", 1, "match", 1, 5, {}},
|
<< FileSearchResultList({{"/file/path/to/filename1.h", 1, "match", 1, 5, {}},
|
||||||
{"/file/path/to/filename2.h", 2, " match", 2, 5, {}}});
|
{"/file/path/to/filename2.h", 2, " match", 2, 5, {}}});
|
||||||
QTest::addRow("oneFileMultipleMatches")
|
QTest::addRow("oneFileMultipleMatches")
|
||||||
<< QByteArray(":/file/path/to/filename.h\n"
|
<< QString(":/file/path/to/filename.h\n"
|
||||||
"1;1 5,7 5:match match\n")
|
"1;1 5,7 5:match match\n")
|
||||||
<< FileSearchResultList({{"/file/path/to/filename.h", 1, "match match", 1, 5, {}},
|
<< FileSearchResultList({{"/file/path/to/filename.h", 1, "match match", 1, 5, {}},
|
||||||
{"/file/path/to/filename.h", 1, "match match", 7, 5, {}}});
|
{"/file/path/to/filename.h", 1, "match match", 7, 5, {}}});
|
||||||
QTest::addRow("multipleFilesWithMultipleMatches")
|
QTest::addRow("multipleFilesWithMultipleMatches")
|
||||||
<< QByteArray(":/file/path/to/filename1.h\n"
|
<< QString(":/file/path/to/filename1.h\n"
|
||||||
"1;1 5,7 5:match match\n"
|
"1;1 5,7 5:match match\n"
|
||||||
"\n"
|
"\n"
|
||||||
":/file/path/to/filename2.h\n"
|
":/file/path/to/filename2.h\n"
|
||||||
@@ -70,7 +70,7 @@ void OutputParserTest::test_data()
|
|||||||
|
|
||||||
void OutputParserTest::test()
|
void OutputParserTest::test()
|
||||||
{
|
{
|
||||||
QFETCH(QByteArray, parserOutput);
|
QFETCH(QString, parserOutput);
|
||||||
QFETCH(FileSearchResultList, results);
|
QFETCH(FileSearchResultList, results);
|
||||||
SilverSearcher::SilverSearcherOutputParser ssop(parserOutput);
|
SilverSearcher::SilverSearcherOutputParser ssop(parserOutput);
|
||||||
const QList<Utils::FileSearchResult> items = ssop.parse();
|
const QList<Utils::FileSearchResult> items = ssop.parse();
|
||||||
|
@@ -30,7 +30,7 @@
|
|||||||
namespace SilverSearcher {
|
namespace SilverSearcher {
|
||||||
|
|
||||||
SilverSearcherOutputParser::SilverSearcherOutputParser(
|
SilverSearcherOutputParser::SilverSearcherOutputParser(
|
||||||
const QByteArray &output, const QRegularExpression ®exp)
|
const QString &output, const QRegularExpression ®exp)
|
||||||
: output(output)
|
: output(output)
|
||||||
, regexp(regexp)
|
, regexp(regexp)
|
||||||
, outputSize(output.size())
|
, outputSize(output.size())
|
||||||
@@ -67,7 +67,7 @@ bool SilverSearcherOutputParser::parseFilePath()
|
|||||||
int startIndex = ++index;
|
int startIndex = ++index;
|
||||||
while (index < outputSize && output[index] != '\n')
|
while (index < outputSize && output[index] != '\n')
|
||||||
++index;
|
++index;
|
||||||
item.fileName = QString::fromUtf8(output.data() + startIndex, index - startIndex);
|
item.fileName = QString(output.data() + startIndex, index - startIndex);
|
||||||
++index;
|
++index;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -77,7 +77,7 @@ bool SilverSearcherOutputParser::parseLineNumber()
|
|||||||
int startIndex = index;
|
int startIndex = index;
|
||||||
while (index < outputSize && output[++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;
|
++index;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -87,7 +87,7 @@ bool SilverSearcherOutputParser::parseMatchIndex()
|
|||||||
int startIndex = index;
|
int startIndex = index;
|
||||||
while (index < outputSize && output[++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;
|
++index;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -97,7 +97,7 @@ bool SilverSearcherOutputParser::parseMatchLength()
|
|||||||
int startIndex = index;
|
int startIndex = index;
|
||||||
while (index < outputSize && output[++index] != ':' && output[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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,7 +105,7 @@ int SilverSearcherOutputParser::parseMatches()
|
|||||||
{
|
{
|
||||||
int matches = 1;
|
int matches = 1;
|
||||||
const int colon = output.indexOf(':', index);
|
const int colon = output.indexOf(':', index);
|
||||||
QByteArray text;
|
QString text;
|
||||||
if (colon != -1) {
|
if (colon != -1) {
|
||||||
const int textStart = colon + 1;
|
const int textStart = colon + 1;
|
||||||
const int newline = output.indexOf('\n', textStart);
|
const int newline = output.indexOf('\n', textStart);
|
||||||
@@ -119,7 +119,7 @@ int SilverSearcherOutputParser::parseMatches()
|
|||||||
parseMatchIndex();
|
parseMatchIndex();
|
||||||
parseMatchLength();
|
parseMatchLength();
|
||||||
if (hasRegexp) {
|
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();
|
item.regexpCapturedTexts = regexp.match(part).capturedTexts();
|
||||||
}
|
}
|
||||||
items << item;
|
items << item;
|
||||||
@@ -133,7 +133,7 @@ bool SilverSearcherOutputParser::parseText()
|
|||||||
{
|
{
|
||||||
int startIndex = index;
|
int startIndex = index;
|
||||||
while (index < outputSize && output[++index] != '\n') { }
|
while (index < outputSize && output[++index] != '\n') { }
|
||||||
item.matchingLine = QString::fromUtf8(output.data() + startIndex, index - startIndex);
|
item.matchingLine = QString(output.data() + startIndex, index - startIndex);
|
||||||
++index;
|
++index;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@@ -28,7 +28,6 @@
|
|||||||
#include <coreplugin/find/searchresultwindow.h>
|
#include <coreplugin/find/searchresultwindow.h>
|
||||||
#include <utils/filesearch.h>
|
#include <utils/filesearch.h>
|
||||||
|
|
||||||
#include <QByteArray>
|
|
||||||
#include <QList>
|
#include <QList>
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
|
|
||||||
@@ -37,11 +36,10 @@ namespace SilverSearcher {
|
|||||||
class SilverSearcherOutputParser
|
class SilverSearcherOutputParser
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
SilverSearcherOutputParser(
|
SilverSearcherOutputParser(const QString &output, const QRegularExpression ®exp = {});
|
||||||
const QByteArray &output,
|
|
||||||
const QRegularExpression ®exp = QRegularExpression());
|
|
||||||
|
|
||||||
QList<Utils::FileSearchResult> parse();
|
QList<Utils::FileSearchResult> parse();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int parseMatches();
|
int parseMatches();
|
||||||
bool parseMatchLength();
|
bool parseMatchLength();
|
||||||
@@ -50,7 +48,7 @@ private:
|
|||||||
bool parseFilePath();
|
bool parseFilePath();
|
||||||
bool parseText();
|
bool parseText();
|
||||||
|
|
||||||
QByteArray output;
|
QString output;
|
||||||
QRegularExpression regexp;
|
QRegularExpression regexp;
|
||||||
bool hasRegexp = false;
|
bool hasRegexp = false;
|
||||||
int outputSize = 0;
|
int outputSize = 0;
|
||||||
|
Reference in New Issue
Block a user