forked from yzt000000/device_config_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_editor.py
More file actions
129 lines (101 loc) · 4.78 KB
/
code_editor.py
File metadata and controls
129 lines (101 loc) · 4.78 KB
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
from PyQt5.QtWidgets import QPlainTextEdit, QWidget, QTextEdit
from PyQt5.QtCore import Qt, QRect, QSize
from PyQt5.QtGui import QPainter, QColor, QFont, QTextCursor, QSyntaxHighlighter, QTextCharFormat, QTextFormat
class LineNumberArea(QWidget):
def __init__(self, editor):
super().__init__(editor)
self.editor = editor
def sizeHint(self):
return QSize(self.editor.line_number_area_width(), 0)
def paintEvent(self, event):
self.editor.line_number_area_paint_event(event)
class CodeEditor(QPlainTextEdit):
def __init__(self, *args):
super(CodeEditor, self).__init__(*args)
self.line_number_area = LineNumberArea(self)
self.blockCountChanged.connect(self.update_line_number_area_width)
self.updateRequest.connect(self.update_line_number_area)
self.cursorPositionChanged.connect(self.highlight_current_line)
self.update_line_number_area_width(0)
self.highlight_current_line()
self.setLineWrapMode(QPlainTextEdit.NoWrap)
self.setTabStopDistance(self.fontMetrics().horizontalAdvance(' ') * 4)
self.setReadOnly(False)
def line_number_area_width(self):
digits = 1
max_num = max(1, self.blockCount())
while max_num >= 10:
max_num //= 10
digits += 1
space = 3 + self.fontMetrics().horizontalAdvance('9') * digits
return space
def update_line_number_area_width(self, _):
self.setViewportMargins(self.line_number_area_width(), 0, 0, 0)
def update_line_number_area(self, rect, dy):
if dy:
self.line_number_area.scroll(0, dy)
else:
self.line_number_area.update(0, rect.y(), self.line_number_area.width(), rect.height())
if rect.contains(self.viewport().rect()):
self.update_line_number_area_width(0)
def resizeEvent(self, event):
super(CodeEditor, self).resizeEvent(event)
cr = self.contentsRect()
self.line_number_area.setGeometry(QRect(cr.left(), cr.top(), self.line_number_area_width(), cr.height()))
def line_number_area_paint_event(self, event):
painter = QPainter(self.line_number_area)
painter.fillRect(event.rect(), Qt.lightGray)
block = self.firstVisibleBlock()
block_number = block.blockNumber()
top = int(self.blockBoundingGeometry(block).translated(self.contentOffset()).top())
bottom = top + int(self.blockBoundingRect(block).height())
while block.isValid() and top <= event.rect().bottom():
if block.isVisible() and bottom >= event.rect().top():
number = str(block_number + 1)
painter.setPen(Qt.black)
painter.drawText(0, top, self.line_number_area.width(), self.fontMetrics().height(), Qt.AlignRight, number)
block = block.next()
top = bottom
bottom = top + int(self.blockBoundingRect(block).height())
block_number += 1
def highlight_current_line(self):
extra_selections = []
if not self.isReadOnly():
selection = QTextEdit.ExtraSelection()
line_color = QColor(Qt.yellow).lighter(160)
selection.format.setBackground(line_color)
selection.format.setProperty(QTextFormat.FullWidthSelection, True)
selection.cursor = self.textCursor()
selection.cursor.clearSelection()
extra_selections.append(selection)
self.setExtraSelections(extra_selections)
def keyPressEvent(self, event):
if event.key() in [Qt.Key_Return, Qt.Key_Enter]:
self.handle_return()
elif event.key() == Qt.Key_Tab:
self.handle_tab()
elif event.key() == Qt.Key_Backspace:
self.handle_backspace()
else:
super(CodeEditor, self).keyPressEvent(event)
def handle_return(self):
cursor = self.textCursor()
current_line = cursor.block().text()
indentation = self._get_indentation(current_line)
# Check if the current line ends with a colon
if current_line.strip().endswith(':'):
indentation += ' ' # Add an extra level of indentation
cursor.insertText('\n' + indentation)
def handle_tab(self):
cursor = self.textCursor()
cursor.insertText(' ') # Insert 4 spaces for a tab
def handle_backspace(self):
cursor = self.textCursor()
current_line = cursor.block().text()
if current_line.endswith(' '): # If line ends with 4 spaces
for _ in range(4): # Remove all 4 spaces at once
cursor.deletePreviousChar()
else:
cursor.deletePreviousChar() # Normal backspace behavior
def _get_indentation(self, line):
return line[:len(line) - len(line.lstrip())]