Skip to content

Commit 0800b6c

Browse files
bpo-33642: IDLE: Use variable number of lines in CodeContext. (GH-7106)
Instead of displaying a fixed number of lines, some blank, Code Context now displays the variable number of actual context lines. When there are no context lines, it shows a single blank line to indicate that the feature is turned on. The Code Context configuration option is changed from 'numlines' (default 3) to 'maxlines' (default 15) to avoid possible interference between user settings for the old and new versions of Code Context. (cherry picked from commit 29996a1) Co-authored-by: Cheryl Sabella <cheryl.sabella@gmail.com>
1 parent 283e12f commit 0800b6c

File tree

6 files changed

+40
-29
lines changed

6 files changed

+40
-29
lines changed

Lib/idlelib/codecontext.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
determine which block you are in. This extension implements a pane at the top
55
of each IDLE edit window which provides block structure hints. These hints are
66
the lines which contain the block opening keywords, e.g. 'if', for the
7-
enclosing block. The number of hint lines is determined by the numlines
7+
enclosing block. The number of hint lines is determined by the maxlines
88
variable in the codecontext section of config-extensions.def. Lines which do
99
not open blocks are not shown in the context hints pane.
1010
@@ -80,7 +80,7 @@ def __init__(self, editwin):
8080
def reload(cls):
8181
"Load class variables from config."
8282
cls.context_depth = idleConf.GetOption("extensions", "CodeContext",
83-
"numlines", type="int", default=3)
83+
"maxlines", type="int", default=15)
8484
## cls.bgcolor = idleConf.GetOption("extensions", "CodeContext",
8585
## "bgcolor", type="str", default="LightGray")
8686
## cls.fgcolor = idleConf.GetOption("extensions", "CodeContext",
@@ -116,7 +116,7 @@ def toggle_code_context_event(self, event=None):
116116
padx += widget.tk.getint(widget.cget('padx'))
117117
border += widget.tk.getint(widget.cget('border'))
118118
self.label = tkinter.Label(
119-
self.editwin.top, text="\n" * (self.context_depth - 1),
119+
self.editwin.top, text="",
120120
anchor=W, justify=LEFT, font=self.textfont,
121121
bg=self.bgcolor, fg=self.fgcolor,
122122
width=1, # Don't request more than we get.
@@ -191,11 +191,10 @@ def update_code_context(self):
191191
stopindent)
192192
self.info.extend(lines)
193193
self.topvisible = new_topvisible
194-
# Empty lines in context pane.
195-
context_strings = [""] * max(0, self.context_depth - len(self.info))
196-
# Followed by the context hint lines.
197-
context_strings += [x[2] for x in self.info[-self.context_depth:]]
198-
self.label["text"] = '\n'.join(context_strings)
194+
# Last context_depth context lines.
195+
context_strings = [x[2] for x in self.info[-self.context_depth:]]
196+
showfirst = 0 if context_strings[0] else 1
197+
self.label["text"] = '\n'.join(context_strings[showfirst:])
199198

200199
def timer_event(self):
201200
"Event on editor text widget triggered every UPDATEINTERVAL ms."

Lib/idlelib/config-extensions.def

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
popupwait= 2000
88

99
[CodeContext]
10-
numlines= 3
11-
visible= False
12-
bgcolor= LightGray
13-
fgcolor= Black
10+
maxlines= 15
1411

1512
[FormatParagraph]
1613
max-width= 72

Lib/idlelib/configdialog.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,7 +1843,7 @@ def create_page_general(self):
18431843
self.format_width = tracers.add(
18441844
StringVar(self), ('extensions', 'FormatParagraph', 'max-width'))
18451845
self.context_lines = tracers.add(
1846-
StringVar(self), ('extensions', 'CodeContext', 'numlines'))
1846+
StringVar(self), ('extensions', 'CodeContext', 'maxlines'))
18471847

18481848
# Create widgets:
18491849
# Section frames.
@@ -1910,7 +1910,7 @@ def create_page_general(self):
19101910
frame_format, textvariable=self.format_width, width=4)
19111911

19121912
frame_context = Frame(frame_editor, borderwidth=0)
1913-
context_title = Label(frame_context, text='Context Lines :')
1913+
context_title = Label(frame_context, text='Max Context Lines :')
19141914
self.context_int = Entry(
19151915
frame_context, textvariable=self.context_lines, width=3)
19161916

@@ -2012,7 +2012,7 @@ def load_general_cfg(self):
20122012
self.format_width.set(idleConf.GetOption(
20132013
'extensions', 'FormatParagraph', 'max-width', type='int'))
20142014
self.context_lines.set(idleConf.GetOption(
2015-
'extensions', 'CodeContext', 'numlines', type='int'))
2015+
'extensions', 'CodeContext', 'maxlines', type='int'))
20162016

