blob: aa796cb47dcec4074ef567700bd71264af1d6896 [file] [log] [blame]
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +00001"""CallTips.py - An IDLE Extension to Jog Your Memory
David Scherer7aced172000-08-15 01:13:23 +00002
Kurt B. Kaisere54710b2002-12-12 19:15:39 +00003Call Tips are floating windows which display function, class, and method
4parameter and docstring information when you type an opening parenthesis, and
5which disappear when you type a closing parenthesis.
Thomas Wouterscf297e42007-02-23 15:07:44 +00006
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +00007"""
Thomas Wouterscf297e42007-02-23 15:07:44 +00008import re
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +00009import sys
David Scherer7aced172000-08-15 01:13:23 +000010import types
Kurt B. Kaiser152b3c22007-08-30 06:35:15 +000011import inspect
David Scherer7aced172000-08-15 01:13:23 +000012
Kurt B. Kaiser2d7f6a02007-08-22 23:01:33 +000013from idlelib import CallTipWindow
14from idlelib.HyperParser import HyperParser
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +000015
16import __main__
17
David Scherer7aced172000-08-15 01:13:23 +000018class CallTips:
19
20 menudefs = [
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000021 ('edit', [
22 ("Show call tip", "<<force-open-calltip>>"),
23 ])
David Scherer7aced172000-08-15 01:13:23 +000024 ]
25
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +000026 def __init__(self, editwin=None):
Raymond Hettingerc5e378d2004-05-04 08:34:56 +000027 if editwin is None: # subprocess and test
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +000028 self.editwin = None
Kurt B. Kaiser152b3c22007-08-30 06:35:15 +000029 else:
30 self.editwin = editwin
31 self.text = editwin.text
32 self.active_calltip = None
33 self._calltip_window = self._make_tk_calltip_window
David Scherer7aced172000-08-15 01:13:23 +000034
35 def close(self):
Kurt B. Kaiser152b3c22007-08-30 06:35:15 +000036 self._calltip_window = None
David Scherer7aced172000-08-15 01:13:23 +000037
David Scherer7aced172000-08-15 01:13:23 +000038 def _make_tk_calltip_window(self):
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +000039 # See __init__ for usage
David Scherer7aced172000-08-15 01:13:23 +000040 return CallTipWindow.CallTip(self.text)
41
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000042 def _remove_calltip_window(self, event=None):
Kurt B. Kaiser152b3c22007-08-30 06:35:15 +000043 if self.active_calltip:
44 self.active_calltip.hidetip()
45 self.active_calltip = None
Kurt B. Kaiserae676472001-07-12 23:10:35 +000046
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000047 def force_open_calltip_event(self, event):
Kurt B. Kaiser152b3c22007-08-30 06:35:15 +000048 "The user selected the menu entry or hotkey, open the tip."
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000049 self.open_calltip(True)
50
51 def try_open_calltip_event(self, event):
52 """Happens when it would be nice to open a CallTip, but not really
Kurt B. Kaiser152b3c22007-08-30 06:35:15 +000053 necessary, for example after an opening bracket, so function calls
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000054 won't be made.
55 """
56 self.open_calltip(False)
57
58 def refresh_calltip_event(self, event):
Kurt B. Kaiser152b3c22007-08-30 06:35:15 +000059 if self.active_calltip and self.active_calltip.is_active():
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000060 self.open_calltip(False)
61
62 def open_calltip(self, evalfuncs):
David Scherer7aced172000-08-15 01:13:23 +000063 self._remove_calltip_window()
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000064
65 hp = HyperParser(self.editwin, "insert")
66 sur_paren = hp.get_surrounding_brackets('(')
67 if not sur_paren:
68 return
69 hp.set_index(sur_paren[0])
70 name = hp.get_expression()
Kurt B. Kaiser152b3c22007-08-30 06:35:15 +000071 if not name:
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000072 return
Kurt B. Kaiser152b3c22007-08-30 06:35:15 +000073 if not evalfuncs and (name.find('(') != -1):
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000074 return
Kurt B. Kaiser152b3c22007-08-30 06:35:15 +000075 argspec = self.fetch_tip(name)
76 if not argspec:
77 return
78 self.active_calltip = self._calltip_window()
79 self.active_calltip.showtip(argspec, sur_paren[0], sur_paren[1])
Kurt B. Kaiser6655e4b2002-12-31 16:03:23 +000080
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +000081 def fetch_tip(self, name):
Kurt B. Kaiser152b3c22007-08-30 06:35:15 +000082 """Return the argument list and docstring of a function or class.
Kurt B. Kaiser6655e4b2002-12-31 16:03:23 +000083
Kurt B. Kaisere54710b2002-12-12 19:15:39 +000084 If there is a Python subprocess, get the calltip there. Otherwise,
Kurt B. Kaiser152b3c22007-08-30 06:35:15 +000085 either this fetch_tip() is running in the subprocess or it was
86 called in an IDLE running without the subprocess.
Kurt B. Kaisere54710b2002-12-12 19:15:39 +000087
88 The subprocess environment is that of the most recently run script. If
89 two unrelated modules are being edited some calltips in the current
90 module may be inoperative if the module was not the last to run.
91
Thomas Wouterscf297e42007-02-23 15:07:44 +000092 To find methods, fetch_tip must be fed a fully qualified name.
93
Kurt B. Kaiser6655e4b2002-12-31 16:03:23 +000094 """
Kurt B. Kaisere54710b2002-12-12 19:15:39 +000095 try:
96 rpcclt = self.editwin.flist.pyshell.interp.rpcclt
97 except:
98 rpcclt = None
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +000099 if rpcclt:
100 return rpcclt.remotecall("exec", "get_the_calltip",
101 (name,), {})
102 else:
103 entity = self.get_entity(name)
Kurt B. Kaiser152b3c22007-08-30 06:35:15 +0000104 return get_argspec(entity)
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +0000105
106 def get_entity(self, name):
Kurt B. Kaiser152b3c22007-08-30 06:35:15 +0000107 "Lookup name in a namespace spanning sys.modules and __main.dict__."
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +0000108 if name:
David Scherer7aced172000-08-15 01:13:23 +0000109 namespace = sys.modules.copy()
110 namespace.update(__main__.__dict__)
111 try:
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +0000112 return eval(name, namespace)
Terry Jan Reedyc5301ef2012-05-27 21:29:17 -0400113 # any exception is possible if evalfuncs True in open_calltip
114 # at least Syntax, Name, Attribute, Index, and Key E. if not
115 except:
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +0000116 return None
David Scherer7aced172000-08-15 01:13:23 +0000117
118def _find_constructor(class_ob):
Kurt B. Kaiser152b3c22007-08-30 06:35:15 +0000119 "Find the nearest __init__() in the class tree."
David Scherer7aced172000-08-15 01:13:23 +0000120 try:
Christian Heimesff737952007-11-27 10:40:20 +0000121 return class_ob.__init__.__func__
David Scherer7aced172000-08-15 01:13:23 +0000122 except AttributeError:
123 for base in class_ob.__bases__:
Kurt B. Kaiser152b3c22007-08-30 06:35:15 +0000124 init = _find_constructor(base)
125 if init:
126 return init
127 return None
David Scherer7aced172000-08-15 01:13:23 +0000128
Kurt B. Kaiser152b3c22007-08-30 06:35:15 +0000129def get_argspec(ob):
Terry Jan Reedyc5301ef2012-05-27 21:29:17 -0400130 """Get a string describing the arguments for the given object,
131 only if it is callable."""
Kurt B. Kaiser152b3c22007-08-30 06:35:15 +0000132 argspec = ""
Terry Jan Reedyc5301ef2012-05-27 21:29:17 -0400133 if ob is not None and hasattr(ob, '__call__'):
Guido van Rossum13257902007-06-07 23:15:56 +0000134 if isinstance(ob, type):
David Scherer7aced172000-08-15 01:13:23 +0000135 fob = _find_constructor(ob)
136 if fob is None:
137 fob = lambda: None
Martin v. Löwis95c95ce2007-07-22 14:41:55 +0000138 elif isinstance(ob, types.MethodType):
Christian Heimesff737952007-11-27 10:40:20 +0000139 fob = ob.__func__
David Scherer7aced172000-08-15 01:13:23 +0000140 else:
141 fob = ob
Guido van Rossum13257902007-06-07 23:15:56 +0000142 if isinstance(fob, (types.FunctionType, types.LambdaType)):
Kurt B. Kaiser152b3c22007-08-30 06:35:15 +0000143 argspec = inspect.formatargspec(*inspect.getfullargspec(fob))
144 pat = re.compile('self\,?\s*')
145 argspec = pat.sub("", argspec)
Kurt B. Kaiser6145a622003-07-23 15:42:14 +0000146 doc = getattr(ob, "__doc__", "")
Kurt B. Kaiser908aece2002-09-15 22:02:58 +0000147 if doc:
Kurt B. Kaiser6145a622003-07-23 15:42:14 +0000148 doc = doc.lstrip()
Kurt B. Kaiser908aece2002-09-15 22:02:58 +0000149 pos = doc.find("\n")
150 if pos < 0 or pos > 70:
151 pos = 70
Kurt B. Kaiser152b3c22007-08-30 06:35:15 +0000152 if argspec:
153 argspec += "\n"
154 argspec += doc[:pos]
155 return argspec
David Scherer7aced172000-08-15 01:13:23 +0000156
157#################################################
158#
159# Test code
160#
Kurt B. Kaiser152b3c22007-08-30 06:35:15 +0000161def main():
David Scherer7aced172000-08-15 01:13:23 +0000162 def t1(): "()"
163 def t2(a, b=None): "(a, b=None)"
Kurt B. Kaiser152b3c22007-08-30 06:35:15 +0000164 def t3(a, *args): "(a, *args)"
165 def t4(*args): "(*args)"
166 def t5(a, *args): "(a, *args)"
167 def t6(a, b=None, *args, **kw): "(a, b=None, *args, **kw)"
David Scherer7aced172000-08-15 01:13:23 +0000168
Thomas Wouterscf297e42007-02-23 15:07:44 +0000169 class TC(object):
Kurt B. Kaiser152b3c22007-08-30 06:35:15 +0000170 "(ai=None, *b)"
171 def __init__(self, ai=None, *b): "(ai=None, *b)"
David Scherer7aced172000-08-15 01:13:23 +0000172 def t1(self): "()"
Thomas Wouterscf297e42007-02-23 15:07:44 +0000173 def t2(self, ai, b=None): "(ai, b=None)"
Kurt B. Kaiser152b3c22007-08-30 06:35:15 +0000174 def t3(self, ai, *args): "(ai, *args)"
175 def t4(self, *args): "(*args)"
176 def t5(self, ai, *args): "(ai, *args)"
177 def t6(self, ai, b=None, *args, **kw): "(ai, b=None, *args, **kw)"
178
179 __main__.__dict__.update(locals())
David Scherer7aced172000-08-15 01:13:23 +0000180
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +0000181 def test(tests):
182 ct = CallTips()
David Scherer7aced172000-08-15 01:13:23 +0000183 failed=[]
184 for t in tests:
185 expected = t.__doc__ + "\n" + t.__doc__
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +0000186 name = t.__name__
Kurt B. Kaiser152b3c22007-08-30 06:35:15 +0000187 # exercise fetch_tip(), not just get_argspec()
Thomas Wouterscf297e42007-02-23 15:07:44 +0000188 try:
Christian Heimesff737952007-11-27 10:40:20 +0000189 qualified_name = "%s.%s" % (t.__self__.__class__.__name__, name)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000190 except AttributeError:
191 qualified_name = name
Kurt B. Kaiser152b3c22007-08-30 06:35:15 +0000192 argspec = ct.fetch_tip(qualified_name)
193 if argspec != expected:
David Scherer7aced172000-08-15 01:13:23 +0000194 failed.append(t)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000195 fmt = "%s - expected %s, but got %s"
Kurt B. Kaiser152b3c22007-08-30 06:35:15 +0000196 print(fmt % (t.__name__, expected, get_argspec(t)))
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000197 print("%d of %d tests failed" % (len(failed), len(tests)))
David Scherer7aced172000-08-15 01:13:23 +0000198
199 tc = TC()
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000200 tests = (t1, t2, t3, t4, t5, t6,
201 TC, tc.t1, tc.t2, tc.t3, tc.t4, tc.t5, tc.t6)
David Scherer7aced172000-08-15 01:13:23 +0000202
203 test(tests)
Kurt B. Kaiser152b3c22007-08-30 06:35:15 +0000204
205if __name__ == '__main__':
206 main()