Skip to content

Commit

Permalink
Merge pull request #2562 from SasView/2555-syntax-highlighting-is-thr…
Browse files Browse the repository at this point in the history
…owing-errors-in-pyside

Syntax highlighting in Pyside6
  • Loading branch information
butlerpd authored Jul 25, 2023
2 parents 37e7d1c + b73da9f commit c92714b
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/sas/qtgui/Utilities/PythonSyntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand Down Expand Up @@ -131,14 +133,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)

Expand All @@ -147,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
Expand All @@ -161,17 +167,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)
# 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:
length = end - start + add + delimiter.matchedLength()
length = end - start + add
self.setCurrentBlockState(0)
# No; multi-line string
else:
Expand All @@ -180,7 +189,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:
Expand Down

0 comments on commit c92714b

Please # to comment.