blob: 0bdeb92adf28cee1827ce3ce23d417b6952ae910 [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
35 # Makes a Tk based calltip window. Used by IDLE, but not Pythonwin.
36 # See __init__ above for how this is used.
Guido van Rossum85b97351999-06-02 16:10:19 +000037 def _make_tk_calltip_window(self):
38 import CallTipWindow
39 return CallTipWindow.CallTip(self.text)
40
41 def _remove_calltip_window(self):
42 if self.calltip:
43 self.calltip.hidetip()
44 self.calltip = None
45
46 def paren_open_event(self, event):
47 self._remove_calltip_window()
48 arg_text = get_arg_text(self.get_object_at_cursor())
49 if arg_text:
50 self.calltip_start = self.text.index("insert")
51 self.calltip = self._make_calltip_window()
52 self.calltip.showtip(arg_text)
Guido van Rossum6290dab1999-06-02 18:12:55 +000053 return "" #so the event is handled normally.
Guido van Rossum85b97351999-06-02 16:10:19 +000054
55 def paren_close_event(self, event):
56 # Now just hides, but later we should check if other
57 # paren'd expressions remain open.
Guido van Rossum85b97351999-06-02 16:10:19 +000058 self._remove_calltip_window()
Guido van Rossum6290dab1999-06-02 18:12:55 +000059 return "" #so the event is handled normally.
Guido van Rossum85b97351999-06-02 16:10:19 +000060
61 def check_calltip_cancel_event(self, event):
Guido van Rossum85b97351999-06-02 16:10:19 +000062 if self.calltip:
63 # If we have moved before the start of the calltip,
64 # or off the calltip line, then cancel the tip.
65 # (Later need to be smarter about multi-line, etc)
66 if self.text.compare("insert", "<=", self.calltip_start) or \
67 self.text.compare("insert", ">", self.calltip_start + " lineend"):
68 self._remove_calltip_window()
Guido van Rossum6290dab1999-06-02 18:12:55 +000069 return "" #so the event is handled normally.
70
71 def calltip_cancel_event(self, event):
72 self._remove_calltip_window()
73 return "" #so the event is handled normally.
74
Guido van Rossum85b97351999-06-02 16:10:19 +000075 def get_object_at_cursor(self,
76 wordchars="._" + string.uppercase + string.lowercase + string.digits):
Guido van Rossum23c115f1999-06-03 12:07:50 +000077 # XXX - This needs to be moved to a better place
78 # so the "." attribute lookup code can also use it.
Guido van Rossum85b97351999-06-02 16:10:19 +000079 text = self.text
Guido van Rossum6290dab1999-06-02 18:12:55 +000080 chars = text.get("insert linestart", "insert")
81 i = len(chars)
82 while i and chars[i-1] in wordchars:
83 i = i-1
84 word = chars[i:]
85 if word:
86 # How is this for a hack!
Guido van Rossum85b97351999-06-02 16:10:19 +000087 import sys, __main__
88 namespace = sys.modules.copy()
89 namespace.update(__main__.__dict__)
90 try:
91 return eval(word, namespace)
92 except:
93 pass
94 return None # Can't find an object.
Guido van Rossum6290dab1999-06-02 18:12:55 +000095
Guido van Rossum85b97351999-06-02 16:10:19 +000096def get_arg_text(ob):
97 # Get a string describing the arguments for the given object.
98 argText = ""
99 if ob is not None:
100 argOffset = 0
101 # bit of a hack for methods - turn it into a function
102 # but we drop the "self" param.
103 if type(ob)==types.MethodType:
104 ob = ob.im_func
105 argOffset = 1
106 # Try and build one for Python defined functions
107 if type(ob) in [types.FunctionType, types.LambdaType]:
108 try:
109 realArgs = ob.func_code.co_varnames[argOffset:ob.func_code.co_argcount]
110 defaults = ob.func_defaults or []
111 defaults = list(map(lambda name: "=%s" % name, defaults))
112 defaults = [""] * (len(realArgs)-len(defaults)) + defaults
Guido van Rossum23c115f1999-06-03 12:07:50 +0000113 items = map(lambda arg, dflt: arg+dflt, realArgs, defaults)
Guido van Rossum85b97351999-06-02 16:10:19 +0000114 if len(realArgs)+argOffset < (len(ob.func_code.co_varnames) - len(ob.func_code.co_names) ):
Guido van Rossum23c115f1999-06-03 12:07:50 +0000115 items.append("...")
116 argText = string.join(items , ", ")
Guido van Rossum85b97351999-06-02 16:10:19 +0000117 argText = "(%s)" % argText
118 except:
119 pass
Guido van Rossum23c115f1999-06-03 12:07:50 +0000120 # See if we can use the docstring
Guido van Rossum6290dab1999-06-02 18:12:55 +0000121 if hasattr(ob, "__doc__") and ob.__doc__:
Guido van Rossum85b97351999-06-02 16:10:19 +0000122 pos = string.find(ob.__doc__, "\n")
123 if pos<0 or pos>70: pos=70
Guido van Rossum6290dab1999-06-02 18:12:55 +0000124 if argText: argText = argText + "\n"
125 argText = argText + ob.__doc__[:pos]
Guido van Rossum85b97351999-06-02 16:10:19 +0000126
127 return argText
128
129#################################################
130#
131# Test code
132#
133if __name__=='__main__':
134
135 def t1(): "()"
136 def t2(a, b=None): "(a, b=None)"
137 def t3(a, *args): "(a, ...)"
138 def t4(*args): "(...)"
139 def t5(a, *args): "(a, ...)"
140 def t6(a, b=None, *args, **kw): "(a, b=None, ...)"
141
142 class TC:
143 def t1(self): "()"
144 def t2(self, a, b=None): "(a, b=None)"
145 def t3(self, a, *args): "(a, ...)"
146 def t4(self, *args): "(...)"
147 def t5(self, a, *args): "(a, ...)"
148 def t6(self, a, b=None, *args, **kw): "(a, b=None, ...)"
149
Guido van Rossum85b97351999-06-02 16:10:19 +0000150 def test( tests ):
151 failed=[]
152 for t in tests:
Guido van Rossum6290dab1999-06-02 18:12:55 +0000153 if get_arg_text(t) != t.__doc__ + "\n" + t.__doc__:
Guido van Rossum85b97351999-06-02 16:10:19 +0000154 failed.append(t)
155 print "%s - expected %s, but got %s" % (t, `t.__doc__`, `get_arg_text(t)`)
156 print "%d of %d tests failed" % (len(failed), len(tests))
157
Guido van Rossum6290dab1999-06-02 18:12:55 +0000158 tc = TC()
159 tests = t1, t2, t3, t4, t5, t6, \
160 tc.t1, tc.t2, tc.t3, tc.t4, tc.t5, tc.t6
Guido van Rossum85b97351999-06-02 16:10:19 +0000161
162 test(tests)