Skip to content

gh-140347: WIP spike: color IDLE with the _pyrepl tokenizer#152756

Draft
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:gh-140347-tokenizer-colorizer
Draft

gh-140347: WIP spike: color IDLE with the _pyrepl tokenizer#152756
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:gh-140347-tokenizer-colorizer

Conversation

@serhiy-storchaka

Copy link
Copy Markdown
Member

Draft / proof-of-concept for gh-140347 — not for merge as-is.

This is the Stage-1 spike from my analysis on the issue: it gives the discussion working code to react to.

What it does

Replace the regexp in ColorDelegator._add_tags_in_section with _pyrepl.utils.gen_colors (the tokenizer-based colorizer that powers the PyREPL). IDLE's incremental engine is untouched — notify_rangeTODOrecolorize_main walking SYNC boundaries still drives everything; only the per-section coloring changes. The regexp is kept solely to mark SYNC boundaries.

gen_colors' categories map cleanly onto IDLE's tags (pyrepl is a superset — it colors builtins too); only number/op are extra and left uncolored. On valid code this is strictly more correct: match/case/type soft keywords and f-strings the regexp can't handle.

Why it's a draft

Three test_colorizer tests fail by design: gen_colors stops at the first TokenError, so one invalid token (e.g. an illegal string prefix) blanks the rest of a section, where the regexp used to continue. IDLE amplifies this because recolorize_main accumulates large SYNC-bounded sections.

The real fix is TokenError recovery in the shared gen_colors (restart tokenizing after a bad line), so PyREPL and IDLE both benefit rather than IDLE reintroducing regexp logic. That, plus the "un-tokenizable line" UX call, is the open question on the issue.

🤖 Generated with Claude Code

Proof of concept, not for merge.  Use _pyrepl.utils.gen_colors in
ColorDelegator._add_tags_in_section instead of the regexp, keeping the
regexp only for SYNC boundaries.

Correct on valid code, but 3 colorizer tests fail by design: gen_colors
stops at the first TokenError, blanking the rest of a section.  See the
issue.
Comment thread Lib/idlelib/colorizer.py
import re
import time

from _pyrepl.utils import gen_colors

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import has to be optional, as Steve did for all the others some time ago. That's why I proposed moving it to _colorize.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will update. This is just a draft for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants