diff --git a/src/calibre/gui2/tweak_book/editor/syntax/base.py b/src/calibre/gui2/tweak_book/editor/syntax/base.py index 1911c145b0..3e59729762 100644 --- a/src/calibre/gui2/tweak_book/editor/syntax/base.py +++ b/src/calibre/gui2/tweak_book/editor/syntax/base.py @@ -10,8 +10,30 @@ from PyQt4.Qt import (QSyntaxHighlighter, QApplication, QCursor, Qt) from ..themes import highlight_to_char_format +class SimpleState(object): + + def __init__(self, value): + self.parse = value + + @property + def value(self): + return self.parse + +def run_loop(state, state_map, set_format, formats, text): + i = 0 + while i < len(text): + fmt = state_map[state.parse](state, text, i, formats) + for num, f in fmt: + if f is not None: + set_format(i, num, f) + i += num + class SyntaxHighlighter(QSyntaxHighlighter): + state_class = SimpleState + state_map = {0:lambda state, text, i, formats:[(len(text), None)]} + formats = {} + def rehighlight(self): self.outlineexplorer_data = {} QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) @@ -31,22 +53,10 @@ class SyntaxHighlighter(QSyntaxHighlighter): state = self.previousBlockState() if state == -1: state = 0 - self.do_highlight(state, unicode(text)) + state = self.state_class(state) + run_loop(state, self.state_map, self.setFormat, self.formats, unicode(text)) + self.setCurrentBlockState(state.value) except: import traceback traceback.print_exc() - def do_highlight(self, state, text): - state = self.state_class(state) - - i = 0 - while i < len(text): - fmt = self.state_map[state.parse](state, text, i, self.formats) - for num, f in fmt: - if f is not None: - self.setFormat(i, num, f) - i += num - - self.setCurrentBlockState(state.value) - -