Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-21756: fix IDLE's "show surrounding parens" for multi-line statements #20753

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 12 additions & 12 deletions Lib/idlelib/hyperparser.py
Expand Up @@ -23,7 +23,7 @@


class HyperParser:
def __init__(self, editwin, index):
def __init__(self, editwin, index, *, end_at_eol=True):
"To initialize, analyze the surroundings of the given index."

self.editwin = editwin
Expand All @@ -34,13 +34,13 @@ def __init__(self, editwin, index):
def index2line(index):
return int(float(index))
lno = index2line(text.index(index))
stopatindex = f"{lno}.end" if end_at_eol else "end-1c"

if not editwin.prompt_last_line:
for context in editwin.num_context_lines:
startat = max(lno - context, 1)
startatindex = repr(startat) + ".0"
stopatindex = "%d.end" % lno
# We add the newline because PyParse requires a newline
startatindex = f"{startat}.0"
# We add a newline because PyParse requires a newline
# at end. We add a space so that index won't be at end
# of line, so that its status will be the same as the
# char before it, if should.
Expand All @@ -52,11 +52,10 @@ def index2line(index):
parser.set_lo(bod or 0)
else:
r = text.tag_prevrange("console", index)
if r:
startatindex = r[1]
else:
startatindex = "1.0"
stopatindex = "%d.end" % lno
startatindex = r[1] if r else "1.0"
if not end_at_eol and (r2 := text.tag_nextrange("console", index)):
stopatindex = f"{r2[0]}-1c"

# We add the newline because PyParse requires it. We add a
# space so that index won't be at end of line, so that its
# status will be the same as the char before it, if should.
Expand Down Expand Up @@ -119,10 +118,11 @@ def get_surrounding_brackets(self, openers='([{', mustclose=False):
If the index given to the HyperParser is surrounded by a
bracket defined in openers (or at least has one before it),
return the indices of the opening bracket and the closing
bracket (or the end of line, whichever comes first).
bracket (or the end of line/file, whichever comes first).

If it is not surrounded by brackets, or the end of line comes
before the closing bracket and mustclose is True, returns None.
If it is not surrounded by brackets, or the end of line/file
comes before the closing bracket and mustclose is True, returns
None.
"""

bracketinglevel = self.bracketing[self.indexbracket][1]
Expand Down
4 changes: 2 additions & 2 deletions Lib/idlelib/parenmatch.py
Expand Up @@ -75,8 +75,8 @@ def deactivate_restore(self):

def flash_paren_event(self, event):
"Handle editor 'show surrounding parens' event (menu or shortcut)."
indices = (HyperParser(self.editwin, "insert")
.get_surrounding_brackets())
hp = HyperParser(self.editwin, "insert", end_at_eol=False)
indices = hp.get_surrounding_brackets()
self.finish_paren_event(indices)
return "break"

Expand Down
@@ -0,0 +1 @@
Fixed IDLE's "show surrounding parens" for multi-line statements.