aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/outputformatter.cpp
diff options
context:
space:
mode:
authorChristian Kandeler <christian.kandeler@qt.io>2020-04-14 15:28:44 +0200
committerChristian Kandeler <christian.kandeler@qt.io>2020-04-14 14:15:26 +0000
commitc0c2df203dd26b6af2be5757501bb3cacd692ef6 (patch)
tree961d12dc7ba7db4fb0c9cb3a338250d65efd64b0 /src/libs/utils/outputformatter.cpp
parent70bddbcab42759c3105db6801a918375449b848c (diff)
Utils: Split up OutputFormatter class
An OutputFormatter takes some string and prints it into a text edit. In addition, it can ask any number of registered OutputLineParsers whether they think any special formatting should be applied to the current line. This mechanism is now properly modeled by our class design, rather than being hidden in a monolithic class where everything had the same type, no matter what its purpose was. Prospective contributors can now simply be pointed to the OutputLineParser class and will see at one glance what they have to do. Change-Id: I9844499f062c94fb038ce73fd6f26576910148c2 Reviewed-by: hjk <hjk@qt.io>
Diffstat (limited to 'src/libs/utils/outputformatter.cpp')
-rw-r--r--src/libs/utils/outputformatter.cpp69
1 files changed, 39 insertions, 30 deletions
diff --git a/src/libs/utils/outputformatter.cpp b/src/libs/utils/outputformatter.cpp
index 452ec5ae06..dbc7e0a677 100644
--- a/src/libs/utils/outputformatter.cpp
+++ b/src/libs/utils/outputformatter.cpp
@@ -48,14 +48,18 @@ public:
AnsiEscapeCodeHandler escapeCodeHandler;
QPair<QString, OutputFormat> incompleteLine;
optional<QTextCharFormat> formatOverride;
- QList<OutputFormatter *> formatters;
- OutputFormatter *nextFormatter = nullptr;
+ QList<OutputLineParser *> lineParsers;
+ OutputLineParser *nextParser = nullptr;
bool boldFontEnabled = true;
bool prependCarriageReturn = false;
};
} // namespace Internal
+OutputLineParser::~OutputLineParser()
+{
+}
+
OutputFormatter::OutputFormatter()
: d(new Internal::OutputFormatterPrivate)
{
@@ -79,12 +83,10 @@ void OutputFormatter::setPlainTextEdit(QPlainTextEdit *plainText)
initFormats();
}
-void OutputFormatter::setFormatters(const QList<OutputFormatter *> &formatters)
+void OutputFormatter::setLineParsers(const QList<OutputLineParser *> &parsers)
{
- for (OutputFormatter * const f : formatters)
- f->setPlainTextEdit(plainTextEdit());
- d->formatters = formatters;
- d->nextFormatter = nullptr;
+ d->lineParsers = parsers;
+ d->nextParser = nullptr;
}
void OutputFormatter::doAppendMessage(const QString &text, OutputFormat format)
@@ -93,7 +95,7 @@ void OutputFormatter::doAppendMessage(const QString &text, OutputFormat format)
const QList<FormattedText> formattedText = parseAnsi(text, charFmt);
const QString cleanLine = std::accumulate(formattedText.begin(), formattedText.end(), QString(),
[](const FormattedText &t1, const FormattedText &t2) { return t1.text + t2.text; });
- const Result res = handleMessage(cleanLine, format);
+ const OutputLineParser::Result res = handleMessage(cleanLine, format);
if (res.newContent) {
append(res.newContent.value(), charFmt);
return;
@@ -102,36 +104,42 @@ void OutputFormatter::doAppendMessage(const QString &text, OutputFormat format)
append(output.text, output.format);
}
-OutputFormatter::Result OutputFormatter::handleMessage(const QString &text, OutputFormat format)
+OutputLineParser::Result OutputFormatter::handleMessage(const QString &text, OutputFormat format)
{
- if (d->nextFormatter) {
- const Result res = d->nextFormatter->handleMessage(text, format);
+ if (d->nextParser) {
+ const OutputLineParser::Result res = d->nextParser->handleLine(text, format);
switch (res.status) {
- case Status::Done:
- d->nextFormatter = nullptr;
+ case OutputLineParser::Status::Done:
+ d->nextParser = nullptr;
return res;
- case Status::InProgress:
+ case OutputLineParser::Status::InProgress:
return res;
- case Status::NotHandled:
+ case OutputLineParser::Status::NotHandled:
QTC_CHECK(false); // TODO: This case will be legal after the merge
- d->nextFormatter = nullptr;
+ d->nextParser = nullptr;
return res;
}
}
- QTC_CHECK(!d->nextFormatter);
- for (OutputFormatter * const formatter : qAsConst(d->formatters)) {
- const Result res = formatter->handleMessage(text, format);
+ QTC_CHECK(!d->nextParser);
+ for (OutputLineParser * const parser : qAsConst(d->lineParsers)) {
+ const OutputLineParser::Result res = parser->handleLine(text, format);
switch (res.status) {
- case Status::Done:
+ case OutputLineParser::Status::Done:
return res;
- case Status::InProgress:
- d->nextFormatter = formatter;
+ case OutputLineParser::Status::InProgress:
+ d->nextParser = parser;
return res;
- case Status::NotHandled:
+ case OutputLineParser::Status::NotHandled:
break;
}
}
- return Status::NotHandled;
+ return OutputLineParser::Status::NotHandled;
+}
+
+void OutputFormatter::reset()
+{
+ for (OutputLineParser * const p : d->lineParsers)
+ p->reset();
}
QTextCharFormat OutputFormatter::charFormat(OutputFormat format) const
@@ -145,7 +153,7 @@ QList<FormattedText> OutputFormatter::parseAnsi(const QString &text, const QText
}
const QList<FormattedText> OutputFormatter::linkifiedText(
- const QList<FormattedText> &text, const OutputFormatter::LinkSpecs &linkSpecs)
+ const QList<FormattedText> &text, const OutputLineParser::LinkSpecs &linkSpecs)
{
if (linkSpecs.isEmpty())
return text;
@@ -171,7 +179,7 @@ const QList<FormattedText> OutputFormatter::linkifiedText(
break;
}
- const LinkSpec &linkSpec = linkSpecs.at(nextLinkSpecIndex);
+ const OutputLineParser::LinkSpec &linkSpec = linkSpecs.at(nextLinkSpecIndex);
const int localLinkStartPos = linkSpec.startPos - totalTextLengthSoFar;
++nextLinkSpecIndex;
@@ -221,10 +229,12 @@ QTextCharFormat OutputFormatter::linkFormat(const QTextCharFormat &inputFormat,
return result;
}
+#ifdef WITH_TESTS
void OutputFormatter::overrideTextCharFormat(const QTextCharFormat &fmt)
{
d->formatOverride = fmt;
}
+#endif // WITH_TESTS
void OutputFormatter::clearLastLine()
{
@@ -266,13 +276,12 @@ void OutputFormatter::dumpIncompleteLine(const QString &line, OutputFormat forma
d->incompleteLine.second = format;
}
-bool OutputFormatter::handleLink(const QString &href)
+void OutputFormatter::handleLink(const QString &href)
{
- for (OutputFormatter * const f : qAsConst(d->formatters)) {
+ for (OutputLineParser * const f : qAsConst(d->lineParsers)) {
if (f->handleLink(href))
- return true;
+ return;
}
- return false;
}
void OutputFormatter::clear()