"""CallTips.py - An IDLE Extension to Jog Your Memory
Call Tips are floating windows which display function, class, and method
parameter and docstring information when you type an opening parenthesis, and
which disappear when you type a closing parenthesis.
Future plans include extending the functionality to include class attributes.
def __init__(self
, editwin
=None):
if editwin
is None: # subprocess and test
self
._make
_calltip
_window
= self
._make
_tk
_calltip
_window
self
._make
_calltip
_window
= None
def _make_tk_calltip_window(self
):
return CallTipWindow
.CallTip(self
.text
)
def _remove_calltip_window(self
):
def paren_open_event(self
, event
):
self
._remove
_calltip
_window
()
name
= self
.get_name_at_cursor()
arg_text
= self
.fetch_tip(name
)
self
.calltip_start
= self
.text
.index("insert")
self
.calltip
= self
._make
_calltip
_window
()
self
.calltip
.showtip(arg_text
)
return "" #so the event is handled normally.
def paren_close_event(self
, event
):
# Now just hides, but later we should check if other
# paren'd expressions remain open.
self
._remove
_calltip
_window
()
return "" #so the event is handled normally.
def check_calltip_cancel_event(self
, event
):
# If we have moved before the start of the calltip,
# or off the calltip line, then cancel the tip.
# (Later need to be smarter about multi-line, etc)
if self
.text
.compare("insert", "<=", self
.calltip_start
) or \
self
.text
.compare("insert", ">", self
.calltip_start
self
._remove
_calltip
_window
()
return "" #so the event is handled normally.
def calltip_cancel_event(self
, event
):
self
._remove
_calltip
_window
()
return "" #so the event is handled normally.
__IDCHARS
= "._" + string
.ascii_letters
+ string
.digits
def get_name_at_cursor(self
):
str = self
.text
.get("insert linestart", "insert")
while i
and str[i
-1] in idchars
:
def fetch_tip(self
, name
):
"""Return the argument list and docstring of a function or class
If there is a Python subprocess, get the calltip there. Otherwise,
either fetch_tip() is running in the subprocess itself or it was called
in an IDLE EditorWindow before any script had been run.
The subprocess environment is that of the most recently run script. If
two unrelated modules are being edited some calltips in the current
module may be inoperative if the module was not the last to run.
rpcclt
= self
.editwin
.flist
.pyshell
.interp
.rpcclt
return rpcclt
.remotecall("exec", "get_the_calltip",
entity
= self
.get_entity(name
)
return get_arg_text(entity
)
def get_entity(self
, name
):
"Lookup name in a namespace spanning sys.modules and __main.dict__"
namespace
= sys
.modules
.copy()
namespace
.update(__main__
.__dict
__)
return eval(name
, namespace
)
def _find_constructor(class_ob
):
# Given a class object, return a function object used for the
# constructor (ie, __init__() ) or None if we can't find one.
return class_ob
.__init
__.im_func
for base
in class_ob
.__bases
__:
rc
= _find_constructor(base
)
if rc
is not None: return rc
"Get a string describing the arguments for the given object"
if type(ob
)==types
.ClassType
:
# Look for the highest __init__ in the class chain.
fob
= _find_constructor(ob
)
elif type(ob
)==types
.MethodType
:
# bit of a hack for methods - turn it into a function
# but we drop the "self" param.
# Try and build one for Python defined functions
if type(fob
) in [types
.FunctionType
, types
.LambdaType
]:
realArgs
= fob
.func_code
.co_varnames
[argOffset
:fob
.func_code
.co_argcount
]
defaults
= fob
.func_defaults
or []
defaults
= list(map(lambda name
: "=%s" % name
, defaults
))
defaults
= [""] * (len(realArgs
)-len(defaults
)) + defaults
items
= map(lambda arg
, dflt
: arg
+dflt
, realArgs
, defaults
)
if fob
.func_code
.co_flags
& 0x4:
if fob
.func_code
.co_flags
& 0x8:
argText
= ", ".join(items
)
argText
= "(%s)" % argText
# See if we can use the docstring
doc
= getattr(ob
, "__doc__", "")
#################################################
def t2(a
, b
=None): "(a, b=None)"
def t3(a
, *args
): "(a, ...)"
def t5(a
, *args
): "(a, ...)"
def t6(a
, b
=None, *args
, **kw
): "(a, b=None, ..., ***)"
def __init__(self
, a
=None, *b
): "(a=None, ...)"
def t2(self
, a
, b
=None): "(a, b=None)"
def t3(self
, a
, *args
): "(a, ...)"
def t4(self
, *args
): "(...)"
def t5(self
, a
, *args
): "(a, ...)"
def t6(self
, a
, b
=None, *args
, **kw
): "(a, b=None, ..., ***)"
expected
= t
.__doc
__ + "\n" + t
.__doc
__
arg_text
= ct
.fetch_tip(name
)
print "%s - expected %s, but got %s" % (t
, expected
,
print "%d of %d tests failed" % (len(failed
), len(tests
))
tests
= (t1
, t2
, t3
, t4
, t5
, t6
,
TC
, tc
.t1
, tc
.t2
, tc
.t3
, tc
.t4
, tc
.t5
, tc
.t6
)