Skip to content

Commit db2957c

Browse files
bpo-33610: IDLE's code-context always shows current context immediately (GH-14821)
Eliminate delay of up to 100ms and accompanying visual artifact. Fix bug of never showing context when hide and show. (cherry picked from commit e0a1f8f) Co-authored-by: Tal Einat <[email protected]>
1 parent 5eb19fd commit db2957c

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

Lib/idlelib/codecontext.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,13 @@ def __init__(self, editwin):
6363
"""
6464
self.editwin = editwin
6565
self.text = editwin.text
66+
self._reset()
67+
68+
def _reset(self):
6669
self.context = None
70+
self.t1 = None
6771
self.topvisible = 1
6872
self.info = [(0, -1, "", False)]
69-
self.t1 = None
7073

7174
@classmethod
7275
def reload(cls):
@@ -112,17 +115,17 @@ def toggle_code_context_event(self, event=None):
112115
padx=padx, border=border, relief=SUNKEN, state='disabled')
113116
self.update_highlight_colors()
114117
self.context.bind('<ButtonRelease-1>', self.jumptoline)
118+
# Get the current context and initiate the recurring update event.
119+
self.timer_event()
115120
# Pack the context widget before and above the text_frame widget,
116121
# thus ensuring that it will appear directly above text_frame.
117122
self.context.pack(side=TOP, fill=X, expand=False,
118123
before=self.editwin.text_frame)
119124
menu_status = 'Hide'
120-
self.t1 = self.text.after(self.UPDATEINTERVAL, self.timer_event)
121125
else:
122126
self.context.destroy()
123-
self.context = None
124127
self.text.after_cancel(self.t1)
125-
self.t1 = None
128+
self._reset()
126129
menu_status = 'Show'
127130
self.editwin.update_menu_label(menu='options', index='* Code Context',
128131
label=f'{menu_status} Code Context')

Lib/idlelib/idle_test/test_codecontext.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def test_toggle_code_context_event(self):
135135
toggle()
136136

137137
# Toggle on.
138-
eq(toggle(), 'break')
138+
toggle()
139139
self.assertIsNotNone(cc.context)
140140
eq(cc.context['font'], self.text['font'])
141141
eq(cc.context['fg'], self.highlight_cfg['foreground'])
@@ -145,11 +145,22 @@ def test_toggle_code_context_event(self):
145145
eq(self.root.tk.call('after', 'info', self.cc.t1)[1], 'timer')
146146

147147
# Toggle off.
148-
eq(toggle(), 'break')
148+
toggle()
149149
self.assertIsNone(cc.context)
150150
eq(cc.editwin.label, 'Show Code Context')
151151
self.assertIsNone(self.cc.t1)
152152

153+
# Scroll down and toggle back on.
154+
line11_context = '\n'.join(x[2] for x in cc.get_context(11)[0])
155+
cc.text.yview(11)
156+
toggle()
157+
eq(cc.context.get('1.0', 'end-1c'), line11_context)
158+
159+
# Toggle off and on again.
160+
toggle()
161+
toggle()
162+
eq(cc.context.get('1.0', 'end-1c'), line11_context)
163+
153164
def test_get_context(self):
154165
eq = self.assertEqual
155166
gc = self.cc.get_context
@@ -329,7 +340,7 @@ def test_font(self):
329340
eq = self.assertEqual
330341
cc = self.cc
331342
save_font = cc.text['font']
332-
test_font = 'TkFixedFont'
343+
test_font = 'TkTextFont'
333344

334345
# Ensure code context is not active.
335346
if cc.context is not None:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix code context not showing the correct context when first toggled on.

0 commit comments

Comments
 (0)