first try to motion to PyQt6
This commit is contained in:
@ -1,7 +1,10 @@
|
||||
# CodeEditor based on QT example, Python syntax highlighter found on Python site
|
||||
import enum
|
||||
import typing
|
||||
from ast import parse
|
||||
|
||||
from PyQt5.QtGui import QTextCharFormat
|
||||
|
||||
from ..Qt import QtGui, QtCore, QtWidgets
|
||||
|
||||
|
||||
@ -14,7 +17,7 @@ def _make_textformats(color, style=''):
|
||||
_format = QtGui.QTextCharFormat()
|
||||
_format.setForeground(_color)
|
||||
if 'bold' in style:
|
||||
_format.setFontWeight(QtGui.QFont.Bold)
|
||||
_format.setFontWeight(QtGui.QFont.Weight.Bold)
|
||||
if 'italic' in style:
|
||||
_format.setFontItalic(True)
|
||||
|
||||
@ -22,17 +25,17 @@ def _make_textformats(color, style=''):
|
||||
|
||||
|
||||
# Syntax styles that can be shared by all languages
|
||||
STYLES = {
|
||||
'keyword': _make_textformats('blue'),
|
||||
'operator': _make_textformats('black'),
|
||||
'brace': _make_textformats('black'),
|
||||
'defclass': _make_textformats('black', 'bold'),
|
||||
'string': _make_textformats('darkGreen'),
|
||||
'comment': _make_textformats('gray', 'italic'),
|
||||
'self': _make_textformats('brown', 'italic'),
|
||||
'property': _make_textformats('brown'),
|
||||
'numbers': _make_textformats('darkRed'),
|
||||
}
|
||||
class CodeStyle(enum.Enum):
|
||||
KEYWORD = _make_textformats('blue')
|
||||
OPERATOR = _make_textformats('black')
|
||||
BRACE = _make_textformats('black')
|
||||
CLASS_DEFINE = _make_textformats('black', 'bold')
|
||||
STRING = _make_textformats('darkGreen')
|
||||
COMMENT = _make_textformats('gray', 'italic')
|
||||
SELF = _make_textformats('brown', 'italic')
|
||||
PROPERTY = _make_textformats('brown')
|
||||
NUMBER = _make_textformats('darkRed')
|
||||
|
||||
|
||||
|
||||
class PythonHighlighter(QtGui.QSyntaxHighlighter):
|
||||
@ -55,43 +58,43 @@ class PythonHighlighter(QtGui.QSyntaxHighlighter):
|
||||
# Multi-line strings (expression, flag, style)
|
||||
# FIXME: The triple-quotes in these two lines will mess up the
|
||||
# syntax highlighting from this point onward
|
||||
self.tri_single = (QtCore.QRegExp("r'{3}"), 1, STYLES['string'])
|
||||
self.tri_double = (QtCore.QRegExp('r?"{3}'), 2, STYLES['string'])
|
||||
self.tri_single = (QtCore.QRegularExpression("r'{3}"), 1, CodeStyle.STRING.value)
|
||||
self.tri_double = (QtCore.QRegularExpression('r?"{3}'), 2, CodeStyle.STRING.value)
|
||||
|
||||
rules = []
|
||||
|
||||
# Keyword, operator, and brace rules
|
||||
rules += [(rf'\b{w}\b', 0, STYLES['keyword']) for w in PythonHighlighter.keywords]
|
||||
rules += [(rf'\b{w}\b', 0, CodeStyle.KEYWORD.value) for w in PythonHighlighter.keywords]
|
||||
|
||||
# Other rules
|
||||
rules += [
|
||||
# 'self'
|
||||
(r'\bself\b', 0, STYLES['self']),
|
||||
(r'\bself\b', 0, CodeStyle.SELF.value),
|
||||
|
||||
# 'def' followed by an identifier
|
||||
(r'\bdef\b\s*(\w+)', 1, STYLES['defclass']),
|
||||
(r'\bdef\b\s*(\w+)', 1, CodeStyle.CLASS_DEFINE.value),
|
||||
# 'class' followed by an identifier
|
||||
(r'\bclass\b\s*(\w+)', 1, STYLES['defclass']),
|
||||
(r'\bclass\b\s*(\w+)', 1, CodeStyle.CLASS_DEFINE.value),
|
||||
|
||||
# decorator @ followed by a word
|
||||
(r'\s*@(\w+)\s*', 0, STYLES['property']),
|
||||
(r'\s*@(\w+)\s*', 0, CodeStyle.PROPERTY.value),
|
||||
|
||||
# Numeric literals
|
||||
(r'\b[+-]?\d+[lL]?\b', 0, STYLES['numbers']),
|
||||
(r'\b[+-]?0[xX][\dA-Fa-f]+[lL]?\b', 0, STYLES['numbers']),
|
||||
(r'\b[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\b', 0, STYLES['numbers']),
|
||||
(r'\b[+-]?\d+[lL]?\b', 0, CodeStyle.NUMBER.value),
|
||||
(r'\b[+-]?0[xX][\dA-Fa-f]+[lL]?\b', 0, CodeStyle.NUMBER.value),
|
||||
(r'\b[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\b', 0, CodeStyle.NUMBER.value),
|
||||
|
||||
# Double-quoted string, possibly containing escape sequences
|
||||
(r'[rf]?"[^"\\]*(\\.[^"\\]*)*"', 0, STYLES['string']),
|
||||
(r'[rf]?"[^"\\]*(\\.[^"\\]*)*"', 0, CodeStyle.STRING.value),
|
||||
# Single-quoted string, possibly containing escape sequences
|
||||
(r"[rf]?'[^'\\]*(\\.[^'\\]*)*'", 0, STYLES['string']),
|
||||
(r"[rf]?'[^'\\]*(\\.[^'\\]*)*'", 0, CodeStyle.STRING.value),
|
||||
|
||||
# From '#' until a newline
|
||||
(r'#[^\n]*', 0, STYLES['comment']),
|
||||
(r'#[^\n]*', 0, CodeStyle.COMMENT.value),
|
||||
]
|
||||
|
||||
# Build a QRegExp for each pattern
|
||||
self.rules = [(QtCore.QRegExp(pat), index, fmt) for (pat, index, fmt) in rules]
|
||||
self.rules = [(QtCore.QRegularExpression(pat), index, fmt) for (pat, index, fmt) in rules]
|
||||
|
||||
def highlightBlock(self, text):
|
||||
"""
|
||||
@ -99,14 +102,12 @@ class PythonHighlighter(QtGui.QSyntaxHighlighter):
|
||||
"""
|
||||
# Do other syntax formatting
|
||||
for expression, nth, rule in self.rules:
|
||||
index = expression.indexIn(text, 0)
|
||||
index = expression.globalMatch(text)
|
||||
|
||||
while index >= 0:
|
||||
# We actually want the index of the nth match
|
||||
index = expression.pos(nth)
|
||||
length = len(expression.cap(nth))
|
||||
self.setFormat(index, length, rule)
|
||||
index = expression.indexIn(text, index + length)
|
||||
while index.hasNext():
|
||||
match = index.next()
|
||||
|
||||
self.setFormat(match.capturedStart(nth), match.capturedLength(nth), rule)
|
||||
|
||||
self.setCurrentBlockState(0)
|
||||
|
||||
@ -115,7 +116,7 @@ class PythonHighlighter(QtGui.QSyntaxHighlighter):
|
||||
if not in_multiline:
|
||||
in_multiline = self.match_multiline(text, *self.tri_double)
|
||||
|
||||
def match_multiline(self, text, delimiter, in_state, style):
|
||||
def match_multiline(self, text: str, delimiter: QtCore.QRegularExpression, in_state: int, style: QtGui.QTextCharFormat):
|
||||
"""
|
||||
Highlighting of multi-line strings. ``delimiter`` should be a
|
||||
``QRegExp`` for triple-single-quotes or triple-double-quotes, and
|
||||
@ -124,40 +125,42 @@ class PythonHighlighter(QtGui.QSyntaxHighlighter):
|
||||
inside a multi-line string when this function is finished.
|
||||
"""
|
||||
# If inside triple-single quotes, start at 0
|
||||
if self.previousBlockState() == in_state:
|
||||
start = 0
|
||||
add = 0
|
||||
# Otherwise, look for the delimiter on this line
|
||||
else:
|
||||
start = delimiter.indexIn(text)
|
||||
# Move past this match
|
||||
add = delimiter.matchedLength()
|
||||
|
||||
# As long as there's a delimiter match on this line...
|
||||
while start >= 0:
|
||||
# Look for the ending delimiter
|
||||
end = delimiter.indexIn(text, start + add)
|
||||
# Ending delimiter on this line?
|
||||
if end >= add:
|
||||
length = end - start + add + delimiter.matchedLength()
|
||||
self.setCurrentBlockState(0)
|
||||
# No; multi-line string
|
||||
else:
|
||||
self.setCurrentBlockState(in_state)
|
||||
try:
|
||||
length = text.length() - start + add
|
||||
except AttributeError:
|
||||
length = len(text) - start + add
|
||||
# Apply formatting
|
||||
self.setFormat(start, length, style)
|
||||
# Look for the next match
|
||||
start = delimiter.indexIn(text, start + length)
|
||||
|
||||
# Return True if still inside a multi-line string, False otherwise
|
||||
if self.currentBlockState() == in_state:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
# TODO
|
||||
return False
|
||||
# if self.previousBlockState() == in_state:
|
||||
# start = 0
|
||||
# add = 0
|
||||
# # Otherwise, look for the delimiter on this line
|
||||
# else:
|
||||
# start = delimiter.match(text).
|
||||
# # Move past this match
|
||||
# add = delimiter.matchedLength()
|
||||
#
|
||||
# # As long as there's a delimiter match on this line...
|
||||
# while start >= 0:
|
||||
# # Look for the ending delimiter
|
||||
# end = delimiter.indexIn(text, start + add)
|
||||
# # Ending delimiter on this line?
|
||||
# if end >= add:
|
||||
# length = end - start + add + delimiter.matchedLength()
|
||||
# self.setCurrentBlockState(0)
|
||||
# # No; multi-line string
|
||||
# else:
|
||||
# self.setCurrentBlockState(in_state)
|
||||
# try:
|
||||
# length = text.length() - start + add
|
||||
# except AttributeError:
|
||||
# length = len(text) - start + add
|
||||
# # Apply formatting
|
||||
# self.setFormat(start, length, style)
|
||||
# # Look for the next match
|
||||
# start = delimiter.indexIn(text, start + length)
|
||||
#
|
||||
# # Return True if still inside a multi-line string, False otherwise
|
||||
# if self.currentBlockState() == in_state:
|
||||
# return True
|
||||
# else:
|
||||
# return False
|
||||
|
||||
|
||||
class LineNumbers(QtWidgets.QWidget):
|
||||
@ -195,19 +198,18 @@ class CodeEditor(QtWidgets.QPlainTextEdit):
|
||||
else:
|
||||
super().keyPressEvent(evt)
|
||||
|
||||
@property
|
||||
def width_linenumber(self):
|
||||
digits = 1
|
||||
count = max(1, self.blockCount())
|
||||
while count >= 10:
|
||||
count /= 10
|
||||
digits += 1
|
||||
space = 6 + self.fontMetrics().width('9') * digits
|
||||
space = 6 + self.fontMetrics().horizontalAdvance('9') * digits
|
||||
|
||||
return space
|
||||
|
||||
def update_width_linenumber(self, _):
|
||||
self.setViewportMargins(self.width_linenumber, 0, 0, 0)
|
||||
self.setViewportMargins(self.width_linenumber(), 0, 0, 0)
|
||||
|
||||
def update_current_area(self, rect, dy):
|
||||
if dy:
|
||||
@ -221,7 +223,7 @@ class CodeEditor(QtWidgets.QPlainTextEdit):
|
||||
super().resizeEvent(evt)
|
||||
|
||||
cr = self.contentsRect()
|
||||
self.current_linenumber.setGeometry(QtCore.QRect(cr.left(), cr.top(), self.width_linenumber, cr.height()))
|
||||
self.current_linenumber.setGeometry(QtCore.QRect(cr.left(), cr.top(), self.width_linenumber(), cr.height()))
|
||||
|
||||
def paintevent_linenumber(self, evt):
|
||||
painter = QtGui.QPainter(self.current_linenumber)
|
||||
@ -255,7 +257,7 @@ class CodeEditor(QtWidgets.QPlainTextEdit):
|
||||
line_color = QtGui.QColor(QtCore.Qt.GlobalColor.yellow).lighter(180)
|
||||
|
||||
selection.format.setBackground(line_color)
|
||||
selection.format.setProperty(QtGui.QTextFormat.FullWidthSelection, True)
|
||||
selection.format.setProperty(QtGui.QTextFormat.Property.FullWidthSelection, True)
|
||||
selection.cursor = self.textCursor()
|
||||
selection.cursor.clearSelection()
|
||||
extra_selections.append(selection)
|
||||
|
Reference in New Issue
Block a user