Fix finding keyword range for clause header after statement ending with semicolon (#21067)

When formatting clause headers for clauses that are not their own node,
like an `else` clause or `finally` clause, we begin searching for the
keyword at the end of the previous statement. However, if the previous
statement ended in a semicolon this caused a panic because we only
expected trivia between the end of the last statement and the keyword.

This PR adjusts the starting point of our search for the keyword to
begin after the optional semicolon in these cases.

Closes #21065
This commit is contained in:
Dylan 2025-10-27 09:52:17 -05:00 committed by GitHub
parent db0e921db1
commit 116611bd39
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 145 additions and 31 deletions

View file

@ -42,3 +42,11 @@ def test4(<RANGE_START> a):
<RANGE_START>if b + c :<RANGE_END> # trailing clause header comment
print("Not formatted" )
def test5():
x = 1
<RANGE_START>try:
a;
finally:
b
<RANGE_END>