blob: 0a9eb30c3b754f1efc4f0c7258fc5d14fe0e58b0 [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
Guido van Rossume689f001999-06-25 16:02:22 +000035 def close(self):
36 self._make_calltip_window = None
37
Guido van Rossum23c115f1999-06-03 12:07:50 +000038 # Makes a Tk based calltip window. Used by IDLE, but not Pythonwin.
39 # See __init__ above for how this is used.
Guido van Rossum85b97351999-06-02 16:10:19 +000040 def _make_tk_calltip_window(self):
41 import CallTipWindow
42 return CallTipWindow.CallTip(self.text)
43
44 def _remove_calltip_window(self):
45 if self.calltip:
46 self.calltip.hidetip()
47 self.calltip = None
Tim Peters70c43782001-01-17 08:48:39 +000048
Guido van Rossum85b97351999-06-02 16:10:19 +000049 def paren_open_event(self, event):
50 self._remove_calltip_window()
51 arg_text = get_arg_text(self.get_object_at_cursor())
52 if arg_text:
53 self.calltip_start = self.text.index("insert")
54 self.calltip = self._make_calltip_window()
55 self.calltip.showtip(arg_text)
Guido van Rossum6290dab1999-06-02 18:12:55 +000056 return "" #so the event is handled normally.
Guido van Rossum85b97351999-06-02 16:10:19 +000057
58 def paren_close_event(self, event):
59 # Now just hides, but later we should check if other
60 # paren'd expressions remain open.
Guido van Rossum85b97351999-06-02 16:10:19 +000061 self._remove_calltip_window()
Guido van Rossum6290dab1999-06-02 18:12:55 +000062 return "" #so the event is handled normally.
Guido van Rossum85b97351999-06-02 16:10:19 +000063
64 def check_calltip_cancel_event(self, event):
Guido van Rossum85b97351999-06-02 16:10:19 +000065 if self.calltip:
66 # If we have moved before the start of the calltip,
67 # or off the calltip line, then cancel the tip.
68 # (Later need to be smarter about multi-line, etc)
69 if self.text.compare("insert", "<=", self.calltip_start) or \
70 self.text.compare("insert", ">", self.calltip_start + " lineend"):
71 self._remove_calltip_window()
Guido van Rossum6290dab1999-06-02 18:12:55 +000072 return "" #so the event is handled normally.
73
74 def calltip_cancel_event(self, event):
75 self._remove_calltip_window()
76 return "" #so the event is handled normally.
77
Guido van Rossum85b97351999-06-02 16:10:19 +000078 def get_object_at_cursor(self,
79 wordchars="._" + string.uppercase + string.lowercase + string.digits):
Guido van Rossum23c115f1999-06-03 12:07:50 +000080 # XXX - This needs to be moved to a better place
81 # so the "." attribute lookup code can also use it.
Guido van Rossum85b97351999-06-02 16:10:19 +000082 text = self.text
Guido van Rossum6290dab1999-06-02 18:12:55 +000083 chars = text.get("insert linestart", "insert")
84 i = len(chars)
85 while i and chars[i-1] in wordchars:
86 i = i-1
87 word = chars[i:]
88 if word:
89 # How is this for a hack!
Guido van Rossum85b97351999-06-02 16:10:19 +000090 import sys, __main__
91 namespace = sys.modules.copy()
92 namespace.update(__main__.__dict__)
93 try:
Tim Peters70c43782001-01-17 08:48:39 +000094 return eval(word, namespace)
Guido van Rossum85b97351999-06-02 16:10:19 +000095 except:
Tim Peters70c43782001-01-17 08:48:39 +000096 pass
Guido van Rossum85b97351999-06-02 16:10:19 +000097 return None # Can't find an object.
Guido van Rossum6290dab1999-06-02 18:12:55 +000098
Guido van Rossumea827e91999-06-10 14:20:26 +000099def _find_constructor(class_ob):
100 # Given a class object, return a function object used for the
101 # constructor (ie, __init__() ) or None if we can't find one.
102 try:
103 return class_ob.__init__.im_func
104 except AttributeError:
105 for base in class_ob.__bases__:
106 rc = _find_constructor(base)
107 if rc is not None: return rc
108 return None
109
Guido van Rossum85b97351999-06-02 16:10:19 +0000110def get_arg_text(ob):
111 # Get a string describing the arguments for the given object.
112 argText = ""
113 if ob is not None:
114 argOffset = 0
Guido van Rossumea827e91999-06-10 14:20:26 +0000115 if type(ob)==types.ClassType:
116 # Look for the highest __init__ in the class chain.
117 fob = _find_constructor(ob)
118 if fob is None:
119 fob = lambda: None
120 else:
121 argOffset = 1
122 elif type(ob)==types.MethodType:
123 # bit of a hack for methods - turn it into a function
124 # but we drop the "self" param.
125 fob = ob.im_func
Guido van Rossum85b97351999-06-02 16:10:19 +0000126 argOffset = 1
Guido van Rossumea827e91999-06-10 14:20:26 +0000127 else:
128 fob = ob
Guido van Rossum85b97351999-06-02 16:10:19 +0000129 # Try and build one for Python defined functions
Guido van Rossumea827e91999-06-10 14:20:26 +0000130 if type(fob) in [types.FunctionType, types.LambdaType]:
Guido van Rossum85b97351999-06-02 16:10:19 +0000131 try:
Guido van Rossumea827e91999-06-10 14:20:26 +0000132 realArgs = fob.func_code.co_varnames[argOffset:fob.func_code.co_argcount]
133 defaults = fob.func_defaults or []
Guido van Rossum85b97351999-06-02 16:10:19 +0000134 defaults = list(map(lambda name: "=%s" % name, defaults))
135 defaults = [""] * (len(realArgs)-len(defaults)) + defaults
Guido van Rossum23c115f1999-06-03 12:07:50 +0000136 items = map(lambda arg, dflt: arg+dflt, realArgs, defaults)
Guido van Rossumea827e91999-06-10 14:20:26 +0000137 if fob.func_code.co_flags & 0x4:
Guido van Rossum23c115f1999-06-03 12:07:50 +0000138 items.append("...")
Guido van Rossumea827e91999-06-10 14:20:26 +0000139 if fob.func_code.co_flags & 0x8:
Guido van Rossum20731771999-06-09 20:34:57 +0000140 items.append("***")
Guido van Rossum23c115f1999-06-03 12:07:50 +0000141 argText = string.join(items , ", ")
Guido van Rossum85b97351999-06-02 16:10:19 +0000142 argText = "(%s)" % argText
143 except:
144 pass
Guido van Rossum23c115f1999-06-03 12:07:50 +0000145 # See if we can use the docstring
Tim Petersa2e2dbe2001-09-16 02:19:49 +0000146 doc = getattr(ob, "__doc__", "")
147 if doc:
148 while doc[:1] in " \t\n":
149 doc = doc[1:]
150 pos = doc.find("\n")
151 if pos < 0 or pos > 70:
152 pos = 70
153 if argText:
154 argText += "\n"
155 argText += doc[:pos]
Guido van Rossum85b97351999-06-02 16:10:19 +0000156
157 return argText
158
159#################################################
160#
161# Test code
162#
163if __name__=='__main__':
164
165 def t1(): "()"
166 def t2(a, b=None): "(a, b=None)"
167 def t3(a, *args): "(a, ...)"
168 def t4(*args): "(...)"
169 def t5(a, *args): "(a, ...)"
Guido van Rossumea827e91999-06-10 14:20:26 +0000170 def t6(a, b=None, *args, **kw): "(a, b=None, ..., ***)"
Guido van Rossum85b97351999-06-02 16:10:19 +0000171
172 class TC:
Guido van Rossumea827e91999-06-10 14:20:26 +0000173 "(a=None, ...)"
174 def __init__(self, a=None, *b): "(a=None, ...)"
Guido van Rossum85b97351999-06-02 16:10:19 +0000175 def t1(self): "()"
176 def t2(self, a, b=None): "(a, b=None)"
177 def t3(self, a, *args): "(a, ...)"
178 def t4(self, *args): "(...)"
179 def t5(self, a, *args): "(a, ...)"
Guido van Rossumea827e91999-06-10 14:20:26 +0000180 def t6(self, a, b=None, *args, **kw): "(a, b=None, ..., ***)"
Guido van Rossum85b97351999-06-02 16:10:19 +0000181
Guido van Rossum85b97351999-06-02 16:10:19 +0000182 def test( tests ):
183 failed=[]
184 for t in tests:
Guido van Rossumea827e91999-06-10 14:20:26 +0000185 expected = t.__doc__ + "\n" + t.__doc__
186 if get_arg_text(t) != expected:
Guido van Rossum85b97351999-06-02 16:10:19 +0000187 failed.append(t)
Guido van Rossumea827e91999-06-10 14:20:26 +0000188 print "%s - expected %s, but got %s" % (t, `expected`, `get_arg_text(t)`)
Guido van Rossum85b97351999-06-02 16:10:19 +0000189 print "%d of %d tests failed" % (len(failed), len(tests))
190
Guido van Rossum6290dab1999-06-02 18:12:55 +0000191 tc = TC()
192 tests = t1, t2, t3, t4, t5, t6, \
Guido van Rossumea827e91999-06-10 14:20:26 +0000193 TC, tc.t1, tc.t2, tc.t3, tc.t4, tc.t5, tc.t6
Guido van Rossum85b97351999-06-02 16:10:19 +0000194
195 test(tests)