| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
from PyQt5 import QtCore, QtGui, QtWidgets |
| 22 |
|
| 23 |
_have_pygments = None |
| 24 |
def have_pygments(): |
| 25 |
global _have_pygments |
| 26 |
global ClassNotFound |
| 27 |
global get_lexer_for_filename |
| 28 |
global get_style_by_name |
| 29 |
global lex |
| 30 |
|
| 31 |
if _have_pygments is None: |
| 32 |
try: |
| 33 |
from pygments.util import ClassNotFound |
| 34 |
from pygments.styles import get_style_by_name |
| 35 |
from pygments import lex |
| 36 |
from pygments.lexers import get_lexer_for_filename |
| 37 |
except ImportError: |
| 38 |
_have_pygments = False |
| 39 |
else: |
| 40 |
_have_pygments = True |
| 41 |
return _have_pygments |
| 42 |
|
| 43 |
|
| 44 |
def highlight_document(edit, filename): |
| 45 |
doc = edit.document() |
| 46 |
|
| 47 |
if not have_pygments(): |
| 48 |
return |
| 49 |
|
| 50 |
try: |
| 51 |
lexer = get_lexer_for_filename(filename, stripnl=False) |
| 52 |
except ClassNotFound: |
| 53 |
return |
| 54 |
|
| 55 |
style = get_style_by_name("default") |
| 56 |
|
| 57 |
font = doc.defaultFont() |
| 58 |
base_format = QtGui.QTextCharFormat() |
| 59 |
base_format.setFont(font) |
| 60 |
token_formats = {} |
| 61 |
|
| 62 |
window = edit.window() |
| 63 |
if hasattr(window, "processEvents"): |
| 64 |
processEvents = window.processEvents |
| 65 |
else: |
| 66 |
processEvents = QtCore.QCoreApplication.processEvents |
| 67 |
|
| 68 |
def get_token_format(token): |
| 69 |
if token in token_formats: |
| 70 |
return token_formats[token] |
| 71 |
|
| 72 |
if token.parent: |
| 73 |
parent_format = get_token_format(token.parent) |
| 74 |
else: |
| 75 |
parent_format = base_format |
| 76 |
|
| 77 |
format = QtGui.QTextCharFormat(parent_format) |
| 78 |
font = format.font() |
| 79 |
if style.styles_token(token): |
| 80 |
tstyle = style.style_for_token(token) |
| 81 |
if tstyle['color']: |
| 82 |
format.setForeground (QtGui.QColor("#"+tstyle['color'])) |
| 83 |
if tstyle['bold']: font.setWeight(QtGui.QFont.Bold) |
| 84 |
if tstyle['italic']: font.setItalic (True) |
| 85 |
if tstyle['underline']: format.setFontUnderline(True) |
| 86 |
if tstyle['bgcolor']: format.setBackground (QtGui.QColor("#"+tstyle['bgcolor'])) |
| 87 |
|
| 88 |
|
| 89 |
token_formats[token] = format |
| 90 |
return format |
| 91 |
|
| 92 |
text = str(doc.toPlainText()) |
| 93 |
|
| 94 |
block_count = 0 |
| 95 |
block = doc.firstBlock() |
| 96 |
assert(isinstance(block, QtGui.QTextBlock)) |
| 97 |
block_pos = 0 |
| 98 |
block_len = block.length() |
| 99 |
block_formats = [] |
| 100 |
|
| 101 |
for token, ttext in lex(text, lexer): |
| 102 |
format_len = len(ttext) |
| 103 |
format = get_token_format(token) |
| 104 |
while format_len > 0: |
| 105 |
format_range = QtGui.QTextLayout.FormatRange() |
| 106 |
format_range.start = block_pos |
| 107 |
format_range.length = min(format_len, block_len) |
| 108 |
format_range.format = format |
| 109 |
block_formats.append(format_range) |
| 110 |
block_len -= format_range.length |
| 111 |
format_len -= format_range.length |
| 112 |
block_pos += format_range.length |
| 113 |
if block_len == 0: |
| 114 |
block.layout().setAdditionalFormats(block_formats) |
| 115 |
doc.markContentsDirty(block.position(), block.length()) |
| 116 |
block = block.next() |
| 117 |
block_pos = 0 |
| 118 |
block_len = block.length() |
| 119 |
block_formats = [] |
| 120 |
|
| 121 |
block_count += 1 |
| 122 |
if block_count % 100 == 0: |
| 123 |
processEvents() |
| 124 |
|
| 125 |
|
| 126 |
if __name__ == "__main__": |
| 127 |
import sys |
| 128 |
app = QtWidgets.QApplication(sys.argv) |
| 129 |
|
| 130 |
python = QtWidgets.QPlainTextEdit() |
| 131 |
f = open('syntaxhighlighter.py', 'r') |
| 132 |
python.setPlainText(f.read()) |
| 133 |
f.close() |
| 134 |
|
| 135 |
python.setWindowTitle('python') |
| 136 |
python.show() |
| 137 |
highlight_document(python, 'syntaxhighlighter.py') |
| 138 |
|
| 139 |
sys.exit(app.exec_()) |
| 140 |
|
| 141 |
|
| 142 |
def format_for_ttype(ttype, format, style=None): |
| 143 |
if have_pygments() and ttype: |
| 144 |
if style is None: |
| 145 |
style = get_style_by_name("default") |
| 146 |
|
| 147 |
font = format.font() |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
while not style.styles_token(ttype) and ttype.parent: |
| 152 |
ttype = ttype.parent |
| 153 |
|
| 154 |
tstyle = style.style_for_token(ttype) |
| 155 |
if tstyle['color']: |
| 156 |
if isinstance(format, QtGui.QPainter): |
| 157 |
format.setPen (QtGui.QColor("#"+tstyle['color'])) |
| 158 |
else: |
| 159 |
format.setForeground (QtGui.QColor("#"+tstyle['color'])) |
| 160 |
if tstyle['bold']: font.setWeight(QtGui.QFont.Bold) |
| 161 |
if tstyle['italic']: font.setItalic (True) |
| 162 |
|
| 163 |
|
| 164 |
if tstyle['bgcolor']: format.setBackground (QtGui.QColor("#"+tstyle['bgcolor'])) |
| 165 |
|
| 166 |
|
| 167 |
return format |
| 168 |
|
| 169 |
class CachedTTypeFormater(object): |
| 170 |
def __init__(self, base_format): |
| 171 |
self.base_format = base_format |
| 172 |
self._cache = {} |
| 173 |
if have_pygments(): |
| 174 |
self.style = get_style_by_name("default") |
| 175 |
|
| 176 |
def format(self, ttype): |
| 177 |
if not have_pygments() or not ttype: |
| 178 |
return self.base_format |
| 179 |
if ttype in self._cache: |
| 180 |
format = self._cache[ttype] |
| 181 |
else: |
| 182 |
format = QtGui.QTextCharFormat(self.base_format) |
| 183 |
self._cache[ttype] = format |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
while not self.style.styles_token(ttype) and ttype.parent: |
| 188 |
ttype = ttype.parent |
| 189 |
self._cache[ttype] = format |
| 190 |
|
| 191 |
format_for_ttype(ttype, format, self.style) |
| 192 |
|
| 193 |
return format |
| 194 |
|
| 195 |
def split_tokens_at_lines(tokens): |
| 196 |
currentLine = [] |
| 197 |
for ttype, value in tokens: |
| 198 |
vsplit = value.splitlines(True) |
| 199 |
for v in vsplit: |
| 200 |
currentLine.append((ttype, v)) |
| 201 |
if v[-1:] in ('\n','\r'): |
| 202 |
yield currentLine |
| 203 |
currentLine = [] |
| 204 |
yield currentLine |