From 6b866665a5b26e44f9a6d9c66eb4eaf5b2b03a95 Mon Sep 17 00:00:00 2001
From: Cheryl Sabella
Date: Thu, 24 May 2018 20:43:30 -0400
Subject: [PATCH 1/5] bpo-33642: IDLE: Use variable number of lines in
CodeContext.
---
Lib/idlelib/codecontext.py | 10 ++++------
Lib/idlelib/config-extensions.def | 2 +-
Lib/idlelib/configdialog.py | 2 +-
Lib/idlelib/idle_test/test_codecontext.py | 14 ++++++++------
.../IDLE/2018-05-24-20-42-44.bpo-33642.J0VQbS.rst | 1 +
5 files changed, 15 insertions(+), 14 deletions(-)
create mode 100644 Misc/NEWS.d/next/IDLE/2018-05-24-20-42-44.bpo-33642.J0VQbS.rst
diff --git a/Lib/idlelib/codecontext.py b/Lib/idlelib/codecontext.py
index 635f68c86e1f6d3..10a2e2149ddc303 100644
--- a/Lib/idlelib/codecontext.py
+++ b/Lib/idlelib/codecontext.py
@@ -80,7 +80,7 @@ def __init__(self, editwin):
def reload(cls):
"Load class variables from config."
cls.context_depth = idleConf.GetOption("extensions", "CodeContext",
- "numlines", type="int", default=3)
+ "numlines", type="int", default=15)
## cls.bgcolor = idleConf.GetOption("extensions", "CodeContext",
## "bgcolor", type="str", default="LightGray")
## cls.fgcolor = idleConf.GetOption("extensions", "CodeContext",
@@ -116,7 +116,7 @@ def toggle_code_context_event(self, event=None):
padx += widget.tk.getint(widget.cget('padx'))
border += widget.tk.getint(widget.cget('border'))
self.label = tkinter.Label(
- self.editwin.top, text="\n" * (self.context_depth - 1),
+ self.editwin.top, text="",
anchor=W, justify=LEFT, font=self.textfont,
bg=self.bgcolor, fg=self.fgcolor,
width=1, # Don't request more than we get.
@@ -191,10 +191,8 @@ def update_code_context(self):
stopindent)
self.info.extend(lines)
self.topvisible = new_topvisible
- # Empty lines in context pane.
- context_strings = [""] * max(0, self.context_depth - len(self.info))
- # Followed by the context hint lines.
- context_strings += [x[2] for x in self.info[-self.context_depth:]]
+ # Last context_depth context lines.
+ context_strings = [x[2] for x in self.info[-self.context_depth:]]
self.label["text"] = '\n'.join(context_strings)
def timer_event(self):
diff --git a/Lib/idlelib/config-extensions.def b/Lib/idlelib/config-extensions.def
index 8689fd965e8eae4..41c3fbd239cfa1d 100644
--- a/Lib/idlelib/config-extensions.def
+++ b/Lib/idlelib/config-extensions.def
@@ -7,7 +7,7 @@
popupwait= 2000
[CodeContext]
-numlines= 3
+numlines= 15
visible= False
bgcolor= LightGray
fgcolor= Black
diff --git a/Lib/idlelib/configdialog.py b/Lib/idlelib/configdialog.py
index 36ac4a23a52dc0a..349a0d67dcf2922 100644
--- a/Lib/idlelib/configdialog.py
+++ b/Lib/idlelib/configdialog.py
@@ -1910,7 +1910,7 @@ def create_page_general(self):
frame_format, textvariable=self.format_width, width=4)
frame_context = Frame(frame_editor, borderwidth=0)
- context_title = Label(frame_context, text='Context Lines :')
+ context_title = Label(frame_context, text='Max Context Lines :')
self.context_int = Entry(
frame_context, textvariable=self.context_lines, width=3)
diff --git a/Lib/idlelib/idle_test/test_codecontext.py b/Lib/idlelib/idle_test/test_codecontext.py
index ed45828641b4cd9..a5f17d4a885384c 100644
--- a/Lib/idlelib/idle_test/test_codecontext.py
+++ b/Lib/idlelib/idle_test/test_codecontext.py
@@ -110,7 +110,7 @@ def test_del(self):
def test_reload(self):
codecontext.CodeContext.reload()
- self.assertEqual(self.cc.context_depth, 3)
+ self.assertEqual(self.cc.context_depth, 15)
def test_toggle_code_context_event(self):
eq = self.assertEqual
@@ -127,7 +127,7 @@ def test_toggle_code_context_event(self):
eq(cc.label['font'], cc.textfont)
eq(cc.label['fg'], cc.fgcolor)
eq(cc.label['bg'], cc.bgcolor)
- eq(cc.label['text'], '\n' * 2)
+ eq(cc.label['text'], '')
# Toggle off.
eq(toggle(), 'break')
@@ -200,7 +200,6 @@ def test_update_code_context(self):
eq(cc.topvisible, 3)
# context_depth is 3 so it pads with blank lines.
eq(cc.label['text'], '\n'
- '\n'
'class C1():')
# Scroll down to line 3. Since it's a comment, nothing changes.
@@ -209,7 +208,6 @@ def test_update_code_context(self):
eq(cc.info, [(0, -1, '', False), (2, 0, 'class C1():', 'class')])
eq(cc.topvisible, 4)
eq(cc.label['text'], '\n'
- '\n'
'class C1():')
# Scroll down to line 4.
@@ -232,7 +230,9 @@ def test_update_code_context(self):
(8, 8, ' if a > b:', 'if'),
(10, 8, ' elif a < b:', 'elif')])
eq(cc.topvisible, 12)
- eq(cc.label['text'], ' def compare(self):\n'
+ eq(cc.label['text'], '\n'
+ 'class C1():\n'
+ ' def compare(self):\n'
' if a > b:\n'
' elif a < b:')
@@ -245,7 +245,9 @@ def test_update_code_context(self):
(8, 8, ' if a > b:', 'if'),
(10, 8, ' elif a < b:', 'elif')])
eq(cc.topvisible, 12)
- eq(cc.label['text'], ' def compare(self):\n'
+ eq(cc.label['text'], '\n'
+ 'class C1():\n'
+ ' def compare(self):\n'
' if a > b:\n'
' elif a < b:')
diff --git a/Misc/NEWS.d/next/IDLE/2018-05-24-20-42-44.bpo-33642.J0VQbS.rst b/Misc/NEWS.d/next/IDLE/2018-05-24-20-42-44.bpo-33642.J0VQbS.rst
new file mode 100644
index 000000000000000..b61572a9cb9f5cb
--- /dev/null
+++ b/Misc/NEWS.d/next/IDLE/2018-05-24-20-42-44.bpo-33642.J0VQbS.rst
@@ -0,0 +1 @@
+IDLE: Use variable number of lines in codecontext.py.
From 284060db2aef30cb05f69d81da201523dd84daf8 Mon Sep 17 00:00:00 2001
From: Cheryl Sabella
Date: Mon, 28 May 2018 17:38:59 -0400
Subject: [PATCH 2/5] Add maxlines and only show blank line if there are no
context lines.
---
Lib/idlelib/codecontext.py | 7 ++++---
Lib/idlelib/config-extensions.def | 3 ++-
Lib/idlelib/configdialog.py | 4 ++--
Lib/idlelib/idle_test/test_codecontext.py | 23 +++++++++++-----------
Lib/idlelib/idle_test/test_configdialog.py | 2 +-
5 files changed, 21 insertions(+), 18 deletions(-)
diff --git a/Lib/idlelib/codecontext.py b/Lib/idlelib/codecontext.py
index 10a2e2149ddc303..b061f3b2fe8eb9f 100644
--- a/Lib/idlelib/codecontext.py
+++ b/Lib/idlelib/codecontext.py
@@ -4,7 +4,7 @@
determine which block you are in. This extension implements a pane at the top
of each IDLE edit window which provides block structure hints. These hints are
the lines which contain the block opening keywords, e.g. 'if', for the
-enclosing block. The number of hint lines is determined by the numlines
+enclosing block. The number of hint lines is determined by the maxlines
variable in the codecontext section of config-extensions.def. Lines which do
not open blocks are not shown in the context hints pane.
@@ -80,7 +80,7 @@ def __init__(self, editwin):
def reload(cls):
"Load class variables from config."
cls.context_depth = idleConf.GetOption("extensions", "CodeContext",
- "numlines", type="int", default=15)
+ "maxlines", type="int", default=15)
## cls.bgcolor = idleConf.GetOption("extensions", "CodeContext",
## "bgcolor", type="str", default="LightGray")
## cls.fgcolor = idleConf.GetOption("extensions", "CodeContext",
@@ -193,7 +193,8 @@ def update_code_context(self):
self.topvisible = new_topvisible
# Last context_depth context lines.
context_strings = [x[2] for x in self.info[-self.context_depth:]]
- self.label["text"] = '\n'.join(context_strings)
+ showfirst = 0 if context_strings[0] else 1
+ self.label["text"] = '\n'.join(context_strings[showfirst:])
def timer_event(self):
"Event on editor text widget triggered every UPDATEINTERVAL ms."
diff --git a/Lib/idlelib/config-extensions.def b/Lib/idlelib/config-extensions.def
index 41c3fbd239cfa1d..de884716d3bfd93 100644
--- a/Lib/idlelib/config-extensions.def
+++ b/Lib/idlelib/config-extensions.def
@@ -7,7 +7,8 @@
popupwait= 2000
[CodeContext]
-numlines= 15
+numlines= 3
+maxlines= 15
visible= False
bgcolor= LightGray
fgcolor= Black
diff --git a/Lib/idlelib/configdialog.py b/Lib/idlelib/configdialog.py
index 349a0d67dcf2922..d18753b9c16982a 100644
--- a/Lib/idlelib/configdialog.py
+++ b/Lib/idlelib/configdialog.py
@@ -1843,7 +1843,7 @@ def create_page_general(self):
self.format_width = tracers.add(
StringVar(self), ('extensions', 'FormatParagraph', 'max-width'))
self.context_lines = tracers.add(
- StringVar(self), ('extensions', 'CodeContext', 'numlines'))
+ StringVar(self), ('extensions', 'CodeContext', 'maxlines'))
# Create widgets:
# Section frames.
@@ -2012,7 +2012,7 @@ def load_general_cfg(self):
self.format_width.set(idleConf.GetOption(
'extensions', 'FormatParagraph', 'max-width', type='int'))
self.context_lines.set(idleConf.GetOption(
- 'extensions', 'CodeContext', 'numlines', type='int'))
+ 'extensions', 'CodeContext', 'maxlines', type='int'))
# Set additional help sources.
self.user_helplist = idleConf.GetAllExtraHelpSourcesList()
diff --git a/Lib/idlelib/idle_test/test_codecontext.py b/Lib/idlelib/idle_test/test_codecontext.py
index a5f17d4a885384c..4c775eee6b5fd1a 100644
--- a/Lib/idlelib/idle_test/test_codecontext.py
+++ b/Lib/idlelib/idle_test/test_codecontext.py
@@ -193,22 +193,26 @@ def test_update_code_context(self):
eq(cc.info, [(0, -1, '', False)])
eq(cc.topvisible, 1)
+ # Scroll down to line 1.
+ cc.text.yview(1)
+ cc.update_code_context()
+ eq(cc.info, [(0, -1, '', False)])
+ eq(cc.topvisible, 2)
+ eq(cc.label['text'], '')
+
# Scroll down to line 2.
cc.text.yview(2)
cc.update_code_context()
eq(cc.info, [(0, -1, '', False), (2, 0, 'class C1():', 'class')])
eq(cc.topvisible, 3)
- # context_depth is 3 so it pads with blank lines.
- eq(cc.label['text'], '\n'
- 'class C1():')
+ eq(cc.label['text'], 'class C1():')
# Scroll down to line 3. Since it's a comment, nothing changes.
cc.text.yview(3)
cc.update_code_context()
eq(cc.info, [(0, -1, '', False), (2, 0, 'class C1():', 'class')])
eq(cc.topvisible, 4)
- eq(cc.label['text'], '\n'
- 'class C1():')
+ eq(cc.label['text'], 'class C1():')
# Scroll down to line 4.
cc.text.yview(4)
@@ -217,8 +221,7 @@ def test_update_code_context(self):
(2, 0, 'class C1():', 'class'),
(4, 4, ' def __init__(self, a, b):', 'def')])
eq(cc.topvisible, 5)
- eq(cc.label['text'], '\n'
- 'class C1():\n'
+ eq(cc.label['text'], 'class C1():\n'
' def __init__(self, a, b):')
# Scroll down to line 11. Last 'def' is removed.
@@ -230,8 +233,7 @@ def test_update_code_context(self):
(8, 8, ' if a > b:', 'if'),
(10, 8, ' elif a < b:', 'elif')])
eq(cc.topvisible, 12)
- eq(cc.label['text'], '\n'
- 'class C1():\n'
+ eq(cc.label['text'], 'class C1():\n'
' def compare(self):\n'
' if a > b:\n'
' elif a < b:')
@@ -245,8 +247,7 @@ def test_update_code_context(self):
(8, 8, ' if a > b:', 'if'),
(10, 8, ' elif a < b:', 'elif')])
eq(cc.topvisible, 12)
- eq(cc.label['text'], '\n'
- 'class C1():\n'
+ eq(cc.label['text'], 'class C1():\n'
' def compare(self):\n'
' if a > b:\n'
' elif a < b:')
diff --git a/Lib/idlelib/idle_test/test_configdialog.py b/Lib/idlelib/idle_test/test_configdialog.py
index 982dc0b7eff7e13..26aba32c47e6aba 100644
--- a/Lib/idlelib/idle_test/test_configdialog.py
+++ b/Lib/idlelib/idle_test/test_configdialog.py
@@ -1170,7 +1170,7 @@ def test_paragraph(self):
def test_context(self):
self.page.context_int.delete(0, 'end')
self.page.context_int.insert(0, '1')
- self.assertEqual(extpage, {'CodeContext': {'numlines': '1'}})
+ self.assertEqual(extpage, {'CodeContext': {'maxlines': '1'}})
def test_source_selected(self):
d = self.page
From 7102a012014d534da600725805ad84899f452132 Mon Sep 17 00:00:00 2001
From: Terry Jan Reedy
Date: Thu, 31 May 2018 21:19:22 -0400
Subject: [PATCH 3/5] Remove unused and therefore unneeded options from
[CodeContext].
---
Lib/idlelib/config-extensions.def | 4 ----
1 file changed, 4 deletions(-)
diff --git a/Lib/idlelib/config-extensions.def b/Lib/idlelib/config-extensions.def
index de884716d3bfd93..7e23fb0a73d1d54 100644
--- a/Lib/idlelib/config-extensions.def
+++ b/Lib/idlelib/config-extensions.def
@@ -7,11 +7,7 @@
popupwait= 2000
[CodeContext]
-numlines= 3
maxlines= 15
-visible= False
-bgcolor= LightGray
-fgcolor= Black
[FormatParagraph]
max-width= 72
From 17a5e1b4e1a382932b31b0d16e0eead3ebca1f96 Mon Sep 17 00:00:00 2001
From: Terry Jan Reedy
Date: Thu, 31 May 2018 22:07:16 -0400
Subject: [PATCH 4/5] Add Code Context entry for Settings Help General tab.
---
Lib/idlelib/configdialog.py | 3 +++
1 file changed, 3 insertions(+)
diff --git a/Lib/idlelib/configdialog.py b/Lib/idlelib/configdialog.py
index d18753b9c16982a..28ad45863941378 100644
--- a/Lib/idlelib/configdialog.py
+++ b/Lib/idlelib/configdialog.py
@@ -2204,6 +2204,9 @@ def detach(self):
'opener' - opener '({[' corresponding to closer; 'parens' - both chars;
'expression' (default) - also everything in between. Flash-delay is how
long to highlight if cursor is not moved (0 means forever).
+
+CodeContext: Maxlines is the maximum number of code context lines to
+display when Code Context is turned on for an editor window.
'''
}
From 07007b1dd6ab2ccbb128600cfd0c8d1d3aca9615 Mon Sep 17 00:00:00 2001
From: Terry Jan Reedy
Date: Thu, 31 May 2018 22:46:04 -0400
Subject: [PATCH 5/5] Expand news entry.
---
.../next/IDLE/2018-05-24-20-42-44.bpo-33642.J0VQbS.rst | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/Misc/NEWS.d/next/IDLE/2018-05-24-20-42-44.bpo-33642.J0VQbS.rst b/Misc/NEWS.d/next/IDLE/2018-05-24-20-42-44.bpo-33642.J0VQbS.rst
index b61572a9cb9f5cb..d8ecb3938d43c51 100644
--- a/Misc/NEWS.d/next/IDLE/2018-05-24-20-42-44.bpo-33642.J0VQbS.rst
+++ b/Misc/NEWS.d/next/IDLE/2018-05-24-20-42-44.bpo-33642.J0VQbS.rst
@@ -1 +1,9 @@
-IDLE: Use variable number of lines in codecontext.py.
+IDLE: Only display actual code context lines.
+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.