blob: b1f100f22233042ff76a61e7aa253c0fadc6ee9a [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,
66 wordchars="._" + string.uppercase + string.lowercase + string.digits):
67 # XXX - This needs to be moved to a better place
68 # so the "." attribute lookup code can also use it.
69 text = self.text
70 chars = text.get("insert linestart", "insert")
71 i = len(chars)
72 while i and chars[i-1] in wordchars:
73 i = i-1
74 word = chars[i:]
75 if word:
76 # How is this for a hack!
77 import sys, __main__
78 namespace = sys.modules.copy()
79 namespace.update(__main__.__dict__)
80 try:
Kurt B. Kaiserae676472001-07-12 23:10:35 +000081 return eval(word, namespace)
David Scherer7aced172000-08-15 01:13:23 +000082 except:
Kurt B. Kaiserae676472001-07-12 23:10:35 +000083 pass
David Scherer7aced172000-08-15 01:13:23 +000084 return None # Can't find an object.
85
86def _find_constructor(class_ob):
87 # Given a class object, return a function object used for the
88 # constructor (ie, __init__() ) or None if we can't find one.
89 try:
90 return class_ob.__init__.im_func
91 except AttributeError:
92 for base in class_ob.__bases__:
93 rc = _find_constructor(base)
94 if rc is not None: return rc
95 return None
96
97def get_arg_text(ob):
98 # Get a string describing the arguments for the given object.
99 argText = ""
100 if ob is not None:
101 argOffset = 0
102 if type(ob)==types.ClassType:
103 # Look for the highest __init__ in the class chain.
104 fob = _find_constructor(ob)
105 if fob is None:
106 fob = lambda: None
107 else:
108 argOffset = 1
109 elif type(ob)==types.MethodType:
110 # bit of a hack for methods - turn it into a function
111 # but we drop the "self" param.
112 fob = ob.im_func
113 argOffset = 1
114 else:
115 fob = ob
116 # Try and build one for Python defined functions
117 if type(fob) in [types.FunctionType, types.LambdaType]:
118 try:
119 realArgs = fob.func_code.co_varnames[argOffset:fob.func_code.co_argcount]
120 defaults = fob.func_defaults or []
121 defaults = list(map(lambda name: "=%s" % name, defaults))
122 defaults = [""] * (len(realArgs)-len(defaults)) + defaults
123 items = map(lambda arg, dflt: arg+dflt, realArgs, defaults)
124 if fob.func_code.co_flags & 0x4:
125 items.append("...")
126 if fob.func_code.co_flags & 0x8:
127 items.append("***")
128 argText = string.join(items , ", ")
129 argText = "(%s)" % argText
130 except:
131 pass
132 # See if we can use the docstring
133 if hasattr(ob, "__doc__") and ob.__doc__:
134 pos = string.find(ob.__doc__, "\n")
135 if pos<0 or pos>70: pos=70
136 if argText: argText = argText + "\n"
137 argText = argText + ob.__doc__[:pos]
138
139 return argText
140
141#################################################
142#
143# Test code
144#
145if __name__=='__main__':
146
147 def t1(): "()"
148 def t2(a, b=None): "(a, b=None)"
149 def t3(a, *args): "(a, ...)"
150 def t4(*args): "(...)"
151 def t5(a, *args): "(a, ...)"
152 def t6(a, b=None, *args, **kw): "(a, b=None, ..., ***)"
153
154 class TC:
155 "(a=None, ...)"
156 def __init__(self, a=None, *b): "(a=None, ...)"
157 def t1(self): "()"
158 def t2(self, a, b=None): "(a, b=None)"
159 def t3(self, a, *args): "(a, ...)"
160 def t4(self, *args): "(...)"
161 def t5(self, a, *args): "(a, ...)"
162 def t6(self, a, b=None, *args, **kw): "(a, b=None, ..., ***)"
163
164 def test( tests ):
165 failed=[]
166 for t in tests:
167 expected = t.__doc__ + "\n" + t.__doc__
168 if get_arg_text(t) != expected:
169 failed.append(t)
170 print "%s - expected %s, but got %s" % (t, `expected`, `get_arg_text(t)`)
171 print "%d of %d tests failed" % (len(failed), len(tests))
172
173 tc = TC()
174 tests = t1, t2, t3, t4, t5, t6, \
175 TC, tc.t1, tc.t2, tc.t3, tc.t4, tc.t5, tc.t6
176
177 test(tests)