forked from qt-creator/qt-creator
Utils: Allow highlighting to be done asynchronously
Change-Id: Ief0e1ba2e2c32b6ad112f4adc9b582b03a521fad Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include "textutils.h"
|
||||
#include "qtcassert.h"
|
||||
|
||||
#include <QPromise>
|
||||
#include <QRegularExpression>
|
||||
#include <QTextBlock>
|
||||
#include <QTextDocument>
|
||||
@@ -299,14 +300,20 @@ static HighlightCallback &codeHighlighter()
|
||||
return s_highlighter;
|
||||
}
|
||||
|
||||
QTextDocument *highlightCode(const QString &code, const QString &mimeType)
|
||||
QFuture<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;
|
||||
|
||||
QPromise<QTextDocument *> promise;
|
||||
promise.start();
|
||||
promise.addResult(doc);
|
||||
promise.finish();
|
||||
|
||||
return promise.future();
|
||||
}
|
||||
|
||||
void setCodeHighlighter(const HighlightCallback &highlighter)
|
||||
|
@@ -5,6 +5,7 @@
|
||||
|
||||
#include "utils_global.h"
|
||||
|
||||
#include <QFuture>
|
||||
#include <QMetaType>
|
||||
#include <QString>
|
||||
|
||||
@@ -84,8 +85,9 @@ QTCREATOR_UTILS_EXPORT QString utf16LineTextInUtf8Buffer(const QByteArray &utf8B
|
||||
|
||||
QTCREATOR_UTILS_EXPORT QDebug &operator<<(QDebug &stream, const Position &pos);
|
||||
|
||||
using HighlightCallback = std::function<QTextDocument *(const QString &, const QString &)>;
|
||||
QTCREATOR_UTILS_EXPORT QTextDocument *highlightCode(const QString &code, const QString &mimeType);
|
||||
using HighlightCallback = std::function<QFuture<QTextDocument *>(const QString &, const QString &)>;
|
||||
QTCREATOR_UTILS_EXPORT QFuture<QTextDocument *> highlightCode(
|
||||
const QString &code, const QString &mimeType);
|
||||
QTCREATOR_UTILS_EXPORT void setCodeHighlighter(const HighlightCallback &highlighter);
|
||||
|
||||
} // Text
|
||||
|
Reference in New Issue
Block a user