OSDN Git Service

Fix crash when spliting GLSL editor
authorLeandro Melo <leandro.melo@nokia.com>
Tue, 2 Aug 2011 10:26:00 +0000 (12:26 +0200)
committerLeandro T. C. Melo <leandro.melo@nokia.com>
Tue, 2 Aug 2011 11:07:36 +0000 (13:07 +0200)
Not a good idea to keep the editor stored since when removing
a split, for example, the editor will go away and the highlighter
is not aware of it anyhow since it actually belongs to the document.

Although it's not necessary for the fix this patch add another
constructor to SyntaxHighlighter which takes the BaseTextDocument.
This is convenient.

Task-number: QTCREATORBUG-5695
Change-Id: Ic91837b7d91ebd3a44c16e2fd589d7f6c5c0c002
Reviewed-on: http://codereview.qt.nokia.com/2508
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Roberto Raggi <roberto.raggi@nokia.com>
src/plugins/glsleditor/glsleditor.cpp
src/plugins/glsleditor/glsleditor.h
src/plugins/glsleditor/glslhighlighter.cpp
src/plugins/glsleditor/glslhighlighter.h
src/plugins/texteditor/syntaxhighlighter.cpp
src/plugins/texteditor/syntaxhighlighter.h

index 18fa552..b6d1f32 100644 (file)
@@ -163,7 +163,7 @@ GLSLTextEditorWidget::GLSLTextEditorWidget(QWidget *parent) :
 
     connect(this, SIGNAL(textChanged()), this, SLOT(updateDocument()));
 
-    baseTextDocument()->setSyntaxHighlighter(new Highlighter(this, document()));
+    new Highlighter(baseTextDocument());
 
 //    if (m_modelManager) {
 //        m_semanticHighlighter->setModelManager(m_modelManager);
@@ -312,7 +312,7 @@ void GLSLTextEditorWidget::updateDocumentNow()
 {
     m_updateDocumentTimer->stop();
 
-    int variant = languageVariant();
+    int variant = languageVariant(mimeType());
     const QString contents = toPlainText(); // get the code from the editor
     const QByteArray preprocessedCode = contents.toLatin1(); // ### use the QtCreator C++ preprocessor.
 
@@ -371,10 +371,9 @@ void GLSLTextEditorWidget::updateDocumentNow()
     }
 }
 
-int GLSLTextEditorWidget::languageVariant() const
+int GLSLTextEditorWidget::languageVariant(const QString &type)
 {
     int variant = 0;
-    QString type = mimeType();
     bool isVertex = false;
     bool isFragment = false;
     bool isDesktop = false;
index ba228f4..7035693 100644 (file)
@@ -100,7 +100,7 @@ public:
 
     QSet<QString> identifiers() const;
 
-    int languageVariant() const;
+    static int languageVariant(const QString &mimeType);
 
     Document::Ptr glslDocument() const;
 
index 7a5cb70..2055ac1 100644 (file)
@@ -34,6 +34,7 @@
 #include <glsl/glsllexer.h>
 #include <glsl/glslparser.h>
 #include <texteditor/basetextdocumentlayout.h>
+#include <texteditor/basetextdocument.h>
 
 #include <QtCore/QDebug>
 
@@ -41,8 +42,8 @@ using namespace GLSLEditor;
 using namespace GLSLEditor::Internal;
 using namespace TextEditor;
 
-Highlighter::Highlighter(GLSLTextEditorWidget *editor, QTextDocument *parent)
-    : TextEditor::SyntaxHighlighter(parent), m_editor(editor)
+Highlighter::Highlighter(BaseTextDocument *parent)
+    : TextEditor::SyntaxHighlighter(parent)
 {
 }
 
@@ -72,7 +73,8 @@ void Highlighter::highlightBlock(const QString &text)
     lex.setState(state);
     lex.setScanKeywords(false);
     lex.setScanComments(true);
-    const int variant = m_editor->languageVariant();
+    const int variant =
+        GLSLTextEditorWidget::languageVariant(static_cast<BaseTextDocument*>(parent())->mimeType());
     lex.setVariant(variant);
 
     int initialState = state;
index e9dc44e..1d52f98 100644 (file)
@@ -60,7 +60,7 @@ public:
         NumGLSLFormats
     };
 
-    explicit Highlighter(GLSLTextEditorWidget *editor, QTextDocument *parent);
+    explicit Highlighter(TextEditor::BaseTextDocument *parent);
     virtual ~Highlighter();
 
     void setFormats(const QVector<QTextCharFormat> &formats);
@@ -72,7 +72,6 @@ protected:
 
 private:
     QTextCharFormat m_formats[NumGLSLFormats];
-    GLSLTextEditorWidget *m_editor;
 };
 
 } // namespace Internal
index 3dfe2b9..f10b203 100644 (file)
@@ -30,6 +30,7 @@
 **************************************************************************/
 
 #include "syntaxhighlighter.h"
+#include "basetextdocument.h"
 
 #include <qtextdocument.h>
 #include <qtextlayout.h>
@@ -324,6 +325,13 @@ SyntaxHighlighter::SyntaxHighlighter(QTextDocument *parent)
     setDocument(parent);
 }
 
+SyntaxHighlighter::SyntaxHighlighter(BaseTextDocument *parent)
+    : d_ptr(new SyntaxHighlighterPrivate)
+{
+    d_ptr->q_ptr = this;
+    parent->setSyntaxHighlighter(this); // Extra logic (including setting the parent).
+}
+
 /*!
     Constructs a SyntaxHighlighter and installs it on \a parent 's
     QTextDocument. The specified QTextEdit also becomes the owner of
index fe61561..112dd65 100644 (file)
@@ -49,6 +49,7 @@ QT_END_NAMESPACE
 
 namespace TextEditor {
 
+class BaseTextDocument;
 class SyntaxHighlighterPrivate;
 
 class TEXTEDITOR_EXPORT SyntaxHighlighter : public QObject
@@ -58,6 +59,7 @@ class TEXTEDITOR_EXPORT SyntaxHighlighter : public QObject
 public:
     SyntaxHighlighter(QObject *parent);
     SyntaxHighlighter(QTextDocument *parent);
+    SyntaxHighlighter(BaseTextDocument *parent);
     SyntaxHighlighter(QTextEdit *parent);
     virtual ~SyntaxHighlighter();