20172017
# Set additional help sources.
20182018
self.user_helplist = idleConf.GetAllExtraHelpSourcesList()
@@ -2204,6 +2204,9 @@ def detach(self):
22042204
'opener' - opener '({[' corresponding to closer; 'parens' - both chars;
22052205
'expression' (default) - also everything in between. Flash-delay is how
22062206
long to highlight if cursor is not moved (0 means forever).
2207+
2208+
CodeContext: Maxlines is the maximum number of code context lines to
2209+
display when Code Context is turned on for an editor window.
22072210
'''
22082211
}
22092212

Lib/idlelib/idle_test/test_codecontext.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def test_del(self):
110110

111111
def test_reload(self):
112112
codecontext.CodeContext.reload()
113-
self.assertEqual(self.cc.context_depth, 3)
113+
self.assertEqual(self.cc.context_depth, 15)
114114

115115
def test_toggle_code_context_event(self):
116116
eq = self.assertEqual
@@ -127,7 +127,7 @@ def test_toggle_code_context_event(self):
127127
eq(cc.label['font'], cc.textfont)
128128
eq(cc.label['fg'], cc.fgcolor)
129129
eq(cc.label['bg'], cc.bgcolor)
130-
eq(cc.label['text'], '\n' * 2)
130+
eq(cc.label['text'], '')
131131

132132
# Toggle off.
133133
eq(toggle(), 'break')
@@ -193,24 +193,26 @@ def test_update_code_context(self):
193193
eq(cc.info, [(0, -1, '', False)])
194194
eq(cc.topvisible, 1)
195195

196+
# Scroll down to line 1.
197+
cc.text.yview(1)
198+
cc.update_code_context()
199+
eq(cc.info, [(0, -1, '', False)])
200+
eq(cc.topvisible, 2)
201+
eq(cc.label['text'], '')
202+
196203
# Scroll down to line 2.
197204
cc.text.yview(2)
198205
cc.update_code_context()
199206
eq(cc.info, [(0, -1, '', False), (2, 0, 'class C1():', 'class')])
200207
eq(cc.topvisible, 3)
201-
# context_depth is 3 so it pads with blank lines.
202-
eq(cc.label['text'], '\n'
203-
'\n'
204-
'class C1():')
208+
eq(cc.label['text'], 'class C1():')
205209

206210
# Scroll down to line 3. Since it's a comment, nothing changes.
207211
cc.text.yview(3)
208212
cc.update_code_context()
209213
eq(cc.info, [(0, -1, '', False), (2, 0, 'class C1():', 'class')])
210214
eq(cc.topvisible, 4)
211-
eq(cc.label['text'], '\n'
212-
'\n'
213-
'class C1():')
215+
eq(cc.label['text'], 'class C1():')
214216

215217
# Scroll down to line 4.
216218
cc.text.yview(4)
@@ -219,8 +221,7 @@ def test_update_code_context(self):
219221
(2, 0, 'class C1():', 'class'),
220222
(4, 4, ' def __init__(self, a, b):', 'def')])
221223
eq(cc.topvisible, 5)
222-
eq(cc.label['text'], '\n'
223-
'class C1():\n'
224+
eq(cc.label['text'], 'class C1():\n'
224225
' def __init__(self, a, b):')
225226

226227
# Scroll down to line 11. Last 'def' is removed.
@@ -232,7 +233,8 @@ def test_update_code_context(self):
232233
(8, 8, ' if a > b:', 'if'),
233234
(10, 8, ' elif a < b:', 'elif')])
234235
eq(cc.topvisible, 12)
235-
eq(cc.label['text'], ' def compare(self):\n'
236+
eq(cc.label['text'], 'class C1():\n'
237+
' def compare(self):\n'
236238
' if a > b:\n'
237239
' elif a < b:')
238240

@@ -245,7 +247,8 @@ def test_update_code_context(self):
245247
(8, 8, ' if a > b:', 'if'),
246248
(10, 8, ' elif a < b:', 'elif')])
247249
eq(cc.topvisible, 12)
248-
eq(cc.label['text'], ' def compare(self):\n'
250+
eq(cc.label['text'], 'class C1():\n'
251+
' def compare(self):\n'
249252
' if a > b:\n'
250253
' elif a < b:')
251254

Lib/idlelib/idle_test/test_configdialog.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,7 @@ def test_paragraph(self):
11701170
def test_context(self):
11711171
self.page.context_int.delete(0, 'end')
11721172
self.page.context_int.insert(0, '1')
1173-
self.assertEqual(extpage, {'CodeContext': {'numlines': '1'}})
1173+
self.assertEqual(extpage, {'CodeContext': {'maxlines': '1'}})
11741174

11751175
def test_source_selected(self):
11761176
d = self.page
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
IDLE: Only display actual code context lines.
2+
Instead of displaying a fixed number of lines, some blank, Code Context
3+
now displays the variable number of actual context lines. When there
4+
are no context lines, it shows a single blank line to indicate that the
5+
feature is turned on.
6+
7+
The Code Context configuration option is changed from 'numlines'
8+
(default 3) to 'maxlines' (default 15) to avoid possible interference
9+
between user settings for the old and new versions of Code Context.

0 commit comments

Comments
 (0)