def __init__(self, document): super(ScriptHighlighter, self).__init__(document) # Highlighting rules self._rules = [] # Numbers style = QtGui.QTextCharFormat() style.setForeground(QtGui.QColor(255, 0, 255)) pattern = QtCore.QRegExp(r'\b[+-]?[0-9]*\.?[0-9]+([eE][+-]?[0-9]+)?\b') self._rules.append((pattern, style)) # True/False style = QtGui.QTextCharFormat() style.setForeground(QtGui.QColor(255, 0, 255)) pattern = QtCore.QRegExp(r'\bTrue\b') self._rules.append((pattern, style)) pattern = QtCore.QRegExp(r'\bFalse\b') self._rules.append((pattern, style)) # Built-in essential functions style = QtGui.QTextCharFormat() style.setForeground(QtGui.QColor(0, 128, 128)) for func in _PYFUNC: pattern = QtCore.QRegExp(r'\b' + str(func) + r'\b') self._rules.append((pattern, style)) # Keywords import keyword style = QtGui.QTextCharFormat() style.setForeground(QtGui.QColor(0, 96, 0)) style.setFontWeight(QtGui.QFont.Bold) for kw in keyword.kwlist: pattern = QtCore.QRegExp(r'\b' + kw + r'\b') self._rules.append((pattern, style)) # Strings self._string_style = QtGui.QTextCharFormat() self._string_style.setForeground(QtGui.QColor(255, 0, 255)) pattern = QtCore.QRegExp(r'"([^"\\]|\\")*"') self._rules.append((pattern, self._string_style)) pattern = QtCore.QRegExp(r"'([^'\\]|\\')*'") self._rules.append((pattern, self._string_style)) # Multi-line strings self._string1 = QtCore.QRegExp(r"'''") self._string2 = QtCore.QRegExp(r'"""') # Comments style = QtGui.QTextCharFormat() style.setForeground(QtGui.QColor(20, 20, 255)) pattern = QtCore.QRegExp(r'#[^\n]*') self._rules.append((pattern, style))
def __init__(self, document): super(ModelHighlighter, self).__init__(document) # Highlighting rules self._rules = [] # Numbers style = QtGui.QTextCharFormat() style.setForeground(QtGui.QColor(255, 0, 255)) pattern = QtCore.QRegExp(r'\b[+-]?[0-9]*\.?[0-9]+([eE][+-]?[0-9]+)?\b') self._rules.append((pattern, style)) # Keywords style = QtGui.QTextCharFormat() style.setForeground(QtGui.QColor(0, 96, 0)) style.setFontWeight(QtGui.QFont.Bold) for keyword in self.KEYWORDS: pattern = QtCore.QRegExp(r'\b' + keyword + r'\b') self._rules.append((pattern, style)) # Meta-data coloring (overrules previous formatting) self._meta_style = QtGui.QTextCharFormat() self._meta_style.setForeground(QtGui.QColor(128, 128, 192)) pattern = QtCore.QRegExp(r':.*') self._rules.append((pattern, self._meta_style)) # Strings (overrule previous formatting, except when commented) self._string_start = QtCore.QRegExp(r'"""') self._string_stop = QtCore.QRegExp(r'"""') self._comment_start = QtCore.QRegExp(r'#[^\n]*') # Comments (overrule all other formatting) style = QtGui.QTextCharFormat() style.setForeground(QtGui.QColor(20, 20, 255)) pattern = QtCore.QRegExp(r'#[^\n]*') self._rules.append((pattern, style))
def __init__(self, document): super(ProtocolHighlighter, self).__init__(document) # Highlighting rules self._rules = [] # Numbers style = QtGui.QTextCharFormat() style.setForeground(QtGui.QColor(255, 0, 255)) pattern = QtCore.QRegExp(r'\b[+-]?[0-9]*\.?[0-9]+([eE][+-]?[0-9]+)?\b') self._rules.append((pattern, style)) # Keyword "next" style = QtGui.QTextCharFormat() style.setForeground(QtGui.QColor(255, 0, 255)) pattern = QtCore.QRegExp(r'\bnext\b') self._rules.append((pattern, style)) # Comments style = QtGui.QTextCharFormat() style.setForeground(QtGui.QColor(20, 20, 255)) pattern = QtCore.QRegExp(r'#[^\n]*') self._rules.append((pattern, style))
def __init__(self, document): super(ProtocolHighlighter, self).__init__(document) # Headers and units self._rule_head = QtCore.QRegExp(r'^(\s*)\[\[[a-zA-Z0-9_]+\]\]') # Highlighting rules self._rules = [] # Numbers pattern = QtCore.QRegExp(r'\b[+-]?[0-9]*\.?[0-9]+([eE][+-]?[0-9]+)?\b') self._rules.append((pattern, STYLE_LITERAL)) # Keyword "next" pattern = QtCore.QRegExp(r'\bnext\b') self._rules.append((pattern, STYLE_KEYWORD_1)) # Comments pattern = QtCore.QRegExp(r'#[^\n]*') self._rules.append((pattern, STYLE_COMMENT))
def highlightBlock(self, text): """ Qt: Called whenever a block should be highlighted. """ # Rule based formatting for (pattern, style) in self._rules: e = QtCore.QRegExp(pattern) i = e.indexIn(text) while i >= 0: n = len(pattern.cap(0)) self.setFormat(i, n, style) i = pattern.indexIn(text, i + n) self.setCurrentBlockState(0)
def highlightBlock(self, text): """ Qt: Called whenever a block should be highlighted. """ # Rule based formatting for (pattern, style) in self._rules: e = QtCore.QRegExp(pattern) i = e.indexIn(text) while i >= 0: # Note: Can't use matchedLength() here because it does quirky # things with the subgroup for the numbers regex. #n = e.matchedLength() n = len(pattern.cap(0)) self.setFormat(i, n, style) i = pattern.indexIn(text, i + n) self.setCurrentBlockState(0) # Multi-line formats # Block states: # 0 Normal # 1 Multi-line string # Check state of previous block if self.previousBlockState() != 1: # Normal block next = start = self._string_start.indexIn(text) comment = self._comment_start.indexIn(text) if next >= 0 and (comment < 0 or comment > next): next += self._string_start.matchedLength() else: start = next = -1 else: start = next = 0 # Find any occurrences of string start / stop while next >= 0: stop = self._string_stop.indexIn(text, next) if stop < 0: # Continuing multi-line string self.setCurrentBlockState(1) self.setFormat(start, len(text) - start, self._meta_style) next = -1 else: # Ending single-line or multi-line string # Format until stop token next = stop = stop + self._string_stop.matchedLength() self.setFormat(start, stop - start, self._meta_style) # Find next string in block, if any next = start = self._string_start.indexIn(text, next) if next >= 0: next += self._string_start.matchedLength()
def highlightBlock(self, text): """ Qt: Called whenever a block should be highlighted. """ # Rule based formatting for (pattern, style) in self._rules: e = QtCore.QRegExp(pattern) i = e.indexIn(text) while i >= 0: # Note: Can't use matchedLength() here because it does quirky # things with the subgroup for the numbers regex. n = len(pattern.cap(0)) self.setFormat(i, n, style) i = pattern.indexIn(text, i + n) self.setCurrentBlockState(0) # Multi-line formats # Block states: # 0 Normal # 1 Multi-line string 1 # 2 Multi-line string 2 def find_start(text, next): s1 = self._string1.indexIn(text, next) s2 = self._string2.indexIn(text, next) if s1 < 0 and s2 < 0: current = 0 start = -1 next = -1 else: if s1 >= 0 and s2 >= 0: current = 1 if s1 < s2 else 2 start = min(s1, s2) elif s1 >= 0: current = 1 start = s1 else: # (s2 >= 0) current = 2 start = s2 next = start + 3 # Check we're not in a comment i = text.rfind('\n', start) + 1 if text[i:i + 1] == '#': current = 0 start = next = -1 return current, start, next # Check state of previous block previous = self.previousBlockState() if previous == 1 or previous == 2: current, start, next = previous, 0, 0 else: current, start, next = find_start(text, 0) # Find any occurrences of string start / stop while next >= 0: if current == 1: stop = self._string1.indexIn(text, next) else: stop = self._string2.indexIn(text, next) if stop < 0: # Continuing multi-line string self.setCurrentBlockState(current) self.setFormat(start, len(text) - start, self._string_style) next = -1 else: # Ending single-line or multi-line string # Format until stop token stop += 3 self.setFormat(start, stop - start, self._string_style) # Find next string in block, if any next = stop + 3 current, start, next = find_start(text, next)
def __init__(self, document): super(ModelHighlighter, self).__init__(document) # Expressions used to find strings & comments self._string_start = QtCore.QRegExp(r'"""') self._string_stop = QtCore.QRegExp(r'"""') self._comment_start = QtCore.QRegExp(r'#[^\n]*') # Headers name = r'[a-zA-Z]+[a-zA-Z0-9_]*' self._rule_head = QtCore.QRegExp(r'^(\s*)\[{1,2}' + name + '\]{1,2}') # Simple rules self._rules = [] # Numbers pattern = QtCore.QRegExp(r'\b[+-]?[0-9]*\.?[0-9]+([eE][+-]?[0-9]+)?\b') self._rules.append((pattern, STYLE_LITERAL)) unit = r'\[[a-zA-Z0-9/^]+\]' self._rules.append((QtCore.QRegExp(unit), STYLE_INLINE_UNIT)) # Keywords for keyword in self.KEYWORD_1: pattern = QtCore.QRegExp(r'\b' + keyword + r'\b') self._rules.append((pattern, STYLE_KEYWORD_1)) for keyword in self.KEYWORD_2: pattern = QtCore.QRegExp(r'\b' + keyword + r'\b') self._rules.append((pattern, STYLE_KEYWORD_2)) # Meta-data coloring self._rules_labels = [ QtCore.QRegExp(r'(\s*)(bind)\s+(' + name + ')'), QtCore.QRegExp(r'(\s*)(label)\s+(' + name + ')'), ] self._rule_meta_key = QtCore.QRegExp(name + r':') self._rule_meta_val = QtCore.QRegExp(r':(\s*)(.+)') self._rule_var_unit = QtCore.QRegExp(r'^(\s*)(in)(\s*)(' + unit + ')') # Comments # Note: For some reason this can _not_ use self._comment_start self._comments = QtCore.QRegExp(r'#[^\n]*')
def __init__(self, document): super(ScriptHighlighter, self).__init__(document) # Headers and units self._rule_head = QtCore.QRegExp(r'^(\s*)\[\[[a-zA-Z0-9_]+\]\]') # Highlighting rules self._rules = [] # Keywords import keyword for kw in keyword.kwlist: pattern = QtCore.QRegExp(r'\b' + kw + r'\b') self._rules.append((pattern, STYLE_KEYWORD_1)) # Built-in essential functions for func in _PYFUNC: pattern = QtCore.QRegExp(r'\b' + str(func) + r'\b') self._rules.append((pattern, STYLE_KEYWORD_2)) # Literals: numbers, True, False, None # Override some keywords pattern = QtCore.QRegExp(r'\b[+-]?[0-9]*\.?[0-9]+([eE][+-]?[0-9]+)?\b') self._rules.append((pattern, STYLE_LITERAL)) pattern = QtCore.QRegExp(r'\bTrue\b') self._rules.append((pattern, STYLE_LITERAL)) pattern = QtCore.QRegExp(r'\bFalse\b') self._rules.append((pattern, STYLE_LITERAL)) pattern = QtCore.QRegExp(r'\bNone\b') self._rules.append((pattern, STYLE_LITERAL)) # Strings pattern = QtCore.QRegExp(r'"([^"\\]|\\")*"') self._rules.append((pattern, STYLE_LITERAL)) pattern = QtCore.QRegExp(r"'([^'\\]|\\')*'") self._rules.append((pattern, STYLE_LITERAL)) # Multi-line strings self._string1 = QtCore.QRegExp(r"'''") self._string2 = QtCore.QRegExp(r'"""') # Comments pattern = QtCore.QRegExp(r'#[^\n]*') self._rules.append((pattern, STYLE_COMMENT))