blob: 82890cbd88b76f7b51ffe768cea8e3740d0311a7 [file] [log] [blame]
David Scherer7aced172000-08-15 01:13:23 +00001# CallTips.py - An IDLE extension that provides "Call Tips" - ie, a floating window that
2# displays parameter information as you open parens.
3
4import string
5import sys
6import types
7
8class CallTips:
9
10 menudefs = [
11 ]
12
David Scherer7aced172000-08-15 01:13:23 +000013 def __init__(self, editwin):
14 self.editwin = editwin
15 self.text = editwin.text
16 self.calltip = None
17 if hasattr(self.text, "make_calltip_window"):
18 self._make_calltip_window = self.text.make_calltip_window
19 else:
20 self._make_calltip_window = self._make_tk_calltip_window
21
22 def close(self):
23 self._make_calltip_window = None
24
25 # Makes a Tk based calltip window. Used by IDLE, but not Pythonwin.
26 # See __init__ above for how this is used.
27 def _make_tk_calltip_window(self):
28 import CallTipWindow
29 return CallTipWindow.CallTip(self.text)
30
31 def _remove_calltip_window(self):
32 if self.calltip:
33 self.calltip.hidetip()
34 self.calltip = None
Kurt B. Kaiserae676472001-07-12 23:10:35 +000035
David Scherer7aced172000-08-15 01:13:23 +000036 def paren_open_event(self, event):
37 self._remove_calltip_window()
38 arg_text = get_arg_text(self.get_object_at_cursor())
39 if arg_text:
40 self.calltip_start = self.text.index("insert")
41 self.calltip = self._make_calltip_window()
42 self.calltip.showtip(arg_text)
43 return "" #so the event is handled normally.
44
45 def paren_close_event(self, event):
46 # Now just hides, but later we should check if other
47 # paren'd expressions remain open.
48 self._remove_calltip_window()
49 return "" #so the event is handled normally.
50
51 def check_calltip_cancel_event(self, event):
52 if self.calltip:
53 # If we have moved before the start of the calltip,
54 # or off the calltip line, then cancel the tip.
55 # (Later need to be smarter about multi-line, etc)
56 if self.text.compare("insert", "<=", self.calltip_start) or \
57 self.text.compare("insert", ">", self.calltip_start + " lineend"):
58 self._remove_calltip_window()
59 return "" #so the event is handled normally.
60
61 def calltip_cancel_event(self, event):
62 self._remove_calltip_window()
63 return "" #so the event is handled normally.
64
65 def get_object_at_cursor(self,
Kurt B. Kaiser92cfaf62002-09-14 00:55:21 +000066 wordchars="._" + string.ascii_letters + string.digits):
67 # Usage of ascii_letters is necessary to avoid UnicodeErrors
68 # if chars contains non-ASCII.
69
David Scherer7aced172000-08-15 01:13:23 +000070 # XXX - This needs to be moved to a better place
71 # so the "." attribute lookup code can also use it.
72 text = self.text
73 chars = text.get("insert linestart", "insert")
74 i = len(chars)
75 while i and chars[i-1] in wordchars:
76 i = i-1
77 word = chars[i:]
78 if word:
79 # How is this for a hack!
80 import sys, __main__
81 namespace = sys.modules.copy()
82 namespace.update(__main__.__dict__)
83 try:
Kurt B. Kaiserae676472001-07-12 23:10:35 +000084 return eval(word, namespace)
David Scherer7aced172000-08-15 01:13:23 +000085 except:
Kurt B. Kaiserae676472001-07-12 23:10:35 +000086 pass
David Scherer7aced172000-08-15 01:13:23 +000087 return None # Can't find an object.
88
89def _find_constructor(class_ob):
90 # Given a class object, return a function object used for the
91 # constructor (ie, __init__() ) or None if we can't find one.
92 try:
93 return class_ob.__init__.im_func
94 except AttributeError:
95 for base in class_ob.__bases__:
96 rc = _find_constructor(base)
97 if rc is not None: return rc
98 return None
99
100def get_arg_text(ob):
101 # Get a string describing the arguments for the given object.
102 argText = ""
103 if ob is not None:
104 argOffset = 0
105 if type(ob)==types.ClassType:
106 # Look for the highest __init__ in the class chain.
107 fob = _find_constructor(ob)
108 if fob is None:
109 fob = lambda: None
110 else:
111 argOffset = 1
112 elif type(ob)==types.MethodType:
113 # bit of a hack for methods - turn it into a function
114 # but we drop the "self" param.
115 fob = ob.im_func
116 argOffset = 1
117 else:
118 fob = ob
119 # Try and build one for Python defined functions
120 if type(fob) in [types.FunctionType, types.LambdaType]:
121 try:
122 realArgs = fob.func_code.co_varnames[argOffset:fob.func_code.co_argcount]
123 defaults = fob.func_defaults or []
124 defaults = list(map(lambda name: "=%s" % name, defaults))
125 defaults = [""] * (len(realArgs)-len(defaults)) + defaults
126 items = map(lambda arg, dflt: arg+dflt, realArgs, defaults)
127 if fob.func_code.co_flags & 0x4:
128 items.append("...")
129 if fob.func_code.co_flags & 0x8:
130 items.append("***")
131 argText = string.join(items , ", ")
132 argText = "(%s)" % argText
133 except:
134 pass
135 # See if we can use the docstring
136 if hasattr(ob, "__doc__") and ob.__doc__:
137 pos = string.find(ob.__doc__, "\n")
138 if pos<0 or pos>70: pos=70
139 if argText: argText = argText + "\n"
140 argText = argText + ob.__doc__[:pos]
141
142 return argText
143
144#################################################
145#
146# Test code
147#
148if __name__=='__main__':
149
150 def t1(): "()"
151 def t2(a, b=None): "(a, b=None)"
152 def t3(a, *args): "(a, ...)"
153 def t4(*args): "(...)"
154 def t5(a, *args): "(a, ...)"
155 def t6(a, b=None, *args, **kw): "(a, b=None, ..., ***)"
156
157 class TC:
158 "(a=None, ...)"
159 def __init__(self, a=None, *b): "(a=None, ...)"
160 def t1(self): "()"
161 def t2(self, a, b=None): "(a, b=None)"
162 def t3(self, a, *args): "(a, ...)"
163 def t4(self, *args): "(...)"
164 def t5(self, a, *args): "(a, ...)"
165 def t6(self, a, b=None, *args, **kw): "(a, b=None, ..., ***)"
166
167 def test( tests ):
168 failed=[]
169 for t in tests:
170 expected = t.__doc__ + "\n" + t.__doc__
171 if get_arg_text(t) != expected:
172 failed.append(t)
173 print "%s - expected %s, but got %s" % (t, `expected`, `get_arg_text(t)`)
174 print "%d of %d tests failed" % (len(failed), len(tests))
175
176 tc = TC()
177 tests = t1, t2, t3, t4, t5, t6, \
178 TC, tc.t1, tc.t2, tc.t3, tc.t4, tc.t5, tc.t6
179
180 test(tests)