def set_style(self, theme): self.styles = { feature: self.format(*style) for feature, style in theme.items() } self.tri_single = (QtCore.QRegExp("'''"), 1, self.styles['comment']) self.tri_double = (QtCore.QRegExp('"""'), 2, self.styles['comment'])
def make_rules(self): # rules rules = [] # function args/kwargs TODO: find correct regex pattern for separate # args and kwargs (words) between parentheses # rules += [('(?:def \w+\()([^)]+)', 1, self.styles['args'])] class_regex = r'(?:class\s+\w+\()([a-zA-Z\.]+)(?:\))' rules += [(class_regex, 1, self.styles['inherited'])] rules += [(r'\b%s\b' % i, 0, self.styles['arguments']) for i in self.arguments] rules += [(r'\b%s\b' % i, 0, self.styles['keyword']) for i in self.keywords] rules += [(i, 0, self.styles['keyword']) for i in self.operatorKeywords] rules += [(r'\b%s\b' % i, 0, self.styles['numbers']) for i in self.truthy] rules += [(r'\b%s\b' % i, 0, self.styles['instantiators']) for i in self.instantiators] rules += [(r'\b%s\b' % i, 0, self.styles['exceptions']) for i in self.exceptions + self.types] rules += [ # function names (r'(?:def\s+|)(\w+)(?:\()', 1, self.styles['function_names']), # class names (r'(?:class\s+)(\w+)(?:\()', 1, self.styles['class_names']), # methods (r'(?:\.)([a-zA-Z\.]+)(?:\()', 1, self.styles['methods']), # decorators (r'(?:@)(\w+)', 1, self.styles['function_names']), # string formatters (r'([rfb])(?:\'|\")', 0, self.styles['formatters']), # integers (r'\b[0-9]+\b', 0, self.styles['numbers']), # Double-quoted string, possibly containing escape sequences (r'"[^"\\]*(\\.[^"\\]*)*"', 0, self.styles['string']), # Single-quoted string, possibly containing escape sequences (r"'[^'\\]*(\\.[^'\\]*)*'", 0, self.styles['string']), ] # Build a QRegExp for each pattern self.rules = [(QtCore.QRegExp(pat), index, fmt) for (pat, index, fmt) in rules]
def __init__(self, document): super(Highlight, self).__init__(document) self.setObjectName('Highlight') theme = themes['Monokai Smooth'] self.styles = { feature: self.format(*style) for feature, style in theme.items() } self.arguments = ['self', 'cls', 'args', 'kwargs'] self.keywords = [ 'and', 'assert', 'break', 'continue', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'yield', 'with' ] self.instantiators = ['def', 'class'] self.exceptions = [ 'BaseException', 'SystemExit', 'KeyboardInterrupt', 'GeneratorExit', 'Exception', 'StopIteration', 'StandardError', 'BufferError', 'ArithmeticError', 'FloatingPointError', 'OverflowError', 'ZeroDivisionError', 'AssertionError', 'AttributeError', 'EnvironmentError', 'IOError', 'OSError', 'WindowsError', 'VMSError', 'EOFError', 'ImportError', 'LookupError', 'IndexError', 'KeyError', 'MemoryError', 'NameError', 'UnboundLocalError', 'ReferenceError', 'RuntimeError', 'NotImplementedError', 'SyntaxError', 'IndentationError', 'TabError', 'SystemError', 'TypeError', 'ValueError', 'UnicodeError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeTranslateError', 'Warning', 'DeprecationWarning', 'PendingDeprecationWarning', 'RuntimeWarning', 'SyntaxWarning', 'UserWarning', 'FutureWarning', 'ImportWarning', 'UnicodeWarning', 'BytesWarning' ] self.operatorKeywords = [ '=', '==', '!=', '<', '<=', '>', '>=', '\+', '-', '\*', '/', '//', '\%', '\*\*', '\+=', '-=', '\*=', '/=', '\%=', '\^', '\|', '\&', '\~', '>>', '<<', ] self.truthy = ['True', 'False', 'None'] self.tri_single = (QtCore.QRegExp("'''"), 1, self.styles['comment']) self.tri_double = (QtCore.QRegExp('"""'), 2, self.styles['comment']) # rules rules = [] # function args/kwargs TODO: find correct regex pattern for separate # args and kwargs (words) between parentheses # rules += [('(?:def \w+\()([^)]+)', 1, self.styles['args'])] class_regex = r'(?:class\s+\w+\()([a-zA-Z\.]+)(?:\))' rules += [(class_regex, 1, self.styles['inherited'])] rules += [(r'\b%s\b' % i, 0, self.styles['arguments']) for i in self.arguments] rules += [(r'\b%s\b' % i, 0, self.styles['keyword']) for i in self.keywords] rules += [(i, 0, self.styles['keyword']) for i in self.operatorKeywords] rules += [(r'\b%s\b' % i, 0, self.styles['numbers']) for i in self.truthy] rules += [(r'\b%s\b' % i, 0, self.styles['instantiators']) for i in self.instantiators] rules += [(r'\b%s\b' % i, 0, self.styles['exceptions']) for i in self.exceptions] rules += [ # function names (r'(?:def\s+|)(\w+)(?:\()', 1, self.styles['function_names']), # class names (r'(?:class\s+)(\w+)(?:\()', 1, self.styles['class_names']), # methods (r'(?:\.)([a-zA-Z\.]+)(?:\()', 1, self.styles['methods']), # decorators (r'(?:@)(\w+)', 1, self.styles['function_names']), # string formatters (r'([rfb])(?:\'|\")', 0, self.styles['formatters']), # integers (r'\b[0-9]+\b', 0, self.styles['numbers']), # Double-quoted string, possibly containing escape sequences (r'"[^"\\]*(\\.[^"\\]*)*"', 0, self.styles['string']), # Single-quoted string, possibly containing escape sequences (r"'[^'\\]*(\\.[^'\\]*)*'", 0, self.styles['string']), ] # Build a QRegExp for each pattern self.rules = [(QtCore.QRegExp(pat), index, fmt) for (pat, index, fmt) in rules]