diff --git a/Lib/idlelib/hyperparser.py b/Lib/idlelib/hyperparser.py index 77baca782b3fdc0..c7af4806ebafda5 100644 --- a/Lib/idlelib/hyperparser.py +++ b/Lib/idlelib/hyperparser.py @@ -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 @@ -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. @@ -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. @@ -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] diff --git a/Lib/idlelib/parenmatch.py b/Lib/idlelib/parenmatch.py index 3fd7aadb2aea84f..384737c9dca03ea 100644 --- a/Lib/idlelib/parenmatch.py +++ b/Lib/idlelib/parenmatch.py @@ -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" diff --git a/Misc/NEWS.d/next/IDLE/2020-06-09-10-47-33.bpo-21756.auszVR.rst b/Misc/NEWS.d/next/IDLE/2020-06-09-10-47-33.bpo-21756.auszVR.rst new file mode 100644 index 000000000000000..599f0a34c3dfc14 --- /dev/null +++ b/Misc/NEWS.d/next/IDLE/2020-06-09-10-47-33.bpo-21756.auszVR.rst @@ -0,0 +1 @@ +Fixed IDLE's "show surrounding parens" for multi-line statements.