Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d00587a

Browse files
committed
1. Add an Options menu entry: Code Context
2. Add a <<toggle-code-context>> envent to the [CodeContext] section of config-extensions.def and also a default-on variable, set to 0. 3. Update the help file to include Code Context. M CodeContext.py M config-extensions.def M help.txt
1 parent 610c7e0 commit d00587a

3 files changed

Lines changed: 43 additions & 27 deletions

File tree

Lib/idlelib/CodeContext.py

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,49 +13,61 @@
1313
import Tkinter
1414
from configHandler import idleConf
1515
from PyShell import PyShell
16-
from string import whitespace
1716
import re
1817

1918
BLOCKOPENERS = dict([(x, None) for x in ("class", "def", "elif", "else",
2019
"except", "finally", "for", "if",
2120
"try", "while")])
2221
INFINITY = 1 << 30
23-
UPDATEINTERVAL = 100 #ms
24-
FONTUPDATEINTERVAL = 1000 #ms
22+
UPDATEINTERVAL = 100 # millisec
23+
FONTUPDATEINTERVAL = 1000 # millisec
2524

2625
getspacesfirstword = lambda s, c=re.compile(r"^(\s*)(\w*)"): c.match(s).groups()
2726

2827
class CodeContext:
29-
menudefs = []
28+
menudefs = [('options', [('!Code Conte_xt', '<<toggle-code-context>>')])]
29+
3030
numlines = idleConf.GetOption("extensions", "CodeContext",
3131
"numlines", type="int", default=3)
3232
bgcolor = idleConf.GetOption("extensions", "CodeContext",
3333
"bgcolor", type="str", default="LightGray")
3434
fgcolor = idleConf.GetOption("extensions", "CodeContext",
3535
"fgcolor", type="str", default="Black")
36+
default_on = idleConf.GetOption("extensions", "CodeContext",
37+
"default_on", type="int", default=0)
3638
def __init__(self, editwin):
3739
if isinstance(editwin, PyShell):
3840
return
3941
self.editwin = editwin
4042
self.text = editwin.text
4143
self.textfont = self.text["font"]
42-
self.label = Tkinter.Label(self.editwin.top,
43-
text="\n" * (self.numlines - 1),
44-
anchor="w", justify="left",
45-
font=self.textfont,
46-
bg=self.bgcolor, fg=self.fgcolor,
47-
relief="sunken",
48-
width=1, # Don't request more than we get
49-
)
50-
self.label.pack(side="top", fill="x", expand=0,
51-
after=self.editwin.status_bar)
44+
self.label = None
5245
# Dummy line, which starts the "block" of the whole document:
5346
self.info = list(self.interesting_lines(1))
5447
self.lastfirstline = 1
48+
if self.default_on:
49+
self.toggle_code_context_event()
50+
self.editwin.setvar('<<toggle-code-context>>', True)
5551
# Start two update cycles, one for context lines, one for font changes.
5652
self.text.after(UPDATEINTERVAL, self.timer_event)
5753
self.text.after(FONTUPDATEINTERVAL, self.font_timer_event)
5854

55+
def toggle_code_context_event(self, event=None):
56+
if not self.label:
57+
self.label = Tkinter.Label(self.editwin.top,
58+
text="\n" * (self.numlines - 1),
59+
anchor="w", justify="left",
60+
font=self.textfont,
61+
bg=self.bgcolor, fg=self.fgcolor,
62+
relief="sunken",
63+
width=1, # Don't request more than we get
64+
)
65+
self.label.pack(side="top", fill="x", expand=0,
66+
after=self.editwin.status_bar)
67+
else:
68+
self.label.destroy()
69+
self.label = None
70+
5971
def get_line_info(self, linenum):
6072
"""Get the line indent value, text, and any block start keyword
6173
@@ -107,7 +119,6 @@ def update_label(self):
107119
del self.info[-1]
108120
if self.info[-1][0] == line_index:
109121
break
110-
# Add the block starting line info to tmpstack
111122
tmpstack.append((line_index, text))
112123
while tmpstack:
113124
self.info.append(tmpstack.pop())
@@ -116,12 +127,13 @@ def update_label(self):
116127
self.label["text"] = '\n'.join(lines)
117128

118129
def timer_event(self):
119-
self.update_label()
130+
if self.label:
131+
self.update_label()
120132
self.text.after(UPDATEINTERVAL, self.timer_event)
121133

122134
def font_timer_event(self):
123135
newtextfont = self.text["font"]
124-
if newtextfont != self.textfont:
136+
if self.label and newtextfont != self.textfont:
125137
self.textfont = newtextfont
126138
self.label["font"] = self.textfont
127139
self.text.after(FONTUPDATEINTERVAL, self.font_timer_event)

Lib/idlelib/config-extensions.def

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
#
44
# Each extension must have at least one section, named after the extension
55
# module. This section must contain an 'enable' item (=1 to enable the
6-
# extension, =0 to disable it) and also contain any other general
7-
# configuration items for the extension. Each extension may also define up to
8-
# two optional sections named ExtensionName_bindings and
9-
# ExtensionName_cfgBindings. If present, ExtensionName_bindings defines virtual
10-
# event bindings for the extension that are not sensibly re-configurable. If
11-
# present, ExtensionName_cfgBindings defines virtual event bindings for the
12-
# extension that may be sensibly re-configured.
6+
# extension, =0 to disable it) and also contain any other general configuration
7+
# items for the extension. Each extension must define at least one section
8+
# named ExtensionName_bindings or ExtensionName_cfgBindings. If present,
9+
# ExtensionName_bindings defines virtual event bindings for the extension that
10+
# are not user re-configurable. If present, ExtensionName_cfgBindings
11+
# defines virtual event bindings for the extension that may be sensibly
12+
# re-configured. If there are no keybindings for a menus' virtual events,
13+
# include lines like <<toggle-code-context>>= (See [CodeContext], below.)
1314

1415
# Currently it is necessary to manually modify this file to change extension
1516
# key bindings and default values. To customize, create
@@ -65,5 +66,8 @@ check-restore=<KeyPress>
6566
[CodeContext]
6667
enable=1
6768
numlines=3
69+
default_on=0
6870
bgcolor=LightGray
6971
fgcolor=Black
72+
[CodeContext_bindings]
73+
toggle-code-context=

Lib/idlelib/help.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ Options Menu:
8888
Startup Preferences may be set, and Additional Help
8989
Souces can be specified.
9090
---
91-
Revert to Default Settings -- Restore original settings. Not
92-
currently implemented - simply delete
93-
your .idlerc file.
91+
Code Context -- Open a pane at the top of the edit window which
92+
shows the block context of the section of code
93+
which is scrolling off the top or the window.
9494

9595
Windows Menu:
9696

0 commit comments

Comments
 (0)