diff options
author | Fabian Kosmale <fabian.kosmale@qt.io> | 2021-03-25 17:24:24 +0100 |
---|---|---|
committer | Fabian Kosmale <fabian.kosmale@qt.io> | 2021-03-25 21:25:23 +0100 |
commit | 603b8e09ac02ac366b97e75a45969ec7139db39e (patch) | |
tree | 9c451b0fe9f658df4b5ad14af1c0c10289669ec1 /tools/qmllint | |
parent | afe3fcb80af2535534dc4c92c683b593dfe1f3c3 (diff) |
qmllint: Document and cleanup IssueLocationWithContext
Add a few comments to describe IssueLocationWithContext. In addition,
remove useles casts. Also remove uselessly constructing QStrings by
adding a few QStringView overloads.
Change-Id: I3c6e03f38cbe251deda66bd9257462d9ee9cd065
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'tools/qmllint')
-rw-r--r-- | tools/qmllint/checkidentifiers.cpp | 34 | ||||
-rw-r--r-- | tools/qmllint/qcoloroutput.cpp | 8 | ||||
-rw-r--r-- | tools/qmllint/qcoloroutput.h | 6 |
3 files changed, 31 insertions, 17 deletions
diff --git a/tools/qmllint/checkidentifiers.cpp b/tools/qmllint/checkidentifiers.cpp index 7350a1f76e..2c64fd9198 100644 --- a/tools/qmllint/checkidentifiers.cpp +++ b/tools/qmllint/checkidentifiers.cpp @@ -33,23 +33,35 @@ #include <QtCore/qsharedpointer.h> #include <stack> +/*! + \internal + Used to print the the line containing the location of a certain error + */ class IssueLocationWithContext { public: - IssueLocationWithContext(const QString &code, const QQmlJS::SourceLocation &location) { + /*! + \internal + \param code: The whole text of a translation unit + \param location: The location where an error occurred. + */ + IssueLocationWithContext(QStringView code, const QQmlJS::SourceLocation &location) { int before = qMax(0,code.lastIndexOf(QLatin1Char('\n'), location.offset)); if (before != 0) before++; - m_beforeText = QStringView{code}.mid(before, int(location.offset - before)); - m_issueText = QStringView{code}.mid(location.offset, location.length); - int after = code.indexOf(QLatin1Char('\n'), int(location.offset + location.length)); - m_afterText = QStringView{code}.mid(int(location.offset + location.length), - int(after - (location.offset+location.length))); + m_beforeText = code.mid(before, location.offset - before); + m_issueText = code.mid(location.offset, location.length); + int after = code.indexOf(QLatin1Char('\n'), location.offset + location.length); + m_afterText = code.mid(location.offset + location.length, + after - (location.offset+location.length)); } + // returns start of the line till first character of location QStringView beforeText() const { return m_beforeText; } + // returns the text at location QStringView issueText() const { return m_issueText; } + // returns any text after location until the end of the line is reached QStringView afterText() const { return m_afterText; } private: @@ -71,10 +83,10 @@ void CheckIdentifiers::printContext( const QString &code, ColorOutput *output, const QQmlJS::SourceLocation &location) { IssueLocationWithContext issueLocationWithContext { code, location }; - if (const QString beforeText = issueLocationWithContext.beforeText().toString(); !beforeText.isEmpty()) + if (const QStringView beforeText = issueLocationWithContext.beforeText(); !beforeText.isEmpty()) output->write(beforeText, Normal); - output->write(issueLocationWithContext.issueText().toString(), Error); - if (const QString afterText = issueLocationWithContext.afterText().toString(); !afterText.isEmpty()) + output->write(issueLocationWithContext.issueText(), Error); + if (const QStringView afterText = issueLocationWithContext.afterText(); !afterText.isEmpty()) output->write(afterText + QLatin1Char('\n'), Normal); int tabCount = issueLocationWithContext.beforeText().count(QLatin1Char('\t')); output->write(QString::fromLatin1(" ").repeated( @@ -465,9 +477,9 @@ bool CheckIdentifiers::operator()( Warning, QStringLiteral("Note")); } IssueLocationWithContext issueLocationWithContext {m_code, location}; - m_colorOut->write(issueLocationWithContext.beforeText().toString(), Normal); + m_colorOut->write(issueLocationWithContext.beforeText(), Normal); m_colorOut->write(rootId + QLatin1Char('.'), Hint); - m_colorOut->write(issueLocationWithContext.issueText().toString(), Normal); + m_colorOut->write(issueLocationWithContext.issueText(), Normal); m_colorOut->write(issueLocationWithContext.afterText() + QLatin1Char('\n'), Normal); } else if (jsId.has_value() && jsId->kind == QQmlJSScope::JavaScriptIdentifier::Injected) { diff --git a/tools/qmllint/qcoloroutput.cpp b/tools/qmllint/qcoloroutput.cpp index 22783ef1b9..6e0a483050 100644 --- a/tools/qmllint/qcoloroutput.cpp +++ b/tools/qmllint/qcoloroutput.cpp @@ -236,7 +236,7 @@ ColorOutput::~ColorOutput() = default; \a message will be printed as is. For instance, no line endings will be inserted. */ -void ColorOutput::write(const QString &message, int colorID) +void ColorOutput::write(QStringView message, int colorID) { if (!d->isSilent()) d->write(colorify(message, colorID)); @@ -279,7 +279,7 @@ void ColorOutput::writeUncolored(const QString &message) This is useful when the colored string is inserted into a translated string(dividing the string into several small strings prevents proper translation). */ -QString ColorOutput::colorify(const QString &message, int colorID) const +QString ColorOutput::colorify(QStringView message, int colorID) const { Q_ASSERT_X(colorID == -1 || d->containsColor(colorID), Q_FUNC_INFO, qPrintable(QString::fromLatin1("There is no color registered by id %1") @@ -295,7 +295,7 @@ QString ColorOutput::colorify(const QString &message, int colorID) const /* If DefaultColor is set, we don't want to color it. */ if (color & DefaultColor) - return message; + return message.toString(); const int foregroundCode = (color & ForegroundMask) >> ForegroundShift; const int backgroundCode = (color & BackgroundMask) >> BackgroundShift; @@ -324,7 +324,7 @@ QString ColorOutput::colorify(const QString &message, int colorID) const return finalMessage; } - return message; + return message.toString(); } /*! diff --git a/tools/qmllint/qcoloroutput.h b/tools/qmllint/qcoloroutput.h index 41b8751432..3b171da631 100644 --- a/tools/qmllint/qcoloroutput.h +++ b/tools/qmllint/qcoloroutput.h @@ -104,10 +104,12 @@ public: void insertMapping(int colorID, ColorCode colorCode); void writeUncolored(const QString &message); - void write(const QString &message, int color = -1); + void write(QStringView message, int color = -1); + // handle QStringBuilder case + Q_WEAK_OVERLOAD void write(const QString &message, int color = -1) { write(QStringView(message), color); } void writePrefixedMessage(const QString &message, MessageColors type, const QString &prefix = QString()); - QString colorify(const QString &message, int color = -1) const; + QString colorify(QStringView message, int color = -1) const; private: QScopedPointer<ColorOutputPrivate> d; |