forked from qt-creator/qt-creator
TextEditor: Register as code highlighter
Change-Id: Ie79cf3805d81f6fef240e88b51569ebc12e1c012 Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
@@ -222,4 +222,49 @@ void handleShutdown()
|
|||||||
delete highlightRepository();
|
delete highlightRepository();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QFuture<QTextDocument *> highlightCode(const QString &code, const QString &mimeType)
|
||||||
|
{
|
||||||
|
QTextDocument *document = new QTextDocument;
|
||||||
|
document->setPlainText(code);
|
||||||
|
|
||||||
|
const HighlighterHelper::Definitions definitions = HighlighterHelper::definitionsForMimeType(
|
||||||
|
mimeType);
|
||||||
|
|
||||||
|
std::shared_ptr<QPromise<QTextDocument *>> promise
|
||||||
|
= std::make_shared<QPromise<QTextDocument *>>();
|
||||||
|
|
||||||
|
promise->start();
|
||||||
|
|
||||||
|
if (definitions.isEmpty()) {
|
||||||
|
promise->addResult(document);
|
||||||
|
promise->finish();
|
||||||
|
return promise->future();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto definition = definitions.first();
|
||||||
|
|
||||||
|
const QString definitionFilesPath
|
||||||
|
= TextEditorSettings::highlighterSettings().definitionFilesPath().toString();
|
||||||
|
|
||||||
|
Highlighter *highlighter = new Highlighter(definitionFilesPath);
|
||||||
|
QObject::connect(highlighter, &Highlighter::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->setDefinition(definition);
|
||||||
|
highlighter->setParent(document);
|
||||||
|
highlighter->setFontSettings(TextEditorSettings::fontSettings());
|
||||||
|
highlighter->setMimeType(mimeType);
|
||||||
|
highlighter->setDocument(document);
|
||||||
|
|
||||||
|
return promise->future();
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace TextEditor::HighlighterHelper
|
} // namespace TextEditor::HighlighterHelper
|
||||||
|
@@ -3,8 +3,13 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "texteditor_global.h"
|
||||||
|
|
||||||
#include <KSyntaxHighlighting/Definition>
|
#include <KSyntaxHighlighting/Definition>
|
||||||
|
|
||||||
|
#include <QFuture>
|
||||||
|
#include <QTextDocument>
|
||||||
|
|
||||||
namespace Utils { class FilePath; }
|
namespace Utils { class FilePath; }
|
||||||
|
|
||||||
namespace TextEditor {
|
namespace TextEditor {
|
||||||
@@ -30,6 +35,8 @@ void reload();
|
|||||||
|
|
||||||
void handleShutdown();
|
void handleShutdown();
|
||||||
|
|
||||||
|
QFuture<QTextDocument *> highlightCode(const QString &code, const QString &mimeType);
|
||||||
|
|
||||||
} // namespace HighlighterHelper
|
} // namespace HighlighterHelper
|
||||||
|
|
||||||
} // namespace TextEditor
|
} // namespace TextEditor
|
||||||
|
@@ -49,8 +49,9 @@
|
|||||||
#include <extensionsystem/iplugin.h>
|
#include <extensionsystem/iplugin.h>
|
||||||
|
|
||||||
#include <utils/fancylineedit.h>
|
#include <utils/fancylineedit.h>
|
||||||
#include <utils/qtcassert.h>
|
|
||||||
#include <utils/macroexpander.h>
|
#include <utils/macroexpander.h>
|
||||||
|
#include <utils/qtcassert.h>
|
||||||
|
#include <utils/textutils.h>
|
||||||
#include <utils/utilsicons.h>
|
#include <utils/utilsicons.h>
|
||||||
|
|
||||||
#include <QMenu>
|
#include <QMenu>
|
||||||
@@ -137,6 +138,8 @@ void TextEditorPlugin::initialize()
|
|||||||
addTestCreator(createCodeAssistTests);
|
addTestCreator(createCodeAssistTests);
|
||||||
addTestCreator(createGenericHighlighterTests);
|
addTestCreator(createGenericHighlighterTests);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Utils::Text::setCodeHighlighter(HighlighterHelper::highlightCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextEditorPlugin::extensionsInitialized()
|
void TextEditorPlugin::extensionsInitialized()
|
||||||
|
Reference in New Issue
Block a user