Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Ensure parsing begins at statement starts
  • Loading branch information
ujan-r committed Sep 25, 2024
commit 927e0bc133b852f64c9a18b34ae943b60bf26053
38 changes: 7 additions & 31 deletions Lib/idlelib/hyperparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,44 +31,20 @@ def __init__(self, editwin, index):

parser = pyparse.Parser(editwin.indentwidth, editwin.tabwidth)

startatindex = text.index('stmt-start')

def index2line(index):
return int(float(index))
lno = index2line(text.index(index))

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
# 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.
parser.set_code(text.get(startatindex, stopatindex)+' \n')
bod = parser.find_good_parse_start(
editwin._build_char_in_string_func(startatindex))
if bod is not None or startat == 1:
break
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
# 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.
parser.set_code(text.get(startatindex, stopatindex)+' \n')
parser.set_lo(0)

# We want what the parser has, minus the last newline and space.
self.rawtext = parser.code[:-2]
statement = text.get(startatindex, '%d.end' % lno)
self.rawtext = statement
parser.set_code(statement + '\n')

# Parser.code apparently preserves the statement we are in, so
# that stopatindex can be used to synchronize the string with
# the text box indices.
self.stopatindex = stopatindex
self.stopatindex = '%d.end' % lno
self.bracketing = parser.get_last_stmt_bracketing()
# find which pairs of bracketing are openers. These always
# correspond to a character of rawtext.
Expand Down