-
Notifications
You must be signed in to change notification settings - Fork 1
/
qtcodeedit.py
237 lines (184 loc) · 7.47 KB
/
qtcodeedit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
from enum import Enum
from typing import NamedTuple
from pygments import lex
from pygments.lexers import get_all_lexers, get_lexer_by_name
from pygments.styles import get_all_styles, get_style_by_name
from pygments.token import Token
from qtpy.QtCore import QEvent, QObject, QRect, Qt, Signal
from qtpy.QtGui import (
QColor,
QFont,
QFontDatabase,
QKeyEvent,
QPainter,
QPalette,
QSyntaxHighlighter,
QTextCharFormat,
)
from qtpy.QtWidgets import QPlainTextEdit, QWidget
class Language(NamedTuple):
name: str
alias: str
class CodeEdit(QPlainTextEdit):
INDENT_SPACES_NUMBER = 4
class Indent(Enum):
WithSpaces = 0
WithTabs = 1
def __init__(self):
super().__init__()
self._indentation_mode = self.Indent.WithSpaces
font = QFontDatabase.systemFont(QFontDatabase.SystemFont.FixedFont)
font.setPointSize(14)
self.setFont(font)
self.highlighter = PygmentsSyntaxHighlighter(self.document())
self.highlighter.styleChanged.connect(self._updatePalette)
self._updatePalette()
self.lineNumberArea = LineNumberArea(self)
def keyPressEvent(self, e: QKeyEvent):
if self._indentation_mode == self.Indent.WithSpaces:
if e.key() == Qt.Key.Key_Tab:
self._indentWithSpaces()
return
if e.key() == Qt.Key.Key_Backtab:
self._unindentWithSpaces()
return
super().keyPressEvent(e)
def languages(self):
return (
Language(name, aliases[0])
for name, aliases, _, _ in get_all_lexers(plugins=False)
if aliases
)
def themes(self):
return get_all_styles()
def setLanguage(self, language: Language | str):
if isinstance(language, Language):
language = language.alias
self.highlighter.setLexer(language)
def setTheme(self, theme):
self.highlighter.setStyle(theme)
def setShowLineNumbers(self, show: bool):
self.lineNumberArea.setVisible(show)
def setIndentationMode(self, mode: Indent):
self._indentation_mode = mode
def _updatePalette(self):
self.setPalette(self.highlighter.stylePalette())
def _indentWithSpaces(self):
cursor = self.textCursor()
cursor.movePosition(cursor.MoveOperation.StartOfBlock)
cursor.insertText(" " * self.INDENT_SPACES_NUMBER)
def _unindentWithSpaces(self):
cursor = self.textCursor()
cursor.movePosition(cursor.MoveOperation.StartOfBlock)
text = cursor.block().text()
n_spaces = len(text) - len(text.lstrip(" "))
for _ in range(min(n_spaces, self.INDENT_SPACES_NUMBER)):
cursor.deleteChar()
class PygmentsSyntaxHighlighter(QSyntaxHighlighter):
DEFAULT_STYLE = "solarized-light"
DEFAULT_LEXER = "python"
styleChanged = Signal()
def __init__(self, document):
super().__init__(document)
self.lexer = get_lexer_by_name(self.DEFAULT_LEXER)
self.style = get_style_by_name(self.DEFAULT_STYLE)
self.format_map = self.createFormatMap(self.style)
def setStyle(self, style_name):
self.style = get_style_by_name(style_name)
self.format_map = self.createFormatMap(self.style)
self.rehighlight()
self.styleChanged.emit()
def setLexer(self, lexer_name):
self.lexer = get_lexer_by_name(lexer_name)
self.rehighlight()
def stylePalette(self) -> QPalette:
style_bg_color = self.style.background_color
style_for_text = self.style.style_for_token(Token.Text)
style_for_comment = self.style.style_for_token(Token.Comment)
palette = QPalette()
bg_color = QColor(f"{style_bg_color}")
text_color = QColor(f"#{style_for_text['color']}")
comment_color = QColor(f"#{style_for_comment['color']}")
palette.setColor(QPalette.ColorRole.Base, bg_color)
palette.setColor(QPalette.ColorRole.Text, text_color)
palette.setColor(QPalette.ColorRole.PlaceholderText, comment_color)
return palette
def highlightBlock(self, text):
"""Apply syntax highlighting to a block of text."""
index = 0
for token_type, token in lex(text, self.lexer):
length = len(token)
self.setFormat(index, length, self.format_map[token_type])
index += length
@staticmethod
def createFormatMap(style):
def to_fmt(token_style):
fmt = QTextCharFormat()
fmt.setForeground(QColor(f"#{token_style['color']}"))
if token_style["bold"]:
fmt.setFontWeight(QFont.Weight.Bold)
if token_style["italic"]:
fmt.setFontItalic(True)
return fmt
return {token_type: to_fmt(token_style) for token_type, token_style in style}
class LineNumberArea(QWidget):
MIN_DIGITS = 3
MARGIN_RIGHT = 3
def __init__(self, editor: CodeEdit):
super().__init__(editor)
self.editor = editor
self.editor.installEventFilter(self)
self.editor.updateRequest.connect(self._onEditorUpdateRequest)
self.editor.blockCountChanged.connect(self._updateWidth)
self._updateWidth()
def paintEvent(self, event):
painter = QPainter(self)
painter.fillRect(event.rect(), self.palette().base())
block = self.editor.firstVisibleBlock()
top = self._topOfBlock(block)
bottom = top + self._blockHeight(block)
while block.isValid() and top <= event.rect().bottom():
if block.isVisible() and bottom >= event.rect().top():
lineNumber = str(block.blockNumber() + 1)
self._paintNumber(painter, lineNumber, top)
block = block.next()
top = bottom
bottom = top + self._blockHeight(block)
def showEvent(self, event):
super().showEvent(event)
self._updateWidth()
def hideEvent(self, event):
super().hideEvent(event)
self._updateWidth()
def eventFilter(self, watched: QObject, event: QEvent):
if watched is not self.editor and event.type() != QEvent.Type.Resize:
return False
cr = self.editor.contentsRect()
self.setGeometry(cr.left(), cr.top(), self.width(), cr.height())
return False
def _updateWidth(self, _=None):
if not self.isVisible():
self.editor.setViewportMargins(0, 0, 0, 0)
return
lineCount = self.editor.blockCount()
digits = max(self.MIN_DIGITS, len(str(lineCount)))
charWidth = self.fontMetrics().horizontalAdvance("9")
space = 3 + self.MARGIN_RIGHT + charWidth * digits
self.setFixedWidth(space)
self.editor.setViewportMargins(space, 0, 0, 0)
def _onEditorUpdateRequest(self, rect, dy):
if dy:
self.scroll(0, dy)
else:
self.update(0, rect.y(), self.width(), rect.height())
def _topOfBlock(self, block):
blockGeom = self.editor.blockBoundingGeometry(block)
return int(round(blockGeom.translated(self.editor.contentOffset()).top()))
def _blockHeight(self, block):
return int(round(self.editor.blockBoundingRect(block).height()))
def _paintNumber(self, painter, number, top):
painter.setPen(self.palette().placeholderText().color())
textRect = QRect(
0, top, self.width() - self.MARGIN_RIGHT, self.fontMetrics().height()
)
painter.drawText(textRect, Qt.AlignmentFlag.AlignRight, number)