Change the method the document is rehighlighted. This is significantly faster

This commit is contained in:
Veronica K. B. Olsen
2019-11-19 21:49:11 +01:00
parent 2758c587af
commit eb68f3efc0
4 changed files with 33 additions and 5 deletions
+17 -4
View File
@@ -304,9 +304,11 @@ class GuiDocEditor(QTextEdit):
# Updating root frame triggers a QTextDocument->contentsChange
# signal, which we do not want as it re-runs the syntax
# highlighter and spell checker, so we block it briefly.
# We then emit a signal that does not trigger re-highlighting.
self.qDocument.blockSignals(True)
self.qDocument.rootFrame().setFrameFormat(docFormat)
self.qDocument.blockSignals(False)
self.qDocument.contentsChange.emit(0,0,0)
return
@@ -372,18 +374,26 @@ class GuiDocEditor(QTextEdit):
self.theParent.mainMenu.setSpellCheck(theMode)
self.theProject.setSpellCheck(theMode)
self.hLight.setSpellCheck(theMode)
self.hLight.rehighlight()
self.reHighlightDocument()
logger.verbose("Spell check is set to %s" % str(theMode))
return True
def updateSpellCheck(self):
def reHighlightDocument(self):
"""Rerun the highlighter to update spell checking status of the
currently loaded text.
currently loaded text. The fastest way to do this, at least as
of Qt 5.13, is to clear the text and put it back.
"""
if self.spellCheck:
self.hLight.rehighlight()
theText = self.getText()
self.clear()
bfTime = time()
self.setPlainText(theText)
afTime = time()
logger.debug("Document re-highlighted in %.3f milliseconds" % (1000*(afTime-bfTime)))
return True
##
@@ -615,6 +625,9 @@ class GuiDocEditor(QTextEdit):
return
def _docChange(self, thePos, charsRemoved, charsAdded):
"""Triggered by QTextDocument->contentsChanged. This also
triggers the syntax highlighter.
"""
self.lastEdit = time()
if not self.docChanged:
self.setDocumentChanged(True)