blob: dc25083e7eb85e2cffac7e3c75f0f69f5685a68b [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,
Martin v. Löwis5428fff2002-08-05 14:53:52 +000079 wordchars="._" + string.ascii_letters + string.digits):
80 # Usage of ascii_letters is necessary to avoid UnicodeErrors
81 # if chars contains non-ASCII.
Tim Peters280488b2002-08-23 18:19:30 +000082
Guido van Rossum23c115f1999-06-03 12:07:50 +000083 # XXX - This needs to be moved to a better place
84 # so the "." attribute lookup code can also use it.
Guido van Rossum85b97351999-06-02 16:10:19 +000085 text = self.text
Guido van Rossum6290dab1999-06-02 18:12:55 +000086 chars = text.get("insert linestart", "insert")
87 i = len(chars)
88 while i and chars[i-1] in wordchars:
89 i = i-1
90 word = chars[i:]
91 if word:
92 # How is this for a hack!
Guido van Rossum85b97351999-06-02 16:10:19 +000093 import sys, __main__
94 namespace = sys.modules.copy()
95 namespace.update(__main__.__dict__)
96 try:
Tim Peters70c43782001-01-17 08:48:39 +000097 return eval(word, namespace)
Guido van Rossum85b97351999-06-02 16:10:19 +000098 except:
Tim Peters70c43782001-01-17 08:48:39 +000099 pass
Guido van Rossum85b97351999-06-02 16:10:19 +0000100 return None # Can't find an object.
Guido van Rossum6290dab1999-06-02 18:12:55 +0000101
Guido van Rossumea827e91999-06-10 14:20:26 +0000102def _find_constructor(class_ob):
103 # Given a class object, return a function object used for the
104 # constructor (ie, __init__() ) or None if we can't find one.
105 try:
106 return class_ob.__init__.im_func
107 except AttributeError:
108 for base in class_ob.__bases__:
109 rc = _find_constructor(base)
110 if rc is not None: return rc
111 return None
112
Guido van Rossum85b97351999-06-02 16:10:19 +0000113def get_arg_text(ob):
114 # Get a string describing the arguments for the given object.
115 argText = ""
116 if ob is not None:
117 argOffset = 0
Guido van Rossumea827e91999-06-10 14:20:26 +0000118 if type(ob)==types.ClassType:
119 # Look for the highest __init__ in the class chain.
120 fob = _find_constructor(ob)
121 if fob is None:
122 fob = lambda: None
123 else:
124 argOffset = 1
125 elif type(ob)==types.MethodType:
126 # bit of a hack for methods - turn it into a function
127 # but we drop the "self" param.
128 fob = ob.im_func
Guido van Rossum85b97351999-06-02 16:10:19 +0000129 argOffset = 1
Guido van Rossumea827e91999-06-10 14:20:26 +0000130 else:
131 fob = ob
Guido van Rossum85b97351999-06-02 16:10:19 +0000132 # Try and build one for Python defined functions
Guido van Rossumea827e91999-06-10 14:20:26 +0000133 if type(fob) in [types.FunctionType, types.LambdaType]:
Guido van Rossum85b97351999-06-02 16:10:19 +0000134 try:
Guido van Rossumea827e91999-06-10 14:20:26 +0000135 realArgs = fob.func_code.co_varnames[argOffset:fob.func_code.co_argcount]
136 defaults = fob.func_defaults or []
Guido van Rossum85b97351999-06-02 16:10:19 +0000137 defaults = list(map(lambda name: "=%s" % name, defaults))
138 defaults = [""] * (len(realArgs)-len(defaults)) + defaults
Guido van Rossum23c115f1999-06-03 12:07:50 +0000139 items = map(lambda arg, dflt: arg+dflt, realArgs, defaults)
Guido van Rossumea827e91999-06-10 14:20:26 +0000140 if fob.func_code.co_flags & 0x4:
Guido van Rossum23c115f1999-06-03 12:07:50 +0000141 items.append("...")
Guido van Rossumea827e91999-06-10 14:20:26 +0000142 if fob.func_code.co_flags & 0x8:
Guido van Rossum20731771999-06-09 20:34:57 +0000143 items.append("***")
Guido van Rossum23c115f1999-06-03 12:07:50 +0000144 argText = string.join(items , ", ")
Guido van Rossum85b97351999-06-02 16:10:19 +0000145 argText = "(%s)" % argText
146 except:
147 pass
Guido van Rossum23c115f1999-06-03 12:07:50 +0000148 # See if we can use the docstring
Tim Petersa2e2dbe2001-09-16 02:19:49 +0000149 doc = getattr(ob, "__doc__", "")
150 if doc:
151 while doc[:1] in " \t\n":
152 doc = doc[1:]
153 pos = doc.find("\n")
154 if pos < 0 or pos > 70:
155 pos = 70
156 if argText:
157 argText += "\n"
158 argText += doc[:pos]
Guido van Rossum85b97351999-06-02 16:10:19 +0000159
160 return argText
161
162#################################################
163#
164# Test code
165#
166if __name__=='__main__':
167
168 def t1(): "()"
169 def t2(a, b=None): "(a, b=None)"
170 def t3(a, *args): "(a, ...)"
171 def t4(*args): "(...)"
172 def t5(a, *args): "(a, ...)"
Guido van Rossumea827e91999-06-10 14:20:26 +0000173 def t6(a, b=None, *args, **kw): "(a, b=None, ..., ***)"
Guido van Rossum85b97351999-06-02 16:10:19 +0000174
175 class TC:
Guido van Rossumea827e91999-06-10 14:20:26 +0000176 "(a=None, ...)"
177 def __init__(self, a=None, *b): "(a=None, ...)"
Guido van Rossum85b97351999-06-02 16:10:19 +0000178 def t1(self): "()"
179 def t2(self, a, b=None): "(a, b=None)"
180 def t3(self, a, *args): "(a, ...)"
181 def t4(self, *args): "(...)"
182 def t5(self, a, *args): "(a, ...)"
Guido van Rossumea827e91999-06-10 14:20:26 +0000183 def t6(self, a, b=None, *args, **kw): "(a, b=None, ..., ***)"
Guido van Rossum85b97351999-06-02 16:10:19 +0000184
Guido van Rossum85b97351999-06-02 16:10:19 +0000185 def test( tests ):
186 failed=[]
187 for t in tests:
Guido van Rossumea827e91999-06-10 14:20:26 +0000188 expected = t.__doc__ + "\n" + t.__doc__
189 if get_arg_text(t) != expected:
Guido van Rossum85b97351999-06-02 16:10:19 +0000190 failed.append(t)
Guido van Rossumea827e91999-06-10 14:20:26 +0000191 print "%s - expected %s, but got %s" % (t, `expected`, `get_arg_text(t)`)
Guido van Rossum85b97351999-06-02 16:10:19 +0000192 print "%d of %d tests failed" % (len(failed), len(tests))
193
Guido van Rossum6290dab1999-06-02 18:12:55 +0000194 tc = TC()
195 tests = t1, t2, t3, t4, t5, t6, \
Guido van Rossumea827e91999-06-10 14:20:26 +0000196 TC, tc.t1, tc.t2, tc.t3, tc.t4, tc.t5, tc.t6
Guido van Rossum85b97351999-06-02 16:10:19 +0000197
198 test(tests)