blob: 413d211ca868046225768774495ac679ab69b8aa [file] [log] [blame]
Guido van Rossum23c115f1999-06-03 12:07:50 +00001# CallTips.py - An IDLE extension that provides "Call Tips" - ie, a floating window that
2# displays parameter information as you open parens.
3
Guido van Rossum85b97351999-06-02 16:10:19 +00004import string
5import sys
6import types
7
8class CallTips:
9
10 menudefs = [
11 ]
12
13 keydefs = {
14 '<<paren-open>>': ['<Key-parenleft>'],
15 '<<paren-close>>': ['<Key-parenright>'],
Guido van Rossum094189f1999-06-02 18:18:57 +000016 '<<check-calltip-cancel>>': ['<KeyRelease>'],
17 '<<calltip-cancel>>': ['<ButtonPress>', '<Key-Escape>'],
Guido van Rossum85b97351999-06-02 16:10:19 +000018 }
Guido van Rossum6290dab1999-06-02 18:12:55 +000019
Guido van Rossum85b97351999-06-02 16:10:19 +000020 windows_keydefs = {
21 }
22
23 unix_keydefs = {
24 }
25
26 def __init__(self, editwin):
27 self.editwin = editwin
28 self.text = editwin.text
29 self.calltip = None
30 if hasattr(self.text, "make_calltip_window"):
31 self._make_calltip_window = self.text.make_calltip_window
32 else:
33 self._make_calltip_window = self._make_tk_calltip_window
Guido van Rossum23c115f1999-06-03 12:07:50 +000034
35 # Makes a Tk based calltip window. Used by IDLE, but not Pythonwin.
36 # See __init__ above for how this is used.
Guido van Rossum85b97351999-06-02 16:10:19 +000037 def _make_tk_calltip_window(self):
38 import CallTipWindow
39 return CallTipWindow.CallTip(self.text)
40
41 def _remove_calltip_window(self):
42 if self.calltip:
43 self.calltip.hidetip()
44 self.calltip = None
45
46 def paren_open_event(self, event):
47 self._remove_calltip_window()
48 arg_text = get_arg_text(self.get_object_at_cursor())
49 if arg_text:
50 self.calltip_start = self.text.index("insert")
51 self.calltip = self._make_calltip_window()
52 self.calltip.showtip(arg_text)
Guido van Rossum6290dab1999-06-02 18:12:55 +000053 return "" #so the event is handled normally.
Guido van Rossum85b97351999-06-02 16:10:19 +000054
55 def paren_close_event(self, event):
56 # Now just hides, but later we should check if other
57 # paren'd expressions remain open.
Guido van Rossum85b97351999-06-02 16:10:19 +000058 self._remove_calltip_window()
Guido van Rossum6290dab1999-06-02 18:12:55 +000059 return "" #so the event is handled normally.
Guido van Rossum85b97351999-06-02 16:10:19 +000060
61 def check_calltip_cancel_event(self, event):
Guido van Rossum85b97351999-06-02 16:10:19 +000062 if self.calltip:
63 # If we have moved before the start of the calltip,
64 # or off the calltip line, then cancel the tip.
65 # (Later need to be smarter about multi-line, etc)
66 if self.text.compare("insert", "<=", self.calltip_start) or \
67 self.text.compare("insert", ">", self.calltip_start + " lineend"):
68 self._remove_calltip_window()
Guido van Rossum6290dab1999-06-02 18:12:55 +000069 return "" #so the event is handled normally.
70
71 def calltip_cancel_event(self, event):
72 self._remove_calltip_window()
73 return "" #so the event is handled normally.
74
Guido van Rossum85b97351999-06-02 16:10:19 +000075 def get_object_at_cursor(self,
76 wordchars="._" + string.uppercase + string.lowercase + string.digits):
Guido van Rossum23c115f1999-06-03 12:07:50 +000077 # XXX - This needs to be moved to a better place
78 # so the "." attribute lookup code can also use it.
Guido van Rossum85b97351999-06-02 16:10:19 +000079 text = self.text
Guido van Rossum6290dab1999-06-02 18:12:55 +000080 chars = text.get("insert linestart", "insert")
81 i = len(chars)
82 while i and chars[i-1] in wordchars:
83 i = i-1
84 word = chars[i:]
85 if word:
86 # How is this for a hack!
Guido van Rossum85b97351999-06-02 16:10:19 +000087 import sys, __main__
88 namespace = sys.modules.copy()
89 namespace.update(__main__.__dict__)
90 try:
91 return eval(word, namespace)
92 except:
93 pass
94 return None # Can't find an object.
Guido van Rossum6290dab1999-06-02 18:12:55 +000095
Guido van Rossumea827e91999-06-10 14:20:26 +000096def _find_constructor(class_ob):
97 # Given a class object, return a function object used for the
98 # constructor (ie, __init__() ) or None if we can't find one.
99 try:
100 return class_ob.__init__.im_func
101 except AttributeError:
102 for base in class_ob.__bases__:
103 rc = _find_constructor(base)
104 if rc is not None: return rc
105 return None
106
Guido van Rossum85b97351999-06-02 16:10:19 +0000107def get_arg_text(ob):
108 # Get a string describing the arguments for the given object.
109 argText = ""
110 if ob is not None:
111 argOffset = 0
Guido van Rossumea827e91999-06-10 14:20:26 +0000112 if type(ob)==types.ClassType:
113 # Look for the highest __init__ in the class chain.
114 fob = _find_constructor(ob)
115 if fob is None:
116 fob = lambda: None
117 else:
118 argOffset = 1
119 elif type(ob)==types.MethodType:
120 # bit of a hack for methods - turn it into a function
121 # but we drop the "self" param.
122 fob = ob.im_func
Guido van Rossum85b97351999-06-02 16:10:19 +0000123 argOffset = 1
Guido van Rossumea827e91999-06-10 14:20:26 +0000124 else:
125 fob = ob
Guido van Rossum85b97351999-06-02 16:10:19 +0000126 # Try and build one for Python defined functions
Guido van Rossumea827e91999-06-10 14:20:26 +0000127 if type(fob) in [types.FunctionType, types.LambdaType]:
Guido van Rossum85b97351999-06-02 16:10:19 +0000128 try:
Guido van Rossumea827e91999-06-10 14:20:26 +0000129 realArgs = fob.func_code.co_varnames[argOffset:fob.func_code.co_argcount]
130 defaults = fob.func_defaults or []
Guido van Rossum85b97351999-06-02 16:10:19 +0000131 defaults = list(map(lambda name: "=%s" % name, defaults))
132 defaults = [""] * (len(realArgs)-len(defaults)) + defaults
Guido van Rossum23c115f1999-06-03 12:07:50 +0000133 items = map(lambda arg, dflt: arg+dflt, realArgs, defaults)
Guido van Rossumea827e91999-06-10 14:20:26 +0000134 if fob.func_code.co_flags & 0x4:
Guido van Rossum23c115f1999-06-03 12:07:50 +0000135 items.append("...")
Guido van Rossumea827e91999-06-10 14:20:26 +0000136 if fob.func_code.co_flags & 0x8:
Guido van Rossum20731771999-06-09 20:34:57 +0000137 items.append("***")
Guido van Rossum23c115f1999-06-03 12:07:50 +0000138 argText = string.join(items , ", ")
Guido van Rossum85b97351999-06-02 16:10:19 +0000139 argText = "(%s)" % argText
140 except:
141 pass
Guido van Rossum23c115f1999-06-03 12:07:50 +0000142 # See if we can use the docstring
Guido van Rossum6290dab1999-06-02 18:12:55 +0000143 if hasattr(ob, "__doc__") and ob.__doc__:
Guido van Rossum85b97351999-06-02 16:10:19 +0000144 pos = string.find(ob.__doc__, "\n")
145 if pos<0 or pos>70: pos=70
Guido van Rossum6290dab1999-06-02 18:12:55 +0000146 if argText: argText = argText + "\n"
147 argText = argText + ob.__doc__[:pos]
Guido van Rossum85b97351999-06-02 16:10:19 +0000148
149 return argText
150
151#################################################
152#
153# Test code
154#
155if __name__=='__main__':
156
157 def t1(): "()"
158 def t2(a, b=None): "(a, b=None)"
159 def t3(a, *args): "(a, ...)"
160 def t4(*args): "(...)"
161 def t5(a, *args): "(a, ...)"
Guido van Rossumea827e91999-06-10 14:20:26 +0000162 def t6(a, b=None, *args, **kw): "(a, b=None, ..., ***)"
Guido van Rossum85b97351999-06-02 16:10:19 +0000163
164 class TC:
Guido van Rossumea827e91999-06-10 14:20:26 +0000165 "(a=None, ...)"
166 def __init__(self, a=None, *b): "(a=None, ...)"
Guido van Rossum85b97351999-06-02 16:10:19 +0000167 def t1(self): "()"
168 def t2(self, a, b=None): "(a, b=None)"
169 def t3(self, a, *args): "(a, ...)"
170 def t4(self, *args): "(...)"
171 def t5(self, a, *args): "(a, ...)"
Guido van Rossumea827e91999-06-10 14:20:26 +0000172 def t6(self, a, b=None, *args, **kw): "(a, b=None, ..., ***)"
Guido van Rossum85b97351999-06-02 16:10:19 +0000173
Guido van Rossum85b97351999-06-02 16:10:19 +0000174 def test( tests ):
175 failed=[]
176 for t in tests:
Guido van Rossumea827e91999-06-10 14:20:26 +0000177 expected = t.__doc__ + "\n" + t.__doc__
178 if get_arg_text(t) != expected:
Guido van Rossum85b97351999-06-02 16:10:19 +0000179 failed.append(t)
Guido van Rossumea827e91999-06-10 14:20:26 +0000180 print "%s - expected %s, but got %s" % (t, `expected`, `get_arg_text(t)`)
Guido van Rossum85b97351999-06-02 16:10:19 +0000181 print "%d of %d tests failed" % (len(failed), len(tests))
182
Guido van Rossum6290dab1999-06-02 18:12:55 +0000183 tc = TC()
184 tests = t1, t2, t3, t4, t5, t6, \
Guido van Rossumea827e91999-06-10 14:20:26 +0000185 TC, tc.t1, tc.t2, tc.t3, tc.t4, tc.t5, tc.t6
Guido van Rossum85b97351999-06-02 16:10:19 +0000186
187 test(tests)