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

Skip to content

Commit 4d5bc60

Browse files
committed
Noam Raphel: Further developemt of CodeContext feature.
The visibility state of the code context pane is now persistent between sessions and the pane does not appear in the shell window. M CodeContext.py M EditorWindow.py M NEWS.txt M PyShell.py M config-extensions.def M configHandler.py
1 parent cfa157d commit 4d5bc60

6 files changed

Lines changed: 86 additions & 48 deletions

File tree

Lib/idlelib/CodeContext.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""CodeContext - Display the block context of code at top of edit window
22
3-
Once code has scolled off the top of the screen, it can be difficult
3+
Once code has scrolled off the top of the screen, it can be difficult
44
to determine which block you are in. This extension implements a pane
55
at the top of each IDLE edit window which provides block structure
66
hints. These hints are the lines which contain the block opening
@@ -12,12 +12,11 @@
1212
"""
1313
import Tkinter
1414
from configHandler import idleConf
15-
from PyShell import PyShell
15+
from sets import Set
1616
import re
1717

18-
BLOCKOPENERS = dict([(x, None) for x in ("class", "def", "elif", "else",
19-
"except", "finally", "for", "if",
20-
"try", "while")])
18+
BLOCKOPENERS = Set(["class", "def", "elif", "else", "except", "finally", "for",
19+
"if", "try", "while"])
2120
INFINITY = 1 << 30
2221
UPDATEINTERVAL = 100 # millisec
2322
FONTUPDATEINTERVAL = 1000 # millisec
@@ -33,19 +32,17 @@ class CodeContext:
3332
"bgcolor", type="str", default="LightGray")
3433
fgcolor = idleConf.GetOption("extensions", "CodeContext",
3534
"fgcolor", type="str", default="Black")
36-
default_on = idleConf.GetOption("extensions", "CodeContext",
37-
"default_on", type="int", default=0)
3835
def __init__(self, editwin):
39-
if isinstance(editwin, PyShell):
40-
return
4136
self.editwin = editwin
4237
self.text = editwin.text
4338
self.textfont = self.text["font"]
4439
self.label = None
4540
# Dummy line, which starts the "block" of the whole document:
4641
self.info = list(self.interesting_lines(1))
4742
self.lastfirstline = 1
48-
if self.default_on:
43+
visible = idleConf.GetOption("extensions", "CodeContext",
44+
"visible", type="bool", default=False)
45+
if visible:
4946
self.toggle_code_context_event()
5047
self.editwin.setvar('<<toggle-code-context>>', True)
5148
# Start two update cycles, one for context lines, one for font changes.
@@ -67,6 +64,9 @@ def toggle_code_context_event(self, event=None):
6764
else:
6865
self.label.destroy()
6966
self.label = None
67+
idleConf.SetOption("extensions", "CodeContext", "visible",
68+
str(self.label is not None))
69+
idleConf.SaveUserCfgFiles()
7070

7171
def get_line_info(self, linenum):
7272
"""Get the line indent value, text, and any block start keyword

Lib/idlelib/EditorWindow.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -751,14 +751,16 @@ def load_standard_extensions(self):
751751
traceback.print_exc()
752752

753753
def get_standard_extension_names(self):
754-
return idleConf.GetExtensions()
754+
return idleConf.GetExtensions(editor_only=True)
755755

756756
def load_extension(self, name):
757757
mod = __import__(name, globals(), locals(), [])
758758
cls = getattr(mod, name)
759+
keydefs = idleConf.GetExtensionBindings(name)
760+
if hasattr(cls, "menudefs"):
761+
self.fill_menus(cls.menudefs, keydefs)
759762
ins = cls(self)
760763
self.extensions[name] = ins
761-
keydefs=idleConf.GetExtensionBindings(name)
762764
if keydefs:
763765
self.apply_bindings(keydefs)
764766
for vevent in keydefs.keys():
@@ -770,8 +772,6 @@ def load_extension(self, name):
770772
methodname = methodname + "_event"
771773
if hasattr(ins, methodname):
772774
self.text.bind(vevent, getattr(ins, methodname))
773-
if hasattr(ins, "menudefs"):
774-
self.fill_menus(ins.menudefs, keydefs)
775775
return ins
776776

777777
def apply_bindings(self, keydefs=None):

Lib/idlelib/NEWS.txt

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
What's New in IDLE 1.1a0?
2-
===================================
2+
=========================
33

