Skip to content

Commit

Permalink
bpo-33768: IDLE: Clicking on code context line moves it to top of edi…
Browse files Browse the repository at this point in the history
…tor (GH-7411)
  • Loading branch information
csabella authored and terryjreedy committed Jun 8, 2018
1 parent 4aa3006 commit 041272b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Lib/idlelib/codecontext.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def toggle_code_context_event(self, event=None):
height=1,
width=1, # Don't request more than we get.
padx=padx, border=border, relief=SUNKEN, state='disabled')
self.context.bind('<ButtonRelease-1>', self.jumptoline)
# Pack the context widget before and above the text_frame widget,
# thus ensuring that it will appear directly above text_frame.
self.context.pack(side=TOP, fill=X, expand=False,
Expand Down Expand Up @@ -196,6 +197,20 @@ def update_code_context(self):
self.context.insert('end', '\n'.join(context_strings[showfirst:]))
self.context['state'] = 'disabled'

def jumptoline(self, event=None):
"Show clicked context line at top of editor."
lines = len(self.info)
if lines == 1: # No context lines are showing.
newtop = 1
else:
# Line number clicked.
contextline = int(float(self.context.index('insert')))
# Lines not displayed due to maxlines.
offset = max(1, lines - self.context_depth) - 1
newtop = self.info[offset + contextline][0]
self.text.yview(f'{newtop}.0')
self.update_code_context()

def timer_event(self):
"Event on editor text widget triggered every UPDATEINTERVAL ms."
if self.context:
Expand Down
34 changes: 34 additions & 0 deletions Lib/idlelib/idle_test/test_codecontext.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def tearDownClass(cls):
del cls.root

def setUp(self):
self.text.yview(0)
self.cc = codecontext.CodeContext(self.editor)

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

def test_jumptoline(self):
eq = self.assertEqual
cc = self.cc
jump = cc.jumptoline

if not cc.context:
cc.toggle_code_context_event()

# Empty context.
cc.text.yview(f'{2}.0')
cc.update_code_context()
eq(cc.topvisible, 2)
cc.context.mark_set('insert', '1.5')
jump()
eq(cc.topvisible, 1)

# 4 lines of context showing.
cc.text.yview(f'{12}.0')
cc.update_code_context()
eq(cc.topvisible, 12)
cc.context.mark_set('insert', '3.0')
jump()
eq(cc.topvisible, 8)

# More context lines than limit.
cc.context_depth = 2
cc.text.yview(f'{12}.0')
cc.update_code_context()
eq(cc.topvisible, 12)
cc.context.mark_set('insert', '1.0')
jump()
eq(cc.topvisible, 8)

@mock.patch.object(codecontext.CodeContext, 'update_code_context')
def test_timer_event(self, mock_update):
# Ensure code context is not active.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Clicking on a context line moves that line to the top of the editor window.

0 comments on commit 041272b

Please sign in to comment.