Skip to content

Commit 2113f88

Browse files
committed
Don't indent if cursor line is already indented
1 parent e4ef49d commit 2113f88

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

Lib/_pyrepl/readline.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,11 @@ def _should_auto_indent(buffer: list[str], pos: int) -> bool:
261261
# a string.
262262
in_string: list[str] = []
263263
in_comment = False
264+
char_line_indent_start = None
265+
char_line_indent = 0
266+
lastchar_line_indent = 0
267+
cursor_line_indent = 0
268+
264269
i = -1
265270
while i < pos - 1:
266271
i += 1
@@ -275,17 +280,27 @@ def _should_auto_indent(buffer: list[str], pos: int) -> bool:
275280
elif char == "\n":
276281
# newline ends a comment
277282
in_comment = False
278-
elif char not in " \t" and not in_comment and not in_string:
279-
# update last_char with non-whitespace chars outside comments and strings
280-
last_char = char
283+
if i < pos - 1 and buffer[i + 1] in " \t":
284+
char_line_indent_start = i + 1
285+
else:
286+
char_line_indent_start = None # clear last line's line_indent_start
287+
char_line_indent = 0
288+
elif char not in " \t":
289+
if char_line_indent_start is not None:
290+
char_line_indent = i - char_line_indent_start
291+
if not in_comment and not in_string:
292+
# update last_char with non-whitespace chars outside comments and strings
293+
last_char = char
294+
lastchar_line_indent = char_line_indent
281295

282296
# update stack
283297
if char in "\"'" and (i == 0 or buffer[i - 1] != "\\"):
284298
if in_string and in_string[-1] == char:
285299
in_string.pop()
286300
else:
287301
in_string.append(char)
288-
return last_char == ":"
302+
cursor_line_indent = char_line_indent
303+
return last_char == ":" and cursor_line_indent <= lastchar_line_indent
289304

290305

291306
class maybe_accept(commands.Command):

0 commit comments

Comments
 (0)