gh-133710: Enhance pyrepl auto-indent #140710
Open
+144
−14
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This modifies
_should_auto_indent()to scan characters from the start of the buffer up to the target positionpos, tracking context to ignore irrelavant content within strings, then checks if the conditions for auto-indent are met.It uses a stack (
str_delims) to track quotes ("or'). When inside a string (stack not empty), characters like#or:are treated as literal text and are ignored. A flag (in_comment) marks when a#(not inside a string) has been encountered, ignoring all subsequent characters on that line as comment text. It tracks the indentation level of the last non-whitespace character (lastchar_line_indent), which helps ensure the colon is at a valid indentation level to trigger auto-indent.The function returns
Trueif the last non-whitespace character (outside comments/strings) is a colon (:), and the indentation level of the line whereposresides is not greater than the indentation of that colon.Hi @wiggin15, as this
_should_auto_indent()function is modified based on your original implementation (#119606), if you could help review when you have some spare time, I would be really appreciated!