44
*Release date: XX-XXX-2004*
55

6+
- CodeContext hint pane visibility state is now persistent across sessions.
7+
The pane no longer appears in the shell window. Added capability to limit
8+
extensions to shell window or editor windows. Noam Raphael addition
9+
to Patch 936169.
10+
11+
- Paragraph reformat width is now a configurable parameter in the
12+
Options GUI.
13+
614
- New Extension: CodeContext. Provides block structuring hints for code
715
which has scrolled above an edit window. Patch 936169 Noam Raphael.
816

@@ -52,7 +60,7 @@ What's New in IDLE 1.1a0?
5260

5361

5462
What's New in IDLE 1.0?
55-
===================================
63+
=======================
5664

5765
*Release date: 29-Jul-2003*
5866

@@ -61,15 +69,15 @@ What's New in IDLE 1.0?
6169

6270

6371
What's New in IDLE 1.0 release candidate 2?
64-
===================================
72+
===========================================
6573

6674
*Release date: 24-Jul-2003*
6775

6876
- Calltip error when docstring was None Python Bug 775541
6977

7078

7179
What's New in IDLE 1.0 release candidate 1?
72-
===================================
80+
===========================================
7381

7482
*Release date: 18-Jul-2003*
7583

@@ -90,7 +98,7 @@ What's New in IDLE 1.0 release candidate 1?
9098

9199

92100
What's New in IDLE 1.0b2?
93-
===================================
101+
=========================
94102

95103
*Release date: 29-Jun-2003*
96104

@@ -131,7 +139,7 @@ What's New in IDLE 1.0b2?
131139

132140

133141
What's New in IDLEfork 0.9b1?
134-
===================================
142+
=============================
135143

136144
*Release date: 02-Jun-2003*
137145

Lib/idlelib/PyShell.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,9 @@ def __init__(self, flist=None):
806806
#
807807
self.pollinterval = 50 # millisec
808808

809+
def get_standard_extension_names(self):
810+
return idleConf.GetExtensions(shell_only=True)
811+
809812
reading = False
810813
executing = False
811814
canceled = False

Lib/idlelib/config-extensions.def

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
1+
# config-extensions.def
2+
#
13
# IDLE reads several config files to determine user preferences. This
24
# file is the default configuration file for IDLE extensions settings.
35
#
46
# Each extension must have at least one section, named after the extension
57
# module. This section must contain an 'enable' item (=1 to enable the
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.)
14-
8+
# extension, =0 to disable it), it may contain 'enable_editor' or 'enable_shell'
9+
# items, to apply it only to editor/shell windows, and may also contain any
10+
# other general configuration items for the extension.
11+
#
12+
# Each extension must define at least one section named ExtensionName_bindings
13+
# or ExtensionName_cfgBindings. If present, ExtensionName_bindings defines
14+
# virtual event bindings for the extension that are not user re-configurable.
15+
# If present, ExtensionName_cfgBindings defines virtual event bindings for the
16+
# extension that may be sensibly re-configured.
17+
#
18+
# If there are no keybindings for a menus' virtual events, include lines like
19+
# <<toggle-code-context>>= (See [CodeContext], below.)
20+
#
1521
# Currently it is necessary to manually modify this file to change extension
1622
# key bindings and default values. To customize, create
1723
# ~/.idlerc/config-extensions.cfg and append the appropriate customized
1824
# section(s). Those sections will override the defaults in this file.
19-
25+
#
2026
# Note: If a keybinding is already in use when the extension is
2127
# loaded, the extension's virtual event's keybinding will be set to ''.
22-
28+
#
2329
# See config-keys.def for notes on specifying keys and extend.txt for
2430
# information on creating IDLE extensions.
2531

@@ -65,8 +71,9 @@ check-restore=<KeyPress>
6571

6672
[CodeContext]
6773
enable=1
74+
enable_shell=0
6875
numlines=3
69-
default_on=0
76+
visible=0
7077
bgcolor=LightGray
7178
fgcolor=Black
7279
[CodeContext_bindings]

Lib/idlelib/configHandler.py

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ def GetUserCfgDir(self):
215215
sys.stderr.write(warn)
216216
return userDir
217217

