From 519804a0d2ce56ffec2caaa3610f7c423cc193be Mon Sep 17 00:00:00 2001 From: David Schulz Date: Thu, 17 Mar 2022 11:25:48 +0100 Subject: [PATCH] QmlJSEditor: prevent possible crash on font settings change Fixes: QTCREATORBUG-27116 Change-Id: Iac38bd4ef20e739da4c4b8b1c072fd2cd648a66a Reviewed-by: Eike Ziller --- .../qmljseditor/qmljssemantichighlighter.cpp | 32 +++++++++++-------- .../qmljseditor/qmljssemantichighlighter.h | 4 ++- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/plugins/qmljseditor/qmljssemantichighlighter.cpp b/src/plugins/qmljseditor/qmljssemantichighlighter.cpp index 1d3e2c13ddf..8909c471e0e 100644 --- a/src/plugins/qmljseditor/qmljssemantichighlighter.cpp +++ b/src/plugins/qmljseditor/qmljssemantichighlighter.cpp @@ -175,9 +175,11 @@ class CollectionTask : protected Visitor { public: CollectionTask(QFutureInterface &futureInterface, - const QmlJSTools::SemanticInfo &semanticInfo) + const QmlJSTools::SemanticInfo &semanticInfo, + const TextEditor::FontSettings &fontSettings) : m_futureInterface(futureInterface) , m_semanticInfo(semanticInfo) + , m_fontSettings(fontSettings) , m_scopeChain(semanticInfo.scopeChain()) , m_scopeBuilder(&m_scopeChain) , m_lineOfLastUse(0) @@ -408,13 +410,11 @@ protected: length = end-begin; } - const TextEditor::FontSettings &fontSettings = TextEditor::TextEditorSettings::fontSettings(); - QTextCharFormat format; if (d.isWarning()) - format = fontSettings.toTextCharFormat(TextEditor::C_WARNING); + format = m_fontSettings.toTextCharFormat(TextEditor::C_WARNING); else - format = fontSettings.toTextCharFormat(TextEditor::C_ERROR); + format = m_fontSettings.toTextCharFormat(TextEditor::C_ERROR); format.setToolTip(d.message); @@ -445,17 +445,15 @@ protected: length = end-begin; } - const TextEditor::FontSettings &fontSettings = TextEditor::TextEditorSettings::fontSettings(); - QTextCharFormat format; if (d.severity == Severity::Warning || d.severity == Severity::MaybeWarning || d.severity == Severity::ReadingTypeInfoWarning) { - format = fontSettings.toTextCharFormat(TextEditor::C_WARNING); + format = m_fontSettings.toTextCharFormat(TextEditor::C_WARNING); } else if (d.severity == Severity::Error || d.severity == Severity::MaybeError) { - format = fontSettings.toTextCharFormat(TextEditor::C_ERROR); + format = m_fontSettings.toTextCharFormat(TextEditor::C_ERROR); } else if (d.severity == Severity::Hint) { - format = fontSettings.toTextCharFormat(TextEditor::C_WARNING); + format = m_fontSettings.toTextCharFormat(TextEditor::C_WARNING); format.setUnderlineColor(Qt::darkGreen); } @@ -534,6 +532,7 @@ private: QFutureInterface &m_futureInterface; const QmlJSTools::SemanticInfo &m_semanticInfo; + const TextEditor::FontSettings &m_fontSettings; ScopeChain m_scopeChain; ScopeBuilder m_scopeBuilder; QStringList m_stateNames; @@ -565,8 +564,11 @@ void SemanticHighlighter::rerun(const QmlJSTools::SemanticInfo &semanticInfo) m_watcher.cancel(); m_startRevision = m_document->document()->revision(); - auto future = Utils::runAsync(QThread::LowestPriority, &SemanticHighlighter::run, - this, semanticInfo); + auto future = Utils::runAsync(QThread::LowestPriority, + &SemanticHighlighter::run, + this, + semanticInfo, + TextEditor::TextEditorSettings::fontSettings()); m_watcher.setFuture(future); m_futureSynchronizer.addFuture(future); } @@ -600,9 +602,11 @@ void SemanticHighlighter::finished() m_document->syntaxHighlighter(), m_watcher.future()); } -void SemanticHighlighter::run(QFutureInterface &futureInterface, const QmlJSTools::SemanticInfo &semanticInfo) +void SemanticHighlighter::run(QFutureInterface &futureInterface, + const QmlJSTools::SemanticInfo &semanticInfo, + const TextEditor::FontSettings &fontSettings) { - CollectionTask task(futureInterface, semanticInfo); + CollectionTask task(futureInterface, semanticInfo, fontSettings); reportMessagesInfo(task.diagnosticRanges(), task.extraFormats()); task.run(); } diff --git a/src/plugins/qmljseditor/qmljssemantichighlighter.h b/src/plugins/qmljseditor/qmljssemantichighlighter.h index f36b9b5518a..c47b2afdb88 100644 --- a/src/plugins/qmljseditor/qmljssemantichighlighter.h +++ b/src/plugins/qmljseditor/qmljssemantichighlighter.h @@ -82,7 +82,9 @@ public: private: void applyResults(int from, int to); void finished(); - void run(QFutureInterface &futureInterface, const QmlJSTools::SemanticInfo &semanticInfo); + void run(QFutureInterface &futureInterface, + const QmlJSTools::SemanticInfo &semanticInfo, + const TextEditor::FontSettings &fontSettings); QFutureWatcher m_watcher; QmlJSEditorDocument *m_document;