blob: 7a3fe13eac6d9cb291de6cdbe4d5b81de670af64 [file] [log] [blame]
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +00001"""CallTips.py - An IDLE Extension to Jog Your Memory
David Scherer7aced172000-08-15 01:13:23 +00002
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +00003Call Tips are floating windows which display function/method parameter
4information as you open the parameter parenthesis, and which disappear when you
5type the closing parenthesis. Future plans include extending the functionality
6to include class attributes.
7
8"""
9import sys
David Scherer7aced172000-08-15 01:13:23 +000010import string
David Scherer7aced172000-08-15 01:13:23 +000011import types
12
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +000013import CallTipWindow
14
15import __main__
16
David Scherer7aced172000-08-15 01:13:23 +000017class CallTips:
18
19 menudefs = [
20 ]
21
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +000022 def __init__(self, editwin=None):
23 if editwin == None: # subprocess and test
24 self.editwin = None
25 return
David Scherer7aced172000-08-15 01:13:23 +000026 self.editwin = editwin
27 self.text = editwin.text
28 self.calltip = None
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +000029 self._make_calltip_window = self._make_tk_calltip_window
David Scherer7aced172000-08-15 01:13:23 +000030
31 def close(self):
32 self._make_calltip_window = None
33
David Scherer7aced172000-08-15 01:13:23 +000034 def _make_tk_calltip_window(self):
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +000035 # See __init__ for usage
David Scherer7aced172000-08-15 01:13:23 +000036 return CallTipWindow.CallTip(self.text)
37
38 def _remove_calltip_window(self):
39 if self.calltip:
40 self.calltip.hidetip()
41 self.calltip = None
Kurt B. Kaiserae676472001-07-12 23:10:35 +000042
David Scherer7aced172000-08-15 01:13:23 +000043 def paren_open_event(self, event):
44 self._remove_calltip_window()
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +000045 name = self.get_name_at_cursor()
46 arg_text = self.fetch_tip(name)
David Scherer7aced172000-08-15 01:13:23 +000047 if arg_text:
48 self.calltip_start = self.text.index("insert")
49 self.calltip = self._make_calltip_window()
50 self.calltip.showtip(arg_text)
51 return "" #so the event is handled normally.
52
53 def paren_close_event(self, event):
54 # Now just hides, but later we should check if other
55 # paren'd expressions remain open.
56 self._remove_calltip_window()
57 return "" #so the event is handled normally.
58
59 def check_calltip_cancel_event(self, event):
60 if self.calltip:
61 # If we have moved before the start of the calltip,
62 # or off the calltip line, then cancel the tip.
63 # (Later need to be smarter about multi-line, etc)
64 if self.text.compare("insert", "<=", self.calltip_start) or \
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +000065 self.text.compare("insert", ">", self.calltip_start
66 + " lineend"):
David Scherer7aced172000-08-15 01:13:23 +000067 self._remove_calltip_window()
68 return "" #so the event is handled normally.
69
70 def calltip_cancel_event(self, event):
71 self._remove_calltip_window()
72 return "" #so the event is handled normally.
73
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +000074 __IDCHARS = "._" + string.ascii_letters + string.digits
Kurt B. Kaiser92cfaf62002-09-14 00:55:21 +000075
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +000076 def get_name_at_cursor(self):
77 idchars = self.__IDCHARS
78 str = self.text.get("insert linestart", "insert")
79 i = len(str)
80 while i and str[i-1] in idchars:
81 i -= 1
82 return str[i:]
83
84 def fetch_tip(self, name):
85 interp = self.editwin and self.editwin.flist.pyshell.interp
86 rpcclt = interp and interp.rpcclt
87 if rpcclt:
88 return rpcclt.remotecall("exec", "get_the_calltip",
89 (name,), {})
90 else:
91 entity = self.get_entity(name)
92 return get_arg_text(entity)
93
94 def get_entity(self, name):
95 if name:
David Scherer7aced172000-08-15 01:13:23 +000096 namespace = sys.modules.copy()
97 namespace.update(__main__.__dict__)
98 try:
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +000099 return eval(name, namespace)
David Scherer7aced172000-08-15 01:13:23 +0000100 except:
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +0000101 return None
David Scherer7aced172000-08-15 01:13:23 +0000102
103def _find_constructor(class_ob):
104 # Given a class object, return a function object used for the
105 # constructor (ie, __init__() ) or None if we can't find one.
106 try:
107 return class_ob.__init__.im_func
108 except AttributeError:
109 for base in class_ob.__bases__:
110 rc = _find_constructor(base)
111 if rc is not None: return rc
112 return None
113
114def get_arg_text(ob):
115 # Get a string describing the arguments for the given object.
116 argText = ""
117 if ob is not None:
118 argOffset = 0
119 if type(ob)==types.ClassType:
120 # Look for the highest __init__ in the class chain.
121 fob = _find_constructor(ob)
122 if fob is None:
123 fob = lambda: None
124 else:
125 argOffset = 1
126 elif type(ob)==types.MethodType:
127 # bit of a hack for methods - turn it into a function
128 # but we drop the "self" param.
129 fob = ob.im_func
130 argOffset = 1
131 else:
132 fob = ob
133 # Try and build one for Python defined functions
134 if type(fob) in [types.FunctionType, types.LambdaType]:
135 try:
136 realArgs = fob.func_code.co_varnames[argOffset:fob.func_code.co_argcount]
137 defaults = fob.func_defaults or []
138 defaults = list(map(lambda name: "=%s" % name, defaults))
139 defaults = [""] * (len(realArgs)-len(defaults)) + defaults
140 items = map(lambda arg, dflt: arg+dflt, realArgs, defaults)
141 if fob.func_code.co_flags & 0x4:
142 items.append("...")
143 if fob.func_code.co_flags & 0x8:
144 items.append("***")
Kurt B. Kaiser908aece2002-09-15 22:02:58 +0000145 argText = ", ".join(items)
David Scherer7aced172000-08-15 01:13:23 +0000146 argText = "(%s)" % argText
147 except:
148 pass
149 # See if we can use the docstring
Kurt B. Kaiser908aece2002-09-15 22:02:58 +0000150 doc = getattr(ob, "__doc__", "")
151 if doc:
152 while doc[:1] in " \t\n":
153 doc = doc[1:]
154 pos = doc.find("\n")
155 if pos < 0 or pos > 70:
156 pos = 70
157 if argText:
158 argText += "\n"
159 argText += doc[:pos]
David Scherer7aced172000-08-15 01:13:23 +0000160 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, ...)"
173 def t6(a, b=None, *args, **kw): "(a, b=None, ..., ***)"
174
175 class TC:
176 "(a=None, ...)"
177 def __init__(self, a=None, *b): "(a=None, ...)"
178 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, ...)"
183 def t6(self, a, b=None, *args, **kw): "(a, b=None, ..., ***)"
184
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +0000185 def test(tests):
186 ct = CallTips()
David Scherer7aced172000-08-15 01:13:23 +0000187 failed=[]
188 for t in tests:
189 expected = t.__doc__ + "\n" + t.__doc__
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +0000190 name = t.__name__
191 arg_text = ct.fetch_tip(name)
192 if arg_text != expected:
David Scherer7aced172000-08-15 01:13:23 +0000193 failed.append(t)
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +0000194 print "%s - expected %s, but got %s" % (t, expected,
195 get_arg_text(entity))
David Scherer7aced172000-08-15 01:13:23 +0000196 print "%d of %d tests failed" % (len(failed), len(tests))
197
198 tc = TC()
Kurt B. Kaiser5afa1df2002-10-10 08:25:24 +0000199 tests = (t1, t2, t3, t4, t5, t6,
200 TC, tc.t1, tc.t2, tc.t3, tc.t4, tc.t5, tc.t6)
David Scherer7aced172000-08-15 01:13:23 +0000201
202 test(tests)