Skip to content

Add support for syntax highlighting in PyREPL #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

ambv
Copy link
Owner

@ambv ambv commented Mar 20, 2025

No description provided.

@ambv ambv force-pushed the pyrepl-syntax-highlighting branch from 627056f to aebc35f Compare March 20, 2025 21:46
@ambv ambv force-pushed the pyrepl-sh-refactor-calc-screen branch from 4ab5a7b to 31c48b2 Compare March 20, 2025 21:46
for c in buffer:
while colors and colors[0].span.end < start_index:
# move past irrelevant spans
colors.pop(0)

Choose a reason for hiding this comment

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

I know this list is not big enough that it matters but it would recommend modifying this to figure out the cutting index and then slicing once

pre_color = ""
post_color = ""

if colors and colors[0].span.start < i and colors[0].span.end > i:

Choose a reason for hiding this comment

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

This approach assumes the next call to disp_str() will properly handle the color continuation. If any buffer modification happens between calls, or if the next call uses different parameters, you'll get incorrect highlighting no?

Maybe this is a no problem but I would recommend to either add some defensive check or an assert

SYNC"]

# Then in the next call, verify consistency
if pending_color_span and colors and colors[0].span == pending_color_span.span:
    # Spans match, continue highlighting
    pre_color = TAG_TO_ANSI[colors[0].tag]
else:
    …

post_color = TAG_TO_ANSI["SYNC"]
colors.pop(0)

chars[-1] = pre_color + chars[-1] + post_color

Choose a reason for hiding this comment

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

Not sure if this matters but this being in a tight loop may be a performance problem

@@ -355,3 +362,60 @@ def test_setpos_from_xy_for_non_printing_char(self):
reader, _ = handle_all_events(events)
reader.setpos_from_xy(8, 0)
self.assertEqual(reader.pos, 7)

def test_syntax_highlighting_basic(self):

Choose a reason for hiding this comment

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

I would recommend adding some extra test for edge cases and failure modes such as incomplete code blocks, syntax errors and other known failure conditions so we know how this will behave

Copy link

@pablogsal pablogsal left a comment

Choose a reason for hiding this comment

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

In general this LGTM so I am approving it but left some comments.

@ambv ambv force-pushed the pyrepl-sh-refactor-calc-screen branch from 31c48b2 to f9f4722 Compare March 21, 2025 14:58
@ambv ambv force-pushed the pyrepl-syntax-highlighting branch from aebc35f to 35f01ca Compare March 21, 2025 17:56
@ambv ambv changed the base branch from pyrepl-sh-refactor-calc-screen to main March 21, 2025 17:56
@ambv
Copy link
Owner Author

ambv commented Mar 21, 2025

Moved on to python#131562.

@ambv ambv closed this Mar 21, 2025
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