forked from qt-creator/qt-creator
Utils: Add simple text highlight function
Change-Id: I0eb740214c975da9f6bea87cef2aee23a63d6125 Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
@@ -678,4 +678,25 @@ QString ansiColoredText(const QString &text, const QColor &color)
|
|||||||
return formatString.arg(color.red()).arg(color.green()).arg(color.blue()).arg(text);
|
return formatString.arg(color.red()).arg(color.green()).arg(color.blue()).arg(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static HighlightCallback &codeHighlighter()
|
||||||
|
{
|
||||||
|
static HighlightCallback s_highlighter;
|
||||||
|
return s_highlighter;
|
||||||
|
}
|
||||||
|
|
||||||
|
QTextDocument *highlightCode(const QString &code, const QString &mimeType)
|
||||||
|
{
|
||||||
|
if (const auto highlighter = codeHighlighter())
|
||||||
|
return highlighter(code, mimeType);
|
||||||
|
|
||||||
|
QTextDocument *doc = new QTextDocument;
|
||||||
|
doc->setPlainText(code);
|
||||||
|
return doc;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setCodeHighlighter(const HighlightCallback &highlighter)
|
||||||
|
{
|
||||||
|
codeHighlighter() = highlighter;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace Utils
|
} // namespace Utils
|
||||||
|
@@ -9,6 +9,7 @@
|
|||||||
#include <QList>
|
#include <QList>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QSyntaxHighlighter>
|
#include <QSyntaxHighlighter>
|
||||||
|
#include <QTextDocument>
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
@@ -140,4 +141,8 @@ private:
|
|||||||
|
|
||||||
QTCREATOR_UTILS_EXPORT QString ansiColoredText(const QString &text, const QColor &color);
|
QTCREATOR_UTILS_EXPORT QString ansiColoredText(const QString &text, const QColor &color);
|
||||||
|
|
||||||
|
using HighlightCallback = std::function<QTextDocument *(const QString &, const QString &)>;
|
||||||
|
QTCREATOR_UTILS_EXPORT QTextDocument *highlightCode(const QString &code, const QString &mimeType);
|
||||||
|
QTCREATOR_UTILS_EXPORT void setCodeHighlighter(const HighlightCallback &highlighter);
|
||||||
|
|
||||||
} // namespace Utils
|
} // namespace Utils
|
||||||
|
Reference in New Issue
Block a user