From 254ced07d8225315fdccb576c013e9350fa783f6 Mon Sep 17 00:00:00 2001 From: rozyczko Date: Fri, 21 Jul 2023 11:37:41 +0200 Subject: [PATCH 1/2] moving highlighting to QRegularExpression syntax --- src/sas/qtgui/Utilities/PythonSyntax.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/sas/qtgui/Utilities/PythonSyntax.py b/src/sas/qtgui/Utilities/PythonSyntax.py index 2a5d88e39a..2d5ce00bda 100644 --- a/src/sas/qtgui/Utilities/PythonSyntax.py +++ b/src/sas/qtgui/Utilities/PythonSyntax.py @@ -131,14 +131,15 @@ def highlightBlock(self, text): """ # Do other syntax formatting for expression, nth, format in self.rules: - index = expression.indexIn(text, 0) + match = expression.match(text) + index = match.capturedStart(0) while index >= 0: # We actually want the index of the nth match - index = expression.pos(nth) - length = len(expression.cap(nth)) + index = match.capturedStart(nth) + length = match.capturedLength(nth) self.setFormat(index, length, format) - index = expression.indexIn(text, index + length) + index = match.capturedStart(index+length) self.setCurrentBlockState(0) @@ -161,17 +162,20 @@ def match_multiline(self, text, delimiter, in_state, style): add = 0 # Otherwise, look for the delimiter on this line else: - start = delimiter.indexIn(text) + match = delimiter.match(text) + start = match.capturedStart(0) + end = match.capturedEnd(0) # Move past this match - add = delimiter.matchedLength() + add = end - start + 1 # 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) + match = delimiter.match(text) + end = match.capturedEnd(0) # Ending delimiter on this line? if end >= add: - length = end - start + add + delimiter.matchedLength() + length = end - start + add self.setCurrentBlockState(0) # No; multi-line string else: @@ -180,7 +184,7 @@ def match_multiline(self, text, delimiter, in_state, style): # Apply formatting self.setFormat(start, length, style) # Look for the next match - start = delimiter.indexIn(text, start + length) + start = match.capturedStart(start + length) # Return True if still inside a multi-line string, False otherwise if self.currentBlockState() == in_state: From b73da9f8387397c5d66beb9bd75c100c05e0295c Mon Sep 17 00:00:00 2001 From: rozyczko Date: Mon, 24 Jul 2023 07:56:36 +0200 Subject: [PATCH 2/2] fixes for multiline comments with raw strings --- src/sas/qtgui/Utilities/PythonSyntax.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/sas/qtgui/Utilities/PythonSyntax.py b/src/sas/qtgui/Utilities/PythonSyntax.py index 2d5ce00bda..b863d4d993 100644 --- a/src/sas/qtgui/Utilities/PythonSyntax.py +++ b/src/sas/qtgui/Utilities/PythonSyntax.py @@ -81,6 +81,8 @@ def __init__(self, document, is_python=True): # syntax highlighting from this point onward self.tri_single = (QRegularExpression("'''"), 1, STYLES['string2']) self.tri_double = (QRegularExpression('"""'), 2, STYLES['string2']) + self.tri_single_raw = (QRegularExpression(r'r\'\'\''), 3, STYLES['string2']) + self.tri_double_raw = (QRegularExpression(r'r\"\"\"'), 4, STYLES['string2']) rules = [] @@ -148,6 +150,9 @@ def highlightBlock(self, text): if not in_multiline: in_multiline = self.match_multiline(text, *self.tri_double) + in_multiline = in_multiline or self.match_multiline(text, *self.tri_single_raw) + if not in_multiline: + in_multiline = self.match_multiline(text, *self.tri_double_raw) def match_multiline(self, text, delimiter, in_state, style): """Do highlighting of multi-line strings. ``delimiter`` should be a @@ -170,8 +175,8 @@ def match_multiline(self, text, delimiter, in_state, style): # As long as there's a delimiter match on this line... while start >= 0: - # Look for the ending delimiter - match = delimiter.match(text) + # Look for the ending delimiter starting from where the last match ended + match = delimiter.match(text, start + add) end = match.capturedEnd(0) # Ending delimiter on this line? if end >= add: