More refactoring

This commit is contained in:
Kovid Goyal 2013-10-30 21:21:02 +05:30
parent b637effcb2
commit dbfbf17146

View File

@ -10,8 +10,30 @@ from PyQt4.Qt import (QSyntaxHighlighter, QApplication, QCursor, Qt)
from ..themes import highlight_to_char_format 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): class SyntaxHighlighter(QSyntaxHighlighter):
state_class = SimpleState
state_map = {0:lambda state, text, i, formats:[(len(text), None)]}
formats = {}
def rehighlight(self): def rehighlight(self):
self.outlineexplorer_data = {} self.outlineexplorer_data = {}
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
@ -31,22 +53,10 @@ class SyntaxHighlighter(QSyntaxHighlighter):
state = self.previousBlockState() state = self.previousBlockState()
if state == -1: if state == -1:
state = 0 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: except:
import traceback import traceback
traceback.print_exc() 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)