Skip to content

Commit e3fd467

Browse files
committed
fix syntax highlighting when line ends in slash
1 parent 790cdae commit e3fd467

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

Lib/idlelib/colorizer.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def any(name, alternates):
1717
def make_pat():
1818
kw = r"\b" + any("KEYWORD", keyword.kwlist) + r"\b"
1919
match_softkw = (
20+
r"(?<!\\\n)" + # last line doesn't end in slash
2021
r"^[ \t]*" + # at beginning of line + possible indentation
2122
r"(?P<MATCH_SOFTKW>match)\b" +
2223
r"(?![ \t]*(?:" + "|".join([ # not followed by ...
@@ -27,11 +28,13 @@ def make_pat():
2728
r"))"
2829
)
2930
case_default = (
31+
r"(?<!\\\n)" + # last line doesn't end in slash
3032
r"^[ \t]*" + # at beginning of line + possible indentation
3133
r"(?P<CASE_SOFTKW>case)" +
3234
r"[ \t]+(?P<CASE_DEFAULT_UNDERSCORE>_\b)"
3335
)
3436
case_softkw_and_pattern = (
37+
r"(?<!\\\n)" + # last line doesn't end in slash
3538
r"^[ \t]*" + # at beginning of line + possible indentation
3639
r"(?P<CASE_SOFTKW2>case)\b" +
3740
r"(?![ \t]*(?:" + "|".join([ # not followed by ...
@@ -45,19 +48,43 @@ def make_pat():
4548
builtinlist = [str(name) for name in dir(builtins)
4649
if not name.startswith('_') and
4750
name not in keyword.kwlist]
48-
builtin = r"([^.'\"\\#]\b|^)" + any("BUILTIN", builtinlist) + r"\b"
51+
builtin = (
52+
r"(?<!" + # make sure there isn't
53+
r"\\\n" + # a slash followed by a newline
54+
r")" +
55+
r"(?<!" + # make sure that there also isn't
56+
r"|".join([
57+
r"\.", # a dot or
58+
r" ", # a space
59+
]) +
60+
r")" +
61+
r"(" + # match any number of
62+
r"[ \t]*" + # spaces/tabs followed by
63+
r"(\\\\)*\\" + # an odd number of slashes followed by
64+
r"\n" + # a newline
65+
r")*" +
66+
r"(" + # also match
67+
r"|".join([ # either
68+
r"[ \t]+", # indentation or
69+
r"\b", # a word boundary
70+
]) +
71+
r")" +
72+
any("BUILTIN", builtinlist) + # followed by a builtin
73+
r"\b" # followed by another word boundary
74+
)
4975
comment = any("COMMENT", [r"#[^\n]*"])
5076
stringprefix = r"(?i:r|u|f|fr|rf|b|br|rb|t|rt|tr)?"
5177
sqstring = stringprefix + r"'[^'\\\n]*(\\.[^'\\\n]*)*'?"
5278
dqstring = stringprefix + r'"[^"\\\n]*(\\.[^"\\\n]*)*"?'
5379
sq3string = stringprefix + r"'''[^'\\]*((\\.|'(?!''))[^'\\]*)*(''')?"
5480
dq3string = stringprefix + r'"""[^"\\]*((\\.|"(?!""))[^"\\]*)*(""")?'
5581
string = any("STRING", [sq3string, dq3string, sqstring, dqstring])
82+
sync = any("SYNC", [r"(?<!\\)\n"]) # no sync if line ends with slash
5683
prog = re.compile("|".join([
5784
builtin, comment, string, kw,
5885
match_softkw, case_default,
5986
case_softkw_and_pattern,
60-
any("SYNC", [r"\n"]),
87+
sync,
6188
]),
6289
re.DOTALL | re.MULTILINE)
6390
return prog

Lib/idlelib/idle_test/test_colorizer.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ async def f(): await g()
5252
'''
5353
case _:'''
5454
"match x:"
55+
self. \\
56+
set
57+
f(self, \\
58+
set())
59+
x = match if match else \\
60+
match
5561
""")
5662

5763

@@ -404,6 +410,7 @@ def test_recolorize_main(self, mock_notify):
404410
('28.25', ('STRING',)), ('28.38', ('STRING',)),
405411
('30.0', ('STRING',)),
406412
('31.1', ('STRING',)),
413+
('33.4', ()), ('35.4', ("BUILTIN",)), ('37.8', ()),
407414
# SYNC at the end of every line.
408415
('1.55', ('SYNC',)), ('2.50', ('SYNC',)), ('3.34', ('SYNC',)),
409416
)
@@ -434,7 +441,11 @@ def test_recolorize_main(self, mock_notify):
434441
eq(text.tag_nextrange('STRING', '8.12'), ('8.14', '8.17'))
435442
eq(text.tag_nextrange('STRING', '8.17'), ('8.19', '8.26'))
436443
eq(text.tag_nextrange('SYNC', '8.0'), ('8.26', '9.0'))
437-
eq(text.tag_nextrange('SYNC', '31.0'), ('31.10', '33.0'))
444+
eq(text.tag_nextrange('SYNC', '31.0'), ('31.10', '32.0'))
445+
eq(text.tag_nextrange('SYNC', '32.0'), ('33.7', '34.0'))
446+
eq(text.tag_nextrange('SYNC', '34.0'), ('35.10', '36.0'))
447+
eq(text.tag_nextrange('SYNC', '36.0'), ('37.13', '39.0'))
448+
eq(text.tag_nextrange('SYNC', '39.0'), ())
438449

439450
def _assert_highlighting(self, source, tag_ranges):
440451
"""Check highlighting of a given piece of code.

0 commit comments

Comments
 (0)