blob: 8d35d5c1305f8bbd28d6d49509977155d654abe8 [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
Guido van Rossum85b97351999-06-02 16:10:19 +00005import types
6
7class CallTips:
8
9 menudefs = [
10 ]
11
12 keydefs = {
13 '<<paren-open>>': ['<Key-parenleft>'],
14 '<<paren-close>>': ['<Key-parenright>'],
Guido van Rossum094189f1999-06-02 18:18:57 +000015 '<<check-calltip-cancel>>': ['<KeyRelease>'],
16 '<<calltip-cancel>>': ['<ButtonPress>', '<Key-Escape>'],
Guido van Rossum85b97351999-06-02 16:10:19 +000017 }
Guido van Rossum6290dab1999-06-02 18:12:55 +000018
Guido van Rossum85b97351999-06-02 16:10:19 +000019 windows_keydefs = {
20 }
21
22 unix_keydefs = {
23 }
24
25 def __init__(self, editwin):
26 self.editwin = editwin
27 self.text = editwin.text
28 self.calltip = None
29 if hasattr(self.text, "make_calltip_window"):
30 self._make_calltip_window = self.text.make_calltip_window
31 else:
32 self._make_calltip_window = self._make_tk_calltip_window
Guido van Rossum23c115f1999-06-03 12:07:50 +000033
Guido van Rossume689f001999-06-25 16:02:22 +000034 def close(self):
35 self._make_calltip_window = None
36
Guido van Rossum23c115f1999-06-03 12:07:50 +000037 # Makes a Tk based calltip window. Used by IDLE, but not Pythonwin.
38 # See __init__ above for how this is used.
Guido van Rossum85b97351999-06-02 16:10:19 +000039 def _make_tk_calltip_window(self):
40 import CallTipWindow
41 return CallTipWindow.CallTip(self.text)
42
43 def _remove_calltip_window(self):
44 if self.calltip:
45 self.calltip.hidetip()
46 self.calltip = None
Tim Peters70c43782001-01-17 08:48:39 +000047
Guido van Rossum85b97351999-06-02 16:10:19 +000048 def paren_open_event(self, event):
49 self._remove_calltip_window()
50 arg_text = get_arg_text(self.get_object_at_cursor())
51 if arg_text:
52 self.calltip_start = self.text.index("insert")
53 self.calltip = self._make_calltip_window()
54 self.calltip.showtip(arg_text)
Guido van Rossum6290dab1999-06-02 18:12:55 +000055 return "" #so the event is handled normally.
Guido van Rossum85b97351999-06-02 16:10:19 +000056
57 def paren_close_event(self, event):
58 # Now just hides, but later we should check if other
59 # paren'd expressions remain open.
Guido van Rossum85b97351999-06-02 16:10:19 +000060 self._remove_calltip_window()
Guido van Rossum6290dab1999-06-02 18:12:55 +000061 return "" #so the event is handled normally.
Guido van Rossum85b97351999-06-02 16:10:19 +000062
63 def check_calltip_cancel_event(self, event):
Guido van Rossum85b97351999-06-02 16:10:19 +000064 if self.calltip:
65 # If we have moved before the start of the calltip,
66 # or off the calltip line, then cancel the tip.
67 # (Later need to be smarter about multi-line, etc)
68 if self.text.compare("insert", "<=", self.calltip_start) or \
69 self.text.compare("insert", ">", self.calltip_start + " lineend"):
70 self._remove_calltip_window()
Guido van Rossum6290dab1999-06-02 18:12:55 +000071 return "" #so the event is handled normally.
72
73 def calltip_cancel_event(self, event):
74 self._remove_calltip_window()
75 return "" #so the event is handled normally.
76
Guido van Rossum85b97351999-06-02 16:10:19 +000077 def get_object_at_cursor(self,
Martin v. Löwis5428fff2002-08-05 14:53:52 +000078 wordchars="._" + string.ascii_letters + string.digits):
79 # Usage of ascii_letters is necessary to avoid UnicodeErrors
80 # if chars contains non-ASCII.
Tim Peters280488b2002-08-23 18:19:30 +000081
Guido van Rossum23c115f1999-06-03 12:07:50 +000082 # XXX - This needs to be moved to a better place
83 # so the "." attribute lookup code can also use it.
Guido van Rossum85b97351999-06-02 16:10:19 +000084 text = self.text
Guido van Rossum6290dab1999-06-02 18:12:55 +000085 chars = text.get("insert linestart", "insert")
86 i = len(chars)
87 while i and chars[i-1] in wordchars:
88 i = i-1
89 word = chars[i:]
90 if word:
91 # How is this for a hack!
Guido van Rossum85b97351999-06-02 16:10:19 +000092 import sys, __main__
93 namespace = sys.modules.copy()
94 namespace.update(__main__.__dict__)
95 try:
Tim Peters70c43782001-01-17 08:48:39 +000096 return eval(word, namespace)
Guido van Rossum85b97351999-06-02 16:10:19 +000097 except:
Tim Peters70c43782001-01-17 08:48:39 +000098 pass
Guido van Rossum85b97351999-06-02 16:10:19 +000099 return None # Can't find an object.
Guido van Rossum6290dab1999-06-02 18:12:55 +0000100
Guido van Rossumea827e91999-06-10 14:20:26 +0000101def _find_constructor(class_ob):
102 # Given a class object, return a function object used for the
103 # constructor (ie, __init__() ) or None if we can't find one.
104 try:
105 return class_ob.__init__.im_func
106 except AttributeError:
107 for base in class_ob.__bases__:
108 rc = _find_constructor(base)
109 if rc is not None: return rc
110 return None
111
Guido van Rossum85b97351999-06-02 16:10:19 +0000112def get_arg_text(ob):
113 # Get a string describing the arguments for the given object.
114 argText = ""
115 if ob is not None:
116 argOffset = 0
Guido van Rossumea827e91999-06-10 14:20:26 +0000117 if type(ob)==types.ClassType:
118 # Look for the highest __init__ in the class chain.
119 fob = _find_constructor(ob)
120 if fob is None:
121 fob = lambda: None
122 else:
123 argOffset = 1
124 elif type(ob)==types.MethodType:
125 # bit of a hack for methods - turn it into a function
126 # but we drop the "self" param.
127 fob = ob.im_func
Guido van Rossum85b97351999-06-02 16:10:19 +0000128 argOffset = 1
Guido van Rossumea827e91999-06-10 14:20:26 +0000129 else:
130 fob = ob
Guido van Rossum85b97351999-06-02 16:10:19 +0000131 # Try and build one for Python defined functions
Guido van Rossumea827e91999-06-10 14:20:26 +0000132 if type(fob) in [types.FunctionType, types.LambdaType]:
Guido van Rossum85b97351999-06-02 16:10:19 +0000133 try:
Guido van Rossumea827e91999-06-10 14:20:26 +0000134 realArgs = fob.func_code.co_varnames[argOffset:fob.func_code.co_argcount]
135 defaults = fob.func_defaults or []
Guido van Rossum85b97351999-06-02 16:10:19 +0000136 defaults = list(map(lambda name: "=%s" % name, defaults))
137 defaults = [""] * (len(realArgs)-len(defaults)) + defaults
Guido van Rossum23c115f1999-06-03 12:07:50 +0000138 items = map(lambda arg, dflt: arg+dflt, realArgs, defaults)
Guido van Rossumea827e91999-06-10 14:20:26 +0000139 if fob.func_code.co_flags & 0x4:
Guido van Rossum23c115f1999-06-03 12:07:50 +0000140 items.append("...")
Guido van Rossumea827e91999-06-10 14:20:26 +0000141 if fob.func_code.co_flags & 0x8:
Guido van Rossum20731771999-06-09 20:34:57 +0000142 items.append("***")
Walter Dörwaldaaab30e2002-09-11 20:36:02 +0000143 argText = ", ".join(items)
Guido van Rossum85b97351999-06-02 16:10:19 +0000144 argText = "(%s)" % argText
145 except:
146 pass
Guido van Rossum23c115f1999-06-03 12:07:50 +0000147 # See if we can use the docstring
Tim Petersa2e2dbe2001-09-16 02:19:49 +0000148 doc = getattr(ob, "__doc__", "")
149 if doc:
150 while doc[:1] in " \t\n":
151 doc = doc[1:]
152 pos = doc.find("\n")
153 if pos < 0 or pos > 70:
154 pos = 70
155 if argText:
156 argText += "\n"
157 argText += doc[:pos]
Guido van Rossum85b97351999-06-02 16:10:19 +0000158
159 return argText
160
161#################################################
162#
163# Test code
164#
165if __name__=='__main__':
166
167 def t1(): "()"
168 def t2(a, b=None): "(a, b=None)"
169 def t3(a, *args): "(a, ...)"
170 def t4(*args): "(...)"
171 def t5(a, *args): "(a, ...)"
Guido van Rossumea827e91999-06-10 14:20:26 +0000172 def t6(a, b=None, *args, **kw): "(a, b=None, ..., ***)"
Guido van Rossum85b97351999-06-02 16:10:19 +0000173
174 class TC:
Guido van Rossumea827e91999-06-10 14:20:26 +0000175 "(a=None, ...)"
176 def __init__(self, a=None, *b): "(a=None, ...)"
Guido van Rossum85b97351999-06-02 16:10:19 +0000177 def t1(self): "()"
178 def t2(self, a, b=None): "(a, b=None)"
179 def t3(self, a, *args): "(a, ...)"
180 def t4(self, *args): "(...)"
181 def t5(self, a, *args): "(a, ...)"
Guido van Rossumea827e91999-06-10 14:20:26 +0000182 def t6(self, a, b=None, *args, **kw): "(a, b=None, ..., ***)"
Guido van Rossum85b97351999-06-02 16:10:19 +0000183
Guido van Rossum85b97351999-06-02 16:10:19 +0000184 def test( tests ):
185 failed=[]
186 for t in tests:
Guido van Rossumea827e91999-06-10 14:20:26 +0000187 expected = t.__doc__ + "\n" + t.__doc__
188 if get_arg_text(t) != expected:
Guido van Rossum85b97351999-06-02 16:10:19 +0000189 failed.append(t)
Guido van Rossumea827e91999-06-10 14:20:26 +0000190 print "%s - expected %s, but got %s" % (t, `expected`, `get_arg_text(t)`)
Guido van Rossum85b97351999-06-02 16:10:19 +0000191 print "%d of %d tests failed" % (len(failed), len(tests))
192
Guido van Rossum6290dab1999-06-02 18:12:55 +0000193 tc = TC()
194 tests = t1, t2, t3, t4, t5, t6, \
Guido van Rossumea827e91999-06-10 14:20:26 +0000195 TC, tc.t1, tc.t2, tc.t3, tc.t4, tc.t5, tc.t6
Guido van Rossum85b97351999-06-02 16:10:19 +0000196
197 test(tests)