forked from qt-creator/qt-creator
GlslEditor: Fix crash in GlslHighlighter
since the async highlighter implementation the parent is not a TextDocument anymore, but a QTextDocument. Pass the required mimeType via the SyntaxHighlighterRunner now to the SyntaxHighlighter. Change-Id: I0afcbe68719195649b4b7040416a743cb01214df Reviewed-by: Artem Sokolovskii <artem.sokolovskii@qt.io>
This commit is contained in:
@@ -47,10 +47,7 @@ void GlslHighlighter::highlightBlock(const QString &text)
|
||||
lex.setState(state);
|
||||
lex.setScanKeywords(false);
|
||||
lex.setScanComments(true);
|
||||
const int variant = languageVariant(parent()
|
||||
? static_cast<TextDocument*>(parent())->mimeType()
|
||||
: QString());
|
||||
lex.setVariant(variant);
|
||||
lex.setVariant(languageVariant(mimeType()));
|
||||
|
||||
int initialState = state;
|
||||
|
||||
|
@@ -61,6 +61,7 @@ public:
|
||||
QList<std::pair<int,TextStyle>> formatCategories;
|
||||
QTextCharFormat whitespaceFormat;
|
||||
bool noAutomaticHighlighting = false;
|
||||
QString mimeType;
|
||||
};
|
||||
|
||||
static bool adjustRange(QTextLayout::FormatRange &range, int from, int charsDelta)
|
||||
@@ -364,6 +365,18 @@ QTextDocument *SyntaxHighlighter::document() const
|
||||
return d->doc;
|
||||
}
|
||||
|
||||
void SyntaxHighlighter::setMimeType(const QString &mimeType)
|
||||
{
|
||||
Q_D(SyntaxHighlighter);
|
||||
d->mimeType = mimeType;
|
||||
}
|
||||
|
||||
QString SyntaxHighlighter::mimeType() const
|
||||
{
|
||||
Q_D(const SyntaxHighlighter);
|
||||
return d->mimeType;
|
||||
}
|
||||
|
||||
/*!
|
||||
\since 4.2
|
||||
|
||||
|
@@ -44,6 +44,9 @@ public:
|
||||
void setDocument(QTextDocument *doc);
|
||||
QTextDocument *document() const;
|
||||
|
||||
void setMimeType(const QString &mimeType);
|
||||
QString mimeType() const;
|
||||
|
||||
static QList<QColor> generateColors(int n, const QColor &background);
|
||||
|
||||
// Don't call in constructors of derived classes
|
||||
|
@@ -30,6 +30,7 @@ public:
|
||||
SyntaxHighlighterRunnerPrivate(SyntaxHighlighterRunner::SyntaxHighlighterCreator creator,
|
||||
QTextDocument *document,
|
||||
bool async,
|
||||
const QString &mimeType,
|
||||
FontSettings fontSettings)
|
||||
{
|
||||
if (async) {
|
||||
@@ -42,6 +43,7 @@ public:
|
||||
m_highlighter.reset(creator());
|
||||
m_highlighter->setFontSettings(fontSettings);
|
||||
m_highlighter->setDocument(m_document);
|
||||
m_highlighter->setMimeType(mimeType);
|
||||
m_highlighter->setParent(m_document);
|
||||
|
||||
connect(m_highlighter.get(),
|
||||
@@ -111,8 +113,9 @@ signals:
|
||||
SyntaxHighlighterRunner::SyntaxHighlighterRunner(SyntaxHighlighterCreator creator,
|
||||
QTextDocument *document,
|
||||
bool async,
|
||||
const QString &mimeType,
|
||||
const TextEditor::FontSettings &fontSettings)
|
||||
: d(new SyntaxHighlighterRunnerPrivate(creator, document, async, fontSettings))
|
||||
: d(new SyntaxHighlighterRunnerPrivate(creator, document, async, mimeType, fontSettings))
|
||||
, m_document(document)
|
||||
{
|
||||
m_useGenericHighlighter = qobject_cast<Highlighter *>(d->m_highlighter.get());
|
||||
|
@@ -24,11 +24,12 @@ class TEXTEDITOR_EXPORT SyntaxHighlighterRunner : public QObject
|
||||
public:
|
||||
using SyntaxHighlighterCreator = std::function<SyntaxHighlighter *()>;
|
||||
|
||||
SyntaxHighlighterRunner(SyntaxHighlighterCreator creator,
|
||||
QTextDocument *document,
|
||||
bool async,
|
||||
const TextEditor::FontSettings &fontSettings
|
||||
= TextEditorSettings::fontSettings());
|
||||
SyntaxHighlighterRunner(
|
||||
SyntaxHighlighterCreator creator,
|
||||
QTextDocument *document,
|
||||
bool async,
|
||||
const QString &mimeType,
|
||||
const TextEditor::FontSettings &fontSettings = TextEditorSettings::fontSettings());
|
||||
virtual ~SyntaxHighlighterRunner();
|
||||
|
||||
void setExtraFormats(const QMap<int, QList<QTextLayout::FormatRange>> &formats);
|
||||
|
@@ -921,7 +921,10 @@ void TextDocument::resetSyntaxHighlighter(const std::function<SyntaxHighlighter
|
||||
= qtcEnvironmentVariable("QTC_USE_THREADED_HIGHLIGHTER", "TRUE").toUpper()
|
||||
== QLatin1String("TRUE");
|
||||
|
||||
d->m_highlighterRunner = new SyntaxHighlighterRunner(creator, document(), threaded && envValue);
|
||||
d->m_highlighterRunner = new SyntaxHighlighterRunner(creator,
|
||||
document(),
|
||||
threaded && envValue,
|
||||
mimeType());
|
||||
}
|
||||
|
||||
void TextDocument::cleanWhitespace(const QTextCursor &cursor)
|
||||
|
@@ -60,7 +60,7 @@ Last)";
|
||||
doc->setPlainText(text);
|
||||
|
||||
highlighterRunner = new SyntaxHighlighterRunner(
|
||||
[this] { return new SyntaxHighlighter(doc, fontsettings); }, doc, false, fontsettings);
|
||||
[this] { return new SyntaxHighlighter(doc, fontsettings); }, doc, false, {}, fontsettings);
|
||||
}
|
||||
|
||||
static const HighlightingResults &highlightingResults()
|
||||
|
Reference in New Issue
Block a user