Skip to content

Commit 9b568a2

Browse files
[3.14] gh-103089: Colorize only the beginning of very long lines in IDLE (GH-157647) (#158022)
gh-103089: Colorize only the beginning of very long lines in IDLE (GH-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. (cherry picked from commit b54ae4c) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 824b67e commit 9b568a2

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."
@@ -344,16 +349,39 @@ def _add_tags_in_section(self, chars, head):
344349
`chars` is a string with the text to parse and to which
345350
highlighting is to be applied.
346351
347-
`head` is the index in the text widget where the text is found.
352+
`head` is the index in the text widget where the text is found.
348353
"""
349-
for m in self.prog.finditer(chars):
354+
# Positions are relative to the start of the current line, so that
355+
# Tk does not resolve them through the previous lines.
356+
line = int(head.split('.')[0])
357+
line_start = 0 # Offset of the current line in chars.
358+
tags = []
359+
pos = 0
360+
while True:
361+
m = self.prog.search(chars, pos)
362+
if m is None:
363+
break
350364
for name, matched_text in matched_named_groups(m):
351365
a, b = m.span(name)
352-
self._add_tag(a, b, head, name)
366+
tags.append((a - line_start, b - line_start, head, name))
353367
if matched_text in ("def", "class"):
354368
if m1 := self.idprog.match(chars, b):
355369
a, b = m1.span(1)
356-
self._add_tag(a, b, head, "DEFINITION")
370+
tags.append((a - line_start, b - line_start,
371+
head, "DEFINITION"))
372+
pos = m.end()
373+
if '\n' in m[0]:
374+
line += m[0].count('\n')
375+
line_start = m.start() + m[0].rindex('\n') + 1
376+
head = f"{line}.0"
377+
elif pos - line_start >= MAX_COLORIZED_LINE:
378+
# The rest of a long line is not colorized.
379+
pos = chars.find('\n', pos)
380+
if pos < 0:
381+
break
382+
# Adding a tag is faster if there are no tags after it.
383+
for args in reversed(tags):
384+
self._add_tag(*args)
357385

358386
def removecolors(self):
359387
"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
@@ -555,6 +555,18 @@ def test_long_multiline_string(self):
555555
e"""
556556
''')
557557
self._assert_highlighting(source, {'STRING': [('1.0', '5.4')]})
558+
source = '"""a\nb""" + str\n'
559+
self._assert_highlighting(source, {'STRING': [('1.0', '2.4')],
560+
'BUILTIN': [('2.7', '2.10')]})
561+
562+
def test_long_line(self):
563+
# gh-103089: only the first MAX_COLORIZED_LINE characters of a line
564+
# are colorized.
565+
n = colorizer.MAX_COLORIZED_LINE
566+
source = f"pass\n{'x' * (n - 3)}'a', 'b'\n'c'\n"
567+
self._assert_highlighting(source, {'KEYWORD': [('1.0', '1.4')],
568+
'STRING': [(f'2.{n-3}', f'2.{n}'),
569+
('3.0', '3.3')]})
558570

559571
@run_in_tk_mainloop(delay=50)
560572
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)