218-
def GetOption(self, configType, section, option, default=None, type=None):
218+
def GetOption(self, configType, section, option, default=None, type=None,
219+
warn_on_default=True):
219220
"""
220221
Get an option value for given config type and given general
221222
configuration section/option or return a default. If type is specified,
@@ -224,21 +225,30 @@ def GetOption(self, configType, section, option, default=None, type=None):
224225
fallback to a useable passed-in default if the option isn't present in
225226
either the user or the default configuration.
226227
configType must be one of ('main','extensions','highlight','keys')
227-
If a default is returned a warning is printed to stderr.
228+
If a default is returned, and warn_on_default is True, a warning is
229+
printed to stderr.
230+
228231
"""
229232
if self.userCfg[configType].has_option(section,option):
230233
return self.userCfg[configType].Get(section, option, type=type)
231234
elif self.defaultCfg[configType].has_option(section,option):
232235
return self.defaultCfg[configType].Get(section, option, type=type)
233236
else: #returning default, print warning
234-
warning=('\n Warning: configHandler.py - IdleConf.GetOption -\n'
235-
' problem retrieving configration option %r\n'
236-
' from section %r.\n'
237-
' returning default value: %r\n' %
238-
(option, section, default))
239-
sys.stderr.write(warning)
237+
if warn_on_default:
238+
warning = ('\n Warning: configHandler.py - IdleConf.GetOption -\n'
239+
' problem retrieving configration option %r\n'
240+
' from section %r.\n'
241+
' returning default value: %r\n' %
242+
(option, section, default))
243+
sys.stderr.write(warning)
240244
return default
241245

246+
def SetOption(self, configType, section, option, value):
247+
"""In user's config file, set section's option to value.
248+
249+
"""
250+
self.userCfg[configType].SetOption(section, option, value)
251+
242252
def GetSectionList(self, configSet, configType):
243253
"""
244254
Get a list of sections from either the user or default config for
@@ -356,10 +366,10 @@ def CurrentKeys(self):
356366
"""
357367
return self.GetOption('main','Keys','name',default='')
358368

359-
def GetExtensions(self, activeOnly=1):
369+
def GetExtensions(self, active_only=True, editor_only=False, shell_only=False):
360370
"""
361371
Gets a list of all idle extensions declared in the config files.
362-
activeOnly - boolean, if true only return active (enabled) extensions
372+
active_only - boolean, if true only return active (enabled) extensions
363373
"""
364374
extns=self.RemoveKeyBindNames(
365375
self.GetSectionList('default','extensions'))
@@ -368,13 +378,23 @@ def GetExtensions(self, activeOnly=1):
368378
for extn in userExtns:
369379
if extn not in extns: #user has added own extension
370380
extns.append(extn)
371-
if activeOnly:
381+
if active_only:
372382
activeExtns=[]
373383
for extn in extns:
374-
if self.GetOption('extensions',extn,'enable',default=1,
375-
type='bool'):
384+
if self.GetOption('extensions', extn, 'enable', default=True,
385+
type='bool'):
376386
#the extension is enabled
377-
activeExtns.append(extn)
387+
if editor_only or shell_only:
388+
if editor_only:
389+
option = "enable_editor"
390+
else:
391+
option = "enable_shell"
392+
if self.GetOption('extensions', extn,option,
393+
default=True, type='bool',
394+
warn_on_default=False):
395+
activeExtns.append(extn)
396+
else:
397+
activeExtns.append(extn)
378398
return activeExtns
379399
else:
380400
return extns
@@ -401,7 +421,7 @@ def GetExtnNameForEvent(self,virtualEvent):
401421
"""
402422
extName=None
403423
vEvent='<<'+virtualEvent+'>>'
404-
for extn in self.GetExtensions(activeOnly=0):
424+
for extn in self.GetExtensions(active_only=0):
405425
for event in self.GetExtensionKeys(extn).keys():
406426
if event == vEvent:
407427
extName=extn
@@ -482,7 +502,7 @@ def GetKeySet(self,keySetName):
482502
in an extension is already in use, that binding is disabled.
483503
"""
484504
keySet=self.GetCoreKeys(keySetName)
485-
activeExtns=self.GetExtensions(activeOnly=1)
505+
activeExtns=self.GetExtensions(active_only=1)
486506
for extn in activeExtns:
487507
extKeys=self.__GetRawExtensionKeys(extn)
488508
if extKeys: #the extension defines keybindings

0 commit comments

Comments
 (0)