forked from qt-creator/qt-creator
CppEditor: Register highlighter
Change-Id: Ia6516d644a855ae44943e831f356763cca46c62f Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -294,7 +294,7 @@ QDebug &operator<<(QDebug &stream, const Position &pos)
|
|||||||
return stream;
|
return stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
static HighlightCallback &codeHighlighter()
|
HighlightCallback &codeHighlighter()
|
||||||
{
|
{
|
||||||
static HighlightCallback s_highlighter;
|
static HighlightCallback s_highlighter;
|
||||||
return s_highlighter;
|
return s_highlighter;
|
||||||
|
@@ -89,6 +89,7 @@ using HighlightCallback = std::function<QFuture<QTextDocument *>(const QString &
|
|||||||
QTCREATOR_UTILS_EXPORT QFuture<QTextDocument *> highlightCode(
|
QTCREATOR_UTILS_EXPORT QFuture<QTextDocument *> highlightCode(
|
||||||
const QString &code, const QString &mimeType);
|
const QString &code, const QString &mimeType);
|
||||||
QTCREATOR_UTILS_EXPORT void setCodeHighlighter(const HighlightCallback &highlighter);
|
QTCREATOR_UTILS_EXPORT void setCodeHighlighter(const HighlightCallback &highlighter);
|
||||||
|
QTCREATOR_UTILS_EXPORT HighlightCallback &codeHighlighter();
|
||||||
|
|
||||||
} // Text
|
} // Text
|
||||||
} // Utils
|
} // Utils
|
||||||
|
@@ -74,6 +74,7 @@
|
|||||||
#include <texteditor/snippets/snippetprovider.h>
|
#include <texteditor/snippets/snippetprovider.h>
|
||||||
#include <texteditor/texteditor.h>
|
#include <texteditor/texteditor.h>
|
||||||
#include <texteditor/texteditorconstants.h>
|
#include <texteditor/texteditorconstants.h>
|
||||||
|
#include <texteditor/texteditorsettings.h>
|
||||||
|
|
||||||
#include <utils/algorithm.h>
|
#include <utils/algorithm.h>
|
||||||
#include <utils/fileutils.h>
|
#include <utils/fileutils.h>
|
||||||
@@ -192,6 +193,37 @@ private:
|
|||||||
CppEditorPluginPrivate *d = nullptr;
|
CppEditorPluginPrivate *d = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
QFuture<QTextDocument *> highlightCode(const QString &code, const QString &mimeType)
|
||||||
|
{
|
||||||
|
QTextDocument *document = new QTextDocument;
|
||||||
|
document->setPlainText(code);
|
||||||
|
|
||||||
|
std::shared_ptr<QPromise<QTextDocument *>> promise
|
||||||
|
= std::make_shared<QPromise<QTextDocument *>>();
|
||||||
|
|
||||||
|
promise->start();
|
||||||
|
|
||||||
|
CppHighlighter *highlighter = new CppHighlighter(document);
|
||||||
|
|
||||||
|
QObject::connect(highlighter, &CppHighlighter::finished, document, [document, promise]() {
|
||||||
|
promise->addResult(document);
|
||||||
|
promise->finish();
|
||||||
|
});
|
||||||
|
|
||||||
|
QFutureWatcher<QTextDocument *> *watcher = new QFutureWatcher<QTextDocument *>(document);
|
||||||
|
QObject::connect(watcher, &QFutureWatcher<QTextDocument *>::canceled, document, [document]() {
|
||||||
|
document->deleteLater();
|
||||||
|
});
|
||||||
|
watcher->setFuture(promise->future());
|
||||||
|
|
||||||
|
highlighter->setParent(document);
|
||||||
|
highlighter->setFontSettings(TextEditorSettings::fontSettings());
|
||||||
|
highlighter->setMimeType(mimeType);
|
||||||
|
highlighter->rehighlight();
|
||||||
|
|
||||||
|
return promise->future();
|
||||||
|
}
|
||||||
|
|
||||||
void CppEditorPlugin::initialize()
|
void CppEditorPlugin::initialize()
|
||||||
{
|
{
|
||||||
d = new CppEditorPluginPrivate;
|
d = new CppEditorPluginPrivate;
|
||||||
@@ -219,6 +251,17 @@ void CppEditorPlugin::initialize()
|
|||||||
d, &CppEditorPluginPrivate::onTaskStarted);
|
d, &CppEditorPluginPrivate::onTaskStarted);
|
||||||
connect(ProgressManager::instance(), &ProgressManager::allTasksFinished,
|
connect(ProgressManager::instance(), &ProgressManager::allTasksFinished,
|
||||||
d, &CppEditorPluginPrivate::onAllTasksFinished);
|
d, &CppEditorPluginPrivate::onAllTasksFinished);
|
||||||
|
|
||||||
|
auto oldHighlighter = Utils::Text::codeHighlighter();
|
||||||
|
Utils::Text::setCodeHighlighter(
|
||||||
|
[oldHighlighter](const QString &code, const QString &mimeType) -> QFuture<QTextDocument *> {
|
||||||
|
if (mimeType == "text/x-c++src" || mimeType == "text/x-c++hdr"
|
||||||
|
|| mimeType == "text/x-csrc" || mimeType == "text/x-chdr") {
|
||||||
|
return highlightCode(code, mimeType);
|
||||||
|
}
|
||||||
|
|
||||||
|
return oldHighlighter(code, mimeType);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void CppEditorPlugin::extensionsInitialized()
|
void CppEditorPlugin::extensionsInitialized()
|
||||||
|
Reference in New Issue
Block a user