2024-09-08 20:57:25 +08:00
|
|
|
from PyQt5.QtWidgets import QApplication, QTextEdit
|
2024-09-09 16:10:46 +08:00
|
|
|
from PyQt5.QtGui import QSyntaxHighlighter, QTextCharFormat, QFont, QFontMetrics, QColor
|
2024-09-08 20:57:25 +08:00
|
|
|
from PyQt5.QtCore import QRegularExpression, Qt
|
2024-09-09 16:10:46 +08:00
|
|
|
import app.env as env
|
2024-09-08 20:57:25 +08:00
|
|
|
class CodeEditor(QTextEdit):
|
2024-09-08 21:14:02 +08:00
|
|
|
def __init__(self, text:str=""):
|
2024-09-08 20:57:25 +08:00
|
|
|
super().__init__()
|
|
|
|
self.setFont(QFont("Courier New", 12))
|
2024-09-08 21:14:02 +08:00
|
|
|
fontMetrics = QFontMetrics(self.font())
|
2024-09-09 16:10:46 +08:00
|
|
|
tabWidth = 4 * fontMetrics.width(' ') # 计算tab的宽度
|
2024-09-08 21:14:02 +08:00
|
|
|
self.setTabStopWidth(tabWidth)
|
|
|
|
self.setPlainText(text)
|
2024-09-08 20:57:25 +08:00
|
|
|
self.highlighter = CodeHighlighter(self.document())
|
|
|
|
self.highlighter.rehighlight()
|
2024-09-09 16:10:46 +08:00
|
|
|
if env.config.theme== "dark":
|
|
|
|
# 设置深色模式的样式
|
|
|
|
self.setStyleSheet("""
|
|
|
|
background-color: #2b2b2b; /* 背景颜色 */
|
|
|
|
color: #ffffff; /* 文本颜色 */
|
|
|
|
selection-background-color: #4e9a06; /* 选中背景颜色 */
|
|
|
|
selection-color: #ffffff; /* 选中文本颜色 */
|
|
|
|
""")
|
2024-09-08 20:57:25 +08:00
|
|
|
|
|
|
|
def paste(self):
|
|
|
|
super().paste()
|
|
|
|
self.highlighter.rehighlight()
|
|
|
|
|
|
|
|
class CodeHighlighter(QSyntaxHighlighter):
|
|
|
|
def __init__(self, document):
|
|
|
|
super().__init__(document)
|
|
|
|
self.highlightingRules = []
|
|
|
|
|
|
|
|
# 定义 JSON 高亮规则
|
|
|
|
self.keyFormat = QTextCharFormat()
|
2024-09-09 16:10:46 +08:00
|
|
|
self.keyFormat.setForeground(QColor("#0000ff")) # 蓝色关键字
|
2024-09-08 20:57:25 +08:00
|
|
|
self.keyFormat.setFontWeight(QFont.Bold)
|
|
|
|
|
|
|
|
self.valueFormat = QTextCharFormat()
|
2024-09-09 16:10:46 +08:00
|
|
|
self.valueFormat.setForeground(QColor("#008000")) # 绿色字符串值
|
2024-09-08 20:57:25 +08:00
|
|
|
|
|
|
|
self.boolFormat = QTextCharFormat()
|
2024-09-09 16:10:46 +08:00
|
|
|
self.boolFormat.setForeground(QColor("#ff0000")) # 红色布尔值
|
2024-09-08 20:57:25 +08:00
|
|
|
|
|
|
|
self.numberFormat = QTextCharFormat()
|
2024-09-09 16:10:46 +08:00
|
|
|
self.numberFormat.setForeground(QColor("#008080")) # 青色数字
|
2024-09-08 20:57:25 +08:00
|
|
|
|
|
|
|
# 创建蓝色格式
|
|
|
|
blueFormat = QTextCharFormat()
|
2024-09-09 16:10:46 +08:00
|
|
|
blueFormat.setForeground(QColor("#0000ff"))
|
2024-09-08 20:57:25 +08:00
|
|
|
|
|
|
|
self.rules = [
|
|
|
|
(QRegularExpression(r'\b(true|false|null)\b'), self.boolFormat),
|
|
|
|
(QRegularExpression(r'\b(-?\d+\.?\d*)\b'), self.numberFormat),
|
|
|
|
(QRegularExpression(r'\{'), blueFormat),
|
|
|
|
(QRegularExpression(r'\}'), blueFormat),
|
|
|
|
(QRegularExpression(r'\['), blueFormat),
|
|
|
|
(QRegularExpression(r'\]'), blueFormat),
|
|
|
|
(QRegularExpression(r'\"(\\.|[^"\\])*\"'), self.valueFormat),
|
|
|
|
(QRegularExpression(r'\b(\w+)\s*:'), self.keyFormat)
|
|
|
|
]
|
|
|
|
|
|
|
|
def highlightBlock(self, text):
|
|
|
|
for pattern, format in self.rules:
|
|
|
|
expression = QRegularExpression(pattern)
|
|
|
|
matchIterator = expression.globalMatch(text)
|
|
|
|
while matchIterator.hasNext():
|
|
|
|
match = matchIterator.next()
|
|
|
|
self.setFormat(match.capturedStart(), match.capturedLength(), format)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import sys
|
|
|
|
app = QApplication(sys.argv)
|
|
|
|
editor = CodeEditor()
|
|
|
|
editor.show()
|
|
|
|
sys.exit(app.exec_())
|