Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Improve PyREPL auto-indentation #133710

Open
Labels
stdlibPython modules in the Lib dirtopic-replRelated to the interactive shelltype-featureA feature request or enhancement
@tanloong

Description

@tanloong

Bug report

On second thought this issue should be an enhancement instead of a bug report. Sorry for the wrong template.

Bug description:

There are some cases where_pyrepl auto-indentation works not well.

Cases

  1. A line ending with: in a multi-line string is wrongly indented.

Observed

>>> s ='''... Note:... ␣␣␣␣|

Expected

>>> s ='''... Note:... |
  1. # inside strings is seen as a comment, the following: is ignored.

Observed

>>>if'' =='#':...|

Expected

>>>if'' =='#':... ␣␣␣␣|
  1. When the entire cursor line is a comment and is already indented, pressing Enter gives a further indent.

Observed

>>> deff():...# foo⤶... ␣␣␣␣␣␣␣␣|

Expected

>>> deff():...# foo⤶... ␣␣␣␣|

Possible solution

Currently_should_auto_indent() parses thebuffer fromright to left and ends at the first newline it encounters. Only the last line that is not a comment line of thebuffer is parsed.

But by parsing fromright to left we can't tell if a# starts a comment or is part of a string. For example if we see a" first and then a#, we don't know if the# is a comment. To know that, we need to know if the" is a string boundary, but the# might comment out the", so we can't be sure. There is a information dependency cycle.

To fix this I made achange to parse thebuffer fromleft to right, keeping track of whether current char is inside a string or a comment. This approach solves the above three cases. However the wholebuffer is parsed on every call of_should_auto_indent(), with very long buffer, there might be noticeable delay when pressing Enter.

This is a big change. It affects how_should_auto_indent() works as a whole. I am hesitated to create a PR and just put it here first to hopefully get feedback.

CPython versions tested on:

3.15

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtopic-replRelated to the interactive shelltype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp