import re import keyword from . import * STYLES = { 'keyword': textformat('darkOrange'), 'builtins': textformat('green'), 'decorator': textformat('darkBlue'), 'defclass': textformat('darkGreen'), 'string': textformat('magenta'), 'comment': textformat('blue'), 'self': textformat('black', 'bold'), 'numbers': textformat('brown'), } # enum for block state class QUOTESTATE: NONE = 0 DOUBLE = 1 SINGLE = 2 TRIPLEOFFSET = 2 # quotes (QUOTESTATE indices) QUOTES = [None, '"', "'", '"""', "'''"] def find_first(subs, text, start): '''Index of first occurence of `subs` in `text`. Return None if not found.''' best = None for sub in subs: index = text.find(sub, start, best) if index != -1: best = index return best python_rules = [ (re.compile(pat), index, fmt) for (pat, index, fmt) in [ # decorators (r'^\s*@[\w\.]+', 0, STYLES['decorator']), # built in names (r'(? 0 and text[start - 1].lower() in 'rub': start -= 1 self.setFormat(start + offset, index - start, STYLES['string']) return state class Highlighter(QtGui.QSyntaxHighlighter): def highlightBlock(self, text): state = max(0, self.previousBlockState()) state = highlightPython(self, state, text) self.setCurrentBlockState(state)