Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
Description
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
- A line ending with
:
in a multi-line string is wrongly indented.
Observed
>>> s ='''... Note:... ␣␣␣␣|
Expected
>>> s ='''... Note:... |
#
inside strings is seen as a comment, the following:
is ignored.
Observed
>>>if'' =='#':...|
Expected
>>>if'' =='#':... ␣␣␣␣|
- 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