2002-10-10 05:25:24 -03:00
|
|
|
"""CallTips.py - An IDLE Extension to Jog Your Memory
|
2000-08-14 22:13:23 -03:00
|
|
|
|
2002-12-12 15:15:39 -04:00
|
|
|
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.
|
2007-02-05 19:02:16 -04:00
|
|
|
|
2002-10-10 05:25:24 -03:00
|
|
|
"""
|
2014-01-26 20:55:07 -04:00
|
|
|
import __main__
|
2007-02-05 19:02:16 -04:00
|
|
|
import re
|
2002-10-10 05:25:24 -03:00
|
|
|
import sys
|
2014-01-26 20:55:07 -04:00
|
|
|
import textwrap
|
2000-08-14 22:13:23 -03:00
|
|
|
import types
|
|
|
|
|
2010-04-02 04:24:52 -03:00
|
|
|
from idlelib import CallTipWindow
|
|
|
|
from idlelib.HyperParser import HyperParser
|
2002-10-10 05:25:24 -03:00
|
|
|
|
|
|
|
|
2000-08-14 22:13:23 -03:00
|
|
|
class CallTips:
|
|
|
|
|
|
|
|
menudefs = [
|
2005-11-18 18:05:48 -04:00
|
|
|
('edit', [
|
|
|
|
("Show call tip", "<<force-open-calltip>>"),
|
|
|
|
])
|
2000-08-14 22:13:23 -03:00
|
|
|
]
|
|
|
|
|
2002-10-10 05:25:24 -03:00
|
|
|
def __init__(self, editwin=None):
|
2004-05-04 05:34:56 -03:00
|
|
|
if editwin is None: # subprocess and test
|
2002-10-10 05:25:24 -03:00
|
|
|
self.editwin = None
|
|
|
|
return
|
2000-08-14 22:13:23 -03:00
|
|
|
self.editwin = editwin
|
|
|
|
self.text = editwin.text
|
|
|
|
self.calltip = None
|
2002-10-10 05:25:24 -03:00
|
|
|
self._make_calltip_window = self._make_tk_calltip_window
|
2000-08-14 22:13:23 -03:00
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self._make_calltip_window = None
|
|
|
|
|
|
|
|
def _make_tk_calltip_window(self):
|
2002-10-10 05:25:24 -03:00
|
|
|
# See __init__ for usage
|
2000-08-14 22:13:23 -03:00
|
|
|
return CallTipWindow.CallTip(self.text)
|
|
|
|
|
2005-11-18 18:05:48 -04:00
|
|
|
def _remove_calltip_window(self, event=None):
|
2000-08-14 22:13:23 -03:00
|
|
|
if self.calltip:
|
|
|
|
self.calltip.hidetip()
|
|
|
|
self.calltip = None
|
2001-07-12 20:10:35 -03:00
|
|
|
|
2005-11-18 18:05:48 -04:00
|
|
|
def force_open_calltip_event(self, event):
|
|
|
|
"""Happens when the user really wants to open a CallTip, even if a
|
|
|
|
function call is needed.
|
|
|
|
"""
|
|
|
|
self.open_calltip(True)
|
2000-08-14 22:13:23 -03:00
|
|
|
|
2005-11-18 18:05:48 -04:00
|
|
|
def try_open_calltip_event(self, event):
|
|
|
|
"""Happens when it would be nice to open a CallTip, but not really
|
2009-02-21 16:27:01 -04:00
|
|
|
necessary, for example after an opening bracket, so function calls
|
2005-11-18 18:05:48 -04:00
|
|
|
won't be made.
|
|
|
|
"""
|
|
|
|
self.open_calltip(False)
|
|
|
|
|
|
|
|
def refresh_calltip_event(self, event):
|
|
|
|
"""If there is already a calltip window, check if it is still needed,
|
|
|
|
and if so, reload it.
|
|
|
|
"""
|
|
|
|
if self.calltip and self.calltip.is_active():
|
|
|
|
self.open_calltip(False)
|
2000-08-14 22:13:23 -03:00
|
|
|
|
2005-11-18 18:05:48 -04:00
|
|
|
def open_calltip(self, evalfuncs):
|
|
|
|
self._remove_calltip_window()
|
2002-10-10 05:25:24 -03:00
|
|
|
|
2005-11-18 18:05:48 -04:00
|
|
|
hp = HyperParser(self.editwin, "insert")
|
|
|
|
sur_paren = hp.get_surrounding_brackets('(')
|
|
|
|
if not sur_paren:
|
|
|
|
return
|
|
|
|
hp.set_index(sur_paren[0])
|
2012-06-03 01:58:36 -03:00
|
|
|
expression = hp.get_expression()
|
|
|
|
if not expression or (not evalfuncs and expression.find('(') != -1):
|
2005-11-18 18:05:48 -04:00
|
|
|
return
|
2012-06-03 01:58:36 -03:00
|
|
|
arg_text = self.fetch_tip(expression)
|
2005-11-18 18:05:48 -04:00
|
|
|
if not arg_text:
|
|
|
|
return
|
|
|
|
self.calltip = self._make_calltip_window()
|
|
|
|
self.calltip.showtip(arg_text, sur_paren[0], sur_paren[1])
|
2002-12-31 12:03:23 -04:00
|
|
|
|
2012-06-03 01:58:36 -03:00
|
|
|
def fetch_tip(self, expression):
|
2002-12-31 12:03:23 -04:00
|
|
|
"""Return the argument list and docstring of a function or class
|
|
|
|
|
2002-12-12 15:15:39 -04:00
|
|
|
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.
|
|
|
|
|
2007-02-05 19:02:16 -04:00
|
|
|
To find methods, fetch_tip must be fed a fully qualified name.
|
|
|
|
|
2002-12-31 12:03:23 -04:00
|
|
|
"""
|
2002-12-12 15:15:39 -04:00
|
|
|
try:
|
|
|
|
rpcclt = self.editwin.flist.pyshell.interp.rpcclt
|
2012-06-03 01:58:36 -03:00
|
|
|
except AttributeError:
|
2002-12-12 15:15:39 -04:00
|
|
|
rpcclt = None
|
2002-10-10 05:25:24 -03:00
|
|
|
if rpcclt:
|
|
|
|
return rpcclt.remotecall("exec", "get_the_calltip",
|
2012-06-03 01:58:36 -03:00
|
|
|
(expression,), {})
|
2002-10-10 05:25:24 -03:00
|
|
|
else:
|
2012-06-03 01:58:36 -03:00
|
|
|
entity = self.get_entity(expression)
|
2002-10-10 05:25:24 -03:00
|
|
|
return get_arg_text(entity)
|
|
|
|
|
2012-06-03 01:58:36 -03:00
|
|
|
def get_entity(self, expression):
|
|
|
|
"""Return the object corresponding to expression evaluated
|
|
|
|
in a namespace spanning sys.modules and __main.dict__.
|
|
|
|
"""
|
|
|
|
if expression:
|
2000-08-14 22:13:23 -03:00
|
|
|
namespace = sys.modules.copy()
|
|
|
|
namespace.update(__main__.__dict__)
|
|
|
|
try:
|
2012-06-03 01:58:36 -03:00
|
|
|
return eval(expression, namespace)
|
|
|
|
except BaseException:
|
|
|
|
# An uncaught exception closes idle, and eval can raise any
|
|
|
|
# exception, especially if user classes are involved.
|
2002-10-10 05:25:24 -03:00
|
|
|
return None
|
2000-08-14 22:13:23 -03:00
|
|
|
|
|
|
|
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.
|
|
|
|
try:
|
|
|
|
return class_ob.__init__.im_func
|
|
|
|
except AttributeError:
|
|
|
|
for base in class_ob.__bases__:
|
|
|
|
rc = _find_constructor(base)
|
|
|
|
if rc is not None: return rc
|
|
|
|
return None
|
|
|
|
|
2014-01-21 21:45:03 -04:00
|
|
|
# The following are used in get_arg_text
|
2014-01-26 20:55:07 -04:00
|
|
|
_MAX_COLS = 85
|
2014-01-21 21:45:03 -04:00
|
|
|
_MAX_LINES = 5 # enough for bytes
|
2014-01-26 20:55:07 -04:00
|
|
|
_INDENT = ' '*4 # for wrapped signatures
|
2014-01-21 21:45:03 -04:00
|
|
|
|
2000-08-14 22:13:23 -03:00
|
|
|
def get_arg_text(ob):
|
2014-01-21 21:45:03 -04:00
|
|
|
'''Return a string describing the signature of a callable object, or ''.
|
|
|
|
|
|
|
|
For Python-coded functions and methods, the first line is introspected.
|
|
|
|
Delete 'self' parameter for classes (.__init__) and bound methods.
|
|
|
|
The next lines are the first lines of the doc string up to the first
|
|
|
|
empty line or _MAX_LINES. For builtins, this typically includes
|
|
|
|
the arguments in addition to the return value.
|
|
|
|
'''
|
|
|
|
argspec = ""
|
2014-01-21 16:36:36 -04:00
|
|
|
try:
|
|
|
|
ob_call = ob.__call__
|
|
|
|
except BaseException:
|
2014-01-26 20:55:07 -04:00
|
|
|
if type(ob) is types.ClassType: # old-style
|
|
|
|
ob_call = ob
|
|
|
|
else:
|
|
|
|
return argspec
|
2014-01-21 16:36:36 -04:00
|
|
|
|
|
|
|
arg_offset = 0
|
|
|
|
if type(ob) in (types.ClassType, types.TypeType):
|
2014-01-26 20:55:07 -04:00
|
|
|
# Look for the first __init__ in the class chain with .im_func.
|
|
|
|
# Slot wrappers (builtins, classes defined in funcs) do not.
|
2014-01-21 16:36:36 -04:00
|
|
|
fob = _find_constructor(ob)
|
|
|
|
if fob is None:
|
|
|
|
fob = lambda: None
|
2014-01-21 01:26:10 -04:00
|
|
|
else:
|
2014-01-21 16:36:36 -04:00
|
|
|
arg_offset = 1
|
|
|
|
elif type(ob) == types.MethodType:
|
|
|
|
# bit of a hack for methods - turn it into a function
|
|
|
|
# and drop the "self" param for bound methods
|
|
|
|
fob = ob.im_func
|
|
|
|
if ob.im_self:
|
|
|
|
arg_offset = 1
|
|
|
|
elif type(ob_call) == types.MethodType:
|
|
|
|
# a callable class instance
|
|
|
|
fob = ob_call.im_func
|
|
|
|
arg_offset = 1
|
|
|
|
else:
|
|
|
|
fob = ob
|
|
|
|
# Try to build one for Python defined functions
|
|
|
|
if type(fob) in [types.FunctionType, types.LambdaType]:
|
|
|
|
argcount = fob.func_code.co_argcount
|
|
|
|
real_args = fob.func_code.co_varnames[arg_offset:argcount]
|
|
|
|
defaults = fob.func_defaults or []
|
|
|
|
defaults = list(map(lambda name: "=%s" % repr(name), defaults))
|
|
|
|
defaults = [""] * (len(real_args) - len(defaults)) + defaults
|
|
|
|
items = map(lambda arg, dflt: arg + dflt, real_args, defaults)
|
|
|
|
if fob.func_code.co_flags & 0x4:
|
|
|
|
items.append("*args")
|
|
|
|
if fob.func_code.co_flags & 0x8:
|
|
|
|
items.append("**kwds")
|
2014-01-21 21:45:03 -04:00
|
|
|
argspec = ", ".join(items)
|
|
|
|
argspec = "(%s)" % re.sub("(?<!\d)\.\d+", "<tuple>", argspec)
|
2014-01-26 20:55:07 -04:00
|
|
|
|
|
|
|
lines = (textwrap.wrap(argspec, _MAX_COLS, subsequent_indent=_INDENT)
|
|
|
|
if len(argspec) > _MAX_COLS else [argspec] if argspec else [])
|
|
|
|
|
2014-01-21 16:36:36 -04:00
|
|
|
if isinstance(ob_call, types.MethodType):
|
|
|
|
doc = ob_call.__doc__
|
|
|
|
else:
|
|
|
|
doc = getattr(ob, "__doc__", "")
|
|
|
|
if doc:
|
2014-01-26 20:55:07 -04:00
|
|
|
for line in doc.split('\n', _MAX_LINES)[:_MAX_LINES]:
|
2014-01-21 21:45:03 -04:00
|
|
|
line = line.strip()
|
|
|
|
if not line:
|
|
|
|
break
|
|
|
|
if len(line) > _MAX_COLS:
|
|
|
|
line = line[: _MAX_COLS - 3] + '...'
|
|
|
|
lines.append(line)
|
|
|
|
argspec = '\n'.join(lines)
|
|
|
|
return argspec
|
2000-08-14 22:13:23 -03:00
|
|
|
|
2014-01-21 01:26:10 -04:00
|
|
|
if __name__ == '__main__':
|
2013-05-30 15:47:33 -03:00
|
|
|
from unittest import main
|
2014-01-21 16:36:36 -04:00
|
|
|
main('idlelib.idle_test.test_calltips', verbosity=2)
|