forked from qt-creator/qt-creator
Editor: use shared highlighter repository
Saves ~40ms for each opened document that uses the generic highlighter. Change-Id: I83555893799c02b223578150352401b97ea73b75 Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io> Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
@@ -68,26 +68,14 @@ TextStyle categoryForTextStyle(int style)
|
||||
return C_TEXT;
|
||||
}
|
||||
|
||||
Highlighter::Highlighter(const QString &definitionFilesPath)
|
||||
: m_repository(new KSyntaxHighlighting::Repository())
|
||||
Highlighter::Highlighter()
|
||||
{
|
||||
m_repository->addCustomSearchPath(definitionFilesPath);
|
||||
const Utils::FilePath dir = Core::ICore::resourcePath("generic-highlighter/syntax");
|
||||
if (dir.exists())
|
||||
m_repository->addCustomSearchPath(dir.parentDir().path());
|
||||
m_repository->reload();
|
||||
|
||||
setTextFormatCategories(QMetaEnum::fromType<KSyntaxHighlighting::Theme::TextStyle>().keyCount(),
|
||||
&categoryForTextStyle);
|
||||
}
|
||||
|
||||
Highlighter::~Highlighter() = default;
|
||||
|
||||
void Highlighter::setDefinitionName(const QString &name)
|
||||
{
|
||||
KSyntaxHighlighting::AbstractHighlighter::setDefinition(m_repository->definitionForName(name));
|
||||
}
|
||||
|
||||
static bool isOpeningParenthesis(QChar c)
|
||||
{
|
||||
return c == QLatin1Char('{') || c == QLatin1Char('[') || c == QLatin1Char('(');
|
||||
|
@@ -17,18 +17,13 @@ class Highlighter : public SyntaxHighlighter, public KSyntaxHighlighting::Abstra
|
||||
Q_OBJECT
|
||||
Q_INTERFACES(KSyntaxHighlighting::AbstractHighlighter)
|
||||
public:
|
||||
Highlighter(const QString &definitionFilesPath);
|
||||
Highlighter();
|
||||
~Highlighter() override;
|
||||
|
||||
void setDefinitionName(const QString &name) override;
|
||||
|
||||
protected:
|
||||
void highlightBlock(const QString &text) override;
|
||||
void applyFormat(int offset, int length, const KSyntaxHighlighting::Format &format) override;
|
||||
void applyFolding(int offset, int length, KSyntaxHighlighting::FoldingRegion region) override;
|
||||
|
||||
private:
|
||||
std::unique_ptr<KSyntaxHighlighting::Repository> m_repository;
|
||||
};
|
||||
|
||||
} // namespace TextEditor
|
||||
|
@@ -258,12 +258,7 @@ QFuture<QTextDocument *> highlightCode(const QString &code, const QString &mimeT
|
||||
return promise->future();
|
||||
}
|
||||
|
||||
auto definition = definitions.first();
|
||||
|
||||
const QString definitionFilesPath
|
||||
= TextEditorSettings::highlighterSettings().definitionFilesPath().toUrlishString();
|
||||
|
||||
Highlighter *highlighter = new Highlighter(definitionFilesPath);
|
||||
Highlighter *highlighter = new Highlighter;
|
||||
QObject::connect(highlighter, &Highlighter::finished, document, [document, promise]() {
|
||||
promise->addResult(document);
|
||||
promise->finish();
|
||||
@@ -275,7 +270,7 @@ QFuture<QTextDocument *> highlightCode(const QString &code, const QString &mimeT
|
||||
});
|
||||
watcher->setFuture(promise->future());
|
||||
|
||||
highlighter->setDefinition(definition);
|
||||
highlighter->setDefinition(definitions.first());
|
||||
highlighter->setParent(document);
|
||||
highlighter->setFontSettings(TextEditorSettings::fontSettings());
|
||||
highlighter->setMimeType(mimeType);
|
||||
|
@@ -3,8 +3,6 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "texteditor_global.h"
|
||||
|
||||
#include <KSyntaxHighlighting/Definition>
|
||||
|
||||
#include <QFuture>
|
||||
|
@@ -53,7 +53,6 @@ public:
|
||||
void setExtraFormats(const QTextBlock &block, const QList<QTextLayout::FormatRange> &formats);
|
||||
virtual void setLanguageFeaturesFlags(unsigned int /*flags*/) {}; // needed for CppHighlighting
|
||||
virtual void setEnabled(bool /*enabled*/) {}; // needed for DiffAndLogHighlighter
|
||||
virtual void setDefinitionName(const QString & /*definitionName*/) {} // needed for Highlighter
|
||||
|
||||
bool syntaxHighlighterUpToDate() const;
|
||||
|
||||
|
@@ -3987,10 +3987,8 @@ void TextEditorWidgetPrivate::configureGenericHighlighter(
|
||||
q->setCodeFoldingSupported(false);
|
||||
}
|
||||
|
||||
const QString definitionFilesPath
|
||||
= TextEditorSettings::highlighterSettings().definitionFilesPath().toUrlishString();
|
||||
m_document->resetSyntaxHighlighter([definitionFilesPath, definition] {
|
||||
auto highlighter = new Highlighter(definitionFilesPath);
|
||||
m_document->resetSyntaxHighlighter([definition] {
|
||||
auto highlighter = new Highlighter;
|
||||
highlighter->setDefinition(definition);
|
||||
return highlighter;
|
||||
});
|
||||
|
Reference in New Issue
Block a user