Skip to content

Commit a486847

Browse files
bpo-33768: IDLE: Clicking on code context line moves it to top of editor (GH-7411)
(cherry picked from commit 041272b) Co-authored-by: Cheryl Sabella <[email protected]>
1 parent 43202e0 commit a486847

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

Lib/idlelib/codecontext.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ def toggle_code_context_event(self, event=None):
117117
height=1,
118118
width=1, # Don't request more than we get.
119119
padx=padx, border=border, relief=SUNKEN, state='disabled')
120+
self.context.bind('<ButtonRelease-1>', self.jumptoline)
120121
# Pack the context widget before and above the text_frame widget,
121122
# thus ensuring that it will appear directly above text_frame.
122123
self.context.pack(side=TOP, fill=X, expand=False,
@@ -196,6 +197,20 @@ def update_code_context(self):
196197
self.context.insert('end', '\n'.join(context_strings[showfirst:]))
197198
self.context['state'] = 'disabled'
198199

200+
def jumptoline(self, event=None):
201+
"Show clicked context line at top of editor."
202+
lines = len(self.info)
203+
if lines == 1: # No context lines are showing.
204+
newtop = 1
205+
else:
206+
# Line number clicked.
207+
contextline = int(float(self.context.index('insert')))
208+
# Lines not displayed due to maxlines.
209+
offset = max(1, lines - self.context_depth) - 1
210+
newtop = self.info[offset + contextline][0]
211+
self.text.yview(f'{newtop}.0')
212+
self.update_code_context()
213+
199214
def timer_event(self):
200215
"Event on editor text widget triggered every UPDATEINTERVAL ms."
201216
if self.context:

Lib/idlelib/idle_test/test_codecontext.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def tearDownClass(cls):
7272
del cls.root
7373

7474
def setUp(self):
75+
self.text.yview(0)
7576
self.cc = codecontext.CodeContext(self.editor)
7677

7778
def tearDown(self):
@@ -264,6 +265,39 @@ def test_update_code_context(self):
264265
# context_depth is 1.
265266
eq(cc.context.get('1.0', 'end-1c'), ' def __init__(self, a, b):')
266267

268+
def test_jumptoline(self):
269+
eq = self.assertEqual
270+
cc = self.cc
271+
jump = cc.jumptoline
272+
273+
if not cc.context:
274+
cc.toggle_code_context_event()
275+
276+
# Empty context.
277+
cc.text.yview(f'{2}.0')
278+
cc.update_code_context()
279+
eq(cc.topvisible, 2)
280+
cc.context.mark_set('insert', '1.5')
281+
jump()
282+
eq(cc.topvisible, 1)
283+
284+
# 4 lines of context showing.
285+
cc.text.yview(f'{12}.0')
286+
cc.update_code_context()
287+
eq(cc.topvisible, 12)
288+
cc.context.mark_set('insert', '3.0')
289+
jump()
290+
eq(cc.topvisible, 8)
291+
292+
# More context lines than limit.
293+
cc.context_depth = 2
294+
cc.text.yview(f'{12}.0')
295+
cc.update_code_context()
296+
eq(cc.topvisible, 12)
297+
cc.context.mark_set('insert', '1.0')
298+
jump()
299+
eq(cc.topvisible, 8)
300+
267301
@mock.patch.object(codecontext.CodeContext, 'update_code_context')
268302
def test_timer_event(self, mock_update):
269303
# Ensure code context is not active.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Clicking on a context line moves that line to the top of the editor window.

0 commit comments

Comments
 (0)