mirror of https://github.com/python/cpython
Updated patch (CodeContext.061217.patch) to
[ 1362975 ] CodeContext - Improved text indentation Tal Einat 16Dec06
This commit is contained in:
parent
3f8aca1164
commit
4864b2bcd7
|
@ -10,6 +10,7 @@ not open blocks are not shown in the context hints pane.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
import Tkinter
|
import Tkinter
|
||||||
|
from Tkconstants import TOP, LEFT, X, W, SUNKEN
|
||||||
from configHandler import idleConf
|
from configHandler import idleConf
|
||||||
import re
|
import re
|
||||||
from sys import maxint as INFINITY
|
from sys import maxint as INFINITY
|
||||||
|
@ -24,7 +25,6 @@ getspacesfirstword =\
|
||||||
|
|
||||||
class CodeContext:
|
class CodeContext:
|
||||||
menudefs = [('options', [('!Code Conte_xt', '<<toggle-code-context>>')])]
|
menudefs = [('options', [('!Code Conte_xt', '<<toggle-code-context>>')])]
|
||||||
|
|
||||||
context_depth = idleConf.GetOption("extensions", "CodeContext",
|
context_depth = idleConf.GetOption("extensions", "CodeContext",
|
||||||
"numlines", type="int", default=3)
|
"numlines", type="int", default=3)
|
||||||
bgcolor = idleConf.GetOption("extensions", "CodeContext",
|
bgcolor = idleConf.GetOption("extensions", "CodeContext",
|
||||||
|
@ -54,66 +54,33 @@ class CodeContext:
|
||||||
|
|
||||||
def toggle_code_context_event(self, event=None):
|
def toggle_code_context_event(self, event=None):
|
||||||
if not self.label:
|
if not self.label:
|
||||||
# The following code attempts to figure out the required border
|
# Calculate the border width and horizontal padding required to
|
||||||
# width and vertical padding required for the CodeContext widget
|
# align the context with the text in the main Text widget.
|
||||||
# to be perfectly aligned with the text in the main Text widget.
|
|
||||||
# This is done by retrieving the appropriate attributes from the
|
|
||||||
# editwin.text and editwin.text_frame widgets.
|
|
||||||
#
|
#
|
||||||
# All values are passed through int(str(<value>)), since some
|
# All values are passed through int(str(<value>)), since some
|
||||||
# values may be pixel objects, which can't simply be added added
|
# values may be pixel objects, which can't simply be added to ints.
|
||||||
# to ints.
|
widgets = self.editwin.text, self.editwin.text_frame
|
||||||
#
|
# Calculate the required vertical padding
|
||||||
# This code is considered somewhat unstable since it relies on
|
|
||||||
# some of Tk's inner workings. However its effect is merely
|
|
||||||
# cosmetic; failure will only cause the CodeContext text to be
|
|
||||||
# somewhat misaligned with the text in the main Text widget.
|
|
||||||
#
|
|
||||||
# To avoid possible errors, all references to the inner workings
|
|
||||||
# of Tk are executed inside try/except blocks.
|
|
||||||
|
|
||||||
widgets_for_width_calc = self.editwin.text, self.editwin.text_frame
|
|
||||||
|
|
||||||
# calculate the required vertical padding
|
|
||||||
padx = 0
|
padx = 0
|
||||||
for widget in widgets_for_width_calc:
|
for widget in widgets:
|
||||||
try:
|
padx += int(str( widget.pack_info()['padx'] ))
|
||||||
# retrieve the "padx" attribte from widget's pack info
|
padx += int(str( widget.cget('padx') ))
|
||||||
padx += int(str( widget.pack_info()['padx'] ))
|
# Calculate the required border width
|
||||||
except:
|
border = 0
|
||||||
pass
|
for widget in widgets:
|
||||||
try:
|
border += int(str( widget.cget('border') ))
|
||||||
# retrieve the widget's "padx" attribte
|
|
||||||
padx += int(str( widget.cget('padx') ))
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# calculate the required border width
|
|
||||||
border_width = 0
|
|
||||||
for widget in widgets_for_width_calc:
|
|
||||||
try:
|
|
||||||
# retrieve the widget's "border" attribte
|
|
||||||
border_width += int(str( widget.cget('border') ))
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
self.label = Tkinter.Label(self.editwin.top,
|
self.label = Tkinter.Label(self.editwin.top,
|
||||||
text="\n" * (self.context_depth - 1),
|
text="\n" * (self.context_depth - 1),
|
||||||
anchor="w", justify="left",
|
anchor=W, justify=LEFT,
|
||||||
font=self.textfont,
|
font=self.textfont,
|
||||||
bg=self.bgcolor, fg=self.fgcolor,
|
bg=self.bgcolor, fg=self.fgcolor,
|
||||||
width=1, #don't request more than we get
|
width=1, #don't request more than we get
|
||||||
padx=padx, #line up with text widget
|
padx=padx, border=border,
|
||||||
border=border_width, #match border width
|
relief=SUNKEN)
|
||||||
relief="sunken",
|
# Pack the label widget before and above the text_frame widget,
|
||||||
)
|
# thus ensuring that it will appear directly above text_frame
|
||||||
|
self.label.pack(side=TOP, fill=X, expand=False,
|
||||||
# CodeContext's label widget is packed before and above the
|
|
||||||
# text_frame widget, thus ensuring that it will appear directly
|
|
||||||
# above it.
|
|
||||||
self.label.pack(side="top", fill="x", expand=False,
|
|
||||||
before=self.editwin.text_frame)
|
before=self.editwin.text_frame)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self.label.destroy()
|
self.label.destroy()
|
||||||
self.label = None
|
self.label = None
|
||||||
|
@ -190,7 +157,6 @@ class CodeContext:
|
||||||
stopindent)
|
stopindent)
|
||||||
self.info.extend(lines)
|
self.info.extend(lines)
|
||||||
self.topvisible = new_topvisible
|
self.topvisible = new_topvisible
|
||||||
|
|
||||||
# empty lines in context pane:
|
# empty lines in context pane:
|
||||||
context_strings = [""] * max(0, self.context_depth - len(self.info))
|
context_strings = [""] * max(0, self.context_depth - len(self.info))
|
||||||
# followed by the context hint lines:
|
# followed by the context hint lines:
|
||||||
|
|
Loading…
Reference in New Issue