Skip to content

Commit b54ae4c

Browse files
gh-103089: Colorize only the beginning of very long lines in IDLE (#157647)
Adding a tag to a Tk text line takes time proportional to the number of tags already in the line, so a line with hundreds of thousands of tokens took hours to colorize. Now only the first 2000 characters of a line are colorized, tag positions are relative to the line start, and tags are added from the end to the start.
1 parent a9d42dc commit b54ae4c

3 files changed

Lines changed: 46 additions & 4 deletions

File tree

‎Lib/idlelib/colorizer.py‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
DEBUG = False
1010

11+
# Adding a tag to a line takes time proportional to the number of tags
12+
# already in the line, so only the beginning of a line is colorized;
13+
# the rest is usually not visible anyway (gh-103089).
14+
MAX_COLORIZED_LINE = 2000
15+
1116

1217
def any(name, alternates):
1318
"Return a named group pattern matching list of alternates."
@@ -350,16 +355,39 @@ def _add_tags_in_section(self, chars, head):
350355
`chars` is a string with the text to parse and to which
351356
highlighting is to be applied.
352357
353-
`head` is the index in the text widget where the text is found.
358+
`head` is the index in the text widget where the text is found.
354359
"""
355-
for m in self.prog.finditer(chars):
360+
# Positions are relative to the start of the current line, so that
361+
# Tk does not resolve them through the previous lines.
362+
line = int(head.split('.')[0])
363+
line_start = 0 # Offset of the current line in chars.
364+
tags = []
365+
pos = 0
366+
while True:
367+
m = self.prog.search(chars, pos)
368+
if m is None:
369+
break
356370
for name, matched_text in matched_named_groups(m):
357371
a, b = m.span(name)
358-
self._add_tag(a, b, head, name)
372+
tags.append((a - line_start, b - line_start, head, name))
359373
if matched_text in ("def", "class"):
360374
if m1 := self.idprog.match(chars, b):
361375
a, b = m1.span(1)
362-
self._add_tag(a, b, head, "DEFINITION")
376+
tags.append((a - line_start, b - line_start,
377+
head, "DEFINITION"))
378+
pos = m.end()
379+
if '\n' in m[0]:
380+
line += m[0].count('\n')
381+
line_start = m.start() + m[0].rindex('\n') + 1
382+
head = f"{line}.0"
383+
elif pos - line_start >= MAX_COLORIZED_LINE:
384+
# The rest of a long line is not colorized.
385+
pos = chars.find('\n', pos)
386+
if pos < 0:
387+
break
388+
# Adding a tag is faster if there are no tags after it.
389+
for args in reversed(tags):
390+
self._add_tag(*args)
363391

364392
def removecolors(self):
365393
"Remove all colorizing tags."

‎Lib/idlelib/idle_test/test_colorizer.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,18 @@ def test_long_multiline_string(self):
573573
e"""
574574
''')
575575
self._assert_highlighting(source, {'STRING': [('1.0', '5.4')]})
576+
source = '"""a\nb""" + str\n'
577+
self._assert_highlighting(source, {'STRING': [('1.0', '2.4')],
578+
'BUILTIN': [('2.7', '2.10')]})
579+
580+
def test_long_line(self):
581+
# gh-103089: only the first MAX_COLORIZED_LINE characters of a line
582+
# are colorized.
583+
n = colorizer.MAX_COLORIZED_LINE
584+
source = f"pass\n{'x' * (n - 3)}'a', 'b'\n'c'\n"
585+
self._assert_highlighting(source, {'KEYWORD': [('1.0', '1.4')],
586+
'STRING': [(f'2.{n-3}', f'2.{n}'),
587+
('3.0', '3.3')]})
576588

577589
@run_in_tk_mainloop(delay=50)
578590
def test_incremental_editing(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
IDLE no longer hangs when opening a file with very long lines.
2+
Only the first 2,000 characters of a line are colorized.

0 commit comments

Comments
 (0)