Browse Source

Markdown Highlighter: speed up at first parse

Le Tan 8 years ago
parent
commit
c7cb95d18b
2 changed files with 18 additions and 4 deletions
  1. 15 4
      src/hgmarkdownhighlighter.cpp
  2. 3 0
      src/hgmarkdownhighlighter.h

+ 15 - 4
src/hgmarkdownhighlighter.cpp

@@ -29,9 +29,16 @@ HGMarkdownHighlighter::HGMarkdownHighlighter(const QVector<HighlightingStyle> &s
                                              const QHash<QString, QTextCharFormat> &codeBlockStyles,
                                              int waitInterval,
                                              QTextDocument *parent)
-    : QSyntaxHighlighter(parent), highlightingStyles(styles),
-      m_codeBlockStyles(codeBlockStyles), m_numOfCodeBlockHighlightsToRecv(0),
-      parsing(0), waitInterval(waitInterval), content(NULL), capacity(0), result(NULL)
+    : QSyntaxHighlighter(parent),
+      highlightingStyles(styles),
+      m_codeBlockStyles(codeBlockStyles),
+      m_numOfCodeBlockHighlightsToRecv(0),
+      parsing(0),
+      waitInterval(waitInterval),
+      m_firstParse(true),
+      content(NULL),
+      capacity(0),
+      result(NULL)
 {
     codeBlockStartExp = QRegExp(VUtils::c_fencedCodeBlockStartRegExp);
     codeBlockEndExp = QRegExp(VUtils::c_fencedCodeBlockEndRegExp);
@@ -541,11 +548,15 @@ void HGMarkdownHighlighter::timerTimeout()
 {
     qDebug() << "HGMarkdownHighlighter start a new parse";
     parse();
-    if (!updateCodeBlocks()) {
+    if (!updateCodeBlocks() || m_firstParse) {
         rehighlight();
     }
 
     highlightChanged();
+
+    if (m_firstParse) {
+        m_firstParse = false;
+    }
 }
 
 void HGMarkdownHighlighter::updateHighlight()

+ 3 - 0
src/hgmarkdownhighlighter.h

@@ -190,6 +190,9 @@ private:
     QTimer *timer;
     int waitInterval;
 
+    // Whether this is the first parse.
+    bool m_firstParse;
+
     char *content;
     int capacity;
     pmh_element **result;