├── README.md ├── example.py └── syntax_pars.py /README.md: -------------------------------------------------------------------------------- 1 | Python syntax highlighting 2 | 3 | If you need a code editor with syntax highlighting, but don't want something as heavyweight as QsciScintilla, you can use the QSyntaxHighlighter class to apply highlighting to a QPlainTextEdit widget. 4 | This example was based on existing work by Carson Farmer and Christophe Kibleur, and an example on the SciPres wiki. One aspect not addressed by this prior work is handling of Python's triple-quoted strings, which may span multiple lines; the QSyntaxHighlighter documentation includes an example for C++ comments, but those have different beginning and ending delimiters /* ... */, whereas Python's triple-quoted strings have the same delimiter at the beginning and end. These are handled by the match_multiline method--there may be an easier way to do this, but it seems to work pretty well, although it still has trouble with triple-quotes embedded inside another string. 5 | 6 | https://wiki.python.org/moin/PyQt/Python%20syntax%20highlighting 7 | 8 | UPD: With Darcula theme from PyCharm 9 | -------------------------------------------------------------------------------- /example.py: -------------------------------------------------------------------------------- 1 | from PyQt5 import QtWidgets 2 | import syntax_pars 3 | 4 | app = QtWidgets.QApplication([]) 5 | editor = QtWidgets.QPlainTextEdit() 6 | editor.setStyleSheet("""QPlainTextEdit{ 7 | font-family:'Consolas'; 8 | color: #ccc; 9 | background-color: #2b2b2b;}""") 10 | highlight = syntax_pars.PythonHighlighter(editor.document()) 11 | editor.show() 12 | 13 | # Load syntax.py into the editor for demo purposes 14 | infile = open('syntax_pars.py', 'r') 15 | editor.setPlainText(infile.read()) 16 | 17 | app.exec_() -------------------------------------------------------------------------------- /syntax_pars.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from PyQt5.QtCore import QRegExp 3 | from PyQt5.QtGui import QColor, QTextCharFormat, QFont, QSyntaxHighlighter 4 | 5 | 6 | def format(color, style=''): 7 | """ 8 | Return a QTextCharFormat with the given attributes. 9 | """ 10 | _color = QColor() 11 | if type(color) is not str: 12 | _color.setRgb(color[0], color[1], color[2]) 13 | else: 14 | _color.setNamedColor(color) 15 | 16 | _format = QTextCharFormat() 17 | _format.setForeground(_color) 18 | if 'bold' in style: 19 | _format.setFontWeight(QFont.Bold) 20 | if 'italic' in style: 21 | _format.setFontItalic(True) 22 | 23 | return _format 24 | 25 | 26 | # Syntax styles that can be shared by all languages 27 | 28 | STYLES = { 29 | 'keyword': format([200, 120, 50], 'bold'), 30 | 'operator': format([150, 150, 150]), 31 | 'brace': format('darkGray'), 32 | 'defclass': format([220, 220, 255], 'bold'), 33 | 'string': format([20, 110, 100]), 34 | 'string2': format([30, 120, 110]), 35 | 'comment': format([128, 128, 128]), 36 | 'self': format([150, 85, 140], 'italic'), 37 | 'numbers': format([100, 150, 190]), 38 | } 39 | 40 | 41 | class PythonHighlighter(QSyntaxHighlighter): 42 | """Syntax highlighter for the Python language. 43 | """ 44 | # Python keywords 45 | 46 | 47 | keywords = [ 48 | 'and', 'assert', 'break', 'class', 'continue', 'def', 49 | 'del', 'elif', 'else', 'except', 'exec', 'finally', 50 | 'for', 'from', 'global', 'if', 'import', 'in', 51 | 'is', 'lambda', 'not', 'or', 'pass', 'print', 52 | 'raise', 'return', 'try', 'while', 'yield', 53 | 'None', 'True', 'False', 54 | ] 55 | 56 | # Python operators 57 | operators = [ 58 | '=', 59 | # Comparison 60 | '==', '!=', '<', '<=', '>', '>=', 61 | # Arithmetic 62 | '\+', '-', '\*', '/', '//', '\%', '\*\*', 63 | # In-place 64 | '\+=', '-=', '\*=', '/=', '\%=', 65 | # Bitwise 66 | '\^', '\|', '\&', '\~', '>>', '<<', 67 | ] 68 | 69 | # Python braces 70 | braces = [ 71 | '\{', '\}', '\(', '\)', '\[', '\]', 72 | ] 73 | 74 | def __init__(self, document): 75 | QSyntaxHighlighter.__init__(self, document) 76 | 77 | # Multi-line strings (expression, flag, style) 78 | # FIXME: The triple-quotes in these two lines will mess up the 79 | # syntax highlighting from this point onward 80 | self.tri_single = (QRegExp("'''"), 1, STYLES['string2']) 81 | self.tri_double = (QRegExp('"""'), 2, STYLES['string2']) 82 | 83 | rules = [] 84 | 85 | # Keyword, operator, and brace rules 86 | rules += [(r'\b%s\b' % w, 0, STYLES['keyword']) 87 | for w in PythonHighlighter.keywords] 88 | rules += [(r'%s' % o, 0, STYLES['operator']) 89 | for o in PythonHighlighter.operators] 90 | rules += [(r'%s' % b, 0, STYLES['brace']) 91 | for b in PythonHighlighter.braces] 92 | 93 | # All other rules 94 | rules += [ 95 | # 'self' 96 | (r'\bself\b', 0, STYLES['self']), 97 | 98 | # Double-quoted string, possibly containing escape sequences 99 | (r'"[^"\\]*(\\.[^"\\]*)*"', 0, STYLES['string']), 100 | # Single-quoted string, possibly containing escape sequences 101 | (r"'[^'\\]*(\\.[^'\\]*)*'", 0, STYLES['string']), 102 | 103 | # 'def' followed by an identifier 104 | (r'\bdef\b\s*(\w+)', 1, STYLES['defclass']), 105 | # 'class' followed by an identifier 106 | (r'\bclass\b\s*(\w+)', 1, STYLES['defclass']), 107 | 108 | # From '#' until a newline 109 | (r'#[^\n]*', 0, STYLES['comment']), 110 | 111 | # Numeric literals 112 | (r'\b[+-]?[0-9]+[lL]?\b', 0, STYLES['numbers']), 113 | (r'\b[+-]?0[xX][0-9A-Fa-f]+[lL]?\b', 0, STYLES['numbers']), 114 | (r'\b[+-]?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\b', 0, STYLES['numbers']), 115 | ] 116 | 117 | # Build a QRegExp for each pattern 118 | self.rules = [(QRegExp(pat), index, fmt) 119 | for (pat, index, fmt) in rules] 120 | 121 | def highlightBlock(self, text): 122 | """Apply syntax highlighting to the given block of text. 123 | """ 124 | # Do other syntax formatting 125 | for expression, nth, format in self.rules: 126 | index = expression.indexIn(text, 0) 127 | 128 | while index >= 0: 129 | # We actually want the index of the nth match 130 | index = expression.pos(nth) 131 | length = len(expression.cap(nth)) 132 | self.setFormat(index, length, format) 133 | index = expression.indexIn(text, index + length) 134 | 135 | self.setCurrentBlockState(0) 136 | 137 | # Do multi-line strings 138 | in_multiline = self.match_multiline(text, *self.tri_single) 139 | if not in_multiline: 140 | in_multiline = self.match_multiline(text, *self.tri_double) 141 | 142 | def match_multiline(self, text, delimiter, in_state, style): 143 | """Do highlighting of multi-line strings. ``delimiter`` should be a 144 | ``QRegExp`` for triple-single-quotes or triple-double-quotes, and 145 | ``in_state`` should be a unique integer to represent the corresponding 146 | state changes when inside those strings. Returns True if we're still 147 | inside a multi-line string when this function is finished. 148 | """ 149 | # If inside triple-single quotes, start at 0 150 | if self.previousBlockState() == in_state: 151 | start = 0 152 | add = 0 153 | # Otherwise, look for the delimiter on this line 154 | else: 155 | start = delimiter.indexIn(text) 156 | # Move past this match 157 | add = delimiter.matchedLength() 158 | 159 | # As long as there's a delimiter match on this line... 160 | while start >= 0: 161 | # Look for the ending delimiter 162 | end = delimiter.indexIn(text, start + add) 163 | # Ending delimiter on this line? 164 | if end >= add: 165 | length = end - start + add + delimiter.matchedLength() 166 | self.setCurrentBlockState(0) 167 | # No; multi-line string 168 | else: 169 | self.setCurrentBlockState(in_state) 170 | length = len(text) - start + add 171 | # Apply formatting 172 | self.setFormat(start, length, style) 173 | # Look for the next match 174 | start = delimiter.indexIn(text, start + length) 175 | 176 | # Return True if still inside a multi-line string, False otherwise 177 | if self.currentBlockState() == in_state: 178 | return True 179 | else: 180 | return False --------------------------------------------------------------